@player-ui/player 1.0.1 → 1.1.0--canary.866.38489

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/package.json CHANGED
@@ -6,12 +6,12 @@
6
6
  "types"
7
7
  ],
8
8
  "name": "@player-ui/player",
9
- "version": "1.0.1",
9
+ "version": "1.1.0--canary.866.38489",
10
10
  "main": "dist/cjs/index.cjs",
11
11
  "dependencies": {
12
- "@player-ui/partial-match-registry": "1.0.1",
13
- "@player-ui/make-flow": "1.0.1",
14
- "@player-ui/types": "1.0.1",
12
+ "@player-ui/partial-match-registry": "1.1.0--canary.866.38489",
13
+ "@player-ui/make-flow": "1.1.0--canary.866.38489",
14
+ "@player-ui/types": "1.1.0--canary.866.38489",
15
15
  "@types/dlv": "^1.1.4",
16
16
  "dequal": "^2.0.2",
17
17
  "dlv": "^1.1.3",
@@ -0,0 +1,124 @@
1
+ import { describe, expect, test, vitest } from "vitest";
2
+ import { Player } from "..";
3
+ import type { Flow, PlayerPlugin, InProgressState } from "..";
4
+
5
+ /** Minimal Flow used to short-circuit setupFlow. */
6
+ const flowFor = (id: string): Flow => ({
7
+ id,
8
+ views: [{ id: "v1", type: "text", value: "hi" }],
9
+ data: {},
10
+ navigation: {
11
+ BEGIN: "F",
12
+ F: {
13
+ startState: "VIEW_1",
14
+ VIEW_1: {
15
+ state_type: "VIEW",
16
+ ref: "v1",
17
+ transitions: { "*": "END" },
18
+ },
19
+ END: { state_type: "END", outcome: "done" },
20
+ },
21
+ },
22
+ });
23
+
24
+ describe("transformContent hook", () => {
25
+ test("default format 'player' passes the payload through unchanged", async () => {
26
+ const player = new Player();
27
+ const flow = flowFor("plain");
28
+ player.start(flow);
29
+ const state = player.getState() as InProgressState;
30
+ expect(state.flow).toBe(flow);
31
+ });
32
+
33
+ test("a plugin tapping for a custom format transforms the payload", async () => {
34
+ const greetPlugin: PlayerPlugin = {
35
+ name: "greet",
36
+ apply(p) {
37
+ p.hooks.transformContent.tap("greet", (content, meta) => {
38
+ if (meta.format !== "greet") return content;
39
+ const { message } = content as { message: string };
40
+ return { ...flowFor("greet"), data: { message } };
41
+ });
42
+ },
43
+ };
44
+
45
+ const player = new Player({ plugins: [greetPlugin] });
46
+ player.start({ message: "Hello world" }, { format: "greet" });
47
+
48
+ const state = player.getState() as InProgressState;
49
+ await vitest.waitFor(() =>
50
+ expect(state.controllers.view.currentView?.lastUpdate).toBeDefined(),
51
+ );
52
+ expect(state.controllers.data.get("message")).toBe("Hello world");
53
+ });
54
+
55
+ test("fires before resolveFlowContent", async () => {
56
+ const order: string[] = [];
57
+
58
+ const orderPlugin: PlayerPlugin = {
59
+ name: "order",
60
+ apply(p) {
61
+ p.hooks.transformContent.tap("order", (c) => {
62
+ order.push("transformContent");
63
+ return c;
64
+ });
65
+ p.hooks.resolveFlowContent.tap("order", (c) => {
66
+ order.push("resolveFlowContent");
67
+ return c;
68
+ });
69
+ },
70
+ };
71
+
72
+ const player = new Player({ plugins: [orderPlugin] });
73
+ player.start(flowFor("order"));
74
+
75
+ expect(order).toEqual(["transformContent", "resolveFlowContent"]);
76
+ });
77
+
78
+ test("multiple format plugins coexist — only matching one transforms", async () => {
79
+ const aPlugin: PlayerPlugin = {
80
+ name: "a",
81
+ apply(p) {
82
+ p.hooks.transformContent.tap("a", (c, meta) =>
83
+ meta.format === "a" ? flowFor("from-a") : c,
84
+ );
85
+ },
86
+ };
87
+ const bPlugin: PlayerPlugin = {
88
+ name: "b",
89
+ apply(p) {
90
+ p.hooks.transformContent.tap("b", (c, meta) =>
91
+ meta.format === "b" ? flowFor("from-b") : c,
92
+ );
93
+ },
94
+ };
95
+
96
+ const player = new Player({ plugins: [aPlugin, bPlugin] });
97
+ player.start({ raw: true }, { format: "b" });
98
+
99
+ const state = player.getState() as InProgressState;
100
+ expect(state.flow.id).toBe("from-b");
101
+ });
102
+
103
+ test("version is plumbed from StartOptions to the hook's meta arg", () => {
104
+ const versions: Array<string | undefined> = [];
105
+ const probe: PlayerPlugin = {
106
+ name: "probe",
107
+ apply(p) {
108
+ p.hooks.transformContent.tap("probe", (c, meta) => {
109
+ versions.push(meta.version);
110
+ // For format !== "demo", pass through so default path runs.
111
+ if (meta.format !== "demo") return c;
112
+ return flowFor("demo");
113
+ });
114
+ },
115
+ };
116
+
117
+ const player = new Player({ plugins: [probe] });
118
+ player.start({ x: 1 }, { format: "demo", version: "2" });
119
+ player.start({ x: 1 }, { format: "demo", version: "1" });
120
+ player.start(flowFor("noversion"));
121
+
122
+ expect(versions).toEqual(["2", "1", undefined]);
123
+ });
124
+ });
@@ -12,7 +12,7 @@ export class ReadOnlyDataController
12
12
  this.controller = controller;
13
13
  }
