@cuecue/app 0.0.25 → 0.0.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +917 -0
- package/package.json +8 -6
- package/lib/index.js +0 -1916
package/dist/index.js
ADDED
|
@@ -0,0 +1,917 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var StateMachine = require('javascript-state-machine');
|
|
6
|
+
var EventEmitter = require('wolfy87-eventemitter');
|
|
7
|
+
var createDebug = require('debug');
|
|
8
|
+
var dayjs = require('dayjs');
|
|
9
|
+
var uuid = require('uuid');
|
|
10
|
+
var matches = require('lodash/matches');
|
|
11
|
+
var isObject = require('lodash/isObject');
|
|
12
|
+
|
|
13
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
|
+
|
|
15
|
+
var StateMachine__default = /*#__PURE__*/_interopDefaultLegacy(StateMachine);
|
|
16
|
+
var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
|
|
17
|
+
var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
|
|
18
|
+
var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
|
|
19
|
+
var matches__default = /*#__PURE__*/_interopDefaultLegacy(matches);
|
|
20
|
+
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
|
|
21
|
+
|
|
22
|
+
class MemoryStore extends EventEmitter__default["default"] {
|
|
23
|
+
constructor(data = {}) {
|
|
24
|
+
super();
|
|
25
|
+
this.data = data || {};
|
|
26
|
+
this.debug = createDebug__default["default"]('cuecue:store:memory');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
findItem(type, id) {
|
|
30
|
+
const items = this.data[type] || [];
|
|
31
|
+
return items.find(matches__default["default"](isObject__default["default"](id) ? id : {
|
|
32
|
+
id
|
|
33
|
+
})) || null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
addItem(type, data) {
|
|
37
|
+
const id = data.id || uuid.v4();
|
|
38
|
+
const savedItem = { ...data,
|
|
39
|
+
id
|
|
40
|
+
};
|
|
41
|
+
this.data = { ...this.data,
|
|
42
|
+
[type]: [...(this.data[type] || []).filter(it => it.id !== id), savedItem]
|
|
43
|
+
};
|
|
44
|
+
return savedItem;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
addItems(type, items) {
|
|
48
|
+
const savedItems = items.map(it => this.addItem(type, it));
|
|
49
|
+
return savedItems;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
updateItem(type, id, data) {
|
|
53
|
+
const index = (this.data[type] || []).findIndex(it => it.id === id);
|
|
54
|
+
|
|
55
|
+
if (index === -1) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const newData = { ...this.data[type][index],
|
|
60
|
+
...data
|
|
61
|
+
};
|
|
62
|
+
this.data = { ...this.data,
|
|
63
|
+
[type]: (this.data[type] || []).map(it => it.id === id ? newData : it)
|
|
64
|
+
};
|
|
65
|
+
return newData;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
deleteItem(type, id) {
|
|
69
|
+
this.data = { ...this.data,
|
|
70
|
+
[type]: (this.data[type] || []).filter(it => it.id !== id)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
deleteItems(type, params = {}) {
|
|
75
|
+
const match = matches__default["default"](params);
|
|
76
|
+
this.data = { ...this.data,
|
|
77
|
+
[type]: (this.data[type] || []).filter(it => !match(it))
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
getItems(type, params = {}) {
|
|
82
|
+
const match = matches__default["default"](params);
|
|
83
|
+
return (this.data[type] || []).filter(it => match(it));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
class Application extends EventEmitter__default["default"] {
|
|
89
|
+
constructor(definition, opts = {}) {
|
|
90
|
+
super();
|
|
91
|
+
this.definition = definition;
|
|
92
|
+
this.options = {
|
|
93
|
+
store: new MemoryStore(),
|
|
94
|
+
debugFunction: createDebug__default["default"]('cuecue:app'),
|
|
95
|
+
getHandle: id => `${id}_${dayjs__default["default"]().format('YYYY_MM_DD')}_${Date.now().toString(36)}`,
|
|
96
|
+
validateCommand: command => ['start', 'end', 'cue', 'uncue', 'define', 'interact', 'reset', 'resetInteractions', 'restart', 'kill'].indexOf(command) !== -1,
|
|
97
|
+
...opts
|
|
98
|
+
};
|
|
99
|
+
const {
|
|
100
|
+
store
|
|
101
|
+
} = this.options;
|
|
102
|
+
this.debug = createDebug__default["default"]('cuecue:app');
|
|
103
|
+
this.store = store;
|
|
104
|
+
this.session = null;
|
|
105
|
+
this.statefulCue = null;
|
|
106
|
+
this.cueDurationInterval = null;
|
|
107
|
+
this.interactions = [];
|
|
108
|
+
this.inputs = [];
|
|
109
|
+
this.outputs = [];
|
|
110
|
+
this.onInit = this.onInit.bind(this);
|
|
111
|
+
this.onInitialized = this.onInitialized.bind(this);
|
|
112
|
+
this.onDestroy = this.onDestroy.bind(this);
|
|
113
|
+
this.onDestroyed = this.onDestroyed.bind(this);
|
|
114
|
+
this.onStart = this.onStart.bind(this);
|
|
115
|
+
this.onIdle = this.onIdle.bind(this);
|
|
116
|
+
this.onUncue = this.onUncue.bind(this);
|
|
117
|
+
this.onEnd = this.onEnd.bind(this);
|
|
118
|
+
this.onCue = this.onCue.bind(this);
|
|
119
|
+
this.onCued = this.onCued.bind(this);
|
|
120
|
+
this.onStop = this.onStop.bind(this);
|
|
121
|
+
this.onStopped = this.onStopped.bind(this);
|
|
122
|
+
this.onInputCommand = this.onInputCommand.bind(this);
|
|
123
|
+
this.onAfterTransition = this.onAfterTransition.bind(this);
|
|
124
|
+
this.onInvalidTransition = this.onInvalidTransition.bind(this);
|
|
125
|
+
this.onPendingTransition = this.onPendingTransition.bind(this);
|
|
126
|
+
this.state = new StateMachine__default["default"]({
|
|
127
|
+
transitions: [{
|
|
128
|
+
name: 'init',
|
|
129
|
+
from: 'none',
|
|
130
|
+
to: 'initialized'
|
|
131
|
+
}, {
|
|
132
|
+
name: 'destroy',
|
|
133
|
+
from: '*',
|
|
134
|
+
to: 'none'
|
|
135
|
+
}, {
|
|
136
|
+
name: 'start',
|
|
137
|
+
from: ['initialized', 'stopped', 'ended'],
|
|
138
|
+
to: 'idle'
|
|
139
|
+
}, {
|
|
140
|
+
name: 'stop',
|
|
141
|
+
from: ['idle', 'cued'],
|
|
142
|
+
to: 'stopped'
|
|
143
|
+
}, {
|
|
144
|
+
name: 'cue',
|
|
145
|
+
from: ['idle', 'cued'],
|
|
146
|
+
to: 'cued'
|
|
147
|
+
}, {
|
|
148
|
+
name: 'uncue',
|
|
149
|
+
from: ['idle', 'cued'],
|
|
150
|
+
to: 'idle'
|
|
151
|
+
}, {
|
|
152
|
+
name: 'end',
|
|
153
|
+
from: ['idle', 'cued'],
|
|
154
|
+
to: 'ended'
|
|
155
|
+
}],
|
|
156
|
+
methods: {
|
|
157
|
+
onBeforeInit: this.onInit,
|
|
158
|
+
onAfterInit: this.onInitialized,
|
|
159
|
+
onBeforeDestroy: this.onDestroy,
|
|
160
|
+
onAfterDestroy: this.onDestroyed,
|
|
161
|
+
onBeforeStart: this.onStart,
|
|
162
|
+
onAfterStart: this.onIdle,
|
|
163
|
+
onBeforeCue: this.onCue,
|
|
164
|
+
onAfterCue: this.onCued,
|
|
165
|
+
onBeforeUncue: this.onUncue,
|
|
166
|
+
onBeforeEnd: this.onEnd,
|
|
167
|
+
onBeforeStop: this.onStop,
|
|
168
|
+
onAfterStop: this.onStopped,
|
|
169
|
+
onAfterTransition: this.onAfterTransition,
|
|
170
|
+
onInvalidTransition: this.onInvalidTransition,
|
|
171
|
+
onPendingTransition: this.onPendingTransition
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
init() {
|
|
177
|
+
return this.state.init();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
destroy() {
|
|
181
|
+
return this.state.destroy();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async start() {
|
|
185
|
+
if (this.stateless()) {
|
|
186
|
+
this.debug('First init');
|
|
187
|
+
await this.init();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return this.state.start();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
stop() {
|
|
194
|
+
return this.state.stop();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
end() {
|
|
198
|
+
return this.state.end();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
cue(id, sessionData = null) {
|
|
202
|
+
return this.state.cue(id, sessionData);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async interact(data, interactionId = null) {
|
|
206
|
+
this.sendInteractToOutputs(data, interactionId);
|
|
207
|
+
const interaction = await this.ensureInteraction(data, interactionId || uuid.v4());
|
|
208
|
+
this.sendInteractionToOutputs(interaction);
|
|
209
|
+
return interaction;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
uncue() {
|
|
213
|
+
return this.state.uncue();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async define(...newCues) {
|
|
217
|
+
const cues = await this.getCues();
|
|
218
|
+
const mergedArray = [...newCues, ...cues]; // The order matters
|
|
219
|
+
|
|
220
|
+
this.debug('humm %O', newCues, cues);
|
|
221
|
+
const set = new Set();
|
|
222
|
+
const mergedCues = mergedArray.filter(item => {
|
|
223
|
+
if (!set.has(item.id)) {
|
|
224
|
+
set.add(item.id);
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return false;
|
|
229
|
+
}, set);
|
|
230
|
+
this.debug('ok %O', mergedCues);
|
|
231
|
+
const list = await this.setCues([...mergedCues]);
|
|
232
|
+
await this.sendCuesToOutputs(list);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async reset() {
|
|
236
|
+
await this.resetSessionInteractions();
|
|
237
|
+
await this.resetSession();
|
|
238
|
+
await this.uncue();
|
|
239
|
+
this.sendCommandToOutputs('reset');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async restart() {
|
|
243
|
+
this.destroy();
|
|
244
|
+
await this.start();
|
|
245
|
+
} // eslint-disable-next-line class-methods-use-this
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
kill() {
|
|
249
|
+
// Test this with something else than nodemon
|
|
250
|
+
process.kill(process.pid, 'SIGUSR2');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
stateless() {
|
|
254
|
+
return this.state.is('none');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
initialized() {
|
|
258
|
+
return this.state.is('initialized');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
started() {
|
|
262
|
+
return this.state.is('idle');
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
idle() {
|
|
266
|
+
return this.state.is('idle');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
stopped() {
|
|
270
|
+
return this.state.is('stopped');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
ended() {
|
|
274
|
+
return this.state.is('ended');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
input(input) {
|
|
278
|
+
this.inputs = [...this.inputs, input];
|
|
279
|
+
input.on('command', this.onInputCommand);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
getCurrentCue() {
|
|
283
|
+
return this.statefulCue;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
getCurrentCueId() {
|
|
287
|
+
const {
|
|
288
|
+
id = null
|
|
289
|
+
} = this.statefulCue || {};
|
|
290
|
+
return id;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
removeInput(input) {
|
|
294
|
+
input.off('command', this.onInputCommand);
|
|
295
|
+
this.inputs = this.inputs.filter(it => it !== input);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
output(output) {
|
|
299
|
+
this.outputs = [...this.outputs, output];
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
removeOutput(output) {
|
|
303
|
+
this.outputs = this.outputs.filter(it => it !== output);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async resetInteractions(...args) {
|
|
307
|
+
const ids = args || [];
|
|
308
|
+
this.resetInteractidonsByIds(ids);
|
|
309
|
+
await this.sendUninteractionsToOutputs(ids);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async resetSessionInteractions() {
|
|
313
|
+
await this.store.deleteItems('interactions', {
|
|
314
|
+
sessionId: this.session.id
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async resetInteractidonsByIds(ids) {
|
|
319
|
+
return Promise.all(ids.map(id => this.store.deleteItems('interactions', {
|
|
320
|
+
sessionId: this.session.id,
|
|
321
|
+
externalId: id
|
|
322
|
+
})));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
getInteractions() {
|
|
326
|
+
const {
|
|
327
|
+
id: sessionId = null
|
|
328
|
+
} = this.session || {};
|
|
329
|
+
return this.store.getItems('interactions', {
|
|
330
|
+
sessionId
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
getInteractionsByCue(cueId) {
|
|
335
|
+
const {
|
|
336
|
+
id: sessionId = null
|
|
337
|
+
} = this.session || {};
|
|
338
|
+
return this.store.getItems('interactions', {
|
|
339
|
+
sessionId,
|
|
340
|
+
cueId
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
getInteractionsByInteractionId(interactionId) {
|
|
345
|
+
const {
|
|
346
|
+
id: sessionId = null
|
|
347
|
+
} = this.session || {};
|
|
348
|
+
return this.store.getItems('interactions', {
|
|
349
|
+
sessionId,
|
|
350
|
+
interactionId
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
getInteractionsByUser(userId) {
|
|
355
|
+
const {
|
|
356
|
+
id: sessionId = null
|
|
357
|
+
} = this.session || {};
|
|
358
|
+
return this.store.getItems('interactions', {
|
|
359
|
+
sessionId,
|
|
360
|
+
userId
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
getCues(session) {
|
|
365
|
+
const {
|
|
366
|
+
id: sessionId = null
|
|
367
|
+
} = this.session || session || {};
|
|
368
|
+
return this.store.getItems('cues', {
|
|
369
|
+
sessionId
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
async setCues(cues, session) {
|
|
374
|
+
const {
|
|
375
|
+
id: sessionId = null
|
|
376
|
+
} = this.session || session || {};
|
|
377
|
+
|
|
378
|
+
if (sessionId !== null) {
|
|
379
|
+
await this.store.deleteItems('cues', {
|
|
380
|
+
sessionId
|
|
381
|
+
});
|
|
382
|
+
await this.store.addItems('cues', cues.map(cueDef => ({
|
|
383
|
+
sessionId,
|
|
384
|
+
...cueDef
|
|
385
|
+
})));
|
|
386
|
+
return this.store.getItems('cues', {
|
|
387
|
+
sessionId
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return Promise.resolve();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
async onInvalidTransition(transition, from, to) {
|
|
395
|
+
this.debug('Invalid transition: %s from: %s to: %s', transition, from, to);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
onPendingTransition(transition, from, to) {
|
|
399
|
+
this.debug('Pending transition: %s from: %s to: %s', transition, from, to);
|
|
400
|
+
return new Promise(resolve => {
|
|
401
|
+
if (transition === 'init') {
|
|
402
|
+
this.once('initialized', resolve);
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
async onAfterTransition(lastTransition) {
|
|
408
|
+
const {
|
|
409
|
+
id: sessionId = null
|
|
410
|
+
} = this.session || {};
|
|
411
|
+
const {
|
|
412
|
+
transition,
|
|
413
|
+
from,
|
|
414
|
+
to
|
|
415
|
+
} = lastTransition || {};
|
|
416
|
+
this.debug('After transition: %O from: %s to: %s', transition, from, to);
|
|
417
|
+
|
|
418
|
+
if (transition !== 'init') {
|
|
419
|
+
await this.sendCommandToOutputs('state', {
|
|
420
|
+
sessionId,
|
|
421
|
+
currentState: to
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
async onInit() {
|
|
427
|
+
this.emit('init');
|
|
428
|
+
|
|
429
|
+
if (this.store !== null && typeof this.store.init !== 'undefined') {
|
|
430
|
+
await this.store.init();
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
await this.initInputs();
|
|
434
|
+
await this.initOutputs();
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
onInitialized() {
|
|
438
|
+
this.debug('initialized');
|
|
439
|
+
return process.nextTick(() => this.emit('initialized'));
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
async onDestroy() {
|
|
443
|
+
this.debug('destroying...');
|
|
444
|
+
this.emit('destroy');
|
|
445
|
+
this.destroyInputs();
|
|
446
|
+
this.destroyOutputs();
|
|
447
|
+
|
|
448
|
+
if (this.store !== null && typeof this.store.destroy !== 'undefined') {
|
|
449
|
+
await this.store.destroy();
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
this.event = null;
|
|
453
|
+
this.inputs = [];
|
|
454
|
+
this.outputs = [];
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
onDestroyed() {
|
|
458
|
+
this.debug('destroyed');
|
|
459
|
+
return process.nextTick(() => this.emit('destroyed'));
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
async onStart() {
|
|
463
|
+
this.debug('starting...');
|
|
464
|
+
this.session = await this.ensureSession();
|
|
465
|
+
this.debug('Session #%s', this.session.id);
|
|
466
|
+
this.debug('Session WTF %O', this.session);
|
|
467
|
+
|
|
468
|
+
try {
|
|
469
|
+
await this.startInputs();
|
|
470
|
+
await this.startOutputs();
|
|
471
|
+
} catch (e) {
|
|
472
|
+
this.debug('Session error %O', e);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const cues = await this.getCues();
|
|
476
|
+
this.sendCuesToOutputs(cues);
|
|
477
|
+
this.sendCommandToOutputs('start');
|
|
478
|
+
this.emit('start');
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
async onIdle() {
|
|
482
|
+
if (!this.session.started && !this.session.ended) {
|
|
483
|
+
await this.startSession();
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
this.debug('idle');
|
|
487
|
+
return process.nextTick(() => {
|
|
488
|
+
this.emit('idle'); // Recover
|
|
489
|
+
|
|
490
|
+
const {
|
|
491
|
+
cue = null,
|
|
492
|
+
ended = false,
|
|
493
|
+
data = null
|
|
494
|
+
} = this.session;
|
|
495
|
+
|
|
496
|
+
if (ended) {
|
|
497
|
+
this.end();
|
|
498
|
+
} else if (cue !== null) {
|
|
499
|
+
this.cue(cue, data);
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
async onUncue() {
|
|
505
|
+
this.stopCueDurationInterval();
|
|
506
|
+
this.statefulCue = null;
|
|
507
|
+
await this.setSessionCue(null);
|
|
508
|
+
await this.sendCommandToOutputs('uncue');
|
|
509
|
+
this.emit('uncue');
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
async onCue(state, cueId, sessionData = null) {
|
|
513
|
+
const cues = await this.getCues();
|
|
514
|
+
const cue = cues.find(({
|
|
515
|
+
id
|
|
516
|
+
}) => id === cueId) || null;
|
|
517
|
+
|
|
518
|
+
if (cue === null) {
|
|
519
|
+
this.debug('Cannot find cue %s', cueId);
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const {
|
|
524
|
+
id = null,
|
|
525
|
+
stateful = false
|
|
526
|
+
} = cue;
|
|
527
|
+
|
|
528
|
+
if (stateful) {
|
|
529
|
+
this.debug('Stateful cue: %s', id);
|
|
530
|
+
this.statefulCue = cue;
|
|
531
|
+
await this.setSessionCue(cue, sessionData);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
await this.sendCueToOutputs(cue, sessionData);
|
|
535
|
+
this.emit('cue', cue, sessionData);
|
|
536
|
+
this.debug('onCue %s %O', cueId, sessionData);
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
async onCued(state, cueId) {
|
|
541
|
+
const cues = await this.getCues();
|
|
542
|
+
const cue = cues.find(({
|
|
543
|
+
id
|
|
544
|
+
}) => id === cueId) || null;
|
|
545
|
+
|
|
546
|
+
if (cue === null) {
|
|
547
|
+
this.debug('Cannot find cue %s', cueId);
|
|
548
|
+
return false;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const {
|
|
552
|
+
duration = null
|
|
553
|
+
} = cue;
|
|
554
|
+
|
|
555
|
+
if (duration !== null) {
|
|
556
|
+
this.startCueDurationInterval(cue);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
return true;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
async onCueDurationEnd(cue) {
|
|
563
|
+
this.debug('Cue %s duration is ended', cue.id);
|
|
564
|
+
const {
|
|
565
|
+
after_duration: afterDuration = null
|
|
566
|
+
} = cue;
|
|
567
|
+
const {
|
|
568
|
+
cues = []
|
|
569
|
+
} = this.definition;
|
|
570
|
+
|
|
571
|
+
switch (afterDuration) {
|
|
572
|
+
case 'next':
|
|
573
|
+
{
|
|
574
|
+
const cueIndex = cues.findIndex(it => it.id === cue.id);
|
|
575
|
+
const nextCue = cueIndex !== -1 && cueIndex < cues.length - 1 ? cues[cueIndex + 1] : null;
|
|
576
|
+
|
|
577
|
+
if (nextCue !== null) {
|
|
578
|
+
this.cue(nextCue.id);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
break;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
default:
|
|
585
|
+
this.uncue();
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
async onStop() {
|
|
591
|
+
this.emit('stop');
|
|
592
|
+
this.stopCueDurationInterval();
|
|
593
|
+
this.statefulCue = null;
|
|
594
|
+
this.sendCommandToOutputs('stop');
|
|
595
|
+
await this.stopInputs();
|
|
596
|
+
await this.stopOutputs();
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
onStopped() {
|
|
600
|
+
this.debug('stopped');
|
|
601
|
+
return process.nextTick(() => this.emit('stopped'));
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
async onEnd() {
|
|
605
|
+
await this.endSession();
|
|
606
|
+
this.stopCueDurationInterval();
|
|
607
|
+
this.session = null;
|
|
608
|
+
this.statefulCue = null;
|
|
609
|
+
this.sendCommandToOutputs('end');
|
|
610
|
+
this.emit('ended');
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
async onInputCommand(command, ...args) {
|
|
614
|
+
this.debug('onInputCommand %s', command, args);
|
|
615
|
+
|
|
616
|
+
try {
|
|
617
|
+
const {
|
|
618
|
+
inputCommands = null,
|
|
619
|
+
validateCommand = null,
|
|
620
|
+
transformCommand = null
|
|
621
|
+
} = this.options;
|
|
622
|
+
const {
|
|
623
|
+
command: finalCommand = command,
|
|
624
|
+
args: finalArgs = args
|
|
625
|
+
} = (transformCommand !== null ? transformCommand(command, args) : null) || {};
|
|
626
|
+
|
|
627
|
+
if (inputCommands !== null && inputCommands.indexOf(finalCommand) === -1) {
|
|
628
|
+
this.debug('command not allowed: %s %o', finalCommand, finalArgs);
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
if (validateCommand !== null && !validateCommand(finalCommand, ...finalArgs)) {
|
|
633
|
+
this.debug('validateCommand failed: %s %o', finalCommand, finalArgs);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
this.emit('input:command', finalCommand, finalArgs);
|
|
638
|
+
this[finalCommand](...finalArgs);
|
|
639
|
+
this.debug(this[finalCommand], finalArgs);
|
|
640
|
+
} catch (e) {
|
|
641
|
+
this.debug(`Error with command "${command}": ${e.message}`);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
async ensureInteraction(data, interactionId) {
|
|
646
|
+
this.debug('Ensuring interaction %s... %O', interactionId, data);
|
|
647
|
+
const interactionIdentifier = {
|
|
648
|
+
id: interactionId,
|
|
649
|
+
sessionId: this.session.id
|
|
650
|
+
};
|
|
651
|
+
const {
|
|
652
|
+
id: cueId,
|
|
653
|
+
interaction: cueHasInteraction = false
|
|
654
|
+
} = this.statefulCue || {};
|
|
655
|
+
|
|
656
|
+
if (cueHasInteraction) {
|
|
657
|
+
interactionIdentifier.cueId = cueId;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
let interaction = await this.store.findItem('interactions', interactionIdentifier);
|
|
661
|
+
|
|
662
|
+
if (interaction !== null) {
|
|
663
|
+
interaction = await this.store.updateItem('interactions', interaction.id, {
|
|
664
|
+
data
|
|
665
|
+
});
|
|
666
|
+
} else {
|
|
667
|
+
interaction = await this.store.addItem('interactions', { ...interactionIdentifier,
|
|
668
|
+
data
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
return interaction;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
startCueDurationInterval(cue) {
|
|
676
|
+
const {
|
|
677
|
+
duration = null
|
|
678
|
+
} = cue;
|
|
679
|
+
const startTime = new Date().getTime();
|
|
680
|
+
this.debug('Starting interval for %i for cue %s...', duration, cue.id);
|
|
681
|
+
this.cueDurationInterval = setInterval(() => {
|
|
682
|
+
const currentTime = new Date().getTime();
|
|
683
|
+
const currentDuration = Math.floor((currentTime - startTime) / 1000);
|
|
684
|
+
|
|
685
|
+
if (currentDuration >= duration) {
|
|
686
|
+
clearInterval(this.cueDurationInterval);
|
|
687
|
+
this.cueDurationInterval = null;
|
|
688
|
+
this.onCueDurationEnd(cue);
|
|
689
|
+
}
|
|
690
|
+
}, 10);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
stopCueDurationInterval() {
|
|
694
|
+
if (this.cueDurationInterval !== null) {
|
|
695
|
+
this.debug('Stopping interval for cue...');
|
|
696
|
+
clearInterval(this.cueDurationInterval);
|
|
697
|
+
this.cueDurationInterval = null;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
async ensureSession() {
|
|
702
|
+
const {
|
|
703
|
+
getHandle
|
|
704
|
+
} = this.options;
|
|
705
|
+
const {
|
|
706
|
+
id: definitionId,
|
|
707
|
+
cues = []
|
|
708
|
+
} = this.definition;
|
|
709
|
+
this.debug('Getting started session...');
|
|
710
|
+
const item = await this.store.findItem('sessions', {
|
|
711
|
+
definition: definitionId,
|
|
712
|
+
started: true,
|
|
713
|
+
ended: false
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
if (item !== null) {
|
|
717
|
+
this.debug('Session found %s.', item.id);
|
|
718
|
+
return item;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
const handle = getHandle(definitionId);
|
|
722
|
+
this.debug('Creating new session "%s"...', handle);
|
|
723
|
+
const newItem = await this.store.addItem('sessions', {
|
|
724
|
+
id: handle,
|
|
725
|
+
definition: definitionId,
|
|
726
|
+
started: false,
|
|
727
|
+
ended: false,
|
|
728
|
+
cue: null
|
|
729
|
+
});
|
|
730
|
+
this.debug('Setting initial "%s" cues...', cues !== null ? cues.length : 0);
|
|
731
|
+
await this.setCues(cues, newItem);
|
|
732
|
+
this.debug('New session "%s"', newItem !== null ? newItem.id : null);
|
|
733
|
+
return newItem;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
async resetSession() {
|
|
737
|
+
const {
|
|
738
|
+
id
|
|
739
|
+
} = this.session;
|
|
740
|
+
this.debug('Resetting session "%s"...', id);
|
|
741
|
+
this.session = await this.store.updateItem('sessions', id, {
|
|
742
|
+
started: true,
|
|
743
|
+
ended: false,
|
|
744
|
+
cue: null
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
async startSession() {
|
|
749
|
+
const {
|
|
750
|
+
id
|
|
751
|
+
} = this.session;
|
|
752
|
+
this.debug('Starting session "%s"...', id);
|
|
753
|
+
this.session = await this.store.updateItem('sessions', id, {
|
|
754
|
+
started: true,
|
|
755
|
+
ended: false
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
async endSession() {
|
|
760
|
+
const {
|
|
761
|
+
id
|
|
762
|
+
} = this.session;
|
|
763
|
+
this.debug('Ending session "%s"...', id);
|
|
764
|
+
this.session = await this.store.updateItem('sessions', id, {
|
|
765
|
+
started: false,
|
|
766
|
+
ended: true,
|
|
767
|
+
cue: null
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
async setSessionCue(cue, sessionData = null) {
|
|
772
|
+
const {
|
|
773
|
+
id
|
|
774
|
+
} = this.session;
|
|
775
|
+
this.debug('Setting session "%s", cue... %o', id, cue);
|
|
776
|
+
this.session = await this.store.updateItem('sessions', id, {
|
|
777
|
+
cue: cue !== null ? cue.id : null,
|
|
778
|
+
data: sessionData
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
async getSessionCue() {
|
|
783
|
+
const cues = await this.getCues();
|
|
784
|
+
const {
|
|
785
|
+
cue: cueId = null
|
|
786
|
+
} = this.session || {};
|
|
787
|
+
const cue = cues.find(c => c.id === cueId) || null;
|
|
788
|
+
return cue;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
getSessionData() {
|
|
792
|
+
const {
|
|
793
|
+
data = null
|
|
794
|
+
} = this.session || {};
|
|
795
|
+
return data;
|
|
796
|
+
}
|
|
797
|
+
/**
|
|
798
|
+
* Call inputs and outputs
|
|
799
|
+
*/
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
initInputs() {
|
|
803
|
+
this.debug('Init inputs...');
|
|
804
|
+
return Promise.all(this.inputs.map(it => typeof it.init !== 'undefined' ? it.init() : Promise.resolve()));
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
initOutputs() {
|
|
808
|
+
this.debug('Init outputs...');
|
|
809
|
+
return Promise.all(this.outputs.map(it => typeof it.init !== 'undefined' ? it.init() : Promise.resolve()));
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
startInputs() {
|
|
813
|
+
this.debug('Start inputs... %s', this.inputs.length);
|
|
814
|
+
return Promise.all(this.inputs.map(it => typeof it.start !== 'undefined' ? it.start() : Promise.resolve()));
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
startOutputs() {
|
|
818
|
+
this.debug('Start outputs... %s', this.outputs.length);
|
|
819
|
+
return Promise.all(this.outputs.map(it => typeof it.start !== 'undefined' ? it.start() : Promise.resolve()));
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
sendCueToOutputs(cue, sessionData = null) {
|
|
823
|
+
this.debug('Send cue to outputs: %O %O', cue, sessionData);
|
|
824
|
+
return Promise.all(this.outputs.map(it => typeof it.cue !== 'undefined' ? it.cue(cue, sessionData) : Promise.resolve()));
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
sendCuesToOutputs(cues) {
|
|
828
|
+
this.debug('Send cues to outputs: %O', cues);
|
|
829
|
+
return Promise.all(this.outputs.map(it => typeof it.cues !== 'undefined' ? it.cues(cues) : Promise.resolve()));
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
sendInteractToOutputs(data, interactionId) {
|
|
833
|
+
this.debug('Send interact to outputs: %O %s', data, interactionId);
|
|
834
|
+
return Promise.all(this.outputs.map(it => typeof it.interact !== 'undefined' ? it.interact(data, interactionId) : Promise.resolve()));
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
sendInteractionToOutputs(interaction) {
|
|
838
|
+
this.debug('Send interaction to outputs: %O', interaction);
|
|
839
|
+
return Promise.all(this.outputs.map(it => typeof it.interaction !== 'undefined' ? it.interaction(interaction) : Promise.resolve()));
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
sendUninteractionToOutputs(interactionId) {
|
|
843
|
+
this.debug('Send interact to outputs: %O %s', interactionId);
|
|
844
|
+
return Promise.all(this.outputs.map(it => typeof it.uninteraction !== 'undefined' ? it.uninteraction(interactionId) : Promise.resolve()));
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
sendUninteractionsToOutputs(interactionIds) {
|
|
848
|
+
this.debug('Send uninteractions to outputs: %O', interactionIds);
|
|
849
|
+
return Promise.all(this.outputs.map(it => typeof it.uninteractions !== 'undefined' ? it.uninteractions(interactionIds) : Promise.resolve()));
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
sendCommandToOutputs(command, ...args) {
|
|
853
|
+
this.debug(`Send command to outputs: %s %o`, command, args);
|
|
854
|
+
return Promise.all(this.outputs.map(it => typeof it.command !== 'undefined' ? it.command(command, ...args) : Promise.resolve()));
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
stopInputs() {
|
|
858
|
+
this.debug('Stop inputs...');
|
|
859
|
+
return Promise.all(this.inputs.map(it => typeof it.stop !== 'undefined' ? it.stop() : Promise.resolve()));
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
stopOutputs() {
|
|
863
|
+
this.debug('Stop inputs...');
|
|
864
|
+
return Promise.all(this.outputs.map(it => typeof it.stop !== 'undefined' ? it.stop() : Promise.resolve()));
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
destroyInputs() {
|
|
868
|
+
this.debug('Destroying inputs...');
|
|
869
|
+
return Promise.all(this.inputs.map(it => typeof it.destroy !== 'undefined' ? it.destroy() : Promise.resolve()));
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
destroyOutputs() {
|
|
873
|
+
this.debug('Destroying outputs...');
|
|
874
|
+
return Promise.all(this.outputs.map(it => typeof it.destroy !== 'undefined' ? it.destroy() : Promise.resolve()));
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
const terminate = (server = null, options = {
|
|
880
|
+
coredump: false,
|
|
881
|
+
timeout: 500
|
|
882
|
+
}) => {
|
|
883
|
+
const debug = createDebug__default["default"]('cuecue:app:terminate');
|
|
884
|
+
|
|
885
|
+
const exit = code => {
|
|
886
|
+
if (options.coredump) {
|
|
887
|
+
process.abort();
|
|
888
|
+
} else {
|
|
889
|
+
process.exit(code);
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
|
|
893
|
+
return (code, reason) => err => {
|
|
894
|
+
debug('Err: %O', err, code, reason);
|
|
895
|
+
|
|
896
|
+
if (err && err instanceof Error) {
|
|
897
|
+
// Log error information somewhere
|
|
898
|
+
debug('Exit: %s %O', err.message, err.stack);
|
|
899
|
+
debug('Trace: %s %s', code, reason);
|
|
900
|
+
} // Attempt a graceful shutdown
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
if (server !== null) {
|
|
904
|
+
debug('Server: %O', server);
|
|
905
|
+
server.destroy(); // could also call exit on callback here
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
setTimeout(exit, options.timeout).unref();
|
|
909
|
+
};
|
|
910
|
+
};
|
|
911
|
+
var utils = {
|
|
912
|
+
terminate
|
|
913
|
+
};
|
|
914
|
+
|
|
915
|
+
exports.MemoryStore = MemoryStore;
|
|
916
|
+
exports["default"] = Application;
|
|
917
|
+
exports.utils = utils;
|