@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
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type React from "react";
|
|
2
|
+
import type { CompletedState, Flow, FlowResult } from "@player-ui/player";
|
|
3
|
+
import type { ReactPlayer, ReactPlayerOptions } from "../player";
|
|
4
|
+
export interface FinalState {
|
|
5
|
+
/** Mark the iteration as complete */
|
|
6
|
+
done: true;
|
|
7
|
+
}
|
|
8
|
+
export interface NextState<T> {
|
|
9
|
+
/** Optional mark the iteration as _not_ completed */
|
|
10
|
+
done?: false;
|
|
11
|
+
/** The next value in the iteration */
|
|
12
|
+
value: T;
|
|
13
|
+
}
|
|
14
|
+
export interface FlowManager {
|
|
15
|
+
/**
|
|
16
|
+
* An iterator implementation that takes the result of the previous flow and returns a new one or completion marker.
|
|
17
|
+
*
|
|
18
|
+
* If `done: true` is passed, the multi-flow experience is completed.
|
|
19
|
+
*
|
|
20
|
+
* @param previousValue - The result of the previous flow.
|
|
21
|
+
*/
|
|
22
|
+
next: (previousValue?: CompletedState) => Promise<FinalState | NextState<Flow>>;
|
|
23
|
+
/**
|
|
24
|
+
* Called when the flow is ended early (the react tree is torn down)
|
|
25
|
+
* Allows clients the opportunity to save-data before destroying the tree
|
|
26
|
+
*/
|
|
27
|
+
terminate?: (data?: FlowResult["data"]) => void;
|
|
28
|
+
}
|
|
29
|
+
export interface FallbackProps {
|
|
30
|
+
/** A callback to reset the flow iteration from the start */
|
|
31
|
+
reset?: () => void;
|
|
32
|
+
/** A callback to retry the last failed segment of the iteration */
|
|
33
|
+
retry?: () => void;
|
|
34
|
+
/** The error that caused the failure */
|
|
35
|
+
error?: Error;
|
|
36
|
+
}
|
|
37
|
+
export interface ManagedPlayerProps extends ReactPlayerOptions {
|
|
38
|
+
/** The manager for populating the next flows */
|
|
39
|
+
manager: FlowManager;
|
|
40
|
+
/** A callback when a flow is started */
|
|
41
|
+
onStartedFlow?: () => void;
|
|
42
|
+
/** A callback when the entire async iteration is completed */
|
|
43
|
+
onComplete?: (finalState?: CompletedState) => void;
|
|
44
|
+
/** A callback for any errors */
|
|
45
|
+
onError?: (e: Error) => void;
|
|
46
|
+
/** A component to render when there are errors */
|
|
47
|
+
fallbackComponent?: React.ComponentType<FallbackProps>;
|
|
48
|
+
}
|
|
49
|
+
export type ManagedPlayerContext = {
|
|
50
|
+
/** The flow manager */
|
|
51
|
+
manager: FlowManager;
|
|
52
|
+
/** The web-player */
|
|
53
|
+
reactPlayer: ReactPlayer;
|
|
54
|
+
/** The config for Player */
|
|
55
|
+
playerConfig: ReactPlayerOptions;
|
|
56
|
+
};
|
|
57
|
+
export type ManagedPlayerState = {
|
|
58
|
+
/** The managed player hasn't started yet */
|
|
59
|
+
value: "not_started";
|
|
60
|
+
/** The context for the managed state */
|
|
61
|
+
context: ManagedPlayerContext;
|
|
62
|
+
} | {
|
|
63
|
+
/** The managed-player is awaiting a response from the flow manager */
|
|
64
|
+
value: "pending";
|
|
65
|
+
/** The context for the managed state */
|
|
66
|
+
context: ManagedPlayerContext & {
|
|
67
|
+
/** The previous completed state */
|
|
68
|
+
prevResult?: CompletedState;
|
|
69
|
+
/** A promise from the flow manager for the next state */
|
|
70
|
+
next: Promise<FinalState | NextState<Flow>>;
|
|
71
|
+
};
|
|
72
|
+
} | {
|
|
73
|
+
/** A flow was retrieved from the flow manager, but hasn't been processed yet */
|
|
74
|
+
value: "loaded";
|
|
75
|
+
/** The context for the managed state */
|
|
76
|
+
context: ManagedPlayerContext & {
|
|
77
|
+
/** The next flow to load */
|
|
78
|
+
flow: Flow;
|
|
79
|
+
/** The previous completed state */
|
|
80
|
+
prevResult?: CompletedState;
|
|
81
|
+
};
|
|
82
|
+
} | {
|
|
83
|
+
/** Player is currently executing a flow */
|
|
84
|
+
value: "running";
|
|
85
|
+
/** The context for the managed state */
|
|
86
|
+
context: ManagedPlayerContext & {
|
|
87
|
+
/** The currently running flow */
|
|
88
|
+
flow: Flow;
|
|
89
|
+
/** A promise for the completed result */
|
|
90
|
+
result: Promise<CompletedState>;
|
|
91
|
+
/** The previous completed result */
|
|
92
|
+
prevResult?: CompletedState;
|
|
93
|
+
};
|
|
94
|
+
} | {
|
|
95
|
+
/** Player has completed a flow */
|
|
96
|
+
value: "completed";
|
|
97
|
+
/** The context for the managed state */
|
|
98
|
+
context: ManagedPlayerContext & {
|
|
99
|
+
/** The result of the completed flow */
|
|
100
|
+
result?: CompletedState;
|
|
101
|
+
};
|
|
102
|
+
} | {
|
|
103
|
+
/** The entire flow iteration has completed */
|
|
104
|
+
value: "ended";
|
|
105
|
+
/** The context for the managed state */
|
|
106
|
+
context: ManagedPlayerContext & {
|
|
107
|
+
/** The result of the last completed flow */
|
|
108
|
+
result?: CompletedState;
|
|
109
|
+
};
|
|
110
|
+
} | {
|
|
111
|
+
/** One of the steps in the flow has resulted in an error */
|
|
112
|
+
value: "error";
|
|
113
|
+
/** The context for the managed state */
|
|
114
|
+
context: ManagedPlayerContext & {
|
|
115
|
+
/** The error that caused the flow to halt */
|
|
116
|
+
error: Error;
|
|
117
|
+
/** the result of the last completed flow */
|
|
118
|
+
prevResult?: CompletedState;
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
export interface ManagerMiddleware {
|
|
122
|
+
/** Middleware for a response from the managed-player */
|
|
123
|
+
next?: <Type>(nextPromise: Promise<Type>) => Promise<Type>;
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { SyncWaterfallHook, AsyncParallelHook } from "tapable-ts";
|
|
3
|
+
import type { CompletedState, PlayerPlugin, Flow } from "@player-ui/player";
|
|
4
|
+
import { Player } from "@player-ui/player";
|
|
5
|
+
import type { AssetRegistryType } from "./asset";
|
|
6
|
+
import type { ReactPlayerProps } from "./app";
|
|
7
|
+
export interface DevtoolsGlobals {
|
|
8
|
+
/** A global for a plugin to load to Player for devtools */
|
|
9
|
+
__PLAYER_DEVTOOLS_PLUGIN?: {
|
|
10
|
+
new (): ReactPlayerPlugin;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export type DevtoolsWindow = typeof window & DevtoolsGlobals;
|
|
14
|
+
export interface ReactPlayerInfo {
|
|
15
|
+
/** Version of the running player */
|
|
16
|
+
playerVersion: string;
|
|
17
|
+
/** Version of the running reactPlayer */
|
|
18
|
+
reactPlayerVersion: string;
|
|
19
|
+
/** Hash of the HEAD commit used to build the current player version */
|
|
20
|
+
playerCommit: string;
|
|
21
|
+
/** Hash of the HEAD commit used to build the current reactPlayer version */
|
|
22
|
+
reactPlayerCommit: string;
|
|
23
|
+
}
|
|
24
|
+
export interface ReactPlayerPlugin extends Partial<PlayerPlugin> {
|
|
25
|
+
/** The name of this plugin */
|
|
26
|
+
name: string;
|
|
27
|
+
/**
|
|
28
|
+
* Attach listeners to the web-player instance
|
|
29
|
+
*/
|
|
30
|
+
applyReact?: (reactPlayer: ReactPlayer) => void;
|
|
31
|
+
}
|
|
32
|
+
export interface ReactPlayerOptions {
|
|
33
|
+
/** A headless player instance to use */
|
|
34
|
+
player?: Player;
|
|
35
|
+
/** A set of plugins to apply to this player */
|
|
36
|
+
plugins?: Array<ReactPlayerPlugin>;
|
|
37
|
+
}
|
|
38
|
+
export type ReactPlayerComponentProps = Record<string, unknown>;
|
|
39
|
+
/** A Player that renders UI through React */
|
|
40
|
+
export declare class ReactPlayer {
|
|
41
|
+
readonly options: ReactPlayerOptions;
|
|
42
|
+
readonly player: Player;
|
|
43
|
+
readonly assetRegistry: AssetRegistryType;
|
|
44
|
+
readonly Component: React.ComponentType<ReactPlayerComponentProps>;
|
|
45
|
+
readonly hooks: {
|
|
46
|
+
/**
|
|
47
|
+
* A hook to create a React Component to be used for Player, regardless of the current flow state
|
|
48
|
+
*/
|
|
49
|
+
webComponent: SyncWaterfallHook<[React.ComponentType<{}>], Record<string, any>>;
|
|
50
|
+
/**
|
|
51
|
+
* A hook to create a React Component that's used to render a specific view.
|
|
52
|
+
* It will be called for each view update from the core player.
|
|
53
|
+
* Typically this will just be `Asset`
|
|
54
|
+
*/
|
|
55
|
+
playerComponent: SyncWaterfallHook<[React.ComponentType<ReactPlayerProps>], Record<string, any>>;
|
|
56
|
+
/**
|
|
57
|
+
* A hook to execute async tasks before the view resets to undefined
|
|
58
|
+
*/
|
|
59
|
+
onBeforeViewReset: AsyncParallelHook<[], Record<string, any>>;
|
|
60
|
+
};
|
|
61
|
+
private viewUpdateSubscription;
|
|
62
|
+
private reactPlayerInfo;
|
|
63
|
+
constructor(options?: ReactPlayerOptions);
|
|
64
|
+
/** Returns the current version of the underlying core Player */
|
|
65
|
+
getPlayerVersion(): string;
|
|
66
|
+
/** Returns the git commit used to build this core Player version */
|
|
67
|
+
getPlayerCommit(): string;
|
|
68
|
+
/** Find instance of [Plugin] that has been registered to the web player */
|
|
69
|
+
findPlugin<Plugin extends ReactPlayerPlugin>(symbol: symbol): Plugin | undefined;
|
|
70
|
+
/** Register and apply [Plugin] if one with the same symbol is not already registered. */
|
|
71
|
+
registerPlugin(plugin: ReactPlayerPlugin): void;
|
|
72
|
+
/** Returns the current version of the running React Player */
|
|
73
|
+
getReactPlayerVersion(): string;
|
|
74
|
+
/** Returns the git commit used to build the React Player version */
|
|
75
|
+
getReactPlayerCommit(): string;
|
|
76
|
+
private createReactPlayerComponent;
|
|
77
|
+
private createReactComp;
|
|
78
|
+
/**
|
|
79
|
+
* Call this method to force the ReactPlayer to wait for the next view-update before performing the next render.
|
|
80
|
+
* If the `suspense` option is set, this will suspend while an update is pending, otherwise nothing will be rendered.
|
|
81
|
+
*/
|
|
82
|
+
setWaitForNextViewUpdate(): Promise<void>;
|
|
83
|
+
start(flow: Flow): Promise<CompletedState>;
|
|
84
|
+
}
|
|
85
|
+
export declare const WebPlayer: typeof ReactPlayer;
|
|
86
|
+
//# sourceMappingURL=player.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Player, PlayerPlugin } from "@player-ui/player";
|
|
2
|
+
export type OnUpdateCallback = (update: any) => void;
|
|
3
|
+
/**
|
|
4
|
+
* A plugin that listens for view updates and publishes an event for when a view is updated
|
|
5
|
+
*/
|
|
6
|
+
export default class OnUpdatePlugin implements PlayerPlugin {
|
|
7
|
+
name: string;
|
|
8
|
+
private readonly onUpdateCallback;
|
|
9
|
+
constructor(onUpdate: OnUpdateCallback);
|
|
10
|
+
apply(player: Player): void;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=onupdate-plugin.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PlayerFlowState, Player } from "@player-ui/player";
|
|
2
|
+
import type { ReactPlayerPlugin } from "../player";
|
|
3
|
+
/**
|
|
4
|
+
* A plugin to tap into state transition changes and call an arbitrary update function
|
|
5
|
+
*/
|
|
6
|
+
export declare class StateTapPlugin implements ReactPlayerPlugin {
|
|
7
|
+
name: string;
|
|
8
|
+
private callbackFunction;
|
|
9
|
+
constructor(callback: (state: PlayerFlowState) => void);
|
|
10
|
+
apply(player: Player): void;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=tapstate-plugin.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trim leading and trailing slashes from string
|
|
3
|
+
*/
|
|
4
|
+
export declare function trimSlashes(str: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Removes any key: value pairs from an object when the value is null or undefined
|
|
7
|
+
*/
|
|
8
|
+
export declare function removeEmptyValuesFromObject(obj: Record<string, any>): Record<string, NonNullable<any>>;
|
|
9
|
+
/** Check if the object has no keys */
|
|
10
|
+
export declare function isEmptyObject(obj: Record<string, unknown>): boolean;
|
|
11
|
+
/** Check if the argument is a function */
|
|
12
|
+
export declare function isFunction<ReturnType>(maybeFn: ReturnType | ((...args: unknown[]) => ReturnType)): maybeFn is (...args: unknown[]) => ReturnType;
|
|
13
|
+
/**
|
|
14
|
+
* Calls function with provided data or returns original value
|
|
15
|
+
*/
|
|
16
|
+
export declare function callOrReturn<ReturnType, FnArgs extends Array<unknown> = unknown[], FnType = (...args: FnArgs) => ReturnType>(maybeFn: FnType | ReturnType, fnArgs: FnArgs): ReturnType;
|
|
17
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { Player, NavigationFlowViewState } from "@player-ui/player";
|
|
3
|
+
export interface PlayerContextType {
|
|
4
|
+
/**
|
|
5
|
+
* An instance of a headless player
|
|
6
|
+
*/
|
|
7
|
+
player?: Player;
|
|
8
|
+
/** The currently rendered view state */
|
|
9
|
+
viewState?: NavigationFlowViewState;
|
|
10
|
+
}
|
|
11
|
+
export declare const PlayerContext: React.Context<PlayerContextType>;
|
|
12
|
+
/**
|
|
13
|
+
* A hook to get the current player
|
|
14
|
+
*/
|
|
15
|
+
export declare const usePlayer: () => Player | undefined;
|
|
16
|
+
//# sourceMappingURL=player-context.d.ts.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Hook to get a constant under a specific namespace */
|
|
2
|
+
export declare function useGetConstantByType(type: string, key: string): unknown;
|
|
3
|
+
/** Get a constant under the default namespace */
|
|
4
|
+
export declare function useGetConstant(key: string): unknown;
|
|
5
|
+
//# sourceMappingURL=shared-constants.d.ts.map
|