14
14
 
15
- get(binding: BindingLike, options?: DataModelOptions | undefined) {
15
+ get(binding: BindingLike, options?: DataModelOptions | undefined): any {
16
16
  return this.controller.get(binding, options);
17
17
  }
18
18
  }
package/src/player.ts CHANGED
@@ -29,6 +29,8 @@ import type {
29
29
  CompletedState,
30
30
  ErrorState,
31
31
  PlayerHooks,
32
+ ContentMeta,
33
+ StartOptions,
32
34
  } from "./types";
33
35
  import { NOT_STARTED_STATE } from "./types";
34
36
 
@@ -125,6 +127,7 @@ export class Player {
125
127
  onStart: new SyncHook<[Flow]>(),
126
128
  onEnd: new SyncHook<[]>(),
127
129
  resolveFlowContent: new SyncWaterfallHook<[Flow]>(),
130
+ transformContent: new SyncWaterfallHook<[unknown, ContentMeta]>(),
128
131
  };
129
132
 
130
133
  constructor(config?: PlayerConfigOptions) {
@@ -513,8 +516,16 @@ export class Player {
513
516
  };
514
517
  }
515
518
 
516
- public async start(payload: Flow): Promise<CompletedState> {
517
- const ref = Symbol(payload?.id ?? "payload");
519
+ public async start(
520
+ payload: unknown,
521
+ options?: StartOptions,
522
+ ): Promise<CompletedState> {
523
+ const meta: ContentMeta = {
524
+ format: options?.format ?? "player",
525
+ version: options?.version,
526
+ };
527
+ const flow = this.hooks.transformContent.call(payload, meta) as Flow;
528
+ const ref = Symbol(flow?.id ?? "payload");
518
529
 
519
530
  /** A check to avoid updating the state for a flow that's not the current one */
520
531
  const maybeUpdateState = <T extends PlayerFlowState>(newState: T) => {
@@ -537,7 +548,7 @@ export class Player {
537
548
  });
538
549
 
539
550
  try {
540
- const { state, start } = this.setupFlow(payload);
551
+ const { state, start } = this.setupFlow(flow);
541
552
  this.setState({
542
553
  ref,
543
554
  ...state,
@@ -564,7 +575,7 @@ export class Player {
564
575
  const errorState: ErrorState = {
565
576
  status: "error",
566
577
  ref,
567
- flow: payload,
578
+ flow,
568
579
  error,
569
580
  };
570
581
 
package/src/types.ts CHANGED
@@ -14,6 +14,36 @@ import type { ReadOnlyDataController } from "./controllers/data/utils";
14
14
  import { SyncHook, SyncWaterfallHook } from "tapable-ts";
15
15
  import { ViewInstance } from "./view";
16
16
 
17
+ /**
18
+ * Metadata describing the incoming content to `Player.start()`. Passed
19
+ * alongside the raw payload through the `transformContent` hook so plugins
20
+ * can decide whether to claim/convert it.
21
+ */
22
+ export interface ContentMeta {
23
+ /**
24
+ * Content format identifier. `"player"` (default) means the input is
25
+ * already a `Flow` and needs no conversion. Anything else is a free-form
26
+ * string a plugin can recognize.
27
+ */
28
+ format: string;
29
+ /**
30
+ * Optional format version (e.g., `"0.9"`). Free-form — plugins decide the
31
+ * convention. Lets a single format plugin dispatch across major/minor
32
+ * versions without the caller picking a different `format` string.
33
+ */
34
+ version?: string;
35
+ }
36
+
37
+ /**
38
+ * Options passed to `Player.start()` alongside the payload.
39
+ */
40
+ export interface StartOptions {
41
+ /** Identifier of the input content's format. Default: `"player"`. */
42
+ format?: string;
43
+ /** Optional content-format version. See `ContentMeta.version`. */
44
+ version?: string;
45
+ }
46
+
17
47
  /**
18
48
  * Public Player Hooks
19
49
  */
@@ -47,6 +77,17 @@ export interface PlayerHooks {
47
77
  [Flow<Asset<string>>],
48
78
  Record<string, any>
49
79
  >;
80
+ /**
81
+ * Transform raw input content into a Player `Flow` before any state is set
82
+ * up. Fires at the top of `Player.start()` — after plugins are applied,
83
+ * before `resolveFlowContent`. Plugins tap this and inspect `meta.format`
84
+ * (and optionally `meta.version`) to decide whether to convert. Plugins
85
+ * that don't handle the format pass the content through unchanged.
86
+ */
87
+ transformContent: SyncWaterfallHook<
88
+ [unknown, ContentMeta],
89
+ Record<string, any>
90
+ >;
50
91
  }
51
92
 
52
93
  /** The status for a flow's execution state */
package/types/player.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- import type { Flow } from "@player-ui/types";
2
1
  import type { Logger } from "./logger";
3
2
  import { TapableLogger } from "./logger";
4
3
  import { ConstantsController } from "./controllers";
5
- import type { PlayerFlowState, CompletedState, PlayerHooks } from "./types";
4
+ import type { PlayerFlowState, CompletedState, PlayerHooks, StartOptions } from "./types";
6
5
  /**
7
6
  Variables injected at build time
8
7
  */
@@ -75,7 +74,7 @@ export declare class Player {
75
74
  private setState;
76
75
  /** Start Player with the given flow */
77
76
  private setupFlow;
78
- start(payload: Flow): Promise<CompletedState>;
77
+ start(payload: unknown, options?: StartOptions): Promise<CompletedState>;
79
78
  }
80
79
  export {};
81
80
  //# sourceMappingURL=player.d.ts.map
package/types/types.d.ts CHANGED
@@ -7,6 +7,34 @@ import type { ViewController, DataController, ValidationController, FlowControll
7
7
  import type { ReadOnlyDataController } from "./controllers/data/utils";
8
8
  import { SyncHook, SyncWaterfallHook } from "tapable-ts";
9
9
  import { ViewInstance } from "./view";
10
+ /**
11
+ * Metadata describing the incoming content to `Player.start()`. Passed
12
+ * alongside the raw payload through the `transformContent` hook so plugins
13
+ * can decide whether to claim/convert it.
14
+ */
15
+ export interface ContentMeta {
16
+ /**
17
+ * Content format identifier. `"player"` (default) means the input is
18
+ * already a `Flow` and needs no conversion. Anything else is a free-form
19
+ * string a plugin can recognize.
20
+ */
21
+ format: string;
22
+ /**
23
+ * Optional format version (e.g., `"0.9"`). Free-form — plugins decide the
24
+ * convention. Lets a single format plugin dispatch across major/minor
25
+ * versions without the caller picking a different `format` string.
26
+ */
27
+ version?: string;
28
+ }
29
+ /**
30
+ * Options passed to `Player.start()` alongside the payload.
31
+ */
32
+ export interface StartOptions {
33
+ /** Identifier of the input content's format. Default: `"player"`. */
34
+ format?: string;
35
+ /** Optional content-format version. See `ContentMeta.version`. */
36
+ version?: string;
37
+ }
10
38
  /**
11
39
  * Public Player Hooks
12
40
  */
@@ -39,6 +67,17 @@ export interface PlayerHooks {
39
67
  resolveFlowContent: SyncWaterfallHook<[
40
68
  Flow<Asset<string>>
41
69
  ], Record<string, any>>;
70
+ /**
71
+ * Transform raw input content into a Player `Flow` before any state is set
72
+ * up. Fires at the top of `Player.start()` — after plugins are applied,
73
+ * before `resolveFlowContent`. Plugins tap this and inspect `meta.format`
74
+ * (and optionally `meta.version`) to decide whether to convert. Plugins
75
+ * that don't handle the format pass the content through unchanged.
76
+ */
77
+ transformContent: SyncWaterfallHook<[
78
+ unknown,
79
+ ContentMeta
80
+ ], Record<string, any>>;
42
81
  }
43
82
  /** The status for a flow's execution state */
44
83
  export type PlayerFlowStatus = "not-started" | "in-progress" | "completed" | "error";