@player-ui/react 0.0.1-next.1

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.
@@ -0,0 +1,330 @@
1
+ import React from 'react';
2
+ import * as _player_ui_player from '@player-ui/player';
3
+ import { Asset, PlayerPlugin, Player, Flow, CompletedState, PlayerFlowState, FlowResult } from '@player-ui/player';
4
+ export * from '@player-ui/player';
5
+ import { AssetRegistryType } from '@player-ui/react-asset';
6
+ import { SyncWaterfallHook, AsyncParallelHook } from 'tapable';
7
+ import * as _player_ui_types from '@player-ui/types';
8
+
9
+ interface WebPlayerProps {
10
+ /**
11
+ * The Content view object to render
12
+ */
13
+ view: Asset;
14
+ }
15
+
16
+ interface DevtoolsGlobals {
17
+ /** A global for a plugin to load to Player for devtools */
18
+ __PLAYER_DEVTOOLS_PLUGIN?: {
19
+ new (): WebPlayerPlugin;
20
+ };
21
+ }
22
+ declare type DevtoolsWindow = typeof window & DevtoolsGlobals;
23
+ interface WebPlayerInfo {
24
+ /** Version of the running player */
25
+ playerVersion: string;
26
+ /** Version of the running webplayer */
27
+ webplayerVersion: string;
28
+ /** Hash of the HEAD commit used to build the current player version */
29
+ playerCommit: string;
30
+ /** Hash of the HEAD commit used to build the current webplayer version */
31
+ webplayerCommit: string;
32
+ }
33
+ interface WebPlayerPlugin extends Partial<PlayerPlugin> {
34
+ /** The name of this plugin */
35
+ name: string;
36
+ /**
37
+ * Attach listeners to the web-player instance
38
+ */
39
+ applyWeb?: (webPlayer: WebPlayer) => void;
40
+ }
41
+ interface WebPlayerOptions {
42
+ /** A headless player instance to use */
43
+ player?: Player;
44
+ /** A set of plugins to apply to this player */
45
+ plugins?: Array<WebPlayerPlugin>;
46
+ /**
47
+ * If the underlying webPlayer.Component should use `React.Suspense` to trigger a loading state while waiting for content or content updates.
48
+ * It requires that a `React.Suspense` component handler be somewhere in the `webPlayer.Component` hierarchy.
49
+ */
50
+ suspend?: boolean;
51
+ }
52
+ declare type WebPlayerComponentProps = Record<string, unknown>;
53
+ /** The React webplayer */
54
+ declare class WebPlayer {
55
+ readonly options: WebPlayerOptions;
56
+ readonly player: Player;
57
+ readonly assetRegistry: AssetRegistryType;
58
+ readonly Component: React.ComponentType<WebPlayerComponentProps>;
59
+ readonly hooks: {
60
+ /**
61
+ * A hook to create a React Component to be used for Player, regardless of the current flow state
62
+ */
63
+ webComponent: SyncWaterfallHook<React.ComponentType<{}>, any, any>;
64
+ /**
65
+ * A hook to create a React Component that's used to render a specific view.
66
+ * It will be called for each view update from the core player.
67
+ * Typically this will just be `Asset`
68
+ */
69
+ playerComponent: SyncWaterfallHook<React.ComponentType<WebPlayerProps>, any, any>;
70
+ /**
71
+ * A hook to execute async tasks before the view resets to undefined
72
+ */
73
+ onBeforeViewReset: AsyncParallelHook<any, any, any>;
74
+ };
75
+ private viewUpdateSubscription;
76
+ private webplayerInfo;
77
+ constructor(options?: WebPlayerOptions);
78
+ /** Returns the current version of the underlying core Player */
79
+ getPlayerVersion(): string;
80
+ /** Returns the git commit used to build this core Player version */
81
+ getPlayerCommit(): string;
82
+ /** Find instance of [Plugin] that has been registered to the web player */
83
+ findPlugin<Plugin extends WebPlayerPlugin>(symbol: symbol): Plugin | undefined;
84
+ /** Register and apply [Plugin] if one with the same symbol is not already registered. */
85
+ registerPlugin(plugin: WebPlayerPlugin): void;
86
+ /** Returns the current version of the running React Player */
87
+ getWebPlayerVersion(): string;
88
+ /** Returns the git commit used to build the React Player version */
89
+ getWebPlayerCommit(): string;
90
+ private createReactComp;
91
+ /**
92
+ * Call this method to force the WebPlayer to wait for the next view-update before performing the next render.
93
+ * If the `suspense` option is set, this will suspend while an update is pending, otherwise nothing will be rendered.
94
+ */
95
+ setWaitForNextViewUpdate(): Promise<void>;
96
+ start(flow: Flow): Promise<CompletedState>;
97
+ }
98
+
99
+ interface UseWebPlayerReturn {
100
+ /** The web-player instance */
101
+ webPlayer: WebPlayer;
102
+ /** Player instance */
103
+ player: Player;
104
+ /** The state of Player */
105
+ playerState: PlayerFlowState;
106
+ }
107
+ /**
108
+ * The `useWebPlayer` hook is an easy way to integrate the web-player into your React app.
109
+ * Simply supply your config, plugins, and an optional flow, which will be automatically started for you when changed.
110
+ */
111
+ declare const useWebPlayer: (options?: WebPlayerOptions | undefined) => UseWebPlayerReturn;
112
+
113
+ interface FinalState {
114
+ /** Mark the iteration as complete */
115
+ done: true;
116
+ }
117
+ interface NextState<T> {
118
+ /** Optional mark the iteration as _not_ completed */
119
+ done?: false;
120
+ /** The next value in the iteration */
121
+ value: T;
122
+ }
123
+ interface FlowManager {
124
+ /**
125
+ * An iterator implementation that takes the result of the previous flow and returns a new one or completion marker.
126
+ *
127
+ * If `done: true` is passed, the multi-flow experience is completed.
128
+ *
129
+ * @param previousValue - The result of the previous flow.
130
+ */
131
+ next: (previousValue?: CompletedState) => Promise<FinalState | NextState<Flow>>;
132
+ /**
133
+ * Called when the flow is ended early (the react tree is torn down)
134
+ * Allows clients the opportunity to save-data before destroying the tree
135
+ */
136
+ terminate?: (data?: FlowResult['data']) => void;
137
+ }
138
+ interface FallbackProps {
139
+ /** A callback to reset the flow iteration from the start */
140
+ reset?: () => void;
141
+ /** A callback to retry the last failed segment of the iteration */
142
+ retry?: () => void;
143
+ /** The error that caused the failure */
144
+ error?: Error;
145
+ }
146
+ interface ManagedPlayerProps extends WebPlayerOptions {
147
+ /** The manager for populating the next flows */
148
+ manager: FlowManager;
149
+ /** A callback when a flow is started */
150
+ onStartedFlow?: () => void;
151
+ /** A callback when the entire async iteration is completed */
152
+ onComplete?: (finalState?: CompletedState) => void;
153
+ /** A callback for any errors */
154
+ onError?: (e: Error) => void;
155
+ /** A component to render when there are errors */
156
+ fallbackComponent?: React.ComponentType<FallbackProps>;
157
+ }
158
+ declare type ManagedPlayerContext = {
159
+ /** The flow manager */
160
+ manager: FlowManager;
161
+ /** The web-player */
162
+ webPlayer: WebPlayer;
163
+ /** The config for Player */
164
+ playerConfig: WebPlayerOptions;
165
+ };
166
+ declare type ManagedPlayerState = {
167
+ /** The managed player hasn't started yet */
168
+ value: 'not_started';
169
+ /** The context for the managed state */
170
+ context: ManagedPlayerContext;
171
+ } | {
172
+ /** The managed-player is awaiting a response from the flow manager */
173
+ value: 'pending';
174
+ /** The context for the managed state */
175
+ context: ManagedPlayerContext & {
176
+ /** The previous completed state */
177
+ prevResult?: CompletedState;
178
+ /** A promise from the flow manager for the next state */
179
+ next: Promise<FinalState | NextState<Flow>>;
180
+ };
181
+ } | {
182
+ /** A flow was retrieved from the flow manager, but hasn't been processed yet */
183
+ value: 'loaded';
184
+ /** The context for the managed state */
185
+ context: ManagedPlayerContext & {
186
+ /** The next flow to load */
187
+ flow: Flow;
188
+ /** The previous completed state */
189
+ prevResult?: CompletedState;
190
+ };
191
+ } | {
192
+ /** Player is currently executing a flow */
193
+ value: 'running';
194
+ /** The context for the managed state */
195
+ context: ManagedPlayerContext & {
196
+ /** The currently running flow */
197
+ flow: Flow;
198
+ /** A promise for the completed result */
199
+ result: Promise<CompletedState>;
200
+ /** The previous completed result */
201
+ prevResult?: CompletedState;
202
+ };
203
+ } | {
204
+ /** Player has completed a flow */
205
+ value: 'completed';
206
+ /** The context for the managed state */
207
+ context: ManagedPlayerContext & {
208
+ /** The result of the completed flow */
209
+ result?: CompletedState;
210
+ };
211
+ } | {
212
+ /** The entire flow iteration has completed */
213
+ value: 'ended';
214
+ /** The context for the managed state */
215
+ context: ManagedPlayerContext & {
216
+ /** The result of the last completed flow */
217
+ result?: CompletedState;
218
+ };
219
+ } | {
220
+ /** One of the steps in the flow has resulted in an error */
221
+ value: 'error';
222
+ /** The context for the managed state */
223
+ context: ManagedPlayerContext & {
224
+ /** The error that caused the flow to halt */
225
+ error: Error;
226
+ /** the result of the last completed flow */
227
+ prevResult?: CompletedState;
228
+ };
229
+ };
230
+ interface ManagerMiddleware {
231
+ /** Middleware for a response from the managed-player */
232
+ next?: <Type>(nextPromise: Promise<Type>) => Promise<Type>;
233
+ }
234
+
235
+ interface StateChangeCallback {
236
+ /** Trigger for state changes */
237
+ onState: (s: ManagedPlayerState) => void;
238
+ }
239
+ /**
240
+ * An object to store the state of the managed player
241
+ */
242
+ declare class ManagedState {
243
+ state?: ManagedPlayerState;
244
+ private callbacks;
245
+ private middleware?;
246
+ constructor({ middleware, }: {
247
+ /** middleware to use in the managed player */
248
+ middleware?: ManagerMiddleware;
249
+ });
250
+ /** Add a listener to state changes */
251
+ addListener(callback: StateChangeCallback): () => void;
252
+ /** start the managed flow */
253
+ start(options: {
254
+ /** the flow manager to use */
255
+ manager: FlowManager;
256
+ /** the config to use when creating a player */
257
+ playerConfig: WebPlayerOptions;
258
+ }): this;
259
+ /** reset starts from nothing */
260
+ reset(): void;
261
+ /** restart starts from the last result */
262
+ restart(): void;
263
+ private setState;
264
+ private processState;
265
+ }
266
+ /** Creates an x-state state machine that persists when this component is no longer renders (due to Suspense) */
267
+ declare const usePersistentStateMachine: (options: {
268
+ /** the flow manager to use */
269
+ manager: FlowManager;
270
+ /** Player config */
271
+ playerConfig: WebPlayerOptions;
272
+ /** Any middleware for the manager */
273
+ middleware?: ManagerMiddleware;
274
+ }) => {
275
+ managedState: ManagedState;
276
+ state: {
277
+ value: "not_started";
278
+ context: ManagedPlayerContext;
279
+ } | {
280
+ value: "pending";
281
+ context: ManagedPlayerContext & {
282
+ prevResult?: _player_ui_player.CompletedState | undefined;
283
+ next: Promise<FinalState | NextState<_player_ui_types.Flow<_player_ui_types.Asset<string>>>>;
284
+ };
285
+ } | {
286
+ value: "loaded";
287
+ context: ManagedPlayerContext & {
288
+ flow: _player_ui_types.Flow<_player_ui_types.Asset<string>>;
289
+ prevResult?: _player_ui_player.CompletedState | undefined;
290
+ };
291
+ } | {
292
+ value: "running";
293
+ context: ManagedPlayerContext & {
294
+ flow: _player_ui_types.Flow<_player_ui_types.Asset<string>>;
295
+ result: Promise<_player_ui_player.CompletedState>;
296
+ prevResult?: _player_ui_player.CompletedState | undefined;
297
+ };
298
+ } | {
299
+ value: "completed";
300
+ context: ManagedPlayerContext & {
301
+ result?: _player_ui_player.CompletedState | undefined;
302
+ };
303
+ } | {
304
+ value: "ended";
305
+ context: ManagedPlayerContext & {
306
+ result?: _player_ui_player.CompletedState | undefined;
307
+ };
308
+ } | {
309
+ value: "error";
310
+ context: ManagedPlayerContext & {
311
+ error: Error;
312
+ prevResult?: _player_ui_player.CompletedState | undefined;
313
+ };
314
+ } | undefined;
315
+ };
316
+ /**
317
+ * A ManagedPlayer is a component responsible for orchestrating multi-flow experiences using Player.
318
+ * Provide a valid `FlowManager` to handle fetching the next flow.
319
+ *
320
+ * `suspense` must be enabled to wait for results in flight.
321
+ */
322
+ declare const ManagedPlayer: (props: ManagedPlayerProps) => JSX.Element | null;
323
+
324
+ /** hook to time a promise and add it to the metrics plugin */
325
+ declare const useRequestTime: () => {
326
+ withRequestTime: <Type>(nextPromise: Promise<Type>) => Promise<Type>;
327
+ RequestTimeMetricsPlugin: WebPlayerPlugin;
328
+ };
329
+
330
+ export { DevtoolsGlobals, DevtoolsWindow, FallbackProps, FinalState, FlowManager, ManagedPlayer, ManagedPlayerContext, ManagedPlayerProps, ManagedPlayerState, ManagerMiddleware, NextState, StateChangeCallback, UseWebPlayerReturn, WebPlayer, WebPlayerComponentProps, WebPlayerInfo, WebPlayerOptions, WebPlayerPlugin, usePersistentStateMachine, useRequestTime, useWebPlayer };