eligius 1.0.4 → 1.0.6

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.
@@ -3,16 +3,63 @@ import fs from 'fs';
3
3
  import path from 'path';
4
4
  import lottie from 'lottie-web';
5
5
  import $ from 'jquery';
6
- import Mousetrap from 'mousetrap';
6
+ import hotkeys from 'hotkeys-js';
7
7
  import { v4 } from 'uuid';
8
8
 
9
+ function prepareValueForSerialization(value) {
10
+ if (Array.isArray(value)) {
11
+ return value.map(prepareValueForSerialization);
12
+ } else if (typeof value === 'object') {
13
+ return Object.fromEntries(Object.entries(value).map(function (_ref) {
14
+ var name = _ref[0],
15
+ value = _ref[1];
16
+ return [name, prepareValueForSerialization(value)];
17
+ }));
18
+ } else if (typeof value === 'function') {
19
+ return value.toString();
20
+ }
21
+
22
+ return value;
23
+ }
24
+
25
+ var DevToolEventListener = /*#__PURE__*/function () {
26
+ function DevToolEventListener(agent) {
27
+ this.agent = void 0;
28
+ this.agent = agent;
29
+ }
30
+
31
+ var _proto = DevToolEventListener.prototype;
32
+
33
+ _proto.handleEvent = function handleEvent(eventName, eventTopic, args) {
34
+ this.agent.postMessage('eligius-diagnostics-event', {
35
+ eventName: eventName,
36
+ eventTopic: eventTopic,
37
+ args: prepareValueForSerialization(args)
38
+ });
39
+ };
40
+
41
+ return DevToolEventListener;
42
+ }();
43
+
44
+ var Diagnostics = /*#__PURE__*/function () {
45
+ function Diagnostics() {}
46
+
47
+ Diagnostics.send = function send(_name, _data) {
48
+ console.log(_name, _data);
49
+ };
50
+
51
+ return Diagnostics;
52
+ }();
53
+ Diagnostics.active = void 0;
54
+
55
+ var DEV_TOOLS_KEY = '__ELIGIUS_DEV_TOOLS__';
56
+
57
+ var copyFunction = typeof structuredClone !== 'undefined' ? structuredClone : function (original) {
58
+ return JSON.parse(JSON.stringify(original));
59
+ };
9
60
  function deepCopy(original) {
10
61
  if (isDefined(original)) {
11
- if (typeof structuredClone !== 'undefined') {
12
- return structuredClone(original);
13
- }
14
-
15
- return JSON.parse(JSON.stringify(original));
62
+ return copyFunction(original);
16
63
  }
17
64
 
18
65
  return original;
@@ -36,8 +83,10 @@ var Action = /*#__PURE__*/function () {
36
83
  var _proto = Action.prototype;
37
84
 
38
85
  _proto.start = function start(initOperationData) {
39
- var _this = this;
86
+ var _this$name,
87
+ _this = this;
40
88
 
89
+ Diagnostics.active && Diagnostics.send('eligius-diagnostics-action', ((_this$name = this.name) != null ? _this$name : 'Action') + " begins executing start operations");
41
90
  var context = {
42
91
  currentIndex: -1,
43
92
  eventbus: this.eventbus
@@ -45,11 +94,20 @@ var Action = /*#__PURE__*/function () {
45
94
  var result = new Promise(function (resolve, reject) {
46
95
  _this.executeOperation(_this.startOperations, 0, resolve, reject, initOperationData, context);
47
96
  })["catch"](function (e) {
97
+ Diagnostics.active && Diagnostics.send('eligius-diagnostics-action-error', {
98
+ name: name,
99
+ error: e
100
+ });
48
101
  console.error("Error in action start '" + _this.name + "'");
49
102
  console.error(e);
50
103
  throw e;
51
104
  });
52
- return result;
105
+ return !Diagnostics.active ? result : result.then(function (operationsResult) {
106
+ var _this$name2;
107
+
108
+ Diagnostics.send('eligius-diagnostics-action', ((_this$name2 = _this.name) != null ? _this$name2 : 'Action') + " finished executing start operations");
109
+ return operationsResult;
110
+ });
53
111
  };
54
112
 
55
113
  _proto.executeOperation = function executeOperation(operations, idx, resolve, reject, previousOperationData, context) {
@@ -78,6 +136,11 @@ var Action = /*#__PURE__*/function () {
78
136
  var operationInfo = operations[idx];
79
137
  var copy = operationInfo.operationData ? deepCopy(operationInfo.operationData) : {};
80
138
  var mergedOperationData = Object.assign(previousOperationData, copy);
139
+ Diagnostics.active && Diagnostics.send('eligius-diagnostics-operation', {
140
+ systemName: operationInfo.systemName,
141
+ operationData: operationInfo.operationData,
142
+ context: context
143
+ });
81
144
  var operationResult = operationInfo.instance.call(context, mergedOperationData);
82
145
 
83
146
  if (isPromise(operationResult)) {
@@ -525,24 +588,30 @@ var EndableAction = /*#__PURE__*/function (_Action) {
525
588
  var _proto = EndableAction.prototype;
526
589
 
527
590
  _proto.end = function end(initOperationData) {
528
- var _this2 = this;
591
+ var _this$name,
592
+ _this2 = this;
529
593
 
530
- if (this.endOperations.length) {
531
- var context = {
532
- currentIndex: -1,
533
- eventbus: this.eventbus
534
- };
535
- var result = new Promise(function (resolve, reject) {
536
- _this2.executeOperation(_this2.endOperations, 0, resolve, reject, initOperationData, context);
537
- })["catch"](function (e) {
538
- console.error("Error in action end '" + _this2.name + "'");
539
- throw e;
540
- });
541
- return result;
594
+ Diagnostics.active && Diagnostics.send('eligius-diagnostics-action', ((_this$name = this.name) != null ? _this$name : 'Action') + " begins executing end operations");
595
+
596
+ if (!this.endOperations.length) {
597
+ return Promise.resolve(initOperationData);
542
598
  }
543
599
 
544
- return new Promise(function (resolve) {
545
- resolve(initOperationData);
600
+ var context = {
601
+ currentIndex: -1,
602
+ eventbus: this.eventbus
603
+ };
604
+ var result = new Promise(function (resolve, reject) {
605
+ _this2.executeOperation(_this2.endOperations, 0, resolve, reject, initOperationData, context);
606
+ })["catch"](function (e) {
607
+ console.error("Error in action end '" + _this2.name + "'");
608
+ throw e;
609
+ });
610
+ return !Diagnostics.active ? result : result.then(function (operationsResult) {
611
+ var _this2$name;
612
+
613
+ Diagnostics.send('eligius-diagnostics-action', ((_this2$name = _this2.name) != null ? _this2$name : 'Action') + " finished executing start operations");
614
+ return operationsResult;
546
615
  });
547
616
  };
548
617
 
@@ -619,16 +688,20 @@ function _generateSourceCode(importPaths) {
619
688
 
620
689
  return "import {" + imp.systemName + "} from '" + ((_imp$path = imp.path) != null ? _imp$path : 'eligius') + "';";
621
690
  }));
691
+ result.push("import { ISimpleResourceImporter } from 'eligius';");
692
+ result.push('');
622
693
  result.push('const imports: Record<string, any> = {');
623
694
  result.push.apply(result, importPaths.map(function (imp) {
624
695
  return imp.systemName + ": { '" + imp.systemName + "': " + imp.systemName + " },";
625
696
  }));
626
697
  result.push('};');
698
+ result.push('');
627
699
  result.push('class EligiusResourceImporter implements ISimpleResourceImporter {');
628
700
  result.push('import(name: string): Record<string, any> {');
629
701
  result.push('if (imports.hasOwnProperty(name)) { return imports[name]; }');
630
702
  result.push('throw Error(`Unknown systemName: ${name}`);');
631
703
  result.push('}}');
704
+ result.push('');
632
705
  result.push('export default EligiusResourceImporter;');
633
706
  return result.join('\n');
634
707
  }
@@ -990,6 +1063,7 @@ TimelineEventNames.DURATION = 'timeline-duration';
990
1063
  TimelineEventNames.TIME = 'timeline-time';
991
1064
  TimelineEventNames.SEEKED = 'timeline-seeked';
992
1065
  TimelineEventNames.COMPLETE = 'timeline-complete';
1066
+ TimelineEventNames.RESTART = 'timeline-restart';
993
1067
  TimelineEventNames.PLAY = 'timeline-play';
994
1068
  TimelineEventNames.STOP = 'timeline-stop';
995
1069
  TimelineEventNames.PAUSE = 'timeline-pause';
@@ -1948,34 +2022,6 @@ var ctrls = {
1948
2022
  SubtitlesController: SubtitlesController
1949
2023
  };
1950
2024
 
1951
- var DevToolEventListener = /*#__PURE__*/function () {
1952
- function DevToolEventListener(agent) {
1953
- this.agent = void 0;
1954
- this.agent = agent;
1955
- }
1956
-
1957
- var _proto = DevToolEventListener.prototype;
1958
-
1959
- _proto.handleEvent = function handleEvent(eventName, eventTopic, args) {
1960
- this.agent.postMessage('eligius-diagnostics-event', {
1961
- eventName: eventName,
1962
- eventTopic: eventTopic
1963
- });
1964
- };
1965
-
1966
- return DevToolEventListener;
1967
- }();
1968
-
1969
- var Diagnostics = /*#__PURE__*/function () {
1970
- function Diagnostics() {}
1971
-
1972
- Diagnostics.send = function send(_name, _data) {};
1973
-
1974
- return Diagnostics;
1975
- }();
1976
-
1977
- var DEV_TOOLS_KEY = '__ELIGIUS_DEV_TOOLS__';
1978
-
1979
2025
  /**
1980
2026
  * EligiusEngine, this is where the magic happens. The engine is responsible for starting and stopping
1981
2027
  * the given timeline provider and triggering the actions associated with it.
@@ -2825,24 +2871,38 @@ var LanguageManager = /*#__PURE__*/function () {
2825
2871
  }();
2826
2872
 
2827
2873
  var EngineFactory = /*#__PURE__*/function () {
2828
- function EngineFactory(importer, windowRef, eventbus) {
2874
+ function EngineFactory(importer, windowRef, options) {
2875
+ var _options$devtools;
2876
+
2829
2877
  this.resizeTimeout = -1;
2830
2878
  this.actionsLookup = {};
2831
2879
  this.importer = void 0;
2832
2880
  this.eventbus = void 0;
2881
+ this._handleSpaceBound = void 0;
2833
2882
  this.importer = importer;
2834
- this.eventbus = eventbus || new Eventbus();
2883
+ this.eventbus = (options == null ? void 0 : options.eventbus) || new Eventbus();
2884
+ Diagnostics.active = (_options$devtools = options == null ? void 0 : options.devtools) != null ? _options$devtools : false;
2835
2885
 
2836
- this._initializeDevtools(this.eventbus);
2886
+ if (Diagnostics.active) {
2887
+ this._initializeDevtools(this.eventbus);
2888
+ }
2837
2889
 
2838
2890
  this.eventbus.on(TimelineEventNames.REQUEST_INSTANCE, this._requestInstanceHandler.bind(this));
2839
2891
  this.eventbus.on(TimelineEventNames.REQUEST_ACTION, this._requestActionHandler.bind(this));
2840
2892
  this.eventbus.on(TimelineEventNames.REQUEST_FUNCTION, this._requestFunctionHandler.bind(this));
2893
+ this._handleSpaceBound = this._handleSpacePress.bind(this);
2894
+ hotkeys('space', this._handleSpaceBound);
2841
2895
  $(windowRef).resize(this._resizeHandler.bind(this));
2842
2896
  }
2843
2897
 
2844
2898
  var _proto = EngineFactory.prototype;
2845
2899
 
2900
+ _proto._handleSpacePress = function _handleSpacePress(keyboardEvent, _hotkeysEvent) {
2901
+ keyboardEvent.preventDefault();
2902
+ this.eventbus.broadcast(TimelineEventNames.PLAY_TOGGLE_REQUEST);
2903
+ return false;
2904
+ };
2905
+
2846
2906
  _proto._initializeDevtools = function _initializeDevtools(eventbus) {
2847
2907
  var diagnosticInfo = window[DEV_TOOLS_KEY];
2848
2908
 
@@ -2854,11 +2914,16 @@ var EngineFactory = /*#__PURE__*/function () {
2854
2914
  Diagnostics.send = function (name, data) {
2855
2915
  agent.postMessage(name, data);
2856
2916
  };
2917
+
2918
+ Diagnostics.send('eligius-diagnostics-factory', 'Diagnostics initialized');
2919
+ } else {
2920
+ console.warn(DEV_TOOLS_KEY + " property not found on window, please install the extension.");
2857
2921
  }
2858
2922
  };
2859
2923
 
2860
2924
  _proto.destroy = function destroy() {
2861
2925
  this.eventbus.clear();
2926
+ hotkeys.unbind('space', this._handleSpaceBound);
2862
2927
  };
2863
2928
 
2864
2929
  _proto._resizeHandler = function _resizeHandler() {
@@ -2903,8 +2968,7 @@ var EngineFactory = /*#__PURE__*/function () {
2903
2968
  };
2904
2969
 
2905
2970
  _proto.createEngine = function createEngine(configuration, resolver) {
2906
- var _configuration$eventA,
2907
- _this2 = this;
2971
+ var _configuration$eventA;
2908
2972
 
2909
2973
  var systemName = configuration.engine.systemName;
2910
2974
 
@@ -2932,18 +2996,11 @@ var EngineFactory = /*#__PURE__*/function () {
2932
2996
  labels = configuration.labels;
2933
2997
  var languageManager = new LanguageManager(language, labels, this.eventbus);
2934
2998
  var engineInstance = new EngineClass(resolvedConfiguration, this.eventbus, timelineProviders, languageManager);
2935
- Mousetrap.bind('space', function (event) {
2936
- event.preventDefault();
2937
-
2938
- _this2.eventbus.broadcast(TimelineEventNames.PLAY_TOGGLE_REQUEST);
2939
-
2940
- return false;
2941
- });
2942
2999
  return engineInstance;
2943
3000
  };
2944
3001
 
2945
3002
  _proto._createTimelineProviders = function _createTimelineProviders(configuration, eventbus) {
2946
- var _this3 = this;
3003
+ var _this2 = this;
2947
3004
 
2948
3005
  var timelineProviderSettings = configuration.timelineProviderSettings;
2949
3006
  var result = Object.entries(timelineProviderSettings).reduce(function (acc, _ref) {
@@ -2954,7 +3011,7 @@ var EngineFactory = /*#__PURE__*/function () {
2954
3011
  return acc;
2955
3012
  }
2956
3013
 
2957
- var timelineProviderClass = _this3._importSystemEntry(settings.systemName);
3014
+ var timelineProviderClass = _this2._importSystemEntry(settings.systemName);
2958
3015
 
2959
3016
  acc[timelineType] = {
2960
3017
  id: settings.id,
@@ -4451,20 +4508,46 @@ var JwPlayerTimelineProvider = /*#__PURE__*/function () {
4451
4508
  return JwPlayerTimelineProvider;
4452
4509
  }();
4453
4510
 
4511
+ // https://gist.github.com/jakearchibald/cb03f15670817001b1157e62a076fe95
4512
+ function animationInterval(ms, signal, callback) {
4513
+ var _document$timeline$cu, _document, _document$timeline;
4514
+
4515
+ // Prefer currentTime, as it'll better sync animations queued in the
4516
+ // same frame, but if it isn't supported, performance.now() is fine.
4517
+ var start = (_document$timeline$cu = (_document = document) == null ? void 0 : (_document$timeline = _document.timeline) == null ? void 0 : _document$timeline.currentTime) != null ? _document$timeline$cu : performance.now();
4518
+
4519
+ function frame(time) {
4520
+ if (signal.aborted) return;
4521
+ callback(time);
4522
+ scheduleFrame(time);
4523
+ }
4524
+
4525
+ function scheduleFrame(time) {
4526
+ var elapsed = time - start;
4527
+ var roundedElapsed = Math.round(elapsed / ms) * ms;
4528
+ var targetNext = start + roundedElapsed + ms;
4529
+ var delay = targetNext - performance.now();
4530
+ setTimeout(function () {
4531
+ return requestAnimationFrame(frame);
4532
+ }, delay);
4533
+ }
4534
+
4535
+ scheduleFrame(start);
4536
+ }
4537
+
4454
4538
  var RequestAnimationFrameTimelineProvider = /*#__PURE__*/function () {
4455
4539
  function RequestAnimationFrameTimelineProvider(eventbus, config) {
4456
4540
  this.eventbus = void 0;
4457
4541
  this.config = void 0;
4458
- this._requestID = -1;
4459
- this._last = 0;
4460
4542
  this._currentPosition = 0;
4461
4543
  this._updateBound = this._update.bind(this);
4462
4544
  this._eventbusListeners = [];
4463
4545
  this._firstFrame = true;
4464
4546
  this._currentPlaylistItem = void 0;
4465
- this._granularity = 1000;
4547
+ this._tickInterval = 1000;
4466
4548
  this._playlist = void 0;
4467
4549
  this._containerElement = void 0;
4550
+ this._abortController = void 0;
4468
4551
  this.playState = 'stopped';
4469
4552
  this.loop = false;
4470
4553
  this.eventbus = eventbus;
@@ -4519,55 +4602,53 @@ var RequestAnimationFrameTimelineProvider = /*#__PURE__*/function () {
4519
4602
  this._eventbusListeners.push(this.eventbus.on(TimelineEventNames.DURATION_REQUEST, this.requestDurationHandler.bind(this)));
4520
4603
  };
4521
4604
 
4522
- _proto._update = function _update(now) {
4605
+ _proto._update = function _update(_now) {
4523
4606
  if (this.playState !== 'running') {
4524
4607
  return;
4525
4608
  }
4526
4609
 
4527
- if (!this._last || now - this._last >= this._granularity) {
4528
- if (!this._last && this._firstFrame) {
4529
- this._firstFrame = false;
4530
- this.eventbus.broadcast(TimelineEventNames.FIRST_FRAME);
4531
- }
4610
+ if (this._firstFrame) {
4611
+ this._firstFrame = false;
4612
+ this.eventbus.broadcast(TimelineEventNames.FIRST_FRAME);
4613
+ }
4532
4614
 
4533
- this._last = now;
4534
- this._currentPosition++;
4615
+ this._currentPosition++;
4535
4616
 
4536
- if (this._currentPosition > this._currentPlaylistItem.duration) {
4537
- if (this.loop) {
4538
- this._reset();
4539
- } else {
4540
- this.stop();
4541
- this.eventbus.broadcast(TimelineEventNames.COMPLETE);
4542
- return;
4543
- }
4544
- }
4617
+ if (this._currentPosition > this._currentPlaylistItem.duration) {
4618
+ if (this.loop) {
4619
+ this._resetToStartPosition();
4545
4620
 
4546
- this.eventbus.broadcast(TimelineEventNames.TIME, [{
4547
- position: this._currentPosition
4548
- }]);
4549
- this.eventbus.broadcast(TimelineEventNames.POSITION_UPDATE, [{
4550
- position: this._currentPosition,
4551
- duration: this._currentPlaylistItem.duration
4552
- }]);
4621
+ this.eventbus.broadcast(TimelineEventNames.RESTART);
4622
+ } else {
4623
+ this.stop();
4624
+ this.eventbus.broadcast(TimelineEventNames.COMPLETE);
4625
+ return;
4626
+ }
4553
4627
  }
4554
4628
 
4555
- this._requestID = requestAnimationFrame(this._updateBound);
4629
+ this.eventbus.broadcast(TimelineEventNames.TIME, [{
4630
+ position: this._currentPosition
4631
+ }]);
4632
+ this.eventbus.broadcast(TimelineEventNames.POSITION_UPDATE, [{
4633
+ position: this._currentPosition,
4634
+ duration: this._currentPlaylistItem.duration
4635
+ }]);
4556
4636
  };
4557
4637
 
4558
4638
  _proto._start = function _start() {
4559
- if (this._requestID && this.playState === 'running') {
4639
+ var _this$_abortControlle;
4640
+
4641
+ if (this.playState === 'running') {
4560
4642
  return;
4561
4643
  }
4562
4644
 
4563
4645
  this.playState = 'running';
4564
- this._requestID = requestAnimationFrame(this._updateBound);
4646
+ (_this$_abortControlle = this._abortController) == null ? void 0 : _this$_abortControlle.abort();
4647
+ this._abortController = new AbortController();
4648
+ animationInterval(this._tickInterval, this._abortController.signal, this._updateBound);
4565
4649
  };
4566
4650
 
4567
- _proto._reset = function _reset() {
4568
- this._cancelAnimationFrame();
4569
-
4570
- this._last = 0;
4651
+ _proto._resetToStartPosition = function _resetToStartPosition() {
4571
4652
  this._currentPosition = 0;
4572
4653
  };
4573
4654
 
@@ -4579,12 +4660,18 @@ var RequestAnimationFrameTimelineProvider = /*#__PURE__*/function () {
4579
4660
  callBack(this._containerElement);
4580
4661
  };
4581
4662
 
4582
- _proto._cancelAnimationFrame = function _cancelAnimationFrame() {
4583
- if (this._requestID) {
4584
- cancelAnimationFrame(this._requestID);
4585
- this._requestID = -1;
4586
- this._last = 0;
4587
- this._currentPosition = 0;
4663
+ _proto._stopAnimationInterval = function _stopAnimationInterval(resetPosition) {
4664
+ var _this$_abortControlle2;
4665
+
4666
+ if (resetPosition === void 0) {
4667
+ resetPosition = true;
4668
+ }
4669
+
4670
+ (_this$_abortControlle2 = this._abortController) == null ? void 0 : _this$_abortControlle2.abort();
4671
+ this._abortController = undefined;
4672
+
4673
+ if (resetPosition) {
4674
+ this._resetToStartPosition();
4588
4675
  }
4589
4676
  };
4590
4677
 
@@ -4629,13 +4716,15 @@ var RequestAnimationFrameTimelineProvider = /*#__PURE__*/function () {
4629
4716
  };
4630
4717
 
4631
4718
  _proto.stop = function stop() {
4632
- this._cancelAnimationFrame();
4719
+ this._stopAnimationInterval();
4633
4720
 
4634
4721
  this.playState = 'stopped';
4635
4722
  this.eventbus.broadcast(TimelineEventNames.STOP);
4636
4723
  };
4637
4724
 
4638
4725
  _proto.pause = function pause() {
4726
+ this._stopAnimationInterval(false);
4727
+
4639
4728
  this.playState = 'paused';
4640
4729
  this.eventbus.broadcast(TimelineEventNames.PAUSE);
4641
4730
  };