@cuecue/app 0.0.23 → 0.0.27

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