@raydiant/playlist-window-manager 0.14.24 → 0.15.0

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.
@@ -57,6 +57,7 @@ export default class PlaylistWindowManager extends EventEmitter {
57
57
  setRuleEngine(ruleEngine: RE.RuleEngine): Promise<void>;
58
58
  getState(): PlaylistWindowManagerState | null;
59
59
  setState(state: PlaylistWindowManagerState): void;
60
+ reinitializeRuleEngineState(newRulesDataStoreObj: RE.RulesDataStore): void;
60
61
  reinitialize(state: PlaylistWindowManagerState, ruleEngine: RE.RuleEngine, shouldInterruptPlayback: boolean): Promise<void>;
61
62
  private registerInterrupt;
62
63
  endCurrentPresentation(): void;
@@ -89,6 +90,7 @@ export default class PlaylistWindowManager extends EventEmitter {
89
90
  private handleCurrentPresentationComplete;
90
91
  private handleCurrentPresentationError;
91
92
  private handleNextPresentationError;
93
+ private checkPreviewAction;
92
94
  private startPreviewCheck;
93
95
  private stopInterruptCheck;
94
96
  private getTheme;
@@ -270,6 +270,35 @@ var PlaylistWindowManager = /** @class */ (function (_super) {
270
270
  }
271
271
  });
272
272
  }); };
273
+ _this.checkPreviewAction = function () { return __awaiter(_this, void 0, void 0, function () {
274
+ var logger, preview;
275
+ return __generator(this, function (_a) {
276
+ switch (_a.label) {
277
+ case 0:
278
+ logger = this.options.logger;
279
+ if (!this.ruleEngine) {
280
+ return [2 /*return*/];
281
+ }
282
+ preview = this.ruleEngine.previewNext();
283
+ if (!this.shouldPreload(preview.presentationId)) return [3 /*break*/, 3];
284
+ logger.info("Preview check replacing preloaded presentation '" + this.nextPresentationId() + "' with new presentation '" + preview.presentationId + "'");
285
+ return [4 /*yield*/, this.removeNextWindow()];
286
+ case 1:
287
+ _a.sent();
288
+ this.nextPresentation = preview;
289
+ return [4 /*yield*/, this.preloadNextWindow()];
290
+ case 2:
291
+ _a.sent();
292
+ _a.label = 3;
293
+ case 3:
294
+ if (preview.shouldInterrupt) {
295
+ logger.info("Interrupting current presentation with '" + preview.presentationId + "'");
296
+ this.registerInterrupt();
297
+ }
298
+ return [2 /*return*/];
299
+ }
300
+ });
301
+ }); };
273
302
  _this.ruleEngine = options.ruleEngine;
274
303
  _this.state = options.state;
275
304
  _this.bounds = options.bounds;
