@player-ui/beacon-plugin 0.3.1-next.1 → 0.3.1

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/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
  }