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