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