@player-ui/beacon-plugin 0.3.1-next.0 → 0.4.0-next.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.
package/dist/index.cjs.js CHANGED
@@ -72,16 +72,18 @@ const _BeaconPlugin = class {
72
72
  });
73
73
  }
74
74
  }
75
- apply(player) {
76
- this.player = player;
77
- this.logger = player.logger;
78
- player.hooks.dataController.tap(this.name, (dataController) => {
79
- this.dataController = dataController;
80
- });
81
- player.hooks.expressionEvaluator.tap(this.name, (expressionEvaluator) => {
82
- this.expressionEvaluator = expressionEvaluator;
75
+ apply(player$1) {
76
+ this.player = player$1;
77
+ this.logger = player$1.logger;
78
+ player$1.hooks.state.tap(this.name, (playerState) => {
79
+ if (playerState.status === "in-progress") {
80
+ this.resolveDataRefs = (data) => player.resolveDataRefs(data, {
81
+ model: playerState.controllers.data,
82
+ evaluate: playerState.controllers.expression.evaluate
83
+ });
84
+ }
83
85
  });
84
- player.hooks.viewController.tap(this.name, (vc) => {
86
+ player$1.hooks.viewController.tap(this.name, (vc) => {
85
87
  this.beaconContext = {
86
88
  view: void 0
87
89
  };
@@ -115,7 +117,7 @@ const _BeaconPlugin = class {
115
117
  });
116
118
  });
117
119
  });
118
- player.hooks.expressionEvaluator.tap(this.name, (evaluator) => {
120
+ player$1.hooks.expressionEvaluator.tap(this.name, (evaluator) => {
119
121
  evaluator.addExpressionFunction("beacon", (_ctx, action, data) => {
120
122
  const view = this.beaconContext.view || {};
121
123
  this.beacon({
@@ -132,8 +134,9 @@ const _BeaconPlugin = class {
132
134
  const { action, element, asset, view } = event;
133
135
  const { view: currentView } = this.beaconContext;
134
136
  setTimeout(() => __async(this, null, function* () {
135
- var _a, _b, _c, _d;
136
- const data = (event == null ? void 0 : event.data) || ((_b = (_a = event.asset) == null ? void 0 : _a.metaData) == null ? void 0 : _b.beacon);
137
+ var _a, _b, _c, _d, _e, _f;
138
+ const unresolvedData = (event == null ? void 0 : event.data) || ((_b = (_a = event.asset) == null ? void 0 : _a.metaData) == null ? void 0 : _b.beacon);
139
+ const data = (_d = (_c = this.resolveDataRefs) == null ? void 0 : _c.call(this, unresolvedData)) != null ? _d : unresolvedData;
137
140
  const defaultBeacon = {
138
141
  action,
139
142
  element,
@@ -141,22 +144,21 @@ const _BeaconPlugin = class {
141
144
  assetId: asset == null ? void 0 : asset.id,
142
145
  viewId: currentView == null ? void 0 : currentView.id
143
146
  };
144
- const state = (_c = this.player) == null ? void 0 : _c.getState();
147
+ const state = (_e = this.player) == null ? void 0 : _e.getState();
145
148
  const hookArgs = __spreadProps(__spreadValues({}, event), {
146
149
  data,
147
150
  state,
148
- view: view || currentView,
151
+ view: view != null ? view : currentView,
149
152
  logger: this.logger
150
153
  });
151
- const beacon = (yield this.hooks.buildBeacon.call(defaultBeacon, hookArgs)) || defaultBeacon;
154
+ let beacon = (yield this.hooks.buildBeacon.call(defaultBeacon, hookArgs)) || defaultBeacon;
155
+ if (beacon !== defaultBeacon && this.resolveDataRefs) {
156
+ beacon = this.resolveDataRefs(beacon);
157
+ }
152
158
  const shouldCancel = this.hooks.cancelBeacon.call(hookArgs) || false;
153
159
  if (!shouldCancel) {
154
- const resolvedBeacon = this.dataController && this.expressionEvaluator ? player.resolveDataRefs(beacon, {
155
- model: this.dataController,
156
- evaluate: this.expressionEvaluator.evaluate
157
- }) : beacon;
158
- (_d = this.logger) == null ? void 0 : _d.debug("Sending beacon event", resolvedBeacon);
159
- this.hooks.publishBeacon.call(resolvedBeacon);
160
+ (_f = this.logger) == null ? void 0 : _f.debug("Sending beacon event", beacon);
161
+ this.hooks.publishBeacon.call(beacon);
160
162
  }
161
163
  }), 0);
162
164
  }
package/dist/index.d.ts CHANGED
@@ -52,8 +52,7 @@ declare class BeaconPlugin implements PlayerPlugin {
52
52
  private player?;
53
53
  private logger?;
54
54
  private beaconContext;
55
- private dataController?;
56
- private expressionEvaluator?;
55
+ private resolveDataRefs?;
57
56
  hooks: {
58
57
  buildBeacon: AsyncSeriesWaterfallHook<[unknown, HookArgs], Record<string, any>>;
59
58
  cancelBeacon: SyncBailHook<[HookArgs], boolean, Record<string, any>>;
package/dist/index.esm.js CHANGED
@@ -71,11 +71,13 @@ const _BeaconPlugin = class {
71
71
  apply(player) {
72
72
  this.player = player;
73
73
  this.logger = player.logger;
74
- player.hooks.dataController.tap(this.name, (dataController) => {
75
- this.dataController = dataController;
76
- });
77
- player.hooks.expressionEvaluator.tap(this.name, (expressionEvaluator) => {
78
- this.expressionEvaluator = expressionEvaluator;
74
+ player.hooks.state.tap(this.name, (playerState) => {
75
+ if (playerState.status === "in-progress") {
76
+ this.resolveDataRefs = (data) => resolveDataRefs(data, {
77
+ model: playerState.controllers.data,
78
+ evaluate: playerState.controllers.expression.evaluate
79
+ });
80
+ }
79
81
  });
80
82
  player.hooks.viewController.tap(this.name, (vc) => {
81
83
  this.beaconContext = {
@@ -128,8 +130,9 @@ const _BeaconPlugin = class {
128
130
  const { action, element, asset, view } = event;
129
131
  const { view: currentView } = this.beaconContext;
130
132
  setTimeout(() => __async(this, null, function* () {
131
- var _a, _b, _c, _d;
132
- const data = (event == null ? void 0 : event.data) || ((_b = (_a = event.asset) == null ? void 0 : _a.metaData) == null ? void 0 : _b.beacon);
133
+ var _a, _b, _c, _d, _e, _f;
134
+ const unresolvedData = (event == null ? void 0 : event.data) || ((_b = (_a = event.asset) == null ? void 0 : _a.metaData) == null ? void 0 : _b.beacon);
135
+ const data = (_d = (_c = this.resolveDataRefs) == null ? void 0 : _c.call(this, unresolvedData)) != null ? _d : unresolvedData;
133
136
  const defaultBeacon = {
134
137
  action,
135
138
  element,
@@ -137,22 +140,21 @@ const _BeaconPlugin = class {
137
140
  assetId: asset == null ? void 0 : asset.id,
138
141
  viewId: currentView == null ? void 0 : currentView.id
139
142
  };
140
- const state = (_c = this.player) == null ? void 0 : _c.getState();
143
+ const state = (_e = this.player) == null ? void 0 : _e.getState();
141
144
  const hookArgs = __spreadProps(__spreadValues({}, event), {
142
145
  data,
143
146
  state,
144
- view: view || currentView,
147
+ view: view != null ? view : currentView,
145
148
  logger: this.logger
146
149
  });
147
- const beacon = (yield this.hooks.buildBeacon.call(defaultBeacon, hookArgs)) || defaultBeacon;
150
+ let beacon = (yield this.hooks.buildBeacon.call(defaultBeacon, hookArgs)) || defaultBeacon;
151
+ if (beacon !== defaultBeacon && this.resolveDataRefs) {
152
+ beacon = this.resolveDataRefs(beacon);
153
+ }
148
154
  const shouldCancel = this.hooks.cancelBeacon.call(hookArgs) || false;
149
155
  if (!shouldCancel) {
150
- const resolvedBeacon = this.dataController && this.expressionEvaluator ? resolveDataRefs(beacon, {
151
- model: this.dataController,
152
- evaluate: this.expressionEvaluator.evaluate
153
- }) : beacon;
154
- (_d = this.logger) == null ? void 0 : _d.debug("Sending beacon event", resolvedBeacon);
155
- this.hooks.publishBeacon.call(resolvedBeacon);
156
+ (_f = this.logger) == null ? void 0 : _f.debug("Sending beacon event", beacon);
157
+ this.hooks.publishBeacon.call(beacon);
156
158
  }
157
159
  }), 0);
158
160
  }
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@player-ui/beacon-plugin",
3
- "version": "0.3.1-next.0",
3
+ "version": "0.4.0-next.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org"
7
7
  },
8
8
  "peerDependencies": {
9
- "@player-ui/player": "0.3.1-next.0",
10
- "@player-ui/types": "0.3.1-next.0"
9
+ "@player-ui/player": "0.4.0-next.0",
10
+ "@player-ui/types": "0.4.0-next.0"
11
11
  },
12
12
  "dependencies": {
13
- "tapable-ts": "^0.1.0",
13
+ "tapable-ts": "^0.2.2",
14
14
  "timm": "^1.6.2",
15
15
  "@babel/runtime": "7.15.4"
16
16
  },
package/src/beacon.ts CHANGED
@@ -3,8 +3,6 @@ import type {
3
3
  Player,
4
4
  PlayerPlugin,
5
5
  PlayerFlowState,
6
- DataController,
7
- ExpressionEvaluator,
8
6
  Logger,
9
7
  } from '@player-ui/player';
10
8
  import { resolveDataRefs } from '@player-ui/player';
@@ -81,8 +79,7 @@ export class BeaconPlugin implements PlayerPlugin {
81
79
  view: undefined,
82
80
  };
83
81
 
84
- private dataController?: DataController;
85
- private expressionEvaluator?: ExpressionEvaluator;
82
+ private resolveDataRefs?: <T>(data: T) => T;
86
83
 
87
84
  public hooks = {
88
85
  buildBeacon: new AsyncSeriesWaterfallHook<[unknown, HookArgs]>(),
@@ -110,12 +107,14 @@ export class BeaconPlugin implements PlayerPlugin {
110
107
  this.player = player;
111
108
  this.logger = player.logger;
112
109
 
113
- player.hooks.dataController.tap(this.name, (dataController) => {
114
- this.dataController = dataController;
115
- });
116
-
117
- player.hooks.expressionEvaluator.tap(this.name, (expressionEvaluator) => {
118
- this.expressionEvaluator = expressionEvaluator;
110
+ player.hooks.state.tap(this.name, (playerState) => {
111
+ if (playerState.status === 'in-progress') {
112
+ this.resolveDataRefs = (data) =>
113
+ resolveDataRefs(data, {
114
+ model: playerState.controllers.data,
115
+ evaluate: playerState.controllers.expression.evaluate,
116
+ });
117
+ }
119
118
  });
120
119
 
121
120
  player.hooks.viewController.tap(this.name, (vc) => {
@@ -185,7 +184,9 @@ export class BeaconPlugin implements PlayerPlugin {
185
184
  const { action, element, asset, view } = event;
186
185
  const { view: currentView } = this.beaconContext;
187
186
  setTimeout(async () => {
188
- const data = event?.data || event.asset?.metaData?.beacon;
187
+ const unresolvedData = event?.data || event.asset?.metaData?.beacon;
188
+
189
+ const data = this.resolveDataRefs?.(unresolvedData) ?? unresolvedData;
189
190
 
190
191
  const defaultBeacon = {
191
192
  action,
@@ -199,25 +200,23 @@ export class BeaconPlugin implements PlayerPlugin {
199
200
  ...event,
200
201
  data,
201
202
  state,
202
- view: view || currentView,
203
+ view: view ?? currentView,
203
204
  logger: this.logger as Logger,
204
205
  };
205
- const beacon =
206
+ let beacon =
206
207
  (await this.hooks.buildBeacon.call(defaultBeacon, hookArgs)) ||
207
208
  defaultBeacon;
209
+
210
+ // Re-resolve data refs in case the hook modified the beacon and introduced more
211
+ if (beacon !== defaultBeacon && this.resolveDataRefs) {
212
+ beacon = this.resolveDataRefs(beacon);
213
+ }
214
+
208
215
  const shouldCancel = this.hooks.cancelBeacon.call(hookArgs) || false;
209
216
 
210
217
  if (!shouldCancel) {
211
- const resolvedBeacon =
212
- this.dataController && this.expressionEvaluator
213
- ? resolveDataRefs(beacon, {
214
- model: this.dataController,
215
- evaluate: this.expressionEvaluator.evaluate,
216
- })
217
- : beacon;
218
-
219
- this.logger?.debug('Sending beacon event', resolvedBeacon);
220
- this.hooks.publishBeacon.call(resolvedBeacon);
218
+ this.logger?.debug('Sending beacon event', beacon);
219
+ this.hooks.publishBeacon.call(beacon);
221
220
  }
222
221
  }, 0);
223
222
  }