@player-ui/react 0.8.0--canary.307.9621 → 0.8.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/cjs/index.cjs +664 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/index.legacy-esm.js +611 -0
- package/dist/index.mjs +611 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +30 -64
- package/src/__tests__/__snapshots__/app.test.tsx.snap +139 -0
- package/src/__tests__/app.test.tsx +244 -0
- package/src/__tests__/helpers/simple-asset-plugin.tsx +114 -0
- package/src/__tests__/hooks.test.tsx +8 -0
- package/src/app.tsx +3 -3
- package/src/asset/__tests__/index.test.tsx +129 -0
- package/src/asset/index.tsx +10 -10
- package/src/hooks.tsx +7 -8
- package/src/index.tsx +8 -8
- package/src/manager/__tests__/managed-player.test.tsx +454 -0
- package/src/manager/managed-player.tsx +31 -36
- package/src/manager/request-time.tsx +8 -8
- package/src/manager/types.ts +12 -12
- package/src/player.tsx +24 -43
- package/src/plugins/onupdate-plugin.ts +3 -3
- package/src/plugins/tapstate-plugin.ts +4 -4
- package/src/utils/__tests__/helpers.test.ts +39 -0
- package/src/utils/__tests__/url.test.ts +14 -0
- package/src/utils/helpers.ts +15 -12
- package/src/utils/index.tsx +6 -6
- package/src/utils/player-context.ts +2 -2
- package/src/utils/shared-constants.tsx +2 -2
- package/src/utils/url.ts +2 -2
- package/src/utils/use-asset-props.tsx +2 -2
- package/src/utils/use-logger.ts +3 -3
- package/types/app.d.ts +14 -0
- package/types/asset/index.d.ts +16 -0
- package/types/hooks.d.ts +17 -0
- package/types/index.d.ts +9 -0
- package/types/manager/managed-player.d.ts +93 -0
- package/types/manager/request-time.d.ts +7 -0
- package/types/manager/types.d.ts +125 -0
- package/types/player.d.ts +86 -0
- package/types/plugins/onupdate-plugin.d.ts +12 -0
- package/types/plugins/tapstate-plugin.d.ts +12 -0
- package/types/utils/helpers.d.ts +17 -0
- package/types/utils/index.d.ts +7 -0
- package/types/utils/player-context.d.ts +16 -0
- package/types/utils/shared-constants.d.ts +5 -0
- package/types/utils/url.d.ts +5 -0
- package/types/utils/use-asset-props.d.ts +7 -0
- package/types/utils/use-logger.d.ts +6 -0
- package/dist/index.cjs.js +0 -690
- package/dist/index.d.ts +0 -397
- package/dist/index.esm.js +0 -658
package/src/manager/types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type React from
|
|
2
|
-
import type { CompletedState, Flow, FlowResult } from
|
|
3
|
-
import type { ReactPlayer, ReactPlayerOptions } from
|
|
1
|
+
import type React from "react";
|
|
2
|
+
import type { CompletedState, Flow, FlowResult } from "@player-ui/player";
|
|
3
|
+
import type { ReactPlayer, ReactPlayerOptions } from "../player";
|
|
4
4
|
|
|
5
5
|
export interface FinalState {
|
|
6
6
|
/** Mark the iteration as complete */
|
|
@@ -24,14 +24,14 @@ export interface FlowManager {
|
|
|
24
24
|
* @param previousValue - The result of the previous flow.
|
|
25
25
|
*/
|
|
26
26
|
next: (
|
|
27
|
-
previousValue?: CompletedState
|
|
27
|
+
previousValue?: CompletedState,
|
|
28
28
|
) => Promise<FinalState | NextState<Flow>>;
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Called when the flow is ended early (the react tree is torn down)
|
|
32
32
|
* Allows clients the opportunity to save-data before destroying the tree
|
|
33
33
|
*/
|
|
34
|
-
terminate?: (data?: FlowResult[
|
|
34
|
+
terminate?: (data?: FlowResult["data"]) => void;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export interface FallbackProps {
|
|
@@ -76,14 +76,14 @@ export type ManagedPlayerContext = {
|
|
|
76
76
|
export type ManagedPlayerState =
|
|
77
77
|
| {
|
|
78
78
|
/** The managed player hasn't started yet */
|
|
79
|
-
value:
|
|
79
|
+
value: "not_started";
|
|
80
80
|
|
|
81
81
|
/** The context for the managed state */
|
|
82
82
|
context: ManagedPlayerContext;
|
|
83
83
|
}
|
|
84
84
|
| {
|
|
85
85
|
/** The managed-player is awaiting a response from the flow manager */
|
|
86
|
-
value:
|
|
86
|
+
value: "pending";
|
|
87
87
|
|
|
88
88
|
/** The context for the managed state */
|
|
89
89
|
context: ManagedPlayerContext & {
|
|
@@ -96,7 +96,7 @@ export type ManagedPlayerState =
|
|
|
96
96
|
}
|
|
97
97
|
| {
|
|
98
98
|
/** A flow was retrieved from the flow manager, but hasn't been processed yet */
|
|
99
|
-
value:
|
|
99
|
+
value: "loaded";
|
|
100
100
|
|
|
101
101
|
/** The context for the managed state */
|
|
102
102
|
context: ManagedPlayerContext & {
|
|
@@ -109,7 +109,7 @@ export type ManagedPlayerState =
|
|
|
109
109
|
}
|
|
110
110
|
| {
|
|
111
111
|
/** Player is currently executing a flow */
|
|
112
|
-
value:
|
|
112
|
+
value: "running";
|
|
113
113
|
/** The context for the managed state */
|
|
114
114
|
context: ManagedPlayerContext & {
|
|
115
115
|
/** The currently running flow */
|
|
@@ -124,7 +124,7 @@ export type ManagedPlayerState =
|
|
|
124
124
|
}
|
|
125
125
|
| {
|
|
126
126
|
/** Player has completed a flow */
|
|
127
|
-
value:
|
|
127
|
+
value: "completed";
|
|
128
128
|
|
|
129
129
|
/** The context for the managed state */
|
|
130
130
|
context: ManagedPlayerContext & {
|
|
@@ -134,7 +134,7 @@ export type ManagedPlayerState =
|
|
|
134
134
|
}
|
|
135
135
|
| {
|
|
136
136
|
/** The entire flow iteration has completed */
|
|
137
|
-
value:
|
|
137
|
+
value: "ended";
|
|
138
138
|
|
|
139
139
|
/** The context for the managed state */
|
|
140
140
|
context: ManagedPlayerContext & {
|
|
@@ -144,7 +144,7 @@ export type ManagedPlayerState =
|
|
|
144
144
|
}
|
|
145
145
|
| {
|
|
146
146
|
/** One of the steps in the flow has resulted in an error */
|
|
147
|
-
value:
|
|
147
|
+
value: "error";
|
|
148
148
|
|
|
149
149
|
/** The context for the managed state */
|
|
150
150
|
context: ManagedPlayerContext & {
|
package/src/player.tsx
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
/* eslint-disable react/no-this-in-sfc */
|
|
2
|
-
import React from
|
|
3
|
-
import { SyncWaterfallHook, AsyncParallelHook } from
|
|
4
|
-
import { Subscribe, useSubscribedState } from
|
|
5
|
-
import { Registry } from
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { SyncWaterfallHook, AsyncParallelHook } from "tapable-ts";
|
|
4
|
+
import { Subscribe, useSubscribedState } from "@player-ui/react-subscribe";
|
|
5
|
+
import { Registry } from "@player-ui/partial-match-registry";
|
|
6
6
|
import type {
|
|
7
7
|
CompletedState,
|
|
8
8
|
PlayerPlugin,
|
|
9
9
|
Flow,
|
|
10
10
|
View,
|
|
11
|
-
} from
|
|
12
|
-
import { Player } from
|
|
13
|
-
import { ErrorBoundary } from
|
|
14
|
-
import type { AssetRegistryType } from
|
|
15
|
-
import { AssetContext } from
|
|
16
|
-
import { PlayerContext } from
|
|
11
|
+
} from "@player-ui/player";
|
|
12
|
+
import { Player } from "@player-ui/player";
|
|
13
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
14
|
+
import type { AssetRegistryType } from "./asset";
|
|
15
|
+
import { AssetContext } from "./asset";
|
|
16
|
+
import { PlayerContext } from "./utils";
|
|
17
17
|
|
|
18
|
-
import type { ReactPlayerProps } from
|
|
19
|
-
import PlayerComp from
|
|
20
|
-
import OnUpdatePlugin from
|
|
18
|
+
import type { ReactPlayerProps } from "./app";
|
|
19
|
+
import PlayerComp from "./app";
|
|
20
|
+
import OnUpdatePlugin from "./plugins/onupdate-plugin";
|
|
21
21
|
|
|
22
|
-
const WEB_PLAYER_VERSION =
|
|
23
|
-
const COMMIT =
|
|
22
|
+
const WEB_PLAYER_VERSION = "__VERSION__";
|
|
23
|
+
const COMMIT = "__GIT_COMMIT__";
|
|
24
24
|
|
|
25
25
|
export interface DevtoolsGlobals {
|
|
26
26
|
/** A global for a plugin to load to Player for devtools */
|
|
@@ -32,7 +32,7 @@ export interface DevtoolsGlobals {
|
|
|
32
32
|
export type DevtoolsWindow = typeof window & DevtoolsGlobals;
|
|
33
33
|
|
|
34
34
|
const _window: DevtoolsWindow | undefined =
|
|
35
|
-
typeof window ===
|
|
35
|
+
typeof window === "undefined" ? undefined : window;
|
|
36
36
|
|
|
37
37
|
export interface ReactPlayerInfo {
|
|
38
38
|
/** Version of the running player */
|
|
@@ -64,12 +64,6 @@ export interface ReactPlayerOptions {
|
|
|
64
64
|
|
|
65
65
|
/** A set of plugins to apply to this player */
|
|
66
66
|
plugins?: Array<ReactPlayerPlugin>;
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* If the underlying reactPlayer.Component should use `React.Suspense` to trigger a loading state while waiting for content or content updates.
|
|
70
|
-
* It requires that a `React.Suspense` component handler be somewhere in the `reactPlayer.Component` hierarchy.
|
|
71
|
-
*/
|
|
72
|
-
suspend?: boolean;
|
|
73
67
|
}
|
|
74
68
|
|
|
75
69
|
export type ReactPlayerComponentProps = Record<string, unknown>;
|
|
@@ -107,15 +101,9 @@ export class ReactPlayer {
|
|
|
107
101
|
constructor(options?: ReactPlayerOptions) {
|
|
108
102
|
this.options = options ?? {};
|
|
109
103
|
|
|
110
|
-
// Default the suspend option to `true` unless explicitly unset
|
|
111
|
-
// Remove the suspend option in the next major
|
|
112
|
-
if (!('suspend' in this.options)) {
|
|
113
|
-
this.options.suspend = true;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
104
|
const Devtools = _window?.__PLAYER_DEVTOOLS_PLUGIN;
|
|
117
105
|
const onUpdatePlugin = new OnUpdatePlugin(
|
|
118
|
-
this.viewUpdateSubscription.publish
|
|
106
|
+
this.viewUpdateSubscription.publish,
|
|
119
107
|
);
|
|
120
108
|
|
|
121
109
|
const plugins = options?.plugins ?? [];
|
|
@@ -125,7 +113,7 @@ export class ReactPlayer {
|
|
|
125
113
|
}
|
|
126
114
|
|
|
127
115
|
const playerPlugins = plugins.filter((p) =>
|
|
128
|
-
Boolean(p.apply)
|
|
116
|
+
Boolean(p.apply),
|
|
129
117
|
) as PlayerPlugin[];
|
|
130
118
|
|
|
131
119
|
this.player = options?.player ?? new Player({ plugins: playerPlugins });
|
|
@@ -159,7 +147,7 @@ export class ReactPlayer {
|
|
|
159
147
|
|
|
160
148
|
/** Find instance of [Plugin] that has been registered to the web player */
|
|
161
149
|
public findPlugin<Plugin extends ReactPlayerPlugin>(
|
|
162
|
-
symbol: symbol
|
|
150
|
+
symbol: symbol,
|
|
163
151
|
): Plugin | undefined {
|
|
164
152
|
return this.options.plugins?.find((el) => el.symbol === symbol) as Plugin;
|
|
165
153
|
}
|
|
@@ -193,7 +181,7 @@ export class ReactPlayer {
|
|
|
193
181
|
onError={(err) => {
|
|
194
182
|
const playerState = this.player.getState();
|
|
195
183
|
|
|
196
|
-
if (playerState.status ===
|
|
184
|
+
if (playerState.status === "in-progress") {
|
|
197
185
|
playerState.fail(err);
|
|
198
186
|
}
|
|
199
187
|
}}
|
|
@@ -214,10 +202,7 @@ export class ReactPlayer {
|
|
|
214
202
|
/** the component to use to render the player */
|
|
215
203
|
const WebPlayerComponent = () => {
|
|
216
204
|
const view = useSubscribedState<View>(this.viewUpdateSubscription);
|
|
217
|
-
|
|
218
|
-
if (this.options.suspend) {
|
|
219
|
-
this.viewUpdateSubscription.suspend();
|
|
220
|
-
}
|
|
205
|
+
this.viewUpdateSubscription.suspend();
|
|
221
206
|
|
|
222
207
|
return (
|
|
223
208
|
<AssetContext.Provider
|
|
@@ -238,12 +223,10 @@ export class ReactPlayer {
|
|
|
238
223
|
* If the `suspense` option is set, this will suspend while an update is pending, otherwise nothing will be rendered.
|
|
239
224
|
*/
|
|
240
225
|
public setWaitForNextViewUpdate() {
|
|
241
|
-
|
|
242
|
-
const shouldCallResetHook =
|
|
243
|
-
this.options.suspend && this.hooks.onBeforeViewReset.isUsed();
|
|
226
|
+
const shouldCallResetHook = this.hooks.onBeforeViewReset.isUsed();
|
|
244
227
|
|
|
245
228
|
return this.viewUpdateSubscription.reset(
|
|
246
|
-
shouldCallResetHook ? this.hooks.onBeforeViewReset.call() : undefined
|
|
229
|
+
shouldCallResetHook ? this.hooks.onBeforeViewReset.call() : undefined,
|
|
247
230
|
);
|
|
248
231
|
}
|
|
249
232
|
|
|
@@ -251,9 +234,7 @@ export class ReactPlayer {
|
|
|
251
234
|
this.setWaitForNextViewUpdate();
|
|
252
235
|
|
|
253
236
|
return this.player.start(flow).finally(async () => {
|
|
254
|
-
|
|
255
|
-
await this.setWaitForNextViewUpdate();
|
|
256
|
-
}
|
|
237
|
+
await this.setWaitForNextViewUpdate();
|
|
257
238
|
});
|
|
258
239
|
}
|
|
259
240
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Player, PlayerPlugin, ViewInstance } from
|
|
1
|
+
import type { Player, PlayerPlugin, ViewInstance } from "@player-ui/player";
|
|
2
2
|
|
|
3
3
|
export type OnUpdateCallback = (update: any) => void;
|
|
4
4
|
|
|
@@ -6,7 +6,7 @@ export type OnUpdateCallback = (update: any) => void;
|
|
|
6
6
|
* A plugin that listens for view updates and publishes an event for when a view is updated
|
|
7
7
|
*/
|
|
8
8
|
export default class OnUpdatePlugin implements PlayerPlugin {
|
|
9
|
-
name =
|
|
9
|
+
name = "view-update";
|
|
10
10
|
|
|
11
11
|
private readonly onUpdateCallback: OnUpdateCallback;
|
|
12
12
|
|
|
@@ -31,7 +31,7 @@ export default class OnUpdatePlugin implements PlayerPlugin {
|
|
|
31
31
|
// Attach listeners and publish an update event for a view already in progress
|
|
32
32
|
const currentPlayerState = player.getState();
|
|
33
33
|
|
|
34
|
-
if (currentPlayerState.status ===
|
|
34
|
+
if (currentPlayerState.status === "in-progress") {
|
|
35
35
|
const { currentView } = currentPlayerState.controllers.view;
|
|
36
36
|
|
|
37
37
|
if (currentView) {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { PlayerFlowState, Player } from
|
|
2
|
-
import type { ReactPlayerPlugin } from
|
|
1
|
+
import type { PlayerFlowState, Player } from "@player-ui/player";
|
|
2
|
+
import type { ReactPlayerPlugin } from "../player";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* A plugin to tap into state transition changes and call an arbitrary update function
|
|
6
6
|
*/
|
|
7
7
|
export class StateTapPlugin implements ReactPlayerPlugin {
|
|
8
|
-
name =
|
|
8
|
+
name = "statetap";
|
|
9
9
|
private callbackFunction: (state: PlayerFlowState) => void;
|
|
10
10
|
|
|
11
11
|
constructor(callback: (state: PlayerFlowState) => void) {
|
|
@@ -13,7 +13,7 @@ export class StateTapPlugin implements ReactPlayerPlugin {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
apply(player: Player) {
|
|
16
|
-
player.hooks.state.tap(
|
|
16
|
+
player.hooks.state.tap("usePlayer", (newPlayerState: PlayerFlowState) => {
|
|
17
17
|
this.callbackFunction(newPlayerState);
|
|
18
18
|
});
|
|
19
19
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { test, expect, vitest } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
trimSlashes,
|
|
4
|
+
removeEmptyValuesFromObject,
|
|
5
|
+
callOrReturn,
|
|
6
|
+
} from "../helpers";
|
|
7
|
+
|
|
8
|
+
test("trims all the slashes from start and end", () => {
|
|
9
|
+
expect(trimSlashes("/test/")).toBe("test");
|
|
10
|
+
expect(trimSlashes("/test")).toBe("test");
|
|
11
|
+
expect(trimSlashes("test/")).toBe("test");
|
|
12
|
+
expect(trimSlashes("//test//")).toBe("test");
|
|
13
|
+
expect(trimSlashes("/te/st/")).toBe("te/st");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("removes all nullable key:values from object", () => {
|
|
17
|
+
expect(
|
|
18
|
+
removeEmptyValuesFromObject({
|
|
19
|
+
a: true,
|
|
20
|
+
b: false,
|
|
21
|
+
c: undefined,
|
|
22
|
+
d: null,
|
|
23
|
+
e: 0,
|
|
24
|
+
}),
|
|
25
|
+
).toMatchInlineSnapshot(`
|
|
26
|
+
{
|
|
27
|
+
"a": true,
|
|
28
|
+
"b": false,
|
|
29
|
+
"e": 0,
|
|
30
|
+
}
|
|
31
|
+
`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("calls arg1 when instance of function with arg2 value", () => {
|
|
35
|
+
const fn = vitest.fn();
|
|
36
|
+
callOrReturn(fn, ["abc"]);
|
|
37
|
+
expect(fn).toHaveBeenCalledWith(["abc"]);
|
|
38
|
+
expect(callOrReturn("123", ["abc"])).toBe("123");
|
|
39
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { buildUrl } from "../url";
|
|
3
|
+
|
|
4
|
+
test("builds a URL with no query parameters added", () => {
|
|
5
|
+
expect(buildUrl("https://example.com")).toMatchInlineSnapshot(
|
|
6
|
+
`"https://example.com/"`,
|
|
7
|
+
);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("builds a URL with object of query parameters added", () => {
|
|
11
|
+
expect(
|
|
12
|
+
buildUrl("https://example.com", { a: 1, b: 2, c: "3", d: true }),
|
|
13
|
+
).toMatchInlineSnapshot(`"https://example.com/?a=1&b=2&c=3&d=true"`);
|
|
14
|
+
});
|
package/src/utils/helpers.ts
CHANGED
|
@@ -2,24 +2,27 @@
|
|
|
2
2
|
* Trim leading and trailing slashes from string
|
|
3
3
|
*/
|
|
4
4
|
export function trimSlashes(str: string) {
|
|
5
|
-
return str.replace(/^\/+|\/+$/g,
|
|
5
|
+
return str.replace(/^\/+|\/+$/g, "");
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Removes any key: value pairs from an object when the value is null or undefined
|
|
10
10
|
*/
|
|
11
11
|
export function removeEmptyValuesFromObject(
|
|
12
|
-
obj: Record<string, any
|
|
12
|
+
obj: Record<string, any>,
|
|
13
13
|
): Record<string, NonNullable<any>> {
|
|
14
|
-
return Object.keys(obj).reduce(
|
|
15
|
-
|
|
14
|
+
return Object.keys(obj).reduce(
|
|
15
|
+
(acc, key) => {
|
|
16
|
+
const value = obj[key];
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
if (value !== null && value !== undefined) {
|
|
19
|
+
acc[key] = value;
|
|
20
|
+
}
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
return acc;
|
|
23
|
+
},
|
|
24
|
+
{} as Record<string, any>,
|
|
25
|
+
);
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
/** Check if the object has no keys */
|
|
@@ -29,9 +32,9 @@ export function isEmptyObject(obj: Record<string, unknown>) {
|
|
|
29
32
|
|
|
30
33
|
/** Check if the argument is a function */
|
|
31
34
|
export function isFunction<ReturnType>(
|
|
32
|
-
maybeFn: ReturnType | ((...args: unknown[]) => ReturnType)
|
|
35
|
+
maybeFn: ReturnType | ((...args: unknown[]) => ReturnType),
|
|
33
36
|
): maybeFn is (...args: unknown[]) => ReturnType {
|
|
34
|
-
return Boolean(maybeFn instanceof Function || typeof maybeFn ===
|
|
37
|
+
return Boolean(maybeFn instanceof Function || typeof maybeFn === "function");
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
/**
|
|
@@ -40,7 +43,7 @@ export function isFunction<ReturnType>(
|
|
|
40
43
|
export function callOrReturn<
|
|
41
44
|
ReturnType,
|
|
42
45
|
FnArgs extends Array<unknown> = unknown[],
|
|
43
|
-
FnType = (...args: FnArgs) => ReturnType
|
|
46
|
+
FnType = (...args: FnArgs) => ReturnType,
|
|
44
47
|
>(maybeFn: FnType | ReturnType, fnArgs: FnArgs): ReturnType {
|
|
45
48
|
if (isFunction(maybeFn)) {
|
|
46
49
|
return maybeFn(fnArgs) as ReturnType;
|
package/src/utils/index.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
1
|
+
export * from "./player-context";
|
|
2
|
+
export * from "./use-logger";
|
|
3
|
+
export * from "./use-asset-props";
|
|
4
|
+
export * from "./helpers";
|
|
5
|
+
export * from "./url";
|
|
6
|
+
export * from "./shared-constants";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { usePlayer } from
|
|
1
|
+
import { usePlayer } from "./player-context";
|
|
2
2
|
|
|
3
3
|
/** Hook to get a constant under a specific namespace */
|
|
4
4
|
export function useGetConstantByType(type: string, key: string): unknown {
|
|
@@ -11,5 +11,5 @@ export function useGetConstantByType(type: string, key: string): unknown {
|
|
|
11
11
|
export function useGetConstant(key: string): unknown {
|
|
12
12
|
const player = usePlayer();
|
|
13
13
|
|
|
14
|
-
return player?.constantsController.getConstants(key,
|
|
14
|
+
return player?.constantsController.getConstants(key, "constants");
|
|
15
15
|
}
|
package/src/utils/url.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { isEmptyObject } from
|
|
1
|
+
import { isEmptyObject } from "./helpers";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Combines a URL with any additional parameters
|
|
5
5
|
*/
|
|
6
6
|
export function buildUrl(
|
|
7
7
|
url: string,
|
|
8
|
-
params: Record<string, unknown> = {}
|
|
8
|
+
params: Record<string, unknown> = {},
|
|
9
9
|
): string {
|
|
10
10
|
const baseUrl = new URL(url);
|
|
11
11
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { Asset } from
|
|
1
|
+
import type { Asset } from "@player-ui/player";
|
|
2
2
|
|
|
3
3
|
/** Common props for any dom node */
|
|
4
4
|
export function useAssetProps(asset: Asset) {
|
|
5
5
|
return {
|
|
6
6
|
id: asset.id,
|
|
7
|
-
|
|
7
|
+
"data-asset-type": asset.type,
|
|
8
8
|
};
|
|
9
9
|
}
|
package/src/utils/use-logger.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Logger } from
|
|
2
|
-
import { NoopLogger } from
|
|
3
|
-
import { usePlayer } from
|
|
1
|
+
import type { Logger } from "@player-ui/player";
|
|
2
|
+
import { NoopLogger } from "@player-ui/player";
|
|
3
|
+
import { usePlayer } from "./player-context";
|
|
4
4
|
|
|
5
5
|
const noopLogger = new NoopLogger();
|
|
6
6
|
|
package/types/app.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { View } from "@player-ui/player";
|
|
3
|
+
export interface ReactPlayerProps {
|
|
4
|
+
/**
|
|
5
|
+
* The Content view object to render
|
|
6
|
+
*/
|
|
7
|
+
view: View;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The entry for the ReactPlayer's React tree
|
|
11
|
+
*/
|
|
12
|
+
declare const ReactPlayer: ({ view }: ReactPlayerProps) => React.JSX.Element;
|
|
13
|
+
export default ReactPlayer;
|
|
14
|
+
//# sourceMappingURL=app.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { Asset as AssetType, AssetWrapper } from "@player-ui/player";
|
|
3
|
+
import type { Registry } from "@player-ui/partial-match-registry";
|
|
4
|
+
export type AssetRegistryType = Registry<React.ComponentType<any>>;
|
|
5
|
+
export interface ContextType {
|
|
6
|
+
/**
|
|
7
|
+
* A registry of Asset -> React Components
|
|
8
|
+
*/
|
|
9
|
+
registry?: AssetRegistryType;
|
|
10
|
+
}
|
|
11
|
+
export declare const AssetContext: React.Context<ContextType>;
|
|
12
|
+
/**
|
|
13
|
+
* A React Component that looks up an implementation from a registry
|
|
14
|
+
*/
|
|
15
|
+
export declare const ReactAsset: (props: AssetType<string> | AssetWrapper<AssetType<string>>) => React.JSX.Element;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
package/types/hooks.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Player, PlayerFlowState } from "@player-ui/player";
|
|
2
|
+
import type { ReactPlayerOptions } from "./player";
|
|
3
|
+
import { ReactPlayer } from "./player";
|
|
4
|
+
export interface UseReactPlayerReturn {
|
|
5
|
+
/** The web-player instance */
|
|
6
|
+
reactPlayer: ReactPlayer;
|
|
7
|
+
/** Player instance */
|
|
8
|
+
player: Player;
|
|
9
|
+
/** The state of Player */
|
|
10
|
+
playerState: PlayerFlowState;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The `useReactPlayer` hook is an easy way to integrate the web-player into your React app.
|
|
14
|
+
* Simply supply your config, plugins, and an optional flow, which will be automatically started for you when changed.
|
|
15
|
+
*/
|
|
16
|
+
export declare const useReactPlayer: (options?: ReactPlayerOptions) => UseReactPlayerReturn;
|
|
17
|
+
//# sourceMappingURL=hooks.d.ts.map
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "@player-ui/player";
|
|
2
|
+
export * from "./player";
|
|
3
|
+
export * from "./hooks";
|
|
4
|
+
export * from "./manager/managed-player";
|
|
5
|
+
export * from "./manager/request-time";
|
|
6
|
+
export * from "./manager/types";
|
|
7
|
+
export * from "./asset";
|
|
8
|
+
export * from "./utils";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { FlowManager, ManagedPlayerProps, ManagedPlayerState, ManagerMiddleware, ManagedPlayerContext } from "./types";
|
|
3
|
+
import type { ReactPlayerOptions } from "../player";
|
|
4
|
+
export interface StateChangeCallback {
|
|
5
|
+
/** Trigger for state changes */
|
|
6
|
+
onState: (s: ManagedPlayerState) => void;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* An object to store the state of the managed player
|
|
10
|
+
*/
|
|
11
|
+
declare class ManagedState {
|
|
12
|
+
state?: ManagedPlayerState;
|
|
13
|
+
private callbacks;
|
|
14
|
+
private middleware?;
|
|
15
|
+
constructor({ middleware, }: {
|
|
16
|
+
/** middleware to use in the managed player */
|
|
17
|
+
middleware?: ManagerMiddleware;
|
|
18
|
+
});
|
|
19
|
+
/** Add a listener to state changes */
|
|
20
|
+
addListener(callback: StateChangeCallback): () => void;
|
|
21
|
+
/** start the managed flow */
|
|
22
|
+
start(options: {
|
|
23
|
+
/** the flow manager to use */
|
|
24
|
+
manager: FlowManager;
|
|
25
|
+
/** the config to use when creating a player */
|
|
26
|
+
playerConfig: ReactPlayerOptions;
|
|
27
|
+
}): this;
|
|
28
|
+
/** reset starts from nothing */
|
|
29
|
+
reset(): void;
|
|
30
|
+
/** restart starts from the last result */
|
|
31
|
+
restart(): void;
|
|
32
|
+
private setState;
|
|
33
|
+
private processState;
|
|
34
|
+
}
|
|
35
|
+
/** Creates an x-state state machine that persists when this component is no longer renders (due to Suspense) */
|
|
36
|
+
export declare const usePersistentStateMachine: (options: {
|
|
37
|
+
/** the flow manager to use */
|
|
38
|
+
manager: FlowManager;
|
|
39
|
+
/** Player config */
|
|
40
|
+
playerConfig: ReactPlayerOptions;
|
|
41
|
+
/** Any middleware for the manager */
|
|
42
|
+
middleware?: ManagerMiddleware;
|
|
43
|
+
}) => {
|
|
44
|
+
managedState: ManagedState;
|
|
45
|
+
state: {
|
|
46
|
+
value: "not_started";
|
|
47
|
+
context: ManagedPlayerContext;
|
|
48
|
+
} | {
|
|
49
|
+
value: "pending";
|
|
50
|
+
context: ManagedPlayerContext & {
|
|
51
|
+
prevResult?: import("@player-ui/player").CompletedState | undefined;
|
|
52
|
+
next: Promise<import("./types").FinalState | import("./types").NextState<import("@player-ui/player").Flow<import("@player-ui/player").Asset<string>>>>;
|
|
53
|
+
};
|
|
54
|
+
} | {
|
|
55
|
+
value: "loaded";
|
|
56
|
+
context: ManagedPlayerContext & {
|
|
57
|
+
flow: import("@player-ui/player").Flow<import("@player-ui/player").Asset<string>>;
|
|
58
|
+
prevResult?: import("@player-ui/player").CompletedState | undefined;
|
|
59
|
+
};
|
|
60
|
+
} | {
|
|
61
|
+
value: "running";
|
|
62
|
+
context: ManagedPlayerContext & {
|
|
63
|
+
flow: import("@player-ui/player").Flow<import("@player-ui/player").Asset<string>>;
|
|
64
|
+
result: Promise<import("@player-ui/player").CompletedState>;
|
|
65
|
+
prevResult?: import("@player-ui/player").CompletedState | undefined;
|
|
66
|
+
};
|
|
67
|
+
} | {
|
|
68
|
+
value: "completed";
|
|
69
|
+
context: ManagedPlayerContext & {
|
|
70
|
+
result?: import("@player-ui/player").CompletedState | undefined;
|
|
71
|
+
};
|
|
72
|
+
} | {
|
|
73
|
+
value: "ended";
|
|
74
|
+
context: ManagedPlayerContext & {
|
|
75
|
+
result?: import("@player-ui/player").CompletedState | undefined;
|
|
76
|
+
};
|
|
77
|
+
} | {
|
|
78
|
+
value: "error";
|
|
79
|
+
context: ManagedPlayerContext & {
|
|
80
|
+
error: Error;
|
|
81
|
+
prevResult?: import("@player-ui/player").CompletedState | undefined;
|
|
82
|
+
};
|
|
83
|
+
} | undefined;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* A ManagedPlayer is a component responsible for orchestrating multi-flow experiences using Player.
|
|
87
|
+
* Provide a valid `FlowManager` to handle fetching the next flow.
|
|
88
|
+
*
|
|
89
|
+
* `suspense` must be enabled to wait for results in flight.
|
|
90
|
+
*/
|
|
91
|
+
export declare const ManagedPlayer: (props: ManagedPlayerProps) => React.JSX.Element | null;
|
|
92
|
+
export {};
|
|
93
|
+
//# sourceMappingURL=managed-player.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ReactPlayerPlugin } from "../player";
|
|
2
|
+
/** hook to time a promise and add it to the metrics plugin */
|
|
3
|
+
export declare const useRequestTime: () => {
|
|
4
|
+
withRequestTime: <Type>(nextPromise: Promise<Type>) => Promise<Type>;
|
|
5
|
+
RequestTimeMetricsPlugin: ReactPlayerPlugin;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=request-time.d.ts.map
|