@player-ui/react 0.8.0--canary.307.9645 → 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,664 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/index.tsx
|
|
32
|
+
var src_exports = {};
|
|
33
|
+
__export(src_exports, {
|
|
34
|
+
AssetContext: () => AssetContext,
|
|
35
|
+
ManagedPlayer: () => ManagedPlayer,
|
|
36
|
+
PlayerContext: () => PlayerContext,
|
|
37
|
+
ReactAsset: () => ReactAsset,
|
|
38
|
+
ReactPlayer: () => ReactPlayer2,
|
|
39
|
+
WebPlayer: () => WebPlayer,
|
|
40
|
+
buildUrl: () => buildUrl,
|
|
41
|
+
callOrReturn: () => callOrReturn,
|
|
42
|
+
isEmptyObject: () => isEmptyObject,
|
|
43
|
+
isFunction: () => isFunction,
|
|
44
|
+
removeEmptyValuesFromObject: () => removeEmptyValuesFromObject,
|
|
45
|
+
trimSlashes: () => trimSlashes,
|
|
46
|
+
useAssetProps: () => useAssetProps,
|
|
47
|
+
useGetConstant: () => useGetConstant,
|
|
48
|
+
useGetConstantByType: () => useGetConstantByType,
|
|
49
|
+
useLogger: () => useLogger,
|
|
50
|
+
usePersistentStateMachine: () => usePersistentStateMachine,
|
|
51
|
+
usePlayer: () => usePlayer,
|
|
52
|
+
useReactPlayer: () => useReactPlayer,
|
|
53
|
+
useRequestTime: () => useRequestTime
|
|
54
|
+
});
|
|
55
|
+
module.exports = __toCommonJS(src_exports);
|
|
56
|
+
__reExport(src_exports, require("@player-ui/player"), module.exports);
|
|
57
|
+
|
|
58
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/player.tsx
|
|
59
|
+
var import_react4 = __toESM(require("react"));
|
|
60
|
+
var import_tapable_ts = require("tapable-ts");
|
|
61
|
+
var import_react_subscribe = require("@player-ui/react-subscribe");
|
|
62
|
+
var import_partial_match_registry = require("@player-ui/partial-match-registry");
|
|
63
|
+
var import_player2 = require("@player-ui/player");
|
|
64
|
+
var import_react_error_boundary = require("react-error-boundary");
|
|
65
|
+
|
|
66
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/asset/index.tsx
|
|
67
|
+
var import_react = __toESM(require("react"));
|
|
68
|
+
var AssetContext = import_react.default.createContext({});
|
|
69
|
+
var ReactAsset = (props) => {
|
|
70
|
+
const { registry } = import_react.default.useContext(AssetContext);
|
|
71
|
+
let unwrapped;
|
|
72
|
+
if ("type" in props && "id" in props) {
|
|
73
|
+
unwrapped = props;
|
|
74
|
+
} else if ("asset" in props) {
|
|
75
|
+
unwrapped = props.asset;
|
|
76
|
+
}
|
|
77
|
+
if (!unwrapped) {
|
|
78
|
+
throw Error(
|
|
79
|
+
`Cannot determine asset type for props: ${JSON.stringify(props)}`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (typeof unwrapped !== "object") {
|
|
83
|
+
throw Error(
|
|
84
|
+
`Asset was not an object got (${typeof unwrapped}) instead: ${unwrapped}`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (unwrapped.type === void 0) {
|
|
88
|
+
const info = unwrapped.id === void 0 ? JSON.stringify(props) : `id: ${unwrapped.id}`;
|
|
89
|
+
throw Error(`Asset is missing type for ${info}`);
|
|
90
|
+
}
|
|
91
|
+
const Impl = registry?.get(unwrapped);
|
|
92
|
+
if (!Impl) {
|
|
93
|
+
throw Error(
|
|
94
|
+
`No implementation found for id: ${unwrapped.id} type: ${unwrapped.type}`
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
return /* @__PURE__ */ import_react.default.createElement(Impl, { key: unwrapped.id, ...unwrapped });
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/utils/player-context.ts
|
|
101
|
+
var import_react2 = __toESM(require("react"));
|
|
102
|
+
var PlayerContext = import_react2.default.createContext({});
|
|
103
|
+
var usePlayer = () => {
|
|
104
|
+
const { player } = import_react2.default.useContext(PlayerContext);
|
|
105
|
+
return player;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/utils/use-logger.ts
|
|
109
|
+
var import_player = require("@player-ui/player");
|
|
110
|
+
var noopLogger = new import_player.NoopLogger();
|
|
111
|
+
function useLogger() {
|
|
112
|
+
const player = usePlayer();
|
|
113
|
+
return player?.logger ?? noopLogger;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/utils/use-asset-props.tsx
|
|
117
|
+
function useAssetProps(asset) {
|
|
118
|
+
return {
|
|
119
|
+
id: asset.id,
|
|
120
|
+
"data-asset-type": asset.type
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/utils/helpers.ts
|
|
125
|
+
function trimSlashes(str) {
|
|
126
|
+
return str.replace(/^\/+|\/+$/g, "");
|
|
127
|
+
}
|
|
128
|
+
function removeEmptyValuesFromObject(obj) {
|
|
129
|
+
return Object.keys(obj).reduce(
|
|
130
|
+
(acc, key) => {
|
|
131
|
+
const value = obj[key];
|
|
132
|
+
if (value !== null && value !== void 0) {
|
|
133
|
+
acc[key] = value;
|
|
134
|
+
}
|
|
135
|
+
return acc;
|
|
136
|
+
},
|
|
137
|
+
{}
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
function isEmptyObject(obj) {
|
|
141
|
+
return Object.keys(obj).length === 0 && obj.constructor === Object;
|
|
142
|
+
}
|
|
143
|
+
function isFunction(maybeFn) {
|
|
144
|
+
return Boolean(maybeFn instanceof Function || typeof maybeFn === "function");
|
|
145
|
+
}
|
|
146
|
+
function callOrReturn(maybeFn, fnArgs) {
|
|
147
|
+
if (isFunction(maybeFn)) {
|
|
148
|
+
return maybeFn(fnArgs);
|
|
149
|
+
}
|
|
150
|
+
return maybeFn;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/utils/url.ts
|
|
154
|
+
function buildUrl(url, params = {}) {
|
|
155
|
+
const baseUrl = new URL(url);
|
|
156
|
+
if (params && isEmptyObject(params)) {
|
|
157
|
+
return baseUrl.toString();
|
|
158
|
+
}
|
|
159
|
+
Object.keys(params).forEach((key) => {
|
|
160
|
+
const value = params[key];
|
|
161
|
+
baseUrl.searchParams.append(key, String(value));
|
|
162
|
+
});
|
|
163
|
+
return baseUrl.toString();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/utils/shared-constants.tsx
|
|
167
|
+
function useGetConstantByType(type, key) {
|
|
168
|
+
const player = usePlayer();
|
|
169
|
+
return player?.constantsController.getConstants(key, type);
|
|
170
|
+
}
|
|
171
|
+
function useGetConstant(key) {
|
|
172
|
+
const player = usePlayer();
|
|
173
|
+
return player?.constantsController.getConstants(key, "constants");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/app.tsx
|
|
177
|
+
var import_react3 = __toESM(require("react"));
|
|
178
|
+
var ReactPlayer = ({ view }) => {
|
|
179
|
+
return /* @__PURE__ */ import_react3.default.createElement(ReactAsset, { ...view });
|
|
180
|
+
};
|
|
181
|
+
var app_default = ReactPlayer;
|
|
182
|
+
|
|
183
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/plugins/onupdate-plugin.ts
|
|
184
|
+
var OnUpdatePlugin = class {
|
|
185
|
+
constructor(onUpdate) {
|
|
186
|
+
this.name = "view-update";
|
|
187
|
+
this.onUpdateCallback = onUpdate;
|
|
188
|
+
}
|
|
189
|
+
apply(player) {
|
|
190
|
+
const updateTap = (updatedView) => {
|
|
191
|
+
this.onUpdateCallback(updatedView);
|
|
192
|
+
};
|
|
193
|
+
const viewTap = (view) => {
|
|
194
|
+
view.hooks.onUpdate.tap(this.name, updateTap);
|
|
195
|
+
};
|
|
196
|
+
player.hooks.view.tap(this.name, viewTap);
|
|
197
|
+
const currentPlayerState = player.getState();
|
|
198
|
+
if (currentPlayerState.status === "in-progress") {
|
|
199
|
+
const { currentView } = currentPlayerState.controllers.view;
|
|
200
|
+
if (currentView) {
|
|
201
|
+
viewTap(currentView);
|
|
202
|
+
const { lastUpdate } = currentView;
|
|
203
|
+
if (lastUpdate) {
|
|
204
|
+
this.onUpdateCallback(lastUpdate);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/player.tsx
|
|
212
|
+
var WEB_PLAYER_VERSION = "__VERSION__";
|
|
213
|
+
var COMMIT = "__GIT_COMMIT__";
|
|
214
|
+
var _window = typeof window === "undefined" ? void 0 : window;
|
|
215
|
+
var ReactPlayer2 = class {
|
|
216
|
+
constructor(options) {
|
|
217
|
+
this.assetRegistry = new import_partial_match_registry.Registry();
|
|
218
|
+
this.hooks = {
|
|
219
|
+
/**
|
|
220
|
+
* A hook to create a React Component to be used for Player, regardless of the current flow state
|
|
221
|
+
*/
|
|
222
|
+
webComponent: new import_tapable_ts.SyncWaterfallHook(),
|
|
223
|
+
/**
|
|
224
|
+
* A hook to create a React Component that's used to render a specific view.
|
|
225
|
+
* It will be called for each view update from the core player.
|
|
226
|
+
* Typically this will just be `Asset`
|
|
227
|
+
*/
|
|
228
|
+
playerComponent: new import_tapable_ts.SyncWaterfallHook(),
|
|
229
|
+
/**
|
|
230
|
+
* A hook to execute async tasks before the view resets to undefined
|
|
231
|
+
*/
|
|
232
|
+
onBeforeViewReset: new import_tapable_ts.AsyncParallelHook()
|
|
233
|
+
};
|
|
234
|
+
this.viewUpdateSubscription = new import_react_subscribe.Subscribe();
|
|
235
|
+
this.options = options ?? {};
|
|
236
|
+
const Devtools = _window?.__PLAYER_DEVTOOLS_PLUGIN;
|
|
237
|
+
const onUpdatePlugin = new OnUpdatePlugin(
|
|
238
|
+
this.viewUpdateSubscription.publish
|
|
239
|
+
);
|
|
240
|
+
const plugins = options?.plugins ?? [];
|
|
241
|
+
if (Devtools) {
|
|
242
|
+
plugins.push(new Devtools());
|
|
243
|
+
}
|
|
244
|
+
const playerPlugins = plugins.filter(
|
|
245
|
+
(p) => Boolean(p.apply)
|
|
246
|
+
);
|
|
247
|
+
this.player = options?.player ?? new import_player2.Player({ plugins: playerPlugins });
|
|
248
|
+
plugins.forEach((plugin) => {
|
|
249
|
+
if (plugin.applyReact) {
|
|
250
|
+
plugin.applyReact(this);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
onUpdatePlugin.apply(this.player);
|
|
254
|
+
this.Component = this.createReactPlayerComponent();
|
|
255
|
+
this.reactPlayerInfo = {
|
|
256
|
+
playerVersion: this.player.getVersion(),
|
|
257
|
+
playerCommit: this.player.getCommit(),
|
|
258
|
+
reactPlayerVersion: WEB_PLAYER_VERSION,
|
|
259
|
+
reactPlayerCommit: COMMIT
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/** Returns the current version of the underlying core Player */
|
|
263
|
+
getPlayerVersion() {
|
|
264
|
+
return this.reactPlayerInfo.playerVersion;
|
|
265
|
+
}
|
|
266
|
+
/** Returns the git commit used to build this core Player version */
|
|
267
|
+
getPlayerCommit() {
|
|
268
|
+
return this.reactPlayerInfo.playerCommit;
|
|
269
|
+
}
|
|
270
|
+
/** Find instance of [Plugin] that has been registered to the web player */
|
|
271
|
+
findPlugin(symbol) {
|
|
272
|
+
return this.options.plugins?.find((el) => el.symbol === symbol);
|
|
273
|
+
}
|
|
274
|
+
/** Register and apply [Plugin] if one with the same symbol is not already registered. */
|
|
275
|
+
registerPlugin(plugin) {
|
|
276
|
+
if (!plugin.applyReact)
|
|
277
|
+
return;
|
|
278
|
+
plugin.applyReact(this);
|
|
279
|
+
this.options.plugins?.push(plugin);
|
|
280
|
+
}
|
|
281
|
+
/** Returns the current version of the running React Player */
|
|
282
|
+
getReactPlayerVersion() {
|
|
283
|
+
return this.reactPlayerInfo.reactPlayerVersion;
|
|
284
|
+
}
|
|
285
|
+
/** Returns the git commit used to build the React Player version */
|
|
286
|
+
getReactPlayerCommit() {
|
|
287
|
+
return this.reactPlayerInfo.reactPlayerCommit;
|
|
288
|
+
}
|
|
289
|
+
createReactPlayerComponent() {
|
|
290
|
+
const BaseComp = this.hooks.webComponent.call(this.createReactComp());
|
|
291
|
+
const ReactPlayerComponent = (props) => {
|
|
292
|
+
return /* @__PURE__ */ import_react4.default.createElement(
|
|
293
|
+
import_react_error_boundary.ErrorBoundary,
|
|
294
|
+
{
|
|
295
|
+
fallbackRender: () => null,
|
|
296
|
+
onError: (err) => {
|
|
297
|
+
const playerState = this.player.getState();
|
|
298
|
+
if (playerState.status === "in-progress") {
|
|
299
|
+
playerState.fail(err);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
/* @__PURE__ */ import_react4.default.createElement(PlayerContext.Provider, { value: { player: this.player } }, /* @__PURE__ */ import_react4.default.createElement(BaseComp, { ...props }))
|
|
304
|
+
);
|
|
305
|
+
};
|
|
306
|
+
return ReactPlayerComponent;
|
|
307
|
+
}
|
|
308
|
+
createReactComp() {
|
|
309
|
+
const ActualPlayerComp = this.hooks.playerComponent.call(app_default);
|
|
310
|
+
const WebPlayerComponent = () => {
|
|
311
|
+
const view = (0, import_react_subscribe.useSubscribedState)(this.viewUpdateSubscription);
|
|
312
|
+
this.viewUpdateSubscription.suspend();
|
|
313
|
+
return /* @__PURE__ */ import_react4.default.createElement(
|
|
314
|
+
AssetContext.Provider,
|
|
315
|
+
{
|
|
316
|
+
value: {
|
|
317
|
+
registry: this.assetRegistry
|
|
318
|
+
}
|
|
319
|
+
},
|
|
320
|
+
view && /* @__PURE__ */ import_react4.default.createElement(ActualPlayerComp, { view })
|
|
321
|
+
);
|
|
322
|
+
};
|
|
323
|
+
return WebPlayerComponent;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Call this method to force the ReactPlayer to wait for the next view-update before performing the next render.
|
|
327
|
+
* If the `suspense` option is set, this will suspend while an update is pending, otherwise nothing will be rendered.
|
|
328
|
+
*/
|
|
329
|
+
setWaitForNextViewUpdate() {
|
|
330
|
+
const shouldCallResetHook = this.hooks.onBeforeViewReset.isUsed();
|
|
331
|
+
return this.viewUpdateSubscription.reset(
|
|
332
|
+
shouldCallResetHook ? this.hooks.onBeforeViewReset.call() : void 0
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
start(flow) {
|
|
336
|
+
this.setWaitForNextViewUpdate();
|
|
337
|
+
return this.player.start(flow).finally(async () => {
|
|
338
|
+
await this.setWaitForNextViewUpdate();
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
var WebPlayer = ReactPlayer2;
|
|
343
|
+
|
|
344
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/hooks.tsx
|
|
345
|
+
var import_player3 = require("@player-ui/player");
|
|
346
|
+
var import_react5 = __toESM(require("react"));
|
|
347
|
+
|
|
348
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/plugins/tapstate-plugin.ts
|
|
349
|
+
var StateTapPlugin = class {
|
|
350
|
+
constructor(callback) {
|
|
351
|
+
this.name = "statetap";
|
|
352
|
+
this.callbackFunction = callback;
|
|
353
|
+
}
|
|
354
|
+
apply(player) {
|
|
355
|
+
player.hooks.state.tap("usePlayer", (newPlayerState) => {
|
|
356
|
+
this.callbackFunction(newPlayerState);
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/hooks.tsx
|
|
362
|
+
var useReactPlayer = (options) => {
|
|
363
|
+
const [playerState, setPlayerState] = import_react5.default.useState(import_player3.NOT_STARTED_STATE);
|
|
364
|
+
const reactPlayer = import_react5.default.useMemo(() => {
|
|
365
|
+
const rp = new ReactPlayer2({
|
|
366
|
+
player: options?.player,
|
|
367
|
+
plugins: [
|
|
368
|
+
...options?.plugins ?? [],
|
|
369
|
+
new StateTapPlugin(setPlayerState)
|
|
370
|
+
]
|
|
371
|
+
});
|
|
372
|
+
return rp;
|
|
373
|
+
}, []);
|
|
374
|
+
const { player } = reactPlayer;
|
|
375
|
+
return {
|
|
376
|
+
reactPlayer,
|
|
377
|
+
player,
|
|
378
|
+
playerState
|
|
379
|
+
};
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/manager/managed-player.tsx
|
|
383
|
+
var import_react7 = __toESM(require("react"));
|
|
384
|
+
|
|
385
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/manager/request-time.tsx
|
|
386
|
+
var import_react6 = require("react");
|
|
387
|
+
var import_metrics_plugin = require("@player-ui/metrics-plugin");
|
|
388
|
+
var useRequestTime = () => {
|
|
389
|
+
const requestTimeRef = (0, import_react6.useRef)({});
|
|
390
|
+
(0, import_react6.useEffect)(() => {
|
|
391
|
+
return () => {
|
|
392
|
+
requestTimeRef.current = {};
|
|
393
|
+
};
|
|
394
|
+
}, [requestTimeRef]);
|
|
395
|
+
const getRequestTime = (0, import_react6.useCallback)(() => {
|
|
396
|
+
const { end, start } = requestTimeRef.current;
|
|
397
|
+
if (end && start) {
|
|
398
|
+
return end - start;
|
|
399
|
+
}
|
|
400
|
+
}, [requestTimeRef]);
|
|
401
|
+
function withRequestTime(nextPromise) {
|
|
402
|
+
const getTime = typeof performance === "undefined" ? Date : performance;
|
|
403
|
+
requestTimeRef.current = { start: getTime.now() };
|
|
404
|
+
return nextPromise.finally(() => {
|
|
405
|
+
requestTimeRef.current = {
|
|
406
|
+
...requestTimeRef.current,
|
|
407
|
+
end: getTime.now()
|
|
408
|
+
};
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
const RequestTimeMetricsPlugin = (0, import_react6.useMemo)(() => {
|
|
412
|
+
return {
|
|
413
|
+
name: "RequestTimeMetricsPlugin",
|
|
414
|
+
apply(player) {
|
|
415
|
+
player.applyTo(
|
|
416
|
+
import_metrics_plugin.MetricsCorePluginSymbol,
|
|
417
|
+
(metricsCorePlugin) => {
|
|
418
|
+
new import_metrics_plugin.RequestTimeWebPlugin(getRequestTime).apply(metricsCorePlugin);
|
|
419
|
+
}
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
}, [getRequestTime]);
|
|
424
|
+
return { withRequestTime, RequestTimeMetricsPlugin };
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/react/player/src/manager/managed-player.tsx
|
|
428
|
+
function identityMiddleware(next) {
|
|
429
|
+
return next;
|
|
430
|
+
}
|
|
431
|
+
var ManagedState = class {
|
|
432
|
+
constructor({
|
|
433
|
+
middleware
|
|
434
|
+
}) {
|
|
435
|
+
this.middleware = middleware;
|
|
436
|
+
this.callbacks = [];
|
|
437
|
+
}
|
|
438
|
+
/** Add a listener to state changes */
|
|
439
|
+
addListener(callback) {
|
|
440
|
+
this.callbacks.push(callback);
|
|
441
|
+
return () => {
|
|
442
|
+
this.callbacks = this.callbacks.filter((s) => s !== callback);
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
/** start the managed flow */
|
|
446
|
+
start(options) {
|
|
447
|
+
const initialState = {
|
|
448
|
+
value: "not_started",
|
|
449
|
+
context: {
|
|
450
|
+
playerConfig: options.playerConfig,
|
|
451
|
+
reactPlayer: new ReactPlayer2(options.playerConfig),
|
|
452
|
+
manager: options.manager
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
this.setState(initialState);
|
|
456
|
+
return this;
|
|
457
|
+
}
|
|
458
|
+
/** reset starts from nothing */
|
|
459
|
+
reset() {
|
|
460
|
+
if (this.state?.value === "error") {
|
|
461
|
+
const { playerConfig, manager } = this.state.context;
|
|
462
|
+
this.start({ playerConfig, manager });
|
|
463
|
+
} else {
|
|
464
|
+
throw new Error("Flow must be in error state to reset");
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
/** restart starts from the last result */
|
|
468
|
+
restart() {
|
|
469
|
+
if (this.state?.value === "error") {
|
|
470
|
+
const { playerConfig, manager, prevResult, reactPlayer } = this.state.context;
|
|
471
|
+
this.setState({
|
|
472
|
+
value: "completed",
|
|
473
|
+
context: {
|
|
474
|
+
playerConfig,
|
|
475
|
+
manager,
|
|
476
|
+
result: prevResult,
|
|
477
|
+
reactPlayer
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
} else {
|
|
481
|
+
throw new Error("Flow must be in error state to restart");
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
async setState(state) {
|
|
485
|
+
this.state = state;
|
|
486
|
+
this.callbacks.forEach((c) => {
|
|
487
|
+
c.onState(state);
|
|
488
|
+
});
|
|
489
|
+
const { manager, reactPlayer, playerConfig } = state.context;
|
|
490
|
+
try {
|
|
491
|
+
const nextState = await this.processState(state, {
|
|
492
|
+
manager,
|
|
493
|
+
reactPlayer,
|
|
494
|
+
playerConfig
|
|
495
|
+
});
|
|
496
|
+
if (nextState) {
|
|
497
|
+
this.setState(nextState);
|
|
498
|
+
}
|
|
499
|
+
} catch (e) {
|
|
500
|
+
this.setState({
|
|
501
|
+
value: "error",
|
|
502
|
+
context: {
|
|
503
|
+
manager,
|
|
504
|
+
reactPlayer,
|
|
505
|
+
playerConfig,
|
|
506
|
+
error: e
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
async processState(state, context) {
|
|
512
|
+
if (state.value === "not_started" || state.value === "completed") {
|
|
513
|
+
const prevResult = state.value === "completed" ? state.context.result : void 0;
|
|
514
|
+
const middleware = this.middleware?.next ?? identityMiddleware;
|
|
515
|
+
return {
|
|
516
|
+
value: "pending",
|
|
517
|
+
context: {
|
|
518
|
+
...context,
|
|
519
|
+
prevResult,
|
|
520
|
+
next: middleware(state.context.manager.next(prevResult))
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
if (state.value === "pending") {
|
|
525
|
+
const nextResult = await state.context.next;
|
|
526
|
+
if (nextResult.done) {
|
|
527
|
+
return {
|
|
528
|
+
value: "ended",
|
|
529
|
+
context: {
|
|
530
|
+
...context,
|
|
531
|
+
result: state.context.prevResult
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
return {
|
|
536
|
+
value: "loaded",
|
|
537
|
+
context: {
|
|
538
|
+
...context,
|
|
539
|
+
prevResult: state.context.prevResult,
|
|
540
|
+
flow: nextResult.value
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
if (state.value === "loaded") {
|
|
545
|
+
return {
|
|
546
|
+
value: "running",
|
|
547
|
+
context: {
|
|
548
|
+
...context,
|
|
549
|
+
flow: state.context.flow,
|
|
550
|
+
prevResult: state.context.prevResult,
|
|
551
|
+
result: state.context.reactPlayer.start(state.context.flow)
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
if (state.value === "running") {
|
|
556
|
+
const result = await state.context.result;
|
|
557
|
+
return {
|
|
558
|
+
value: "completed",
|
|
559
|
+
context: {
|
|
560
|
+
...context,
|
|
561
|
+
result
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
var managedPlayerStateMachines = /* @__PURE__ */ new WeakMap();
|
|
568
|
+
var usePersistentStateMachine = (options) => {
|
|
569
|
+
const keyRef = import_react7.default.useRef({
|
|
570
|
+
_key: Symbol("managed-player")
|
|
571
|
+
});
|
|
572
|
+
const managedState = managedPlayerStateMachines.get(keyRef.current) ?? new ManagedState({ middleware: options.middleware });
|
|
573
|
+
managedPlayerStateMachines.set(keyRef.current, managedState);
|
|
574
|
+
const [state, setState] = import_react7.default.useState(managedState.state);
|
|
575
|
+
import_react7.default.useEffect(() => {
|
|
576
|
+
const unsub = managedState.addListener({
|
|
577
|
+
onState: (s) => {
|
|
578
|
+
setState(s);
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
if (managedState.state === void 0) {
|
|
582
|
+
managedState.start(options);
|
|
583
|
+
}
|
|
584
|
+
return unsub;
|
|
585
|
+
}, []);
|
|
586
|
+
return { managedState, state };
|
|
587
|
+
};
|
|
588
|
+
var ManagedPlayer = (props) => {
|
|
589
|
+
const { withRequestTime, RequestTimeMetricsPlugin } = useRequestTime();
|
|
590
|
+
const { state, managedState } = usePersistentStateMachine({
|
|
591
|
+
manager: props.manager,
|
|
592
|
+
middleware: { next: withRequestTime },
|
|
593
|
+
playerConfig: {
|
|
594
|
+
plugins: [...props?.plugins ?? [], RequestTimeMetricsPlugin],
|
|
595
|
+
player: props.player
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
import_react7.default.useEffect(() => {
|
|
599
|
+
if (state?.value === "ended") {
|
|
600
|
+
props.onComplete?.(state?.context.result);
|
|
601
|
+
} else if (state?.value === "error") {
|
|
602
|
+
props.onError?.(state?.context.error);
|
|
603
|
+
} else if (state?.value === "running") {
|
|
604
|
+
props.onStartedFlow?.();
|
|
605
|
+
}
|
|
606
|
+
}, [state]);
|
|
607
|
+
import_react7.default.useEffect(() => {
|
|
608
|
+
return () => {
|
|
609
|
+
const playerState = state?.context.reactPlayer.player.getState();
|
|
610
|
+
if (state?.value === "running" && playerState?.status === "in-progress") {
|
|
611
|
+
props.manager.terminate?.(playerState.controllers.data.serialize());
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
}, [props.manager, state?.context.reactPlayer.player, state?.value]);
|
|
615
|
+
if (state?.value === "error") {
|
|
616
|
+
if (props.fallbackComponent) {
|
|
617
|
+
return /* @__PURE__ */ import_react7.default.createElement(
|
|
618
|
+
props.fallbackComponent,
|
|
619
|
+
{
|
|
620
|
+
reset: () => {
|
|
621
|
+
managedState.reset();
|
|
622
|
+
},
|
|
623
|
+
retry: () => {
|
|
624
|
+
managedState.restart();
|
|
625
|
+
},
|
|
626
|
+
error: state.context.error
|
|
627
|
+
}
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
if (!props.onError) {
|
|
631
|
+
throw state.context.error;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
if (state?.context.reactPlayer) {
|
|
635
|
+
const { Component } = state.context.reactPlayer;
|
|
636
|
+
return /* @__PURE__ */ import_react7.default.createElement(Component, null);
|
|
637
|
+
}
|
|
638
|
+
return null;
|
|
639
|
+
};
|
|
640
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
641
|
+
0 && (module.exports = {
|
|
642
|
+
AssetContext,
|
|
643
|
+
ManagedPlayer,
|
|
644
|
+
PlayerContext,
|
|
645
|
+
ReactAsset,
|
|
646
|
+
ReactPlayer,
|
|
647
|
+
WebPlayer,
|
|
648
|
+
buildUrl,
|
|
649
|
+
callOrReturn,
|
|
650
|
+
isEmptyObject,
|
|
651
|
+
isFunction,
|
|
652
|
+
removeEmptyValuesFromObject,
|
|
653
|
+
trimSlashes,
|
|
654
|
+
useAssetProps,
|
|
655
|
+
useGetConstant,
|
|
656
|
+
useGetConstantByType,
|
|
657
|
+
useLogger,
|
|
658
|
+
usePersistentStateMachine,
|
|
659
|
+
usePlayer,
|
|
660
|
+
useReactPlayer,
|
|
661
|
+
useRequestTime,
|
|
662
|
+
...require("@player-ui/player")
|
|
663
|
+
});
|
|
664
|
+
//# sourceMappingURL=index.cjs.map
|