-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternalAPI.js
More file actions
57 lines (47 loc) · 1.52 KB
/
internalAPI.js
File metadata and controls
57 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const shortid = require('shortid');
class BitfocusCloudError extends Error {
constructor(name, message) {
super(message);
this.name = name;
}
}
module.exports = {
async clientCommand(remoteId, name, ...args) {
const callerId = shortid.generate();
const replyChannel = 'companionProcResult:' + callerId;
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new BitfocusCloudError('call_timeout', 'ClientCommand timeout for ' + name));
this.sockets.forEach(socket => {
socket.unsubscribe(replyChannel);
socket.closeChannel(replyChannel);
});
}, 10000);
let isHandeled = false;
this.sockets.forEach(socket => {
(async () => {
for await (let data of socket.subscribe(replyChannel)) {
if (isHandeled) {
socket.unsubscribe(replyChannel);
socket.closeChannel(replyChannel);
return
}
console.log('::::::: Got response for command %o', remoteId + ':' + name)
clearTimeout(timer);
isHandeled = true;
if (data.error) {
reject(new BitfocusCloudError('rpc_error', 'rpc error: ' + data.error));
} else {
resolve(data.result);
}
socket.unsubscribe(replyChannel);
socket.closeChannel(replyChannel);
break;
}
})();
console.log("%%%%% SENDING COMMAND TO A CONNECTION: ", `companionProc:${remoteId}:${name}`);
socket.transmitPublish(`companionProc:${remoteId}:${name}`, { args, callerId });
});
});
}
}