-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0_x.js
184 lines (174 loc) · 5.87 KB
/
0_x.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const debugSignal = require('debug')('fbp-client:adapter:signal');
const debugRequest = require('debug')('fbp-client:adapter:request');
const debugResponse = require('debug')('fbp-client:adapter:response');
class Adapter {
constructor(client, version) {
this.client = client;
this.commands = [];
this.listener = null;
this.version = version;
this.subscribe();
this.tick();
}
subscribe() {
const onMessage = (message) => {
if (this.listener) {
// Give current command the first dibs on the message
this.listener(message);
return;
}
debugSignal(`${message.protocol}:${message.command}`);
// If there is no listener, treat it as signal
this.client.validate(`/${message.protocol}/output/${message.command}`, message)
.then(() => this.client.canReceive(message.protocol, message.command))
.then(() => this.client.signal(message), err => this.client.protocolError(err));
};
this.client.transport.on('message', onMessage);
this.client.transport.on('status', ({ online }) => {
if (!online) {
return;
}
this.tick();
});
}
send(protocol, command, payload, secret) {
return new Promise((resolve, reject) => {
const isAcceptedResponse = (response) => {
if (response.protocol !== protocol) {
return false;
}
const expectedResponses = [];
// Handle cases where response has different command
switch (`${protocol}:${command}`) {
case 'runtime:packet': {
expectedResponses.push('packetsent');
break;
}
case 'component:getsource': {
expectedResponses.push('source');
break;
}
case 'component:source': {
expectedResponses.push('component');
break;
}
case 'component:list': {
expectedResponses.push('component');
expectedResponses.push('componentsready');
break;
}
case 'network:start': {
expectedResponses.push('started');
break;
}
case 'network:stop': {
expectedResponses.push('stopped');
break;
}
case 'network:getstatus': {
expectedResponses.push('status');
break;
}
default: {
expectedResponses.push(command);
}
}
expectedResponses.push('error');
if (expectedResponses.indexOf(response.command) === -1) {
return false;
}
return true;
};
// Placeholder for results in cases where we have to collect multiple messages
const results = [];
// How to run the command when its time comes
const execute = () => {
let timeout = null;
this.listener = (message) => {
this.client.validate(`/${message.protocol}/output/${message.command}`, message)
.then(() => this.client.canReceive(message.protocol, message.command))
.then(() => {
if (!isAcceptedResponse(message)) {
// Unrelated message, treat as signal
debugSignal(`${message.protocol}:${message.command}`);
this.client.signal(message);
return;
}
// No need to wait for timeout
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
debugResponse(`${message.protocol}:${message.command} for request ${protocol}:${command}`);
if (message.command === 'error') {
const err = new Error(message.payload.message);
err.stack = message.payload.stack;
this.listener = null;
reject(err);
this.tick();
return;
}
if (protocol === 'component' && command === 'list') {
// For component listings, we collect results until componentsready
if (message.command === 'componentsready') {
this.listener = null;
resolve(results);
this.tick();
return;
}
results.push(message.payload);
return;
}
this.listener = null;
resolve(message.payload);
this.tick();
}, (err) => {
this.listener = null;
reject(err);
this.tick();
});
};
debugRequest(`${protocol}:${command}`);
const withSecret = payload;
withSecret.secret = secret;
this.client.transport.send(protocol, command, withSecret);
if (protocol === 'component') {
// Component protocol messages can do lots of I/O, set no timeout
return;
}
if (protocol === 'runtime' && command === 'packet' && this.version <= 0.6) {
// runtime:packetssent was added in 0.7. Before that there was no response
// If there is no error inside a short time, we assume it succeeded
timeout = setTimeout(() => {
this.listener = null;
resolve(payload);
this.tick();
}, 10);
return;
}
// Deal with commands timing out
timeout = setTimeout(() => {
this.listener = null;
reject(new Error(`${protocol}:${command} timed out`));
this.tick();
}, this.client.options.commandTimeout);
};
this.commands.push(execute);
this.tick();
});
}
tick() {
if (!this.commands.length) {
return;
}
if (!this.client.isConnected()) {
return;
}
if (this.listener) {
return;
}
const command = this.commands.shift();
command();
}
}
module.exports = (client, version) => new Adapter(client, version);