@@ -430,6 +459,39 @@ var PlaylistWindowManager = /** @class */ (function (_super) {
430
459
  PlaylistWindowManager.prototype.setState = function (state) {
431
460
  this.state = state;
432
461
  };
462
+ // For setting dynamic tags data from workers and reevaluate rules for playlists
463
+ // input: newRulesDataStoreObj: {'workers': {'ai': {'percentMales': 30}}}
464
+ PlaylistWindowManager.prototype.reinitializeRuleEngineState = function (newRulesDataStoreObj) {
465
+ var _this = this;
466
+ var logger = this.options.logger;
467
+ logger.info('Dynamic Rule engine state change event received!');
468
+ if (this.ruleEngine) {
469
+ logger.info("Existing Rule Data Store: ", this.ruleEngine.state);
470
+ logger.info("New Rule Data Store data received: ", newRulesDataStoreObj);
471
+ this.ruleEngine.state.rulesDataStore = newRulesDataStoreObj;
472
+ }
473
+ else {
474
+ logger.warn("No Rule engine found for reinitiazling the state!");
475
+ return;
476
+ }
477
+ // re-evaluate the next presentations depending upon the new rule engine state
478
+ var checkOverridePresentation = function () { return __awaiter(_this, void 0, void 0, function () {
479
+ return __generator(this, function (_a) {
480
+ if (this.ruleEngine) {
481
+ this.checkPreviewAction();
482
+ // calling reinitialize Rule engine state for inner playlist windows (for MZ)
483
+ if (this.currentWindow) {
484
+ this.currentWindow.reinitializePresentationWindowRuleEngineState(newRulesDataStoreObj);
485
+ }
486
+ if (this.nextWindow) {
487
+ this.nextWindow.reinitializePresentationWindowRuleEngineState(newRulesDataStoreObj);
488
+ }
489
+ }
490
+ return [2 /*return*/];
491
+ });
492
+ }); };
493
+ checkOverridePresentation();
494
+ };
433
495
  // Replace state and rule engine, and reinitialize the window manager.
434
496
  PlaylistWindowManager.prototype.reinitialize = function (state, ruleEngine, shouldInterruptPlayback) {
435
497
  return __awaiter(this, void 0, void 0, function () {
@@ -860,39 +922,19 @@ var PlaylistWindowManager = /** @class */ (function (_super) {
860
922
  PlaylistWindowManager.prototype.startPreviewCheck = function () {
861
923
  var _this = this;
862
924
  var checkPreview = function () { return __awaiter(_this, void 0, void 0, function () {
863
- var logger, preview, now, nextMinute, timeUntilNextMinute;
925
+ var now, nextMinute, timeUntilNextMinute;
864
926
  return __generator(this, function (_a) {
865
- switch (_a.label) {
866
- case 0:
867
- logger = this.options.logger;
868
- if (!this.ruleEngine) return [3 /*break*/, 4];
869
- preview = this.ruleEngine.previewNext();
870
- if (!this.shouldPreload(preview.presentationId)) return [3 /*break*/, 3];
871
- logger.info("Preview check replacing preloaded presentation '" + this.nextPresentationId() + "' with new presentation '" + preview.presentationId + "'");
872
- return [4 /*yield*/, this.removeNextWindow()];
873
- case 1:
874
- _a.sent();
875
- this.nextPresentation = preview;
876
- return [4 /*yield*/, this.preloadNextWindow()];
877
- case 2:
878
- _a.sent();
879
- _a.label = 3;
880
- case 3:
881
- if (preview.shouldInterrupt) {
882
- logger.info("Interrupting current presentation with '" + preview.presentationId + "'");
883
- this.registerInterrupt();
884
- }
885
- _a.label = 4;
886
- case 4:
887
- now = new Date();
888
- nextMinute = new Date();
889
- nextMinute.setMilliseconds(0);
890
- nextMinute.setSeconds(1);
891
- nextMinute.setMinutes(now.getMinutes() + 1);
892
- timeUntilNextMinute = +nextMinute - +now;
893
- this.interruptCheckTimeout = setTimeout(checkPreview, timeUntilNextMinute);
894
- return [2 /*return*/];
927
+ if (this.ruleEngine) {
928
+ this.checkPreviewAction();
895
929
  }
930
+ now = new Date();
931
+ nextMinute = new Date();
932
+ nextMinute.setMilliseconds(0);
933
+ nextMinute.setSeconds(1);
934
+ nextMinute.setMinutes(now.getMinutes() + 1);
935
+ timeUntilNextMinute = +nextMinute - +now;
936
+ this.interruptCheckTimeout = setTimeout(checkPreview, timeUntilNextMinute);
937
+ return [2 /*return*/];
896
938
  });
897
939
  }); };
898
940
  checkPreview();
@@ -60,6 +60,7 @@ export default class PresentationWindow {
60
60
  show(isFocused?: boolean): void;
61
61
  hide(): void;
62
62
  remove(): Promise<void>;
63
+ reinitializePresentationWindowRuleEngineState(newRulesDataStoreObj: RE.RulesDataStore): void;
63
64
  play(): void;
64
65
  pause(): void;
65
66
  resume(): void;
@@ -63,7 +63,7 @@ var PlaylistWindowManager_1 = __importDefault(require("./PlaylistWindowManager")
63
63
  var WindowTimeoutError_1 = __importDefault(require("./WindowTimeoutError"));
64
64
  var WindowLoadError_1 = __importDefault(require("./WindowLoadError"));
65
65
  // TODO: Automatically inject latest version when building in CI.
66
- var defaultAppRendererUrl = 'https://apps.raydiant.com/1.4.6/index.html';
66
+ var defaultAppRendererUrl = 'https://apps.raydiant.com/1.4.7/index.html';
67
67
  var isValidNumber = function (value) {
68
68
  return typeof value === 'number' && !isNaN(value);
69
69
  };
@@ -255,6 +255,12 @@ var PresentationWindow = /** @class */ (function () {
255
255
  });
256
256
  });
257
257
  };
258
+ // Method to modify the rule engine state for the inner playlist windows (called from outer playlist window manager)
259
+ PresentationWindow.prototype.reinitializePresentationWindowRuleEngineState = function (newRulesDataStoreObj) {
260
+ Object.values(this.windowManagers).forEach(function (wm) {
261
+ wm.reinitializeRuleEngineState(newRulesDataStoreObj);
262
+ });
263
+ };
258
264
  PresentationWindow.prototype.play = function () {
259
265
  var _this = this;
260
266
  var _a = this.options, applicationVersion = _a.applicationVersion, presentation = _a.presentation;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function createEmbeddedApp(presentation, applicationVersion) {
4
4
  // build matches
5
- var parameterRegex = /(:([\w]+))(.?$|[^\w])/gm;
5
+ var parameterRegex = /(:([\w-]+))(.?$|[^\w-])/gm;
6
6
  var matches = [];
7
7
  var match = null;
8
8
  do {
@@ -14,8 +14,9 @@ function createEmbeddedApp(presentation, applicationVersion) {
14
14
  // replace
15
15
  var url = applicationVersion.embeddedUrlFormat || '';
16
16
  matches.forEach(function (m) {
17
- var variable = m[1];
18
- var _a = variable.split(':'), variableName = _a[1];
17
+ var queryParamVariableName = m[1]; // Where the value of your input will be injected. E.g. ":location"
18
+ var queryParamName = m[2]; // The query param name. E.g. "location"
19
+ var _a = queryParamVariableName.split(':'), variableName = _a[1];
19
20
  // Don't replace variables that are numbers to allow setting
20
21
  // port numbers in the embedded app url.
21
22
  if (!isNaN(Number(variableName)))
@@ -24,12 +25,16 @@ function createEmbeddedApp(presentation, applicationVersion) {
24
25
  // If the presentation property is not found (this shouldn't happen), default to encoding the value.
25
26
  // Otherwise, only skip encoding if the presentation property's encode property is false.
26
27
  var shouldEncodeValue = !presentationProperty || presentationProperty.encode !== false;
27
- var value = presentation.applicationVariables[m[2]];
28
+ var value = presentation.applicationVariables[queryParamName];
28
29
  if (value === undefined)
29
30
  value = '';
30
- if (shouldEncodeValue)
31
+ if (Array.isArray(value)) {
32
+ value = JSON.stringify(value);
33
+ }
34
+ if (shouldEncodeValue) {
31
35
  value = encodeURIComponent(value);
32
- url = url.replace(variable, value);
36
+ }
37
+ url = url.replace(queryParamVariableName, value);
33
38
  });
34
39
  return url;
35
40
  }
@@ -43,6 +43,11 @@ test('Replaces variable with underscore', function () {
43
43
  embeddedUrlFormat: 'https://example.com/:foo_1'
44
44
  }))).toBe('https://example.com/bar');
45
45
  });
46
+ test('Replaces variable with hyphen', function () {
47
+ expect(embeddedApps_1.default(createPresentation({ applicationVariables: { 'foo-1': 'bar' } }), createApplicationVersion({
48
+ embeddedUrlFormat: 'https://example.com/:foo-1'
49
+ }))).toBe('https://example.com/bar');
50
+ });
46
51
  test('Does not replace variable that is a number', function () {
47
52
  expect(embeddedApps_1.default(createPresentation({ applicationVariables: { foo: 'bar' } }), createApplicationVersion({
48
53
  embeddedUrlFormat: 'http://localhost:8080?foo=:foo'
@@ -63,3 +68,10 @@ test('Does not encode value if presentation property disables encoding', functio
63
68
  presentationProperties: [{ name: 'url', encode: false }]
64
69
  }))).toBe("https://raydiant.com");
65
70
  });
71
+ test('Encodes a variables containing an array', function () {
72
+ var fooValue = ['option-a', 'option-b', 'option-c'];
73
+ var fooValueEncoded = '%5B%22option-a%22%2C%22option-b%22%2C%22option-c%22%5D';
74
+ expect(embeddedApps_1.default(createPresentation({ applicationVariables: { foo: fooValue } }), createApplicationVersion({
75
+ embeddedUrlFormat: "http://localhost:8080?foo=:foo"
76
+ }))).toBe("http://localhost:8080?foo=" + fooValueEncoded);
77
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raydiant/playlist-window-manager",
3
- "version": "0.14.24",
3
+ "version": "0.15.0",
4
4
  "description": "Window manager for playlist playback.",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -12,8 +12,8 @@
12
12
  },
13
13
  "dependencies": {
14
14
  "fast-deep-equal": "^3.1.3",
15
- "@raydiant/playlist-rule-engine": "3.1.15",
16
- "raydiant-kit": "4.7.1",
15
+ "@raydiant/playlist-rule-engine": "3.2.0",
16
+ "raydiant-kit": "4.7.3",
17
17
  "uuid": "^8.3.0",
18
18
  "eventemitter3": "^4.0.7"
19
19
  },
@@ -265,6 +265,43 @@ export default class PlaylistWindowManager extends EventEmitter {
265
265
  this.state = state;
266
266
  }
267
267
 
268
+ // For setting dynamic tags data from workers and reevaluate rules for playlists
269
+ // input: newRulesDataStoreObj: {'workers': {'ai': {'percentMales': 30}}}
270
+ public reinitializeRuleEngineState(newRulesDataStoreObj: RE.RulesDataStore) {
271
+ const { logger } = this.options;
272
+
273
+ logger.info('Dynamic Rule engine state change event received!');
274
+ if (this.ruleEngine) {
275
+ logger.info(`Existing Rule Data Store: `, this.ruleEngine.state);
276
+ logger.info(`New Rule Data Store data received: `, newRulesDataStoreObj);
277
+ this.ruleEngine.state.rulesDataStore = newRulesDataStoreObj;
278
+ } else {
279
+ logger.warn(`No Rule engine found for reinitiazling the state!`);
280
+ return;
281
+ }
282
+
283
+ // re-evaluate the next presentations depending upon the new rule engine state
284
+ const checkOverridePresentation = async () => {
285
+ if (this.ruleEngine) {
286
+ this.checkPreviewAction();
287
+
288
+ // calling reinitialize Rule engine state for inner playlist windows (for MZ)
289
+ if (this.currentWindow) {
290
+ this.currentWindow.reinitializePresentationWindowRuleEngineState(
291
+ newRulesDataStoreObj
292
+ );
293
+ }
294
+
295
+ if (this.nextWindow) {
296
+ this.nextWindow.reinitializePresentationWindowRuleEngineState(
297
+ newRulesDataStoreObj
298
+ );
299
+ }
300
+ }
301
+ };
302
+ checkOverridePresentation();
303
+ }
304
+
268
305
  // Replace state and rule engine, and reinitialize the window manager.
269
306
  public async reinitialize(
270
307
  state: PlaylistWindowManagerState,
@@ -854,30 +891,36 @@ export default class PlaylistWindowManager extends EventEmitter {
854
891
  }
855
892
  };
856
893
 
857
- private startPreviewCheck() {
858
- const checkPreview = async () => {
859
- const { logger } = this.options;
894
+ private checkPreviewAction = async () => {
895
+ const { logger } = this.options;
896
+ if (!this.ruleEngine) {
897
+ return;
898
+ }
899
+ const preview = this.ruleEngine.previewNext();
860
900
 
861
- if (this.ruleEngine) {
862
- const preview = this.ruleEngine.previewNext();
901
+ if (this.shouldPreload(preview.presentationId)) {
902
+ logger.info(
903
+ `Preview check replacing preloaded presentation '${this.nextPresentationId()}' with new presentation '${
904
+ preview.presentationId
905
+ }'`
906
+ );
907
+ await this.removeNextWindow();
908
+ this.nextPresentation = preview;
909
+ await this.preloadNextWindow();
910
+ }
863
911
 
864
- if (this.shouldPreload(preview.presentationId)) {
865
- logger.info(
866
- `Preview check replacing preloaded presentation '${this.nextPresentationId()}' with new presentation '${
867
- preview.presentationId
868
- }'`
869
- );
870
- await this.removeNextWindow();
871
- this.nextPresentation = preview;
872
- await this.preloadNextWindow();
873
- }
912
+ if (preview.shouldInterrupt) {
913
+ logger.info(
914
+ `Interrupting current presentation with '${preview.presentationId}'`
915
+ );
916
+ this.registerInterrupt();
917
+ }
918
+ };
874
919
 
875
- if (preview.shouldInterrupt) {
876
- logger.info(
877
- `Interrupting current presentation with '${preview.presentationId}'`
878
- );
879
- this.registerInterrupt();
880
- }
920
+ private startPreviewCheck() {
921
+ const checkPreview = async () => {
922
+ if (this.ruleEngine) {
923
+ this.checkPreviewAction();
881
924
  }
882
925
 
883
926
  // Check the preview again on the next minute.
@@ -49,7 +49,7 @@ export interface PresentationWindowOptions {
49
49
  }
50
50
 
51
51
  // TODO: Automatically inject latest version when building in CI.
52
- const defaultAppRendererUrl = 'https://apps.raydiant.com/1.4.6/index.html';
52
+ const defaultAppRendererUrl = 'https://apps.raydiant.com/1.4.7/index.html';
53
53
 
54
54
  const isValidNumber = (value: any) =>
55
55
  typeof value === 'number' && !isNaN(value);
@@ -180,6 +180,15 @@ export default class PresentationWindow {
180
180
  }
181
181
  }
182
182
 
183
+ // Method to modify the rule engine state for the inner playlist windows (called from outer playlist window manager)
184
+ public reinitializePresentationWindowRuleEngineState(
185
+ newRulesDataStoreObj: RE.RulesDataStore
186
+ ) {
187
+ Object.values(this.windowManagers).forEach(wm => {
188
+ wm.reinitializeRuleEngineState(newRulesDataStoreObj);
189
+ });
190
+ }
191
+
183
192
  public play(): void {
184
193
  const { applicationVersion, presentation } = this.options;
185
194
 
@@ -77,6 +77,17 @@ test('Replaces variable with underscore', () => {
77
77
  ).toBe('https://example.com/bar');
78
78
  });
79
79
 
80
+ test('Replaces variable with hyphen', () => {
81
+ expect(
82
+ createEmbeddedApp(
83
+ createPresentation({ applicationVariables: { 'foo-1': 'bar' } }),
84
+ createApplicationVersion({
85
+ embeddedUrlFormat: 'https://example.com/:foo-1'
86
+ })
87
+ )
88
+ ).toBe('https://example.com/bar');
89
+ });
90
+
80
91
  test('Does not replace variable that is a number', () => {
81
92
  expect(
82
93
  createEmbeddedApp(
@@ -116,3 +127,18 @@ test('Does not encode value if presentation property disables encoding', () => {
116
127
  )
117
128
  ).toBe(`https://raydiant.com`);
118
129
  });
130
+
131
+ test('Encodes a variables containing an array', () => {
132
+ const fooValue = ['option-a', 'option-b', 'option-c'];
133
+ const fooValueEncoded =
134
+ '%5B%22option-a%22%2C%22option-b%22%2C%22option-c%22%5D';
135
+
136
+ expect(
137
+ createEmbeddedApp(
138
+ createPresentation({ applicationVariables: { foo: fooValue } }),
139
+ createApplicationVersion({
140
+ embeddedUrlFormat: `http://localhost:8080?foo=:foo`
141
+ })
142
+ )
143
+ ).toBe(`http://localhost:8080?foo=${fooValueEncoded}`);
144
+ });
@@ -5,7 +5,7 @@ export default function createEmbeddedApp(
5
5
  applicationVersion: ApplicationVersion
6
6
  ) {
7
7
  // build matches
8
- const parameterRegex = /(:([\w]+))(.?$|[^\w])/gm;
8
+ const parameterRegex = /(:([\w-]+))(.?$|[^\w-])/gm;
9
9
  const matches = [];
10
10
  let match = null;
11
11
  do {
@@ -19,8 +19,10 @@ export default function createEmbeddedApp(
19
19
  // replace
20
20
  let url = applicationVersion.embeddedUrlFormat || '';
21
21
  matches.forEach(m => {
22
- const variable = m[1];
23
- const [, variableName] = variable.split(':');
22
+ const queryParamVariableName = m[1]; // Where the value of your input will be injected. E.g. ":location"
23
+ const queryParamName = m[2]; // The query param name. E.g. "location"
24
+
25
+ const [, variableName] = queryParamVariableName.split(':');
24
26
  // Don't replace variables that are numbers to allow setting
25
27
  // port numbers in the embedded app url.
26
28
  if (!isNaN(Number(variableName))) return;
@@ -34,11 +36,16 @@ export default function createEmbeddedApp(
34
36
  const shouldEncodeValue =
35
37
  !presentationProperty || presentationProperty.encode !== false;
36
38
 
37
- let value = presentation.applicationVariables[m[2]];
39
+ let value = presentation.applicationVariables[queryParamName];
38
40
  if (value === undefined) value = '';
39
- if (shouldEncodeValue) value = encodeURIComponent(value);
41
+ if (Array.isArray(value)) {
42
+ value = JSON.stringify(value);
43
+ }
44
+ if (shouldEncodeValue) {
45
+ value = encodeURIComponent(value);
46
+ }
40
47
 
41
- url = url.replace(variable, value);
48
+ url = url.replace(queryParamVariableName, value);
42
49
  });
43
50
 
44
51
  return url;