@player-devtools/basic-plugin 1.0.1-next.0 → 1.1.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/src/plugin.ts CHANGED
@@ -8,17 +8,21 @@ import {
8
8
  generateUUID,
9
9
  } from "@player-devtools/plugin";
10
10
  import type { DevtoolsPluginInteractionEvent } from "@player-devtools/types";
11
- import { dsetAssign } from "@player-devtools/utils";
11
+ import {
12
+ StateContextPlugin,
13
+ dataContextKey,
14
+ getContextPlugin,
15
+ type ContextPlugin,
16
+ type SubscriptionToken,
17
+ } from "@player-ui/context-plugin";
12
18
  import type {
13
- DataController,
14
19
  ExpressionEvaluator,
15
20
  Flow,
16
21
  Player,
17
- ViewInstance,
18
22
  Logger,
19
23
  } from "@player-ui/player";
20
- import { produce } from "immer";
21
24
 
25
+ import { playerConfigContextKey, type PlayerConfig } from "./context";
22
26
  import { Evaluation } from "./types";
23
27
 
24
28
  /** Taps into the Player and ReactPlayer hooks and leverage the WrapperComponent to define and process the content. */
@@ -34,7 +38,7 @@ export class BasicDevtoolsPlugin extends DevtoolsPlugin {
34
38
 
35
39
  data: Record<string, unknown> = {};
36
40
 
37
- playerConfig: Record<string, unknown> = {};
41
+ playerConfig: Partial<PlayerConfig> = {};
38
42
 
39
43
  logs: {
40
44
  severity: string;
@@ -47,9 +51,11 @@ export class BasicDevtoolsPlugin extends DevtoolsPlugin {
47
51
 
48
52
  expressionEvaluator?: WeakRef<ExpressionEvaluator>;
49
53
 
50
- view?: WeakRef<ViewInstance>;
51
-
52
- dataController?: WeakRef<DataController>;
54
+ /** Subscriptions are tied to the context plugin they were made on, which differs per Player */
55
+ private subscriptions: Array<{
56
+ context: ContextPlugin;
57
+ token: SubscriptionToken;
58
+ }> = [];
53
59
 
54
60
  overrideFlow?: Player["start"];
55
61
 
@@ -60,26 +66,51 @@ export class BasicDevtoolsPlugin extends DevtoolsPlugin {
60
66
 
61
67
  super.apply(player);
62
68
 
63
- // Config
64
- this.playerConfig = {
65
- version: player.getVersion(),
66
- plugins: player.getPlugins().map((plugin) => plugin.name),
67
- };
69
+ // State is read from the context plugin rather than tapping Player hooks directly
70
+ const context = getContextPlugin(player);
68
71
 
69
- this.dispatchDataUpdate({ playerConfig: this.playerConfig });
70
-
71
- // Data
72
- player.hooks.dataController.tap(this.name, (dataController) => {
73
- dataController.hooks.onUpdate.tap(this.name, (updates) => {
74
- this.data = produce(this.data, (draft) => {
75
- updates.forEach(({ binding, newValue }) => {
76
- dsetAssign(draft, ["data", ...binding.asArray()], newValue);
77
- });
78
- });
72
+ if (!player.getPlugins().some((p) => p.name === "state-context")) {
73
+ player.registerPlugin(new StateContextPlugin());
74
+ }
79
75
 
80
- this.dispatchDataUpdate({ data: this.data });
76
+ // Subscriptions persist across flows on the context plugin, so drop any
77
+ // left by an earlier apply of this plugin, on whichever Player it was.
78
+ this.subscriptions.forEach(({ context: previous, token }) =>
79
+ previous.unsubscribe(token),
80
+ );
81
+
82
+ this.subscriptions = [
83
+ {
84
+ context,
85
+ token: context.subscribe(playerConfigContextKey, (playerConfig) => {
86
+ // The context clears at flow end; keep the last known config until republished
87
+ if (!playerConfig) return;
88
+
89
+ this.playerConfig = { ...playerConfig };
90
+ this.dispatchDataUpdate({ playerConfig: this.playerConfig });
91
+ }),
92
+ },
93
+ {
94
+ context,
95
+ token: context.subscribe(dataContextKey, (model) => {
96
+ // The model is Player's live data model, so clone it rather than let immer freeze it
97
+ this.data = {
98
+ data:
99
+ model === undefined ? model : JSON.parse(JSON.stringify(model)),
100
+ };
101
+ this.dispatchDataUpdate({ data: this.data });
102
+ }),
103
+ },
104
+ ];
105
+
106
+ // The context store is reset when a flow ends, so republish on each start
107
+ const publishConfig = () =>
108
+ context.set(playerConfigContextKey, {
109
+ version: player.getVersion(),
110
+ plugins: player.getPlugins().map((plugin) => plugin.name),
81
111
  });
82
- });
112
+
113
+ publishConfig();
83
114
 
84
115
  player.logger.hooks.log.tap(this.name, (severity, message) => {
85
116
  this.logs = [...this.logs, { severity, message }];
@@ -94,10 +125,7 @@ export class BasicDevtoolsPlugin extends DevtoolsPlugin {
94
125
  this.dispatchDataUpdate({ flow: this.flow });
95
126
  });
96
127
 
97
- // View
98
- player.hooks.view.tap(this.name, (view) => {
99
- this.view = new WeakRef(view);
100
- });
128
+ player.hooks.onStart.tap(this.name, publishConfig);
101
129
 
102
130
  // Expression evaluator
103
131
  player.hooks.expressionEvaluator.tap(this.name, (evaluator) => {
@@ -0,0 +1,8 @@
1
+ /** Player version and the names of the plugins registered on it. */
2
+ export interface PlayerConfig {
3
+ version: string;
4
+ plugins: string[];
5
+ }
6
+ /** Published by the basic devtools plugin so other context consumers can read the Player config. */
7
+ export declare const playerConfigContextKey: import("@player-ui/context-plugin").ContextKey<PlayerConfig>;
8
+ //# sourceMappingURL=context.d.ts.map
package/types/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { BasicDevtoolsPlugin } from "./plugin";
2
+ export { playerConfigContextKey, type PlayerConfig } from "./context";
2
3
  //# sourceMappingURL=index.d.ts.map
package/types/plugin.d.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  import { DevtoolsPlugin, DevtoolsPluginOptions } from "@player-devtools/plugin";
2
2
  import type { DevtoolsPluginInteractionEvent } from "@player-devtools/types";
3
- import type { DataController, ExpressionEvaluator, Flow, Player, ViewInstance, Logger } from "@player-ui/player";
3
+ import type { ExpressionEvaluator, Flow, Player, Logger } from "@player-ui/player";
4
+ import { type PlayerConfig } from "./context";
4
5
  /** Taps into the Player and ReactPlayer hooks and leverage the WrapperComponent to define and process the content. */
5
6
  export declare class BasicDevtoolsPlugin extends DevtoolsPlugin {
6
7
  constructor(options: Omit<DevtoolsPluginOptions, "pluginData">);
7
8
  name: string;
8
9
  data: Record<string, unknown>;
9
- playerConfig: Record<string, unknown>;
10
+ playerConfig: Partial<PlayerConfig>;
10
11
  logs: {
11
12
  severity: string;
12
13
  message: unknown;
@@ -14,8 +15,8 @@ export declare class BasicDevtoolsPlugin extends DevtoolsPlugin {
14
15
  flow?: Flow;
15
16
  logger?: WeakRef<Logger>;
16
17
  expressionEvaluator?: WeakRef<ExpressionEvaluator>;
17
- view?: WeakRef<ViewInstance>;
18
- dataController?: WeakRef<DataController>;
18
+ /** Subscriptions are tied to the context plugin they were made on, which differs per Player */
19
+ private subscriptions;
19
20
  overrideFlow?: Player["start"];
20
21
  apply(player: Player): void;
21
22
  private evaluateExpression;
@@ -1,59 +0,0 @@
1
- import { describe } from "vitest";
2
-
3
- describe("BasicDevtoolsPlugin", () => {
4
- // TODO: Add tests for BasicDevtoolsPlugin
5
- });
6
-
7
- // describe("BasicWevDevtoolPlugins", () => {
8
- // let plugin: BasicWebDevtoolsPlugin;
9
- // let mockPlayer: Player;
10
- //
11
- // beforeEach(() => {
12
- // plugin = new BasicWebDevtoolsPlugin("test-id");
13
- // mockPlayer = {
14
- // hooks: {
15
- // dataController: {
16
- // tap: vi.fn(),
17
- // } as any,
18
- // onStart: {
19
- // tap: vi.fn(),
20
- // } as any,
21
- // view: {
22
- // tap: vi.fn(),
23
- // } as any,
24
- // expressionEvaluator: {
25
- // tap: vi.fn(),
26
- // } as any,
27
- // },
28
- // logger: {
29
- // hooks: {
30
- // log: vi.fn(),
31
- // },
32
- // },
33
- // } as unknown as Player;
34
- // });
35
- //
36
- // it("should not apply if devtools are inactive", () => {
37
- // Object.defineProperty(window, "localStorage", {
38
- // value: {
39
- // getItem: vi.fn(() => "false"),
40
- // },
41
- // writable: true,
42
- // });
43
- //
44
- // const consoleSpy = vi.spyOn(console, "log");
45
- // plugin.apply(mockPlayer);
46
- // expect(consoleSpy).toHaveBeenCalledWith(PLUGIN_INACTIVE_WARNING);
47
- // expect(plugin.playerConfig).toEqual({});
48
- // });
49
- //
50
- // it("should initialize with default Values", () => {
51
- // expect(plugin.name).toBe(PLUGIN_ID);
52
- // expect(plugin.logs).toEqual([]);
53
- // expect(plugin.data).toEqual({});
54
- // expect(plugin.flow).toBeUndefined();
55
- // expect(plugin.expressionEvaluator).toBeUndefined();
56
- // expect(plugin.view).toBeUndefined();
57
- // expect(plugin.dataController).toBeUndefined();
58
- // });
59
- // });