@player-ui/react 0.15.4--canary.881.37421 → 0.15.4--canary.885.37636

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.
@@ -10,7 +10,6 @@ import {
10
10
  import { ManagedPlayer } from "../managed-player";
11
11
  import type { FlowManager, FallbackProps } from "../types";
12
12
  import { SimpleAssetPlugin } from "../../__tests__/helpers/simple-asset-plugin";
13
- import { InProgressState } from "@player-ui/player";
14
13
 
15
14
  vitest.mock("@player-ui/metrics-plugin", async () => {
16
15
  const actual: object = await vitest.importActual("@player-ui/metrics-plugin");
@@ -453,11 +452,7 @@ describe.each([
453
452
 
454
453
  await screen.findByTestId("flow-1");
455
454
  result.unmount();
456
-
457
- const terminateArgument: InProgressState =
458
- manager.terminate?.mock.calls[0][0];
459
- expect(terminateArgument.flow).toMatchSnapshot();
460
- expect(terminateArgument.status).toBe("in-progress");
455
+ expect(manager.terminate).toBeCalledWith({ returns: { id: "123" } });
461
456
  });
462
457
 
463
458
  test("handles new manager", async () => {
@@ -562,13 +557,7 @@ describe.each([
562
557
  let newManagerBtn = await screen.findByTestId("newManager");
563
558
  await user.click(newManagerBtn);
564
559
 
565
- // terminate should receive an InProgressState
566
- expect(previousManager.current.terminate).toBeCalled();
567
- const terminateArgument1: InProgressState =
568
- previousManager.current.terminate?.mock.calls[0][0];
569
- expect(terminateArgument1.flow).toMatchSnapshot();
570
- expect(terminateArgument1.status).toBe("in-progress");
571
-
560
+ expect(previousManager.current.terminate).toBeCalledWith({});
572
561
  expect(previousManager.current.next).toBeCalledTimes(1);
573
562
  expect(manager.next).toBeCalledTimes(1);
574
563
  await screen.findByTestId("flow-1-2");
@@ -577,13 +566,7 @@ describe.each([
577
566
  await user.click(newManagerBtn);
578
567
 
579
568
  const prevMan = previousManager.current;
580
- // terminate should receive an InProgressState
581
- expect(prevMan.terminate).toBeCalled();
582
- const terminateArgument2: InProgressState =
583
- prevMan.terminate?.mock.calls[0][0];
584
- expect(terminateArgument2.flow).toMatchSnapshot();
585
- expect(terminateArgument2.status).toBe("in-progress");
586
-
569
+ expect(prevMan.terminate).toBeCalledWith({});
587
570
  expect(prevMan.next).toBeCalledTimes(1);
588
571
  expect(manager.next).toBeCalledTimes(1);
589
572
  await screen.findByTestId("flow-1-3");
@@ -252,7 +252,9 @@ export const usePersistentStateMachine = (options: {
252
252
  oldManagedState.state?.value === "running" &&
253
253
  playerState?.status === "in-progress"
254
254
  ) {
255
- previousManager.current.terminate?.(playerState);
255
+ previousManager.current.terminate?.(
256
+ playerState.controllers.data.serialize(),
257
+ );
256
258
  }
257
259
  }
258
260
 
@@ -339,7 +341,7 @@ export const ManagedPlayer = (
339
341
  } else if (state?.value === "error") {
340
342
  props.onError?.(state?.context.error);
341
343
  } else if (state?.value === "running") {
342
- props.onStartedFlow?.(state.context.flow);
344
+ props.onStartedFlow?.();
343
345
  }
344
346
  }
345
347
 
@@ -350,7 +352,7 @@ export const ManagedPlayer = (
350
352
  const playerState = state?.context.reactPlayer.player.getState();
351
353
 
352
354
  if (state?.value === "running" && playerState?.status === "in-progress") {
353
- props.manager.terminate?.(playerState);
355
+ props.manager.terminate?.(playerState.controllers.data.serialize());
354
356
  }
355
357
  };
356
358
  }, [props.manager, state?.context.reactPlayer.player, state?.value]);
@@ -1,5 +1,5 @@
1
1
  import type React from "react";
2
- import type { CompletedState, Flow, InProgressState } from "@player-ui/player";
2
+ import type { CompletedState, Flow, FlowResult } from "@player-ui/player";
3
3
  import type { ReactPlayer, ReactPlayerOptions } from "../player";
4
4
 
5
5
  export interface FinalState {
@@ -7,16 +7,14 @@ export interface FinalState {
7
7
  done: true;
8
8
  }
9
9
 
10
- export interface NextState {
10
+ export interface NextState<T> {
11
11
  /** Optional mark the iteration as _not_ completed */
12
- done: false;
12
+ done?: false;
13
13
 
14
14
  /** The next value in the iteration */
15
- value: Flow;
15
+ value: T;
16
16
  }
17
17
 
18
- /** A JS Iterator that returns a new flow or completion marker
19
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator */
20
18
  export interface FlowManager {
21
19
  /**
22
20
  * An iterator implementation that takes the result of the previous flow and returns a new one or completion marker.
@@ -25,13 +23,15 @@ export interface FlowManager {
25
23
  *
26
24
  * @param previousValue - The result of the previous flow.
27
25
  */
28
- next: (result?: CompletedState) => Promise<FinalState | NextState>;
26
+ next: (
27
+ previousValue?: CompletedState,
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?: (state?: InProgressState) => void;
34
+ terminate?: (data?: FlowResult["data"]) => void;
35
35
  }
36
36
 
37
37
  export interface FallbackProps {
@@ -50,7 +50,7 @@ export interface ManagedPlayerProps extends ReactPlayerOptions {
50
50
  manager: FlowManager;
51
51
 
52
52
  /** A callback when a flow is started */
53
- onStartedFlow?: (flow: Flow) => void;
53
+ onStartedFlow?: () => void;
54
54
 
55
55
  /** A callback when the entire async iteration is completed */
56
56
  onComplete?: (finalState?: CompletedState) => void;
@@ -91,7 +91,7 @@ export type ManagedPlayerState =
91
91
  prevResult?: CompletedState;
92
92
 
93
93
  /** A promise from the flow manager for the next state */
94
- next: Promise<FinalState | NextState>;
94
+ next: Promise<FinalState | NextState<Flow>>;
95
95
  };
96
96
  }
97
97
  | {
package/src/player.tsx CHANGED
@@ -1,10 +1,6 @@
1
1
  import React from "react";
2
2
  import { SyncWaterfallHook, AsyncParallelHook } from "tapable-ts";
3
- import {
4
- Subscribe,
5
- useSubscribedState,
6
- useSubscriber,
7
- } from "@player-ui/react-subscribe";
3
+ import { Subscribe, useSubscribedState } from "@player-ui/react-subscribe";
8
4
  import { Registry } from "@player-ui/partial-match-registry";
9
5
  import type {
10
6
  CompletedState,
@@ -14,7 +10,7 @@ import type {
14
10
  PlayerInfo,
15
11
  } from "@player-ui/player";
16
12
  import { Player } from "@player-ui/player";
17
- import { ErrorBoundary, FallbackProps } from "react-error-boundary";
13
+ import { ErrorBoundary } from "react-error-boundary";
18
14
  import type { AssetRegistryType } from "./asset";
19
15
  import { AssetContext } from "./asset";
20
16
  import { PlayerContext } from "./utils";
@@ -23,10 +19,6 @@ import type { ReactPlayerProps } from "./app";
23
19
  import { ReactPlayer as PlayerComp } from "./app";
24
20
  import { OnUpdatePlugin } from "./plugins/onupdate-plugin";
25
21
 
26
- /** Backup context for receiving ReactPlayerComponentProps when components setup in the webComponent call don't pass the props down to their inner components. */
27
- export const ReactPlayerPropsContext: React.Context<ReactPlayerComponentProps> =
28
- React.createContext<ReactPlayerComponentProps>({ isInErrorState: false });
29
-
30
22
  export interface DevtoolsGlobals {
31
23
  /** A global for a plugin to load to Player for devtools */
32
24
  __PLAYER_DEVTOOLS_PLUGIN?: {
@@ -60,11 +52,7 @@ export interface ReactPlayerOptions {
60
52
  plugins?: Array<ReactPlayerPlugin>;
61
53
  }
62
54
 
63
- export type ReactPlayerComponentProps = {
64
- /** Whether or not player is currently recovering from an error. */
65
- isInErrorState?: boolean;
66
- [key: string]: unknown;
67
- };
55
+ export type ReactPlayerComponentProps = Record<string, unknown>;
68
56
 
69
57
  /** A Player that renders UI through React */
70
58
  export class ReactPlayer {
@@ -76,10 +64,7 @@ export class ReactPlayer {
76
64
  /**
77
65
  * A hook to create a React Component to be used for Player, regardless of the current flow state
78
66
  */
79
- webComponent: SyncWaterfallHook<
80
- [React.ComponentType<any>],
81
- Record<string, any>
82
- >;
67
+ webComponent: SyncWaterfallHook<[React.ComponentType], Record<string, any>>;
83
68
  /**
84
69
  * A hook to create a React Component that's used to render a specific view.
85
70
  * It will be called for each view update from the core player.
@@ -112,8 +97,7 @@ export class ReactPlayer {
112
97
  onBeforeViewReset: new AsyncParallelHook(),
113
98
  };
114
99
 
115
- public readonly viewUpdateSubscription: Subscribe<View> =
116
- new Subscribe<View>();
100
+ public readonly viewUpdateSubscription = new Subscribe<View>();
117
101
  private reactPlayerInfo: ReactPlayerInfo;
118
102
 
119
103
  constructor(options?: ReactPlayerOptions) {
@@ -169,20 +153,11 @@ export class ReactPlayer {
169
153
  }
170
154
 
171
155
  /** Register and apply [Plugin] if one with the same symbol is not already registered. */
172
- public registerPlugin(plugin: ReactPlayerPlugin | PlayerPlugin): void {
173
- if (plugin.apply) {
174
- this.player.registerPlugin(plugin as PlayerPlugin);
175
- }
176
-
177
- if ((plugin as ReactPlayerPlugin).applyReact) {
178
- (plugin as ReactPlayerPlugin).applyReact?.(this);
179
- }
180
-
181
- if (!this.options.plugins) {
182
- this.options.plugins = [];
183
- }
156
+ public registerPlugin(plugin: ReactPlayerPlugin): void {
157
+ if (!plugin.applyReact) return;
184
158
 
185
- this.options.plugins.push(plugin);
159
+ plugin.applyReact(this);
160
+ this.options.plugins?.push(plugin);
186
161
  }
187
162
 
188
163
  /**
@@ -206,118 +181,20 @@ export class ReactPlayer {
206
181
 
207
182
  /** Wrap the Error boundary and context provider after the hook call to catch anything wrapped by the hook */
208
183
  const ReactPlayerComponent = (props: ReactPlayerComponentProps) => {
209
- const trackedErrors = React.useRef(new Map<Error, boolean>());
210
- const [errorSubId, setErrorSubId] = React.useState<number | undefined>(
211
- undefined,
212
- );
213
- const { subscribe, unsubscribe } = useSubscriber(
214
- this.viewUpdateSubscription,
215
- );
216
-
217
- const componentProps: ReactPlayerComponentProps = React.useMemo(
218
- () => ({
219
- ...props,
220
- isInErrorState: errorSubId !== undefined,
221
- }),
222
- [props, errorSubId],
223
- );
224
-
225
- /** Callback to remove all tracked errors and unsub from */
226
- const clearErrorTracking = React.useCallback(() => {
227
- trackedErrors.current.clear();
228
- setErrorSubId((prev) => {
229
- if (prev !== undefined) {
230
- unsubscribe(prev);
231
- }
232
-
233
- return undefined;
234
- });
235
- }, []);
236
-
237
- React.useEffect(() => {
238
- // Clear errors and error subscription on unmount
239
- return clearErrorTracking;
240
- }, [clearErrorTracking]);
241
-
242
- /** capture error and return true or false to represent if we are recovering from the error or not. */
243
- const captureError = React.useCallback(
244
- (err: Error) => {
245
- // If player isn't in progress we can't actually render anything so render errors are irrelevant.
246
- const playerState = this.player.getState();
247
- if (playerState.status !== "in-progress") {
248
- this.player.logger.warn(
249
- `[ReactPlayer]: An error occurred during rendering but was ignored due to a change in the player state (current state: '${playerState.status}'). Error Details:`,
250
- err,
251
- );
252
- return false;
253
- }
254
-
255
- // Only capture each error once.
256
- const currentError = trackedErrors.current.get(err);
257
- if (currentError !== undefined) {
258
- return currentError;
259
- }
260
-
261
- let isRecovering = false;
262
- setErrorSubId((prev) => {
263
- // subscribe only if no subscription available.
264
- // Needs to happen before capture error to ensure error recovery isn't missed
265
- const subId =
266
- prev === undefined
267
- ? subscribe(clearErrorTracking, {
268
- initializeWithPreviousValue: false,
269
- })
270
- : prev;
271
-
272
- // Get skipped state after trying to capture.
273
- isRecovering = playerState.controllers.error.captureError(err);
274
- trackedErrors.current.set(err, isRecovering);
275
-
276
- // If we can't recover from the error, avoid updating state to stay in error boundary
277
- if (!isRecovering) {
278
- // Unsub if not previously subbed since we don't need to reset the view
279
- if (subId !== prev) {
280
- unsubscribe(subId);
281
- }
282
- return prev;
283
- }
284
-
285
- return subId;
286
- });
287
-
288
- return isRecovering;
289
- },
290
- [errorSubId],
291
- );
292
-
293
184
  return (
294
185
  <ErrorBoundary
295
- fallbackRender={(fallbackProps: FallbackProps) => {
296
- const isRecovering = captureError(fallbackProps.error);
186
+ fallbackRender={() => null}
187
+ onError={(err) => {
188
+ const playerState = this.player.getState();
297
189
 
298
- if (!isRecovering) {
299
- // Display nothing if not recovering. Let the player state fail and handle what the view will be.
300
- return null;
190
+ if (playerState.status === "in-progress") {
191
+ playerState.fail(err);
301
192
  }
302
- fallbackProps.resetErrorBoundary();
303
-
304
- // Render the same as on success when recovering to preserve the react tree.
305
- return (
306
- <ReactPlayerPropsContext.Provider
307
- value={{ ...componentProps, isInErrorState: true }}
308
- >
309
- <PlayerContext.Provider value={{ player: this.player }}>
310
- <BaseComp {...componentProps} isInErrorState />
311
- </PlayerContext.Provider>
312
- </ReactPlayerPropsContext.Provider>
313
- );
314
193
  }}
315
194
  >
316
- <ReactPlayerPropsContext.Provider value={{ ...componentProps }}>
317
- <PlayerContext.Provider value={{ player: this.player }}>
318
- <BaseComp {...componentProps} />
319
- </PlayerContext.Provider>
320
- </ReactPlayerPropsContext.Provider>
195
+ <PlayerContext.Provider value={{ player: this.player }}>
196
+ <BaseComp {...props} />
197
+ </PlayerContext.Provider>
321
198
  </ErrorBoundary>
322
199
  );
323
200
  };
@@ -329,27 +206,17 @@ export class ReactPlayer {
329
206
  const ActualPlayerComp = this.hooks.playerComponent.call(PlayerComp);
330
207
 
331
208
  /** the component to use to render the player */
332
- const WebPlayerComponent: React.ComponentType = (): React.ReactElement => {
333
- const { isInErrorState } = React.useContext(ReactPlayerPropsContext);
209
+ const WebPlayerComponent = () => {
334
210
  const view = useSubscribedState<View>(this.viewUpdateSubscription);
335
- const lastSuccessfulView = React.useRef<View | undefined>(undefined);
336
211
  this.viewUpdateSubscription.suspend();
337
212
 
338
- React.useEffect(() => {
339
- if (!isInErrorState) {
340
- lastSuccessfulView.current = view;
341
- }
342
- }, [isInErrorState, view]);
343
-
344
- const displayedView = isInErrorState ? lastSuccessfulView.current : view;
345
-
346
213
  return (
347
214
  <AssetContext.Provider
348
215
  value={{
349
216
  registry: this.assetRegistry,
350
217
  }}
351
218
  >
352
- {displayedView && <ActualPlayerComp view={displayedView} />}
219
+ {view && <ActualPlayerComp view={view} />}
353
220
  </AssetContext.Provider>
354
221
  );
355
222
  };
@@ -1,16 +1,10 @@
1
- import { ErrorSeverity, type Asset, type PlayerErrorMetadata } from "@player-ui/player";
2
- export type AssetRenderErrorMetadata = {
3
- assetId: string;
4
- };
5
- export declare class AssetRenderError extends Error implements PlayerErrorMetadata<AssetRenderErrorMetadata> {
1
+ import type { Asset } from "@player-ui/player";
2
+ export declare class AssetRenderError extends Error {
6
3
  readonly rootAsset: Asset;
7
4
  readonly innerException?: unknown | undefined;
8
5
  private assetParentPath;
9
6
  initialMessage: string;
10
7
  innerExceptionMessage: string;
11
- readonly type: string;
12
- readonly severity: ErrorSeverity;
13
- readonly metadata: AssetRenderErrorMetadata;
14
8
  constructor(rootAsset: Asset, message?: string, innerException?: unknown | undefined);
15
9
  private updateMessage;
16
10
  getAssetPathMessage(): string;
@@ -1,18 +1,16 @@
1
1
  import type React from "react";
2
- import type { CompletedState, Flow, InProgressState } from "@player-ui/player";
2
+ import type { CompletedState, Flow, FlowResult } from "@player-ui/player";
3
3
  import type { ReactPlayer, ReactPlayerOptions } from "../player";
4
4
  export interface FinalState {
5
5
  /** Mark the iteration as complete */
6
6
  done: true;
7
7
  }
8
- export interface NextState {
8
+ export interface NextState<T> {
9
9
  /** Optional mark the iteration as _not_ completed */
10
- done: false;
10
+ done?: false;
11
11
  /** The next value in the iteration */
12
- value: Flow;
12
+ value: T;
13
13
  }
14
- /** A JS Iterator that returns a new flow or completion marker
15
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator */
16
14
  export interface FlowManager {
17
15
  /**
18
16
  * An iterator implementation that takes the result of the previous flow and returns a new one or completion marker.
@@ -21,12 +19,12 @@ export interface FlowManager {
21
19
  *
22
20
  * @param previousValue - The result of the previous flow.
23
21
  */
24
- next: (result?: CompletedState) => Promise<FinalState | NextState>;
22
+ next: (previousValue?: CompletedState) => Promise<FinalState | NextState<Flow>>;
25
23
  /**
26
24
  * Called when the flow is ended early (the react tree is torn down)
27
25
  * Allows clients the opportunity to save-data before destroying the tree
28
26
  */
29
- terminate?: (state?: InProgressState) => void;
27
+ terminate?: (data?: FlowResult["data"]) => void;
30
28
  }
31
29
  export interface FallbackProps {
32
30
  /** A callback to reset the flow iteration from the start */
@@ -40,7 +38,7 @@ export interface ManagedPlayerProps extends ReactPlayerOptions {
40
38
  /** The manager for populating the next flows */
41
39
  manager: FlowManager;
42
40
  /** A callback when a flow is started */
43
- onStartedFlow?: (flow: Flow) => void;
41
+ onStartedFlow?: () => void;
44
42
  /** A callback when the entire async iteration is completed */
45
43
  onComplete?: (finalState?: CompletedState) => void;
46
44
  /** A callback for any errors */
@@ -69,7 +67,7 @@ export type ManagedPlayerState = {
69
67
  /** The previous completed state */
70
68
  prevResult?: CompletedState;
71
69
  /** A promise from the flow manager for the next state */
72
- next: Promise<FinalState | NextState>;
70
+ next: Promise<FinalState | NextState<Flow>>;
73
71
  };
74
72
  } | {
75
73
  /** A flow was retrieved from the flow manager, but hasn't been processed yet */
package/types/player.d.ts CHANGED
@@ -1,12 +1,10 @@
1
1
  import React from "react";
2
2
  import { SyncWaterfallHook, AsyncParallelHook } from "tapable-ts";
3
3
  import { Subscribe } from "@player-ui/react-subscribe";
4
- import type { CompletedState, PlayerPlugin, Flow, View, PlayerInfo } from "@player-ui/player";
4
+ import type { CompletedState, PlayerPlugin, Flow, PlayerInfo } from "@player-ui/player";
5
5
  import { Player } from "@player-ui/player";
6
6
  import type { AssetRegistryType } from "./asset";
7
7
  import type { ReactPlayerProps } from "./app";
8
- /** Backup context for receiving ReactPlayerComponentProps when components setup in the webComponent call don't pass the props down to their inner components. */
9
- export declare const ReactPlayerPropsContext: React.Context<ReactPlayerComponentProps>;
10
8
  export interface DevtoolsGlobals {
11
9
  /** A global for a plugin to load to Player for devtools */
12
10
  __PLAYER_DEVTOOLS_PLUGIN?: {
@@ -29,11 +27,7 @@ export interface ReactPlayerOptions {
29
27
  /** A set of plugins to apply to this player */
30
28
  plugins?: Array<ReactPlayerPlugin>;
31
29
  }
32
- export type ReactPlayerComponentProps = {
33
- /** Whether or not player is currently recovering from an error. */
34
- isInErrorState?: boolean;
35
- [key: string]: unknown;
36
- };
30
+ export type ReactPlayerComponentProps = Record<string, unknown>;
37
31
  /** A Player that renders UI through React */
38
32
  export declare class ReactPlayer {
39
33
  readonly options: ReactPlayerOptions;
@@ -44,9 +38,7 @@ export declare class ReactPlayer {
44
38
  /**
45
39
  * A hook to create a React Component to be used for Player, regardless of the current flow state
46
40
  */
47
- webComponent: SyncWaterfallHook<[
48
- React.ComponentType<any>
49
- ], Record<string, any>>;
41
+ webComponent: SyncWaterfallHook<[React.ComponentType], Record<string, any>>;
50
42
  /**
51
43
  * A hook to create a React Component that's used to render a specific view.
52
44
  * It will be called for each view update from the core player.
@@ -60,7 +52,9 @@ export declare class ReactPlayer {
60
52
  */
61
53
  onBeforeViewReset: AsyncParallelHook<[], Record<string, any>>;
62
54
  };
63
- readonly viewUpdateSubscription: Subscribe<View>;
55
+ readonly viewUpdateSubscription: Subscribe<import("@player-ui/player").Asset<string> & {
56
+ validation?: Array<import("@player-ui/player").Validation.CrossfieldReference>;
57
+ }>;
64
58
  private reactPlayerInfo;
65
59
  constructor(options?: ReactPlayerOptions);
66
60
  /** Returns the current version Player */
@@ -70,7 +64,7 @@ export declare class ReactPlayer {
70
64
  /** Find instance of [Plugin] that has been registered to the web player */
71
65
  findPlugin<Plugin extends ReactPlayerPlugin>(symbol: symbol): Plugin | undefined;
72
66
  /** Register and apply [Plugin] if one with the same symbol is not already registered. */
73
- registerPlugin(plugin: ReactPlayerPlugin | PlayerPlugin): void;
67
+ registerPlugin(plugin: ReactPlayerPlugin): void;
74
68
  /**
75
69
  * Returns the current version of the running React Player
76
70
  * @deprecated use `getPlayerVersion()` instead. Will be removed next major