@cuecue/app 0.0.26 → 0.0.30

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 +917 -0
  2. package/package.json +8 -6
  3. package/lib/index.js +0 -1916
package/lib/index.js DELETED
@@ -1,1916 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
6
- var _regeneratorRuntime = require('@babel/runtime/regenerator');
7
- var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
8
- var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
9
- var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
10
- var _createClass = require('@babel/runtime/helpers/createClass');
11
- var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
12
- var _inherits = require('@babel/runtime/helpers/inherits');
13
- var _createSuper = require('@babel/runtime/helpers/createSuper');
14
- var StateMachine = require('javascript-state-machine');
15
- var EventEmitter = require('wolfy87-eventemitter');
16
- var createDebug = require('debug');
17
- var dayjs = require('dayjs');
18
- var uuid = require('uuid');
19
- var _defineProperty = require('@babel/runtime/helpers/defineProperty');
20
- var matches = require('lodash/matches');
21
- var isObject = require('lodash/isObject');
22
-
23
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
24
-
25
- var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
26
- var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
27
- var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
28
- var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);
29
- var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
30
- var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
31
- var _assertThisInitialized__default = /*#__PURE__*/_interopDefaultLegacy(_assertThisInitialized);
32
- var _inherits__default = /*#__PURE__*/_interopDefaultLegacy(_inherits);
33
- var _createSuper__default = /*#__PURE__*/_interopDefaultLegacy(_createSuper);
34
- var StateMachine__default = /*#__PURE__*/_interopDefaultLegacy(StateMachine);
35
- var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
36
- var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
37
- var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
38
- var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
39
- var matches__default = /*#__PURE__*/_interopDefaultLegacy(matches);
40
- var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
41
-
42
- var MemoryStore = /*#__PURE__*/function (_EventEmitter) {
43
- _inherits__default['default'](MemoryStore, _EventEmitter);
44
-
45
- var _super = _createSuper__default['default'](MemoryStore);
46
-
47
- function MemoryStore() {
48
- var _this;
49
-
50
- var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
51
-
52
- _classCallCheck__default['default'](this, MemoryStore);
53
-
54
- _this = _super.call(this);
55
- _this.data = data || {};
56
- _this.debug = createDebug__default['default']('cuecue:store:memory');
57
- return _this;
58
- }
59
-
60
- _createClass__default['default'](MemoryStore, [{
61
- key: "findItem",
62
- value: function findItem(type, id) {
63
- var items = this.data[type] || [];
64
- return items.find(matches__default['default'](isObject__default['default'](id) ? id : {
65
- id: id
66
- })) || null;
67
- }
68
- }, {
69
- key: "addItem",
70
- value: function addItem(type, data) {
71
- var id = data.id || uuid.v4();
72
-
73
- var savedItem = _objectSpread__default['default'](_objectSpread__default['default']({}, data), {}, {
74
- id: id
75
- });
76
-
77
- this.data = _objectSpread__default['default'](_objectSpread__default['default']({}, this.data), {}, _defineProperty__default['default']({}, type, [].concat(_toConsumableArray__default['default']((this.data[type] || []).filter(function (it) {
78
- return it.id !== id;
79
- })), [savedItem])));
80
- return savedItem;
81
- }
82
- }, {
83
- key: "addItems",
84
- value: function addItems(type, items) {
85
- var _this2 = this;
86
-
87
- var savedItems = items.map(function (it) {
88
- return _this2.addItem(type, it);
89
- });
90
- return savedItems;
91
- }
92
- }, {
93
- key: "updateItem",
94
- value: function updateItem(type, id, data) {
95
- var index = (this.data[type] || []).findIndex(function (it) {
96
- return it.id === id;
97
- });
98
-
99
- if (index === -1) {
100
- return null;
101
- }
102
-
103
- var newData = _objectSpread__default['default'](_objectSpread__default['default']({}, this.data[type][index]), data);
104
-
105
- this.data = _objectSpread__default['default'](_objectSpread__default['default']({}, this.data), {}, _defineProperty__default['default']({}, type, (this.data[type] || []).map(function (it) {
106
- return it.id === id ? newData : it;
107
- })));
108
- return newData;
109
- }
110
- }, {
111
- key: "deleteItem",
112
- value: function deleteItem(type, id) {
113
- this.data = _objectSpread__default['default'](_objectSpread__default['default']({}, this.data), {}, _defineProperty__default['default']({}, type, (this.data[type] || []).filter(function (it) {
114
- return it.id !== id;
115
- })));
116
- }
117
- }, {
118
- key: "deleteItems",
119
- value: function deleteItems(type) {
120
- var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
121
- var match = matches__default['default'](params);
122
- this.data = _objectSpread__default['default'](_objectSpread__default['default']({}, this.data), {}, _defineProperty__default['default']({}, type, (this.data[type] || []).filter(function (it) {
123
- return !match(it);
124
- })));
125
- }
126
- }, {
127
- key: "getItems",
128
- value: function getItems(type) {
129
- var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
130
- var match = matches__default['default'](params);
131
- return (this.data[type] || []).filter(function (it) {
132
- return match(it);
133
- });
134
- }
135
- }]);
136
-
137
- return MemoryStore;
138
- }(EventEmitter__default['default']);
139
-
140
- var Application = /*#__PURE__*/function (_EventEmitter) {
141
- _inherits__default['default'](Application, _EventEmitter);
142
-
143
- var _super = _createSuper__default['default'](Application);
144
-
145
- function Application(definition) {
146
- var _this;
147
-
148
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
149
-
150
- _classCallCheck__default['default'](this, Application);
151
-
152
- _this = _super.call(this);
153
- _this.definition = definition;
154
- _this.options = _objectSpread__default['default']({
155
- store: new MemoryStore(),
156
- debugFunction: createDebug__default['default']('cuecue:app'),
157
- getHandle: function getHandle(id) {
158
- return "".concat(id, "_").concat(dayjs__default['default']().format('YYYY_MM_DD'), "_").concat(Date.now().toString(36));
159
- },
160
- validateCommand: function validateCommand(command) {
161
- return ['start', 'end', 'cue', 'uncue', 'define', 'interact', 'reset', 'resetInteractions', 'restart', 'kill'].indexOf(command) !== -1;
162
- }
163
- }, opts);
164
- var store = _this.options.store;
165
- _this.debug = createDebug__default['default']('cuecue:app');
166
- _this.store = store;
167
- _this.session = null;
168
- _this.statefulCue = null;
169
- _this.cueDurationInterval = null;
170
- _this.interactions = [];
171
- _this.inputs = [];
172
- _this.outputs = [];
173
- _this.onInit = _this.onInit.bind(_assertThisInitialized__default['default'](_this));
174
- _this.onInitialized = _this.onInitialized.bind(_assertThisInitialized__default['default'](_this));
175
- _this.onDestroy = _this.onDestroy.bind(_assertThisInitialized__default['default'](_this));
176
- _this.onDestroyed = _this.onDestroyed.bind(_assertThisInitialized__default['default'](_this));
177
- _this.onStart = _this.onStart.bind(_assertThisInitialized__default['default'](_this));
178
- _this.onIdle = _this.onIdle.bind(_assertThisInitialized__default['default'](_this));
179
- _this.onUncue = _this.onUncue.bind(_assertThisInitialized__default['default'](_this));
180
- _this.onEnd = _this.onEnd.bind(_assertThisInitialized__default['default'](_this));
181
- _this.onCue = _this.onCue.bind(_assertThisInitialized__default['default'](_this));
182
- _this.onCued = _this.onCued.bind(_assertThisInitialized__default['default'](_this));
183
- _this.onStop = _this.onStop.bind(_assertThisInitialized__default['default'](_this));
184
- _this.onStopped = _this.onStopped.bind(_assertThisInitialized__default['default'](_this));
185
- _this.onInputCommand = _this.onInputCommand.bind(_assertThisInitialized__default['default'](_this));
186
- _this.onAfterTransition = _this.onAfterTransition.bind(_assertThisInitialized__default['default'](_this));
187
- _this.onInvalidTransition = _this.onInvalidTransition.bind(_assertThisInitialized__default['default'](_this));
188
- _this.onPendingTransition = _this.onPendingTransition.bind(_assertThisInitialized__default['default'](_this));
189
- _this.state = new StateMachine__default['default']({
190
- transitions: [{
191
- name: 'init',
192
- from: 'none',
193
- to: 'initialized'
194
- }, {
195
- name: 'destroy',
196
- from: '*',
197
- to: 'none'
198
- }, {
199
- name: 'start',
200
- from: ['initialized', 'stopped', 'ended'],
201
- to: 'idle'
202
- }, {
203
- name: 'stop',
204
- from: ['idle', 'cued'],
205
- to: 'stopped'
206
- }, {
207
- name: 'cue',
208
- from: ['idle', 'cued'],
209
- to: 'cued'
210
- }, {
211
- name: 'uncue',
212
- from: ['idle', 'cued'],
213
- to: 'idle'
214
- }, {
215
- name: 'end',
216
- from: ['idle', 'cued'],
217
- to: 'ended'
218
- }],
219
- methods: {
220
- onBeforeInit: _this.onInit,
221
- onAfterInit: _this.onInitialized,
222
- onBeforeDestroy: _this.onDestroy,
223
- onAfterDestroy: _this.onDestroyed,
224
- onBeforeStart: _this.onStart,
225
- onAfterStart: _this.onIdle,
226
- onBeforeCue: _this.onCue,
227
- onAfterCue: _this.onCued,
228
- onBeforeUncue: _this.onUncue,
229
- onBeforeEnd: _this.onEnd,
230
- onBeforeStop: _this.onStop,
231
- onAfterStop: _this.onStopped,
232
- onAfterTransition: _this.onAfterTransition,
233
- onInvalidTransition: _this.onInvalidTransition,
234
- onPendingTransition: _this.onPendingTransition
235
- }
236
- });
237
- return _this;
238
- }
239
-
240
- _createClass__default['default'](Application, [{
241
- key: "init",
242
- value: function init() {
243
- return this.state.init();
244
- }
245
- }, {
246
- key: "destroy",
247
- value: function destroy() {
248
- return this.state.destroy();
249
- }
250
- }, {
251
- key: "start",
252
- value: function () {
253
- var _start = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee() {
254
- return _regeneratorRuntime__default['default'].wrap(function _callee$(_context) {
255
- while (1) {
256
- switch (_context.prev = _context.next) {
257
- case 0:
258
- if (!this.stateless()) {
259
- _context.next = 4;
260
- break;
261
- }
262
-
263
- this.debug('First init');
264
- _context.next = 4;
265
- return this.init();
266
-
267
- case 4:
268
- return _context.abrupt("return", this.state.start());
269
-
270
- case 5:
271
- case "end":
272
- return _context.stop();
273
- }
274
- }
275
- }, _callee, this);
276
- }));
277
-
278
- function start() {
279
- return _start.apply(this, arguments);
280
- }
281
-
282
- return start;
283
- }()
284
- }, {
285
- key: "stop",
286
- value: function stop() {
287
- return this.state.stop();
288
- }
289
- }, {
290
- key: "end",
291
- value: function end() {
292
- return this.state.end();
293
- }
294
- }, {
295
- key: "cue",
296
- value: function cue(id) {
297
- var sessionData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
298
- return this.state.cue(id, sessionData);
299
- }
300
- }, {
301
- key: "interact",
302
- value: function () {
303
- var _interact = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee2(data) {
304
- var interactionId,
305
- interaction,
306
- _args2 = arguments;
307
- return _regeneratorRuntime__default['default'].wrap(function _callee2$(_context2) {
308
- while (1) {
309
- switch (_context2.prev = _context2.next) {
310
- case 0:
311
- interactionId = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : null;
312
- this.sendInteractToOutputs(data, interactionId);
313
- _context2.next = 4;
314
- return this.ensureInteraction(data, interactionId || uuid.v4());
315
-
316
- case 4:
317
- interaction = _context2.sent;
318
- this.sendInteractionToOutputs(interaction);
319
- return _context2.abrupt("return", interaction);
320
-
321
- case 7:
322
- case "end":
323
- return _context2.stop();
324
- }
325
- }
326
- }, _callee2, this);
327
- }));
328
-
329
- function interact(_x) {
330
- return _interact.apply(this, arguments);
331
- }
332
-
333
- return interact;
334
- }()
335
- }, {
336
- key: "uncue",
337
- value: function uncue() {
338
- return this.state.uncue();
339
- }
340
- }, {
341
- key: "define",
342
- value: function () {
343
- var _define = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee3() {
344
- var cues,
345
- _len,
346
- newCues,
347
- _key,
348
- mergedArray,
349
- set,
350
- mergedCues,
351
- list,
352
- _args3 = arguments;
353
-
354
- return _regeneratorRuntime__default['default'].wrap(function _callee3$(_context3) {
355
- while (1) {
356
- switch (_context3.prev = _context3.next) {
357
- case 0:
358
- _context3.next = 2;
359
- return this.getCues();
360
-
361
- case 2:
362
- cues = _context3.sent;
363
-
364
- for (_len = _args3.length, newCues = new Array(_len), _key = 0; _key < _len; _key++) {
365
- newCues[_key] = _args3[_key];
366
- }
367
-
368
- mergedArray = [].concat(newCues, _toConsumableArray__default['default'](cues)); // The order matters
369
-
370
- this.debug('humm %O', newCues, cues);
371
- set = new Set();
372
- mergedCues = mergedArray.filter(function (item) {
373
- if (!set.has(item.id)) {
374
- set.add(item.id);
375
- return true;
376
- }
377
-
378
- return false;
379
- }, set);
380
- this.debug('ok %O', mergedCues);
381
- _context3.next = 11;
382
- return this.setCues(_toConsumableArray__default['default'](mergedCues));
383
-
384
- case 11:
385
- list = _context3.sent;
386
- _context3.next = 14;
387
- return this.sendCuesToOutputs(list);
388
-
389
- case 14:
390
- case "end":
391
- return _context3.stop();
392
- }
393
- }
394
- }, _callee3, this);
395
- }));
396
-
397
- function define() {
398
- return _define.apply(this, arguments);
399
- }
400
-
401
- return define;
402
- }()
403
- }, {
404
- key: "reset",
405
- value: function () {
406
- var _reset = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee4() {
407
- return _regeneratorRuntime__default['default'].wrap(function _callee4$(_context4) {
408
- while (1) {
409
- switch (_context4.prev = _context4.next) {
410
- case 0:
411
- _context4.next = 2;
412
- return this.resetSessionInteractions();
413
-
414
- case 2:
415
- _context4.next = 4;
416
- return this.resetSession();
417
-
418
- case 4:
419
- _context4.next = 6;
420
- return this.uncue();
421
-
422
- case 6:
423
- this.sendCommandToOutputs('reset');
424
-
425
- case 7:
426
- case "end":
427
- return _context4.stop();
428
- }
429
- }
430
- }, _callee4, this);
431
- }));
432
-
433
- function reset() {
434
- return _reset.apply(this, arguments);
435
- }
436
-
437
- return reset;
438
- }()
439
- }, {
440
- key: "restart",
441
- value: function () {
442
- var _restart = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee5() {
443
- return _regeneratorRuntime__default['default'].wrap(function _callee5$(_context5) {
444
- while (1) {
445
- switch (_context5.prev = _context5.next) {
446
- case 0:
447
- this.destroy();
448
- _context5.next = 3;
449
- return this.start();
450
-
451
- case 3:
452
- case "end":
453
- return _context5.stop();
454
- }
455
- }
456
- }, _callee5, this);
457
- }));
458
-
459
- function restart() {
460
- return _restart.apply(this, arguments);
461
- }
462
-
463
- return restart;
464
- }() // eslint-disable-next-line class-methods-use-this
465
-
466
- }, {
467
- key: "kill",
468
- value: function kill() {
469
- // Test this with something else than nodemon
470
- process.kill(process.pid, 'SIGUSR2');
471
- }
472
- }, {
473
- key: "stateless",
474
- value: function stateless() {
475
- return this.state.is('none');
476
- }
477
- }, {
478
- key: "initialized",
479
- value: function initialized() {
480
- return this.state.is('initialized');
481
- }
482
- }, {
483
- key: "started",
484
- value: function started() {
485
- return this.state.is('idle');
486
- }
487
- }, {
488
- key: "idle",
489
- value: function idle() {
490
- return this.state.is('idle');
491
- }
492
- }, {
493
- key: "stopped",
494
- value: function stopped() {
495
- return this.state.is('stopped');
496
- }
497
- }, {
498
- key: "ended",
499
- value: function ended() {
500
- return this.state.is('ended');
501
- }
502
- }, {
503
- key: "input",
504
- value: function input(_input) {
505
- this.inputs = [].concat(_toConsumableArray__default['default'](this.inputs), [_input]);
506
-
507
- _input.on('command', this.onInputCommand);
508
- }
509
- }, {
510
- key: "getCurrentCue",
511
- value: function getCurrentCue() {
512
- return this.statefulCue;
513
- }
514
- }, {
515
- key: "getCurrentCueId",
516
- value: function getCurrentCueId() {
517
- var _ref = this.statefulCue || {},
518
- _ref$id = _ref.id,
519
- id = _ref$id === void 0 ? null : _ref$id;
520
-
521
- return id;
522
- }
523
- }, {
524
- key: "removeInput",
525
- value: function removeInput(input) {
526
- input.off('command', this.onInputCommand);
527
- this.inputs = this.inputs.filter(function (it) {
528
- return it !== input;
529
- });
530
- }
531
- }, {
532
- key: "output",
533
- value: function output(_output) {
534
- this.outputs = [].concat(_toConsumableArray__default['default'](this.outputs), [_output]);
535
- }
536
- }, {
537
- key: "removeOutput",
538
- value: function removeOutput(output) {
539
- this.outputs = this.outputs.filter(function (it) {
540
- return it !== output;
541
- });
542
- }
543
- }, {
544
- key: "resetInteractions",
545
- value: function () {
546
- var _resetInteractions = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee6() {
547
- var _len2,
548
- args,
549
- _key2,
550
- ids,
551
- _args6 = arguments;
552
-
553
- return _regeneratorRuntime__default['default'].wrap(function _callee6$(_context6) {
554
- while (1) {
555
- switch (_context6.prev = _context6.next) {
556
- case 0:
557
- for (_len2 = _args6.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
558
- args[_key2] = _args6[_key2];
559
- }
560
-
561
- ids = args || [];
562
- this.resetInteractidonsByIds(ids);
563
- _context6.next = 5;
564
- return this.sendUninteractionsToOutputs(ids);
565
-
566
- case 5:
567
- case "end":
568
- return _context6.stop();
569
- }
570
- }
571
- }, _callee6, this);
572
- }));
573
-
574
- function resetInteractions() {
575
- return _resetInteractions.apply(this, arguments);
576
- }
577
-
578
- return resetInteractions;
579
- }()
580
- }, {
581
- key: "resetSessionInteractions",
582
- value: function () {
583
- var _resetSessionInteractions = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee7() {
584
- return _regeneratorRuntime__default['default'].wrap(function _callee7$(_context7) {
585
- while (1) {
586
- switch (_context7.prev = _context7.next) {
587
- case 0:
588
- _context7.next = 2;
589
- return this.store.deleteItems('interactions', {
590
- sessionId: this.session.id
591
- });
592
-
593
- case 2:
594
- case "end":
595
- return _context7.stop();
596
- }
597
- }
598
- }, _callee7, this);
599
- }));
600
-
601
- function resetSessionInteractions() {
602
- return _resetSessionInteractions.apply(this, arguments);
603
- }
604
-
605
- return resetSessionInteractions;
606
- }()
607
- }, {
608
- key: "resetInteractidonsByIds",
609
- value: function () {
610
- var _resetInteractidonsByIds = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee8(ids) {
611
- var _this2 = this;
612
-
613
- return _regeneratorRuntime__default['default'].wrap(function _callee8$(_context8) {
614
- while (1) {
615
- switch (_context8.prev = _context8.next) {
616
- case 0:
617
- return _context8.abrupt("return", Promise.all(ids.map(function (id) {
618
- return _this2.store.deleteItems('interactions', {
619
- sessionId: _this2.session.id,
620
- externalId: id
621
- });
622
- })));
623
-
624
- case 1:
625
- case "end":
626
- return _context8.stop();
627
- }
628
- }
629
- }, _callee8);
630
- }));
631
-
632
- function resetInteractidonsByIds(_x2) {
633
- return _resetInteractidonsByIds.apply(this, arguments);
634
- }
635
-
636
- return resetInteractidonsByIds;
637
- }()
638
- }, {
639
- key: "getInteractions",
640
- value: function getInteractions() {
641
- var _ref2 = this.session || {},
642
- _ref2$id = _ref2.id,
643
- sessionId = _ref2$id === void 0 ? null : _ref2$id;
644
-
645
- return this.store.getItems('interactions', {
646
- sessionId: sessionId
647
- });
648
- }
649
- }, {
650
- key: "getInteractionsByCue",
651
- value: function getInteractionsByCue(cueId) {
652
- var _ref3 = this.session || {},
653
- _ref3$id = _ref3.id,
654
- sessionId = _ref3$id === void 0 ? null : _ref3$id;
655
-
656
- return this.store.getItems('interactions', {
657
- sessionId: sessionId,
658
- cueId: cueId
659
- });
660
- }
661
- }, {
662
- key: "getInteractionsByInteractionId",
663
- value: function getInteractionsByInteractionId(interactionId) {
664
- var _ref4 = this.session || {},
665
- _ref4$id = _ref4.id,
666
- sessionId = _ref4$id === void 0 ? null : _ref4$id;
667
-
668
- return this.store.getItems('interactions', {
669
- sessionId: sessionId,
670
- interactionId: interactionId
671
- });
672
- }
673
- }, {
674
- key: "getInteractionsByUser",
675
- value: function getInteractionsByUser(userId) {
676
- var _ref5 = this.session || {},
677
- _ref5$id = _ref5.id,
678
- sessionId = _ref5$id === void 0 ? null : _ref5$id;
679
-
680
- return this.store.getItems('interactions', {
681
- sessionId: sessionId,
682
- userId: userId
683
- });
684
- }
685
- }, {
686
- key: "getCues",
687
- value: function getCues(session) {
688
- var _ref6 = this.session || session || {},
689
- _ref6$id = _ref6.id,
690
- sessionId = _ref6$id === void 0 ? null : _ref6$id;
691
-
692
- return this.store.getItems('cues', {
693
- sessionId: sessionId
694
- });
695
- }
696
- }, {
697
- key: "setCues",
698
- value: function () {
699
- var _setCues = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee9(cues, session) {
700
- var _ref7, _ref7$id, sessionId;
701
-
702
- return _regeneratorRuntime__default['default'].wrap(function _callee9$(_context9) {
703
- while (1) {
704
- switch (_context9.prev = _context9.next) {
705
- case 0:
706
- _ref7 = this.session || session || {}, _ref7$id = _ref7.id, sessionId = _ref7$id === void 0 ? null : _ref7$id;
707
-
708
- if (!(sessionId !== null)) {
709
- _context9.next = 7;
710
- break;
711
- }
712
-
713
- _context9.next = 4;
714
- return this.store.deleteItems('cues', {
715
- sessionId: sessionId
716
- });
717
-
718
- case 4:
719
- _context9.next = 6;
720
- return this.store.addItems('cues', cues.map(function (cueDef) {
721
- return _objectSpread__default['default']({
722
- sessionId: sessionId
723
- }, cueDef);
724
- }));
725
-
726
- case 6:
727
- return _context9.abrupt("return", this.store.getItems('cues', {
728
- sessionId: sessionId
729
- }));
730
-
731
- case 7:
732
- return _context9.abrupt("return", Promise.resolve());
733
-
734
- case 8:
735
- case "end":
736
- return _context9.stop();
737
- }
738
- }
739
- }, _callee9, this);
740
- }));
741
-
742
- function setCues(_x3, _x4) {
743
- return _setCues.apply(this, arguments);
744
- }
745
-
746
- return setCues;
747
- }()
748
- }, {
749
- key: "onInvalidTransition",
750
- value: function () {
751
- var _onInvalidTransition = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee10(transition, from, to) {
752
- return _regeneratorRuntime__default['default'].wrap(function _callee10$(_context10) {
753
- while (1) {
754
- switch (_context10.prev = _context10.next) {
755
- case 0:
756
- this.debug('Invalid transition: %s from: %s to: %s', transition, from, to);
757
-
758
- case 1:
759
- case "end":
760
- return _context10.stop();
761
- }
762
- }
763
- }, _callee10, this);
764
- }));
765
-
766
- function onInvalidTransition(_x5, _x6, _x7) {
767
- return _onInvalidTransition.apply(this, arguments);
768
- }
769
-
770
- return onInvalidTransition;
771
- }()
772
- }, {
773
- key: "onPendingTransition",
774
- value: function onPendingTransition(transition, from, to) {
775
- var _this3 = this;
776
-
777
- this.debug('Pending transition: %s from: %s to: %s', transition, from, to);
778
- return new Promise(function (resolve) {
779
- if (transition === 'init') {
780
- _this3.once('initialized', resolve);
781
- }
782
- });
783
- }
784
- }, {
785
- key: "onAfterTransition",
786
- value: function () {
787
- var _onAfterTransition = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee11(lastTransition) {
788
- var _ref8, _ref8$id, sessionId, _ref9, transition, from, to;
789
-
790
- return _regeneratorRuntime__default['default'].wrap(function _callee11$(_context11) {
791
- while (1) {
792
- switch (_context11.prev = _context11.next) {
793
- case 0:
794
- _ref8 = this.session || {}, _ref8$id = _ref8.id, sessionId = _ref8$id === void 0 ? null : _ref8$id;
795
- _ref9 = lastTransition || {}, transition = _ref9.transition, from = _ref9.from, to = _ref9.to;
796
- this.debug('After transition: %O from: %s to: %s', transition, from, to);
797
-
798
- if (!(transition !== 'init')) {
799
- _context11.next = 6;
800
- break;
801
- }
802
-
803
- _context11.next = 6;
804
- return this.sendCommandToOutputs('state', {
805
- sessionId: sessionId,
806
- currentState: to
807
- });
808
-
809
- case 6:
810
- case "end":
811
- return _context11.stop();
812
- }
813
- }
814
- }, _callee11, this);
815
- }));
816
-
817
- function onAfterTransition(_x8) {
818
- return _onAfterTransition.apply(this, arguments);
819
- }
820
-
821
- return onAfterTransition;
822
- }()
823
- }, {
824
- key: "onInit",
825
- value: function () {
826
- var _onInit = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee12() {
827
- return _regeneratorRuntime__default['default'].wrap(function _callee12$(_context12) {
828
- while (1) {
829
- switch (_context12.prev = _context12.next) {
830
- case 0:
831
- this.emit('init');
832
-
833
- if (!(this.store !== null && typeof this.store.init !== 'undefined')) {
834
- _context12.next = 4;
835
- break;
836
- }
837
-
838
- _context12.next = 4;
839
- return this.store.init();
840
-
841
- case 4:
842
- _context12.next = 6;
843
- return this.initInputs();
844
-
845
- case 6:
846
- _context12.next = 8;
847
- return this.initOutputs();
848
-
849
- case 8:
850
- case "end":
851
- return _context12.stop();
852
- }
853
- }
854
- }, _callee12, this);
855
- }));
856
-
857
- function onInit() {
858
- return _onInit.apply(this, arguments);
859
- }
860
-
861
- return onInit;
862
- }()
863
- }, {
864
- key: "onInitialized",
865
- value: function onInitialized() {
866
- var _this4 = this;
867
-
868
- this.debug('initialized');
869
- return process.nextTick(function () {
870
- return _this4.emit('initialized');
871
- });
872
- }
873
- }, {
874
- key: "onDestroy",
875
- value: function () {
876
- var _onDestroy = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee13() {
877
- return _regeneratorRuntime__default['default'].wrap(function _callee13$(_context13) {
878
- while (1) {
879
- switch (_context13.prev = _context13.next) {
880
- case 0:
881
- this.debug('destroying...');
882
- this.emit('destroy');
883
- this.destroyInputs();
884
- this.destroyOutputs();
885
-
886
- if (!(this.store !== null && typeof this.store.destroy !== 'undefined')) {
887
- _context13.next = 7;
888
- break;
889
- }
890
-
891
- _context13.next = 7;
892
- return this.store.destroy();
893
-
894
- case 7:
895
- this.event = null;
896
- this.inputs = [];
897
- this.outputs = [];
898
-
899
- case 10:
900
- case "end":
901
- return _context13.stop();
902
- }
903
- }
904
- }, _callee13, this);
905
- }));
906
-
907
- function onDestroy() {
908
- return _onDestroy.apply(this, arguments);
909
- }
910
-
911
- return onDestroy;
912
- }()
913
- }, {
914
- key: "onDestroyed",
915
- value: function onDestroyed() {
916
- var _this5 = this;
917
-
918
- this.debug('destroyed');
919
- return process.nextTick(function () {
920
- return _this5.emit('destroyed');
921
- });
922
- }
923
- }, {
924
- key: "onStart",
925
- value: function () {
926
- var _onStart = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee14() {
927
- var cues;
928
- return _regeneratorRuntime__default['default'].wrap(function _callee14$(_context14) {
929
- while (1) {
930
- switch (_context14.prev = _context14.next) {
931
- case 0:
932
- this.debug('starting...');
933
- _context14.next = 3;
934
- return this.ensureSession();
935
-
936
- case 3:
937
- this.session = _context14.sent;
938
- this.debug('Session #%s', this.session.id);
939
- this.debug('Session WTF %O', this.session);
940
- _context14.prev = 6;
941
- _context14.next = 9;
942
- return this.startInputs();
943
-
944
- case 9:
945
- _context14.next = 11;
946
- return this.startOutputs();
947
-
948
- case 11:
949
- _context14.next = 16;
950
- break;
951
-
952
- case 13:
953
- _context14.prev = 13;
954
- _context14.t0 = _context14["catch"](6);
955
- this.debug('Session error %O', _context14.t0);
956
-
957
- case 16:
958
- _context14.next = 18;
959
- return this.getCues();
960
-
961
- case 18:
962
- cues = _context14.sent;
963
- this.sendCuesToOutputs(cues);
964
- this.sendCommandToOutputs('start');
965
- this.emit('start');
966
-
967
- case 22:
968
- case "end":
969
- return _context14.stop();
970
- }
971
- }
972
- }, _callee14, this, [[6, 13]]);
973
- }));
974
-
975
- function onStart() {
976
- return _onStart.apply(this, arguments);
977
- }
978
-
979
- return onStart;
980
- }()
981
- }, {
982
- key: "onIdle",
983
- value: function () {
984
- var _onIdle = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee15() {
985
- var _this6 = this;
986
-
987
- return _regeneratorRuntime__default['default'].wrap(function _callee15$(_context15) {
988
- while (1) {
989
- switch (_context15.prev = _context15.next) {
990
- case 0:
991
- if (!(!this.session.started && !this.session.ended)) {
992
- _context15.next = 3;
993
- break;
994
- }
995
-
996
- _context15.next = 3;
997
- return this.startSession();
998
-
999
- case 3:
1000
- this.debug('idle');
1001
- return _context15.abrupt("return", process.nextTick(function () {
1002
- _this6.emit('idle'); // Recover
1003
-
1004
-
1005
- var _this6$session = _this6.session,
1006
- _this6$session$cue = _this6$session.cue,
1007
- cue = _this6$session$cue === void 0 ? null : _this6$session$cue,
1008
- _this6$session$ended = _this6$session.ended,
1009
- ended = _this6$session$ended === void 0 ? false : _this6$session$ended,
1010
- _this6$session$data = _this6$session.data,
1011
- data = _this6$session$data === void 0 ? null : _this6$session$data;
1012
-
1013
- if (ended) {
1014
- _this6.end();
1015
- } else if (cue !== null) {
1016
- _this6.cue(cue, data);
1017
- }
1018
- }));
1019
-
1020
- case 5:
1021
- case "end":
1022
- return _context15.stop();
1023
- }
1024
- }
1025
- }, _callee15, this);
1026
- }));
1027
-
1028
- function onIdle() {
1029
- return _onIdle.apply(this, arguments);
1030
- }
1031
-
1032
- return onIdle;
1033
- }()
1034
- }, {
1035
- key: "onUncue",
1036
- value: function () {
1037
- var _onUncue = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee16() {
1038
- return _regeneratorRuntime__default['default'].wrap(function _callee16$(_context16) {
1039
- while (1) {
1040
- switch (_context16.prev = _context16.next) {
1041
- case 0:
1042
- this.stopCueDurationInterval();
1043
- this.statefulCue = null;
1044
- _context16.next = 4;
1045
- return this.setSessionCue(null);
1046
-
1047
- case 4:
1048
- _context16.next = 6;
1049
- return this.sendCommandToOutputs('uncue');
1050
-
1051
- case 6:
1052
- this.emit('uncue');
1053
-
1054
- case 7:
1055
- case "end":
1056
- return _context16.stop();
1057
- }
1058
- }
1059
- }, _callee16, this);
1060
- }));
1061
-
1062
- function onUncue() {
1063
- return _onUncue.apply(this, arguments);
1064
- }
1065
-
1066
- return onUncue;
1067
- }()
1068
- }, {
1069
- key: "onCue",
1070
- value: function () {
1071
- var _onCue = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee17(state, cueId) {
1072
- var sessionData,
1073
- cues,
1074
- cue,
1075
- _cue$id,
1076
- id,
1077
- _cue$stateful,
1078
- stateful,
1079
- _args17 = arguments;
1080
-
1081
- return _regeneratorRuntime__default['default'].wrap(function _callee17$(_context17) {
1082
- while (1) {
1083
- switch (_context17.prev = _context17.next) {
1084
- case 0:
1085
- sessionData = _args17.length > 2 && _args17[2] !== undefined ? _args17[2] : null;
1086
- _context17.next = 3;
1087
- return this.getCues();
1088
-
1089
- case 3:
1090
- cues = _context17.sent;
1091
- cue = cues.find(function (_ref10) {
1092
- var id = _ref10.id;
1093
- return id === cueId;
1094
- }) || null;
1095
-
1096
- if (!(cue === null)) {
1097
- _context17.next = 8;
1098
- break;
1099
- }
1100
-
1101
- this.debug('Cannot find cue %s', cueId);
1102
- return _context17.abrupt("return", false);
1103
-
1104
- case 8:
1105
- _cue$id = cue.id, id = _cue$id === void 0 ? null : _cue$id, _cue$stateful = cue.stateful, stateful = _cue$stateful === void 0 ? false : _cue$stateful;
1106
-
1107
- if (!stateful) {
1108
- _context17.next = 14;
1109
- break;
1110
- }
1111
-
1112
- this.debug('Stateful cue: %s', id);
1113
- this.statefulCue = cue;
1114
- _context17.next = 14;
1115
- return this.setSessionCue(cue, sessionData);
1116
-
1117
- case 14:
1118
- _context17.next = 16;
1119
- return this.sendCueToOutputs(cue, sessionData);
1120
-
1121
- case 16:
1122
- this.emit('cue', cue, sessionData);
1123
- this.debug('onCue %s %O', cueId, sessionData);
1124
- return _context17.abrupt("return", true);
1125
-
1126
- case 19:
1127
- case "end":
1128
- return _context17.stop();
1129
- }
1130
- }
1131
- }, _callee17, this);
1132
- }));
1133
-
1134
- function onCue(_x9, _x10) {
1135
- return _onCue.apply(this, arguments);
1136
- }
1137
-
1138
- return onCue;
1139
- }()
1140
- }, {
1141
- key: "onCued",
1142
- value: function () {
1143
- var _onCued = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee18(state, cueId) {
1144
- var cues, cue, _cue$duration, duration;
1145
-
1146
- return _regeneratorRuntime__default['default'].wrap(function _callee18$(_context18) {
1147
- while (1) {
1148
- switch (_context18.prev = _context18.next) {
1149
- case 0:
1150
- _context18.next = 2;
1151
- return this.getCues();
1152
-
1153
- case 2:
1154
- cues = _context18.sent;
1155
- cue = cues.find(function (_ref11) {
1156
- var id = _ref11.id;
1157
- return id === cueId;
1158
- }) || null;
1159
-
1160
- if (!(cue === null)) {
1161
- _context18.next = 7;
1162
- break;
1163
- }
1164
-
1165
- this.debug('Cannot find cue %s', cueId);
1166
- return _context18.abrupt("return", false);
1167
-
1168
- case 7:
1169
- _cue$duration = cue.duration, duration = _cue$duration === void 0 ? null : _cue$duration;
1170
-
1171
- if (duration !== null) {
1172
- this.startCueDurationInterval(cue);
1173
- }
1174
-
1175
- return _context18.abrupt("return", true);
1176
-
1177
- case 10:
1178
- case "end":
1179
- return _context18.stop();
1180
- }
1181
- }
1182
- }, _callee18, this);
1183
- }));
1184
-
1185
- function onCued(_x11, _x12) {
1186
- return _onCued.apply(this, arguments);
1187
- }
1188
-
1189
- return onCued;
1190
- }()
1191
- }, {
1192
- key: "onCueDurationEnd",
1193
- value: function () {
1194
- var _onCueDurationEnd = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee19(cue) {
1195
- var _cue$after_duration, afterDuration, _this$definition$cues, cues, cueIndex, nextCue;
1196
-
1197
- return _regeneratorRuntime__default['default'].wrap(function _callee19$(_context19) {
1198
- while (1) {
1199
- switch (_context19.prev = _context19.next) {
1200
- case 0:
1201
- this.debug('Cue %s duration is ended', cue.id);
1202
- _cue$after_duration = cue.after_duration, afterDuration = _cue$after_duration === void 0 ? null : _cue$after_duration;
1203
- _this$definition$cues = this.definition.cues, cues = _this$definition$cues === void 0 ? [] : _this$definition$cues;
1204
- _context19.t0 = afterDuration;
1205
- _context19.next = _context19.t0 === 'next' ? 6 : 10;
1206
- break;
1207
-
1208
- case 6:
1209
- cueIndex = cues.findIndex(function (it) {
1210
- return it.id === cue.id;
1211
- });
1212
- nextCue = cueIndex !== -1 && cueIndex < cues.length - 1 ? cues[cueIndex + 1] : null;
1213
-
1214
- if (nextCue !== null) {
1215
- this.cue(nextCue.id);
1216
- }
1217
-
1218
- return _context19.abrupt("break", 12);
1219
-
1220
- case 10:
1221
- this.uncue();
1222
- return _context19.abrupt("break", 12);
1223
-
1224
- case 12:
1225
- case "end":
1226
- return _context19.stop();
1227
- }
1228
- }
1229
- }, _callee19, this);
1230
- }));
1231
-
1232
- function onCueDurationEnd(_x13) {
1233
- return _onCueDurationEnd.apply(this, arguments);
1234
- }
1235
-
1236
- return onCueDurationEnd;
1237
- }()
1238
- }, {
1239
- key: "onStop",
1240
- value: function () {
1241
- var _onStop = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee20() {
1242
- return _regeneratorRuntime__default['default'].wrap(function _callee20$(_context20) {
1243
- while (1) {
1244
- switch (_context20.prev = _context20.next) {
1245
- case 0:
1246
- this.emit('stop');
1247
- this.stopCueDurationInterval();
1248
- this.statefulCue = null;
1249
- this.sendCommandToOutputs('stop');
1250
- _context20.next = 6;
1251
- return this.stopInputs();
1252
-
1253
- case 6:
1254
- _context20.next = 8;
1255
- return this.stopOutputs();
1256
-
1257
- case 8:
1258
- case "end":
1259
- return _context20.stop();
1260
- }
1261
- }
1262
- }, _callee20, this);
1263
- }));
1264
-
1265
- function onStop() {
1266
- return _onStop.apply(this, arguments);
1267
- }
1268
-
1269
- return onStop;
1270
- }()
1271
- }, {
1272
- key: "onStopped",
1273
- value: function onStopped() {
1274
- var _this7 = this;
1275
-
1276
- this.debug('stopped');
1277
- return process.nextTick(function () {
1278
- return _this7.emit('stopped');
1279
- });
1280
- }
1281
- }, {
1282
- key: "onEnd",
1283
- value: function () {
1284
- var _onEnd = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee21() {
1285
- return _regeneratorRuntime__default['default'].wrap(function _callee21$(_context21) {
1286
- while (1) {
1287
- switch (_context21.prev = _context21.next) {
1288
- case 0:
1289
- _context21.next = 2;
1290
- return this.endSession();
1291
-
1292
- case 2:
1293
- this.stopCueDurationInterval();
1294
- this.session = null;
1295
- this.statefulCue = null;
1296
- this.sendCommandToOutputs('end');
1297
- this.emit('ended');
1298
-
1299
- case 7:
1300
- case "end":
1301
- return _context21.stop();
1302
- }
1303
- }
1304
- }, _callee21, this);
1305
- }));
1306
-
1307
- function onEnd() {
1308
- return _onEnd.apply(this, arguments);
1309
- }
1310
-
1311
- return onEnd;
1312
- }()
1313
- }, {
1314
- key: "onInputCommand",
1315
- value: function () {
1316
- var _onInputCommand = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee22(command) {
1317
- var _len3,
1318
- args,
1319
- _key3,
1320
- _this$options,
1321
- _this$options$inputCo,
1322
- inputCommands,
1323
- _this$options$validat,
1324
- validateCommand,
1325
- _this$options$transfo,
1326
- transformCommand,
1327
- _ref12,
1328
- _ref12$command,
1329
- finalCommand,
1330
- _ref12$args,
1331
- finalArgs,
1332
- _args22 = arguments;
1333
-
1334
- return _regeneratorRuntime__default['default'].wrap(function _callee22$(_context22) {
1335
- while (1) {
1336
- switch (_context22.prev = _context22.next) {
1337
- case 0:
1338
- for (_len3 = _args22.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
1339
- args[_key3 - 1] = _args22[_key3];
1340
- }
1341
-
1342
- this.debug('onInputCommand %s', command, args);
1343
- _context22.prev = 2;
1344
- _this$options = this.options, _this$options$inputCo = _this$options.inputCommands, inputCommands = _this$options$inputCo === void 0 ? null : _this$options$inputCo, _this$options$validat = _this$options.validateCommand, validateCommand = _this$options$validat === void 0 ? null : _this$options$validat, _this$options$transfo = _this$options.transformCommand, transformCommand = _this$options$transfo === void 0 ? null : _this$options$transfo;
1345
- _ref12 = (transformCommand !== null ? transformCommand(command, args) : null) || {}, _ref12$command = _ref12.command, finalCommand = _ref12$command === void 0 ? command : _ref12$command, _ref12$args = _ref12.args, finalArgs = _ref12$args === void 0 ? args : _ref12$args;
1346
-
1347
- if (!(inputCommands !== null && inputCommands.indexOf(finalCommand) === -1)) {
1348
- _context22.next = 8;
1349
- break;
1350
- }
1351
-
1352
- this.debug('command not allowed: %s %o', finalCommand, finalArgs);
1353
- return _context22.abrupt("return");
1354
-
1355
- case 8:
1356
- if (!(validateCommand !== null && !validateCommand.apply(void 0, [finalCommand].concat(_toConsumableArray__default['default'](finalArgs))))) {
1357
- _context22.next = 11;
1358
- break;
1359
- }
1360
-
1361
- this.debug('validateCommand failed: %s %o', finalCommand, finalArgs);
1362
- return _context22.abrupt("return");
1363
-
1364
- case 11:
1365
- this.emit('input:command', finalCommand, finalArgs);
1366
- this[finalCommand].apply(this, _toConsumableArray__default['default'](finalArgs));
1367
- this.debug(this[finalCommand], finalArgs);
1368
- _context22.next = 19;
1369
- break;
1370
-
1371
- case 16:
1372
- _context22.prev = 16;
1373
- _context22.t0 = _context22["catch"](2);
1374
- this.debug("Error with command \"".concat(command, "\": ").concat(_context22.t0.message));
1375
-
1376
- case 19:
1377
- case "end":
1378
- return _context22.stop();
1379
- }
1380
- }
1381
- }, _callee22, this, [[2, 16]]);
1382
- }));
1383
-
1384
- function onInputCommand(_x14) {
1385
- return _onInputCommand.apply(this, arguments);
1386
- }
1387
-
1388
- return onInputCommand;
1389
- }()
1390
- }, {
1391
- key: "ensureInteraction",
1392
- value: function () {
1393
- var _ensureInteraction = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee23(data, interactionId) {
1394
- var interactionIdentifier, _ref13, cueId, _ref13$interaction, cueHasInteraction, interaction;
1395
-
1396
- return _regeneratorRuntime__default['default'].wrap(function _callee23$(_context23) {
1397
- while (1) {
1398
- switch (_context23.prev = _context23.next) {
1399
- case 0:
1400
- this.debug('Ensuring interaction %s... %O', interactionId, data);
1401
- interactionIdentifier = {
1402
- id: interactionId,
1403
- sessionId: this.session.id
1404
- };
1405
- _ref13 = this.statefulCue || {}, cueId = _ref13.id, _ref13$interaction = _ref13.interaction, cueHasInteraction = _ref13$interaction === void 0 ? false : _ref13$interaction;
1406
-
1407
- if (cueHasInteraction) {
1408
- interactionIdentifier.cueId = cueId;
1409
- }
1410
-
1411
- _context23.next = 6;
1412
- return this.store.findItem('interactions', interactionIdentifier);
1413
-
1414
- case 6:
1415
- interaction = _context23.sent;
1416
-
1417
- if (!(interaction !== null)) {
1418
- _context23.next = 13;
1419
- break;
1420
- }
1421
-
1422
- _context23.next = 10;
1423
- return this.store.updateItem('interactions', interaction.id, {
1424
- data: data
1425
- });
1426
-
1427
- case 10:
1428
- interaction = _context23.sent;
1429
- _context23.next = 16;
1430
- break;
1431
-
1432
- case 13:
1433
- _context23.next = 15;
1434
- return this.store.addItem('interactions', _objectSpread__default['default'](_objectSpread__default['default']({}, interactionIdentifier), {}, {
1435
- data: data
1436
- }));
1437
-
1438
- case 15:
1439
- interaction = _context23.sent;
1440
-
1441
- case 16:
1442
- return _context23.abrupt("return", interaction);
1443
-
1444
- case 17:
1445
- case "end":
1446
- return _context23.stop();
1447
- }
1448
- }
1449
- }, _callee23, this);
1450
- }));
1451
-
1452
- function ensureInteraction(_x15, _x16) {
1453
- return _ensureInteraction.apply(this, arguments);
1454
- }
1455
-
1456
- return ensureInteraction;
1457
- }()
1458
- }, {
1459
- key: "startCueDurationInterval",
1460
- value: function startCueDurationInterval(cue) {
1461
- var _this8 = this;
1462
-
1463
- var _cue$duration2 = cue.duration,
1464
- duration = _cue$duration2 === void 0 ? null : _cue$duration2;
1465
- var startTime = new Date().getTime();
1466
- this.debug('Starting interval for %i for cue %s...', duration, cue.id);
1467
- this.cueDurationInterval = setInterval(function () {
1468
- var currentTime = new Date().getTime();
1469
- var currentDuration = Math.floor((currentTime - startTime) / 1000);
1470
-
1471
- if (currentDuration >= duration) {
1472
- clearInterval(_this8.cueDurationInterval);
1473
- _this8.cueDurationInterval = null;
1474
-
1475
- _this8.onCueDurationEnd(cue);
1476
- }
1477
- }, 10);
1478
- }
1479
- }, {
1480
- key: "stopCueDurationInterval",
1481
- value: function stopCueDurationInterval() {
1482
- if (this.cueDurationInterval !== null) {
1483
- this.debug('Stopping interval for cue...');
1484
- clearInterval(this.cueDurationInterval);
1485
- this.cueDurationInterval = null;
1486
- }
1487
- }
1488
- }, {
1489
- key: "ensureSession",
1490
- value: function () {
1491
- var _ensureSession = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee24() {
1492
- var getHandle, _this$definition, definitionId, _this$definition$cues2, cues, item, handle, newItem;
1493
-
1494
- return _regeneratorRuntime__default['default'].wrap(function _callee24$(_context24) {
1495
- while (1) {
1496
- switch (_context24.prev = _context24.next) {
1497
- case 0:
1498
- getHandle = this.options.getHandle;
1499
- _this$definition = this.definition, definitionId = _this$definition.id, _this$definition$cues2 = _this$definition.cues, cues = _this$definition$cues2 === void 0 ? [] : _this$definition$cues2;
1500
- this.debug('Getting started session...');
1501
- _context24.next = 5;
1502
- return this.store.findItem('sessions', {
1503
- definition: definitionId,
1504
- started: true,
1505
- ended: false
1506
- });
1507
-
1508
- case 5:
1509
- item = _context24.sent;
1510
-
1511
- if (!(item !== null)) {
1512
- _context24.next = 9;
1513
- break;
1514
- }
1515
-
1516
- this.debug('Session found %s.', item.id);
1517
- return _context24.abrupt("return", item);
1518
-
1519
- case 9:
1520
- handle = getHandle(definitionId);
1521
- this.debug('Creating new session "%s"...', handle);
1522
- _context24.next = 13;
1523
- return this.store.addItem('sessions', {
1524
- id: handle,
1525
- definition: definitionId,
1526
- started: false,
1527
- ended: false,
1528
- cue: null
1529
- });
1530
-
1531
- case 13:
1532
- newItem = _context24.sent;
1533
- this.debug('Setting initial "%s" cues...', cues !== null ? cues.length : 0);
1534
- _context24.next = 17;
1535
- return this.setCues(cues, newItem);
1536
-
1537
- case 17:
1538
- this.debug('New session "%s"', newItem !== null ? newItem.id : null);
1539
- return _context24.abrupt("return", newItem);
1540
-
1541
- case 19:
1542
- case "end":
1543
- return _context24.stop();
1544
- }
1545
- }
1546
- }, _callee24, this);
1547
- }));
1548
-
1549
- function ensureSession() {
1550
- return _ensureSession.apply(this, arguments);
1551
- }
1552
-
1553
- return ensureSession;
1554
- }()
1555
- }, {
1556
- key: "resetSession",
1557
- value: function () {
1558
- var _resetSession = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee25() {
1559
- var id;
1560
- return _regeneratorRuntime__default['default'].wrap(function _callee25$(_context25) {
1561
- while (1) {
1562
- switch (_context25.prev = _context25.next) {
1563
- case 0:
1564
- id = this.session.id;
1565
- this.debug('Resetting session "%s"...', id);
1566
- _context25.next = 4;
1567
- return this.store.updateItem('sessions', id, {
1568
- started: true,
1569
- ended: false,
1570
- cue: null
1571
- });
1572
-
1573
- case 4:
1574
- this.session = _context25.sent;
1575
-
1576
- case 5:
1577
- case "end":
1578
- return _context25.stop();
1579
- }
1580
- }
1581
- }, _callee25, this);
1582
- }));
1583
-
1584
- function resetSession() {
1585
- return _resetSession.apply(this, arguments);
1586
- }
1587
-
1588
- return resetSession;
1589
- }()
1590
- }, {
1591
- key: "startSession",
1592
- value: function () {
1593
- var _startSession = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee26() {
1594
- var id;
1595
- return _regeneratorRuntime__default['default'].wrap(function _callee26$(_context26) {
1596
- while (1) {
1597
- switch (_context26.prev = _context26.next) {
1598
- case 0:
1599
- id = this.session.id;
1600
- this.debug('Starting session "%s"...', id);
1601
- _context26.next = 4;
1602
- return this.store.updateItem('sessions', id, {
1603
- started: true,
1604
- ended: false
1605
- });
1606
-
1607
- case 4:
1608
- this.session = _context26.sent;
1609
-
1610
- case 5:
1611
- case "end":
1612
- return _context26.stop();
1613
- }
1614
- }
1615
- }, _callee26, this);
1616
- }));
1617
-
1618
- function startSession() {
1619
- return _startSession.apply(this, arguments);
1620
- }
1621
-
1622
- return startSession;
1623
- }()
1624
- }, {
1625
- key: "endSession",
1626
- value: function () {
1627
- var _endSession = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee27() {
1628
- var id;
1629
- return _regeneratorRuntime__default['default'].wrap(function _callee27$(_context27) {
1630
- while (1) {
1631
- switch (_context27.prev = _context27.next) {
1632
- case 0:
1633
- id = this.session.id;
1634
- this.debug('Ending session "%s"...', id);
1635
- _context27.next = 4;
1636
- return this.store.updateItem('sessions', id, {
1637
- started: false,
1638
- ended: true,
1639
- cue: null
1640
- });
1641
-
1642
- case 4:
1643
- this.session = _context27.sent;
1644
-
1645
- case 5:
1646
- case "end":
1647
- return _context27.stop();
1648
- }
1649
- }
1650
- }, _callee27, this);
1651
- }));
1652
-
1653
- function endSession() {
1654
- return _endSession.apply(this, arguments);
1655
- }
1656
-
1657
- return endSession;
1658
- }()
1659
- }, {
1660
- key: "setSessionCue",
1661
- value: function () {
1662
- var _setSessionCue = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee28(cue) {
1663
- var sessionData,
1664
- id,
1665
- _args28 = arguments;
1666
- return _regeneratorRuntime__default['default'].wrap(function _callee28$(_context28) {
1667
- while (1) {
1668
- switch (_context28.prev = _context28.next) {
1669
- case 0:
1670
- sessionData = _args28.length > 1 && _args28[1] !== undefined ? _args28[1] : null;
1671
- id = this.session.id;
1672
- this.debug('Setting session "%s", cue... %o', id, cue);
1673
- _context28.next = 5;
1674
- return this.store.updateItem('sessions', id, {
1675
- cue: cue !== null ? cue.id : null,
1676
- data: sessionData
1677
- });
1678
-
1679
- case 5:
1680
- this.session = _context28.sent;
1681
-
1682
- case 6:
1683
- case "end":
1684
- return _context28.stop();
1685
- }
1686
- }
1687
- }, _callee28, this);
1688
- }));
1689
-
1690
- function setSessionCue(_x17) {
1691
- return _setSessionCue.apply(this, arguments);
1692
- }
1693
-
1694
- return setSessionCue;
1695
- }()
1696
- }, {
1697
- key: "getSessionCue",
1698
- value: function () {
1699
- var _getSessionCue = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee29() {
1700
- var cues, _ref14, _ref14$cue, cueId, cue;
1701
-
1702
- return _regeneratorRuntime__default['default'].wrap(function _callee29$(_context29) {
1703
- while (1) {
1704
- switch (_context29.prev = _context29.next) {
1705
- case 0:
1706
- _context29.next = 2;
1707
- return this.getCues();
1708
-
1709
- case 2:
1710
- cues = _context29.sent;
1711
- _ref14 = this.session || {}, _ref14$cue = _ref14.cue, cueId = _ref14$cue === void 0 ? null : _ref14$cue;
1712
- cue = cues.find(function (c) {
1713
- return c.id === cueId;
1714
- }) || null;
1715
- return _context29.abrupt("return", cue);
1716
-
1717
- case 6:
1718
- case "end":
1719
- return _context29.stop();
1720
- }
1721
- }
1722
- }, _callee29, this);
1723
- }));
1724
-
1725
- function getSessionCue() {
1726
- return _getSessionCue.apply(this, arguments);
1727
- }
1728
-
1729
- return getSessionCue;
1730
- }()
1731
- }, {
1732
- key: "getSessionData",
1733
- value: function getSessionData() {
1734
- var _ref15 = this.session || {},
1735
- _ref15$data = _ref15.data,
1736
- data = _ref15$data === void 0 ? null : _ref15$data;
1737
-
1738
- return data;
1739
- }
1740
- /**
1741
- * Call inputs and outputs
1742
- */
1743
-
1744
- }, {
1745
- key: "initInputs",
1746
- value: function initInputs() {
1747
- this.debug('Init inputs...');
1748
- return Promise.all(this.inputs.map(function (it) {
1749
- return typeof it.init !== 'undefined' ? it.init() : Promise.resolve();
1750
- }));
1751
- }
1752
- }, {
1753
- key: "initOutputs",
1754
- value: function initOutputs() {
1755
- this.debug('Init outputs...');
1756
- return Promise.all(this.outputs.map(function (it) {
1757
- return typeof it.init !== 'undefined' ? it.init() : Promise.resolve();
1758
- }));
1759
- }
1760
- }, {
1761
- key: "startInputs",
1762
- value: function startInputs() {
1763
- this.debug('Start inputs... %s', this.inputs.length);
1764
- return Promise.all(this.inputs.map(function (it) {
1765
- return typeof it.start !== 'undefined' ? it.start() : Promise.resolve();
1766
- }));
1767
- }
1768
- }, {
1769
- key: "startOutputs",
1770
- value: function startOutputs() {
1771
- this.debug('Start outputs... %s', this.outputs.length);
1772
- return Promise.all(this.outputs.map(function (it) {
1773
- return typeof it.start !== 'undefined' ? it.start() : Promise.resolve();
1774
- }));
1775
- }
1776
- }, {
1777
- key: "sendCueToOutputs",
1778
- value: function sendCueToOutputs(cue) {
1779
- var sessionData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
1780
- this.debug('Send cue to outputs: %O %O', cue, sessionData);
1781
- return Promise.all(this.outputs.map(function (it) {
1782
- return typeof it.cue !== 'undefined' ? it.cue(cue, sessionData) : Promise.resolve();
1783
- }));
1784
- }
1785
- }, {
1786
- key: "sendCuesToOutputs",
1787
- value: function sendCuesToOutputs(cues) {
1788
- this.debug('Send cues to outputs: %O', cues);
1789
- return Promise.all(this.outputs.map(function (it) {
1790
- return typeof it.cues !== 'undefined' ? it.cues(cues) : Promise.resolve();
1791
- }));
1792
- }
1793
- }, {
1794
- key: "sendInteractToOutputs",
1795
- value: function sendInteractToOutputs(data, interactionId) {
1796
- this.debug('Send interact to outputs: %O %s', data, interactionId);
1797
- return Promise.all(this.outputs.map(function (it) {
1798
- return typeof it.interact !== 'undefined' ? it.interact(data, interactionId) : Promise.resolve();
1799
- }));
1800
- }
1801
- }, {
1802
- key: "sendInteractionToOutputs",
1803
- value: function sendInteractionToOutputs(interaction) {
1804
- this.debug('Send interaction to outputs: %O', interaction);
1805
- return Promise.all(this.outputs.map(function (it) {
1806
- return typeof it.interaction !== 'undefined' ? it.interaction(interaction) : Promise.resolve();
1807
- }));
1808
- }
1809
- }, {
1810
- key: "sendUninteractionToOutputs",
1811
- value: function sendUninteractionToOutputs(interactionId) {
1812
- this.debug('Send interact to outputs: %O %s', interactionId);
1813
- return Promise.all(this.outputs.map(function (it) {
1814
- return typeof it.uninteraction !== 'undefined' ? it.uninteraction(interactionId) : Promise.resolve();
1815
- }));
1816
- }
1817
- }, {
1818
- key: "sendUninteractionsToOutputs",
1819
- value: function sendUninteractionsToOutputs(interactionIds) {
1820
- this.debug('Send uninteractions to outputs: %O', interactionIds);
1821
- return Promise.all(this.outputs.map(function (it) {
1822
- return typeof it.uninteractions !== 'undefined' ? it.uninteractions(interactionIds) : Promise.resolve();
1823
- }));
1824
- }
1825
- }, {
1826
- key: "sendCommandToOutputs",
1827
- value: function sendCommandToOutputs(command) {
1828
- for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
1829
- args[_key4 - 1] = arguments[_key4];
1830
- }
1831
-
1832
- this.debug("Send command to outputs: %s %o", command, args);
1833
- return Promise.all(this.outputs.map(function (it) {
1834
- return typeof it.command !== 'undefined' ? it.command.apply(it, [command].concat(args)) : Promise.resolve();
1835
- }));
1836
- }
1837
- }, {
1838
- key: "stopInputs",
1839
- value: function stopInputs() {
1840
- this.debug('Stop inputs...');
1841
- return Promise.all(this.inputs.map(function (it) {
1842
- return typeof it.stop !== 'undefined' ? it.stop() : Promise.resolve();
1843
- }));
1844
- }
1845
- }, {
1846
- key: "stopOutputs",
1847
- value: function stopOutputs() {
1848
- this.debug('Stop inputs...');
1849
- return Promise.all(this.outputs.map(function (it) {
1850
- return typeof it.stop !== 'undefined' ? it.stop() : Promise.resolve();
1851
- }));
1852
- }
1853
- }, {
1854
- key: "destroyInputs",
1855
- value: function destroyInputs() {
1856
- this.debug('Destroying inputs...');
1857
- return Promise.all(this.inputs.map(function (it) {
1858
- return typeof it.destroy !== 'undefined' ? it.destroy() : Promise.resolve();
1859
- }));
1860
- }
1861
- }, {
1862
- key: "destroyOutputs",
1863
- value: function destroyOutputs() {
1864
- this.debug('Destroying outputs...');
1865
- return Promise.all(this.outputs.map(function (it) {
1866
- return typeof it.destroy !== 'undefined' ? it.destroy() : Promise.resolve();
1867
- }));
1868
- }
1869
- }]);
1870
-
1871
- return Application;
1872
- }(EventEmitter__default['default']);
1873
-
1874
- var terminate = function terminate() {
1875
- var server = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
1876
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
1877
- coredump: false,
1878
- timeout: 500
1879
- };
1880
- var debug = createDebug__default['default']('cuecue:app:terminate');
1881
-
1882
- var exit = function exit(code) {
1883
- if (options.coredump) {
1884
- process.abort();
1885
- } else {
1886
- process.exit(code);
1887
- }
1888
- };
1889
-
1890
- return function (code, reason) {
1891
- return function (err) {
1892
- debug('Err: %O', err, code, reason);
1893
-
1894
- if (err && err instanceof Error) {
1895
- // Log error information somewhere
1896
- debug('Exit: %s %O', err.message, err.stack);
1897
- debug('Trace: %s %s', code, reason);
1898
- } // Attempt a graceful shutdown
1899
-
1900
-
1901
- if (server !== null) {
1902
- debug('Server: %O', server);
1903
- server.destroy(); // could also call exit on callback here
1904
- }
1905
-
1906
- setTimeout(exit, options.timeout).unref();
1907
- };
1908
- };
1909
- };
1910
- var utils = {
1911
- terminate: terminate
1912
- };
1913
-
1914
- exports.MemoryStore = MemoryStore;
1915
- exports.default = Application;
1916
- exports.utils = utils;