@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.
- package/dist/index.cjs.js +543 -0
- package/dist/index.d.ts +330 -0
- package/dist/index.esm.js +526 -0
- package/package.json +25 -0
- package/src/app.tsx +23 -0
- package/src/hooks.tsx +47 -0
- package/src/index.tsx +6 -0
- package/src/manager/managed-player.tsx +319 -0
- package/src/manager/request-time.tsx +63 -0
- package/src/manager/types.ts +162 -0
- package/src/player.tsx +245 -0
- package/src/plugins/onupdate-plugin.ts +47 -0
- package/src/plugins/tapstate-plugin.ts +20 -0
- package/src/utils/desc.d.ts +2 -0
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var player = require('@player-ui/player');
|
|
7
|
+
var reactAsset = require('@player-ui/react-asset');
|
|
8
|
+
var reactErrorBoundary = require('react-error-boundary');
|
|
9
|
+
var reactUtils = require('@player-ui/react-utils');
|
|
10
|
+
var tapable = require('tapable');
|
|
11
|
+
var reactSubscribe = require('@player-ui/react-subscribe');
|
|
12
|
+
var partialMatchRegistry = require('@player-ui/partial-match-registry');
|
|
13
|
+
var metricsPlugin = require('@player-ui/metrics-plugin');
|
|
14
|
+
|
|
15
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
16
|
+
|
|
17
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
18
|
+
|
|
19
|
+
var __defProp$2 = Object.defineProperty;
|
|
20
|
+
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
21
|
+
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
22
|
+
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
23
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
24
|
+
var __spreadValues$2 = (a, b) => {
|
|
25
|
+
for (var prop in b || (b = {}))
|
|
26
|
+
if (__hasOwnProp$2.call(b, prop))
|
|
27
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
28
|
+
if (__getOwnPropSymbols$2)
|
|
29
|
+
for (var prop of __getOwnPropSymbols$2(b)) {
|
|
30
|
+
if (__propIsEnum$2.call(b, prop))
|
|
31
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
32
|
+
}
|
|
33
|
+
return a;
|
|
34
|
+
};
|
|
35
|
+
const WebPlayer$1 = ({ view }) => {
|
|
36
|
+
return /* @__PURE__ */ React__default["default"].createElement("div", null, /* @__PURE__ */ React__default["default"].createElement(reactAsset.Asset, __spreadValues$2({}, view)));
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
class OnUpdatePlugin {
|
|
40
|
+
constructor(onUpdate) {
|
|
41
|
+
this.name = "view-update";
|
|
42
|
+
this.onUpdateCallback = onUpdate;
|
|
43
|
+
}
|
|
44
|
+
apply(player) {
|
|
45
|
+
const updateTap = (updatedView) => {
|
|
46
|
+
this.onUpdateCallback(updatedView);
|
|
47
|
+
};
|
|
48
|
+
const viewTap = (view) => {
|
|
49
|
+
view.hooks.onUpdate.tap(this.name, updateTap);
|
|
50
|
+
};
|
|
51
|
+
player.hooks.view.tap(this.name, viewTap);
|
|
52
|
+
const currentPlayerState = player.getState();
|
|
53
|
+
if (currentPlayerState.status === "in-progress") {
|
|
54
|
+
const { currentView } = currentPlayerState.controllers.view;
|
|
55
|
+
if (currentView) {
|
|
56
|
+
viewTap(currentView);
|
|
57
|
+
const { lastUpdate } = currentView;
|
|
58
|
+
if (lastUpdate) {
|
|
59
|
+
this.onUpdateCallback(lastUpdate);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
var __async$1 = (__this, __arguments, generator) => {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
var fulfilled = (value) => {
|
|
69
|
+
try {
|
|
70
|
+
step(generator.next(value));
|
|
71
|
+
} catch (e) {
|
|
72
|
+
reject(e);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var rejected = (value) => {
|
|
76
|
+
try {
|
|
77
|
+
step(generator.throw(value));
|
|
78
|
+
} catch (e) {
|
|
79
|
+
reject(e);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
83
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
const WEB_PLAYER_VERSION = "!!STABLE_VERSION!!";
|
|
87
|
+
const COMMIT = "!!STABLE_GIT_COMMIT!!";
|
|
88
|
+
const _window = typeof window === "undefined" ? void 0 : window;
|
|
89
|
+
class WebPlayer {
|
|
90
|
+
constructor(options) {
|
|
91
|
+
this.assetRegistry = new partialMatchRegistry.Registry();
|
|
92
|
+
this.hooks = {
|
|
93
|
+
webComponent: new tapable.SyncWaterfallHook(["webComponent"]),
|
|
94
|
+
playerComponent: new tapable.SyncWaterfallHook(["playerComponent"]),
|
|
95
|
+
onBeforeViewReset: new tapable.AsyncParallelHook()
|
|
96
|
+
};
|
|
97
|
+
this.viewUpdateSubscription = new reactSubscribe.Subscribe();
|
|
98
|
+
var _a, _b;
|
|
99
|
+
this.options = options != null ? options : {};
|
|
100
|
+
if (!("suspend" in this.options)) {
|
|
101
|
+
this.options.suspend = true;
|
|
102
|
+
}
|
|
103
|
+
const Devtools = _window == null ? void 0 : _window.__PLAYER_DEVTOOLS_PLUGIN;
|
|
104
|
+
const onUpdatePlugin = new OnUpdatePlugin(this.viewUpdateSubscription.publish);
|
|
105
|
+
const plugins = (_a = options == null ? void 0 : options.plugins) != null ? _a : [];
|
|
106
|
+
if (Devtools) {
|
|
107
|
+
plugins.push(new Devtools());
|
|
108
|
+
}
|
|
109
|
+
const playerPlugins = plugins.filter((p) => Boolean(p.apply));
|
|
110
|
+
this.player = (_b = options == null ? void 0 : options.player) != null ? _b : new player.Player({ plugins: playerPlugins });
|
|
111
|
+
plugins.forEach((plugin) => {
|
|
112
|
+
if (plugin.applyWeb) {
|
|
113
|
+
plugin.applyWeb(this);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
onUpdatePlugin.apply(this.player);
|
|
117
|
+
this.Component = this.hooks.webComponent.call(this.createReactComp());
|
|
118
|
+
this.webplayerInfo = {
|
|
119
|
+
playerVersion: this.player.getVersion(),
|
|
120
|
+
playerCommit: this.player.getCommit(),
|
|
121
|
+
webplayerVersion: WEB_PLAYER_VERSION,
|
|
122
|
+
webplayerCommit: COMMIT
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
getPlayerVersion() {
|
|
126
|
+
return this.webplayerInfo.playerVersion;
|
|
127
|
+
}
|
|
128
|
+
getPlayerCommit() {
|
|
129
|
+
return this.webplayerInfo.playerCommit;
|
|
130
|
+
}
|
|
131
|
+
findPlugin(symbol) {
|
|
132
|
+
var _a;
|
|
133
|
+
return (_a = this.options.plugins) == null ? void 0 : _a.find((el) => el.symbol === symbol);
|
|
134
|
+
}
|
|
135
|
+
registerPlugin(plugin) {
|
|
136
|
+
var _a;
|
|
137
|
+
if (!plugin.applyWeb)
|
|
138
|
+
return;
|
|
139
|
+
plugin.applyWeb(this);
|
|
140
|
+
(_a = this.options.plugins) == null ? void 0 : _a.push(plugin);
|
|
141
|
+
}
|
|
142
|
+
getWebPlayerVersion() {
|
|
143
|
+
return this.webplayerInfo.webplayerVersion;
|
|
144
|
+
}
|
|
145
|
+
getWebPlayerCommit() {
|
|
146
|
+
return this.webplayerInfo.webplayerCommit;
|
|
147
|
+
}
|
|
148
|
+
createReactComp() {
|
|
149
|
+
const ActualPlayerComp = this.hooks.playerComponent.call(WebPlayer$1);
|
|
150
|
+
const WebPlayerComponent = () => {
|
|
151
|
+
const view = reactSubscribe.useSubscribedState(this.viewUpdateSubscription);
|
|
152
|
+
if (this.options.suspend) {
|
|
153
|
+
this.viewUpdateSubscription.suspend();
|
|
154
|
+
}
|
|
155
|
+
return /* @__PURE__ */ React__default["default"].createElement(reactErrorBoundary.ErrorBoundary, {
|
|
156
|
+
fallbackRender: () => null,
|
|
157
|
+
onError: (err) => {
|
|
158
|
+
const playerState = this.player.getState();
|
|
159
|
+
if (playerState.status === "in-progress") {
|
|
160
|
+
playerState.fail(err);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}, /* @__PURE__ */ React__default["default"].createElement(reactUtils.PlayerContext.Provider, {
|
|
164
|
+
value: { player: this.player }
|
|
165
|
+
}, /* @__PURE__ */ React__default["default"].createElement(reactAsset.AssetContext.Provider, {
|
|
166
|
+
value: {
|
|
167
|
+
registry: this.assetRegistry
|
|
168
|
+
}
|
|
169
|
+
}, view && /* @__PURE__ */ React__default["default"].createElement(ActualPlayerComp, {
|
|
170
|
+
view
|
|
171
|
+
}))));
|
|
172
|
+
};
|
|
173
|
+
return WebPlayerComponent;
|
|
174
|
+
}
|
|
175
|
+
setWaitForNextViewUpdate() {
|
|
176
|
+
const shouldCallResetHook = this.options.suspend && this.hooks.onBeforeViewReset.isUsed();
|
|
177
|
+
return this.viewUpdateSubscription.reset(shouldCallResetHook ? this.hooks.onBeforeViewReset.promise() : void 0);
|
|
178
|
+
}
|
|
179
|
+
start(flow) {
|
|
180
|
+
this.setWaitForNextViewUpdate();
|
|
181
|
+
return this.player.start(flow).finally(() => __async$1(this, null, function* () {
|
|
182
|
+
var _a;
|
|
183
|
+
if ((_a = this.options) == null ? void 0 : _a.suspend) {
|
|
184
|
+
yield this.setWaitForNextViewUpdate();
|
|
185
|
+
}
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
class StateTapPlugin {
|
|
191
|
+
constructor(callback) {
|
|
192
|
+
this.name = "statetap";
|
|
193
|
+
this.callbackFunction = callback;
|
|
194
|
+
}
|
|
195
|
+
apply(player) {
|
|
196
|
+
player.hooks.state.tap("usePlayer", (newPlayerState) => {
|
|
197
|
+
this.callbackFunction(newPlayerState);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const useWebPlayer = (options) => {
|
|
203
|
+
const [playerState, setPlayerState] = React__default["default"].useState(player.NOT_STARTED_STATE);
|
|
204
|
+
const webPlayer = React__default["default"].useMemo(() => {
|
|
205
|
+
var _a;
|
|
206
|
+
const wp = new WebPlayer({
|
|
207
|
+
player: options == null ? void 0 : options.player,
|
|
208
|
+
plugins: [
|
|
209
|
+
...(_a = options == null ? void 0 : options.plugins) != null ? _a : [],
|
|
210
|
+
new StateTapPlugin(setPlayerState)
|
|
211
|
+
],
|
|
212
|
+
suspend: options == null ? void 0 : options.suspend
|
|
213
|
+
});
|
|
214
|
+
return wp;
|
|
215
|
+
}, []);
|
|
216
|
+
const { player: player$1 } = webPlayer;
|
|
217
|
+
return {
|
|
218
|
+
webPlayer,
|
|
219
|
+
player: player$1,
|
|
220
|
+
playerState
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
var __defProp$1 = Object.defineProperty;
|
|
225
|
+
var __defProps$1 = Object.defineProperties;
|
|
226
|
+
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
227
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
228
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
229
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
230
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
231
|
+
var __spreadValues$1 = (a, b) => {
|
|
232
|
+
for (var prop in b || (b = {}))
|
|
233
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
234
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
235
|
+
if (__getOwnPropSymbols$1)
|
|
236
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
237
|
+
if (__propIsEnum$1.call(b, prop))
|
|
238
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
239
|
+
}
|
|
240
|
+
return a;
|
|
241
|
+
};
|
|
242
|
+
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
243
|
+
const useRequestTime = () => {
|
|
244
|
+
const requestTimeRef = React.useRef({});
|
|
245
|
+
React.useEffect(() => {
|
|
246
|
+
return () => {
|
|
247
|
+
requestTimeRef.current = {};
|
|
248
|
+
};
|
|
249
|
+
}, [requestTimeRef]);
|
|
250
|
+
const getRequestTime = React.useCallback(() => {
|
|
251
|
+
const { end, start } = requestTimeRef.current;
|
|
252
|
+
if (end && start) {
|
|
253
|
+
return end - start;
|
|
254
|
+
}
|
|
255
|
+
}, [requestTimeRef]);
|
|
256
|
+
function withRequestTime(nextPromise) {
|
|
257
|
+
const getTime = typeof performance === "undefined" ? Date : performance;
|
|
258
|
+
requestTimeRef.current = { start: getTime.now() };
|
|
259
|
+
return nextPromise.finally(() => {
|
|
260
|
+
requestTimeRef.current = __spreadProps$1(__spreadValues$1({}, requestTimeRef.current), {
|
|
261
|
+
end: getTime.now()
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
const RequestTimeMetricsPlugin = React.useMemo(() => {
|
|
266
|
+
return {
|
|
267
|
+
name: "RequestTimeMetricsPlugin",
|
|
268
|
+
apply(player) {
|
|
269
|
+
player.applyTo(metricsPlugin.MetricsCorePluginSymbol, (metricsCorePlugin) => {
|
|
270
|
+
new metricsPlugin.RequestTimeWebPlugin(getRequestTime).apply(metricsCorePlugin);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}, [getRequestTime]);
|
|
275
|
+
return { withRequestTime, RequestTimeMetricsPlugin };
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
var __defProp = Object.defineProperty;
|
|
279
|
+
var __defProps = Object.defineProperties;
|
|
280
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
281
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
282
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
283
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
284
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
285
|
+
var __spreadValues = (a, b) => {
|
|
286
|
+
for (var prop in b || (b = {}))
|
|
287
|
+
if (__hasOwnProp.call(b, prop))
|
|
288
|
+
__defNormalProp(a, prop, b[prop]);
|
|
289
|
+
if (__getOwnPropSymbols)
|
|
290
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
291
|
+
if (__propIsEnum.call(b, prop))
|
|
292
|
+
__defNormalProp(a, prop, b[prop]);
|
|
293
|
+
}
|
|
294
|
+
return a;
|
|
295
|
+
};
|
|
296
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
297
|
+
var __async = (__this, __arguments, generator) => {
|
|
298
|
+
return new Promise((resolve, reject) => {
|
|
299
|
+
var fulfilled = (value) => {
|
|
300
|
+
try {
|
|
301
|
+
step(generator.next(value));
|
|
302
|
+
} catch (e) {
|
|
303
|
+
reject(e);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
var rejected = (value) => {
|
|
307
|
+
try {
|
|
308
|
+
step(generator.throw(value));
|
|
309
|
+
} catch (e) {
|
|
310
|
+
reject(e);
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
314
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
function identityMiddleware(next) {
|
|
318
|
+
return next;
|
|
319
|
+
}
|
|
320
|
+
class ManagedState {
|
|
321
|
+
constructor({
|
|
322
|
+
middleware
|
|
323
|
+
}) {
|
|
324
|
+
this.middleware = middleware;
|
|
325
|
+
this.callbacks = [];
|
|
326
|
+
}
|
|
327
|
+
addListener(callback) {
|
|
328
|
+
this.callbacks.push(callback);
|
|
329
|
+
return () => {
|
|
330
|
+
this.callbacks = this.callbacks.filter((s) => s !== callback);
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
start(options) {
|
|
334
|
+
const playerConfig = __spreadProps(__spreadValues({}, options.playerConfig), {
|
|
335
|
+
suspend: true
|
|
336
|
+
});
|
|
337
|
+
const initialState = {
|
|
338
|
+
value: "not_started",
|
|
339
|
+
context: {
|
|
340
|
+
playerConfig,
|
|
341
|
+
webPlayer: new WebPlayer(playerConfig),
|
|
342
|
+
manager: options.manager
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
this.setState(initialState);
|
|
346
|
+
return this;
|
|
347
|
+
}
|
|
348
|
+
reset() {
|
|
349
|
+
var _a;
|
|
350
|
+
if (((_a = this.state) == null ? void 0 : _a.value) === "error") {
|
|
351
|
+
const { playerConfig, manager } = this.state.context;
|
|
352
|
+
this.start({ playerConfig, manager });
|
|
353
|
+
} else {
|
|
354
|
+
throw new Error("Flow must be in error state to reset");
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
restart() {
|
|
358
|
+
var _a;
|
|
359
|
+
if (((_a = this.state) == null ? void 0 : _a.value) === "error") {
|
|
360
|
+
const { playerConfig, manager, prevResult, webPlayer } = this.state.context;
|
|
361
|
+
this.setState({
|
|
362
|
+
value: "completed",
|
|
363
|
+
context: {
|
|
364
|
+
playerConfig,
|
|
365
|
+
manager,
|
|
366
|
+
result: prevResult,
|
|
367
|
+
webPlayer
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
} else {
|
|
371
|
+
throw new Error("Flow must be in error state to restart");
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
setState(state) {
|
|
375
|
+
return __async(this, null, function* () {
|
|
376
|
+
this.state = state;
|
|
377
|
+
this.callbacks.forEach((c) => {
|
|
378
|
+
c.onState(state);
|
|
379
|
+
});
|
|
380
|
+
const { manager, webPlayer, playerConfig } = state.context;
|
|
381
|
+
try {
|
|
382
|
+
const nextState = yield this.processState(state, {
|
|
383
|
+
manager,
|
|
384
|
+
webPlayer,
|
|
385
|
+
playerConfig
|
|
386
|
+
});
|
|
387
|
+
if (nextState) {
|
|
388
|
+
this.setState(nextState);
|
|
389
|
+
}
|
|
390
|
+
} catch (e) {
|
|
391
|
+
this.setState({
|
|
392
|
+
value: "error",
|
|
393
|
+
context: {
|
|
394
|
+
manager,
|
|
395
|
+
webPlayer,
|
|
396
|
+
playerConfig,
|
|
397
|
+
error: e
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
processState(state, context) {
|
|
404
|
+
return __async(this, null, function* () {
|
|
405
|
+
var _a, _b;
|
|
406
|
+
if (state.value === "not_started" || state.value === "completed") {
|
|
407
|
+
const prevResult = state.value === "completed" ? state.context.result : void 0;
|
|
408
|
+
const middleware = (_b = (_a = this.middleware) == null ? void 0 : _a.next) != null ? _b : identityMiddleware;
|
|
409
|
+
return {
|
|
410
|
+
value: "pending",
|
|
411
|
+
context: __spreadProps(__spreadValues({}, context), {
|
|
412
|
+
prevResult,
|
|
413
|
+
next: middleware(state.context.manager.next(prevResult))
|
|
414
|
+
})
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
if (state.value === "pending") {
|
|
418
|
+
const nextResult = yield state.context.next;
|
|
419
|
+
if (nextResult.done) {
|
|
420
|
+
return {
|
|
421
|
+
value: "ended",
|
|
422
|
+
context: __spreadProps(__spreadValues({}, context), {
|
|
423
|
+
result: state.context.prevResult
|
|
424
|
+
})
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
return {
|
|
428
|
+
value: "loaded",
|
|
429
|
+
context: __spreadProps(__spreadValues({}, context), {
|
|
430
|
+
prevResult: state.context.prevResult,
|
|
431
|
+
flow: nextResult.value
|
|
432
|
+
})
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
if (state.value === "loaded") {
|
|
436
|
+
return {
|
|
437
|
+
value: "running",
|
|
438
|
+
context: __spreadProps(__spreadValues({}, context), {
|
|
439
|
+
flow: state.context.flow,
|
|
440
|
+
prevResult: state.context.prevResult,
|
|
441
|
+
result: state.context.webPlayer.start(state.context.flow)
|
|
442
|
+
})
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
if (state.value === "running") {
|
|
446
|
+
const result = yield state.context.result;
|
|
447
|
+
return {
|
|
448
|
+
value: "completed",
|
|
449
|
+
context: __spreadProps(__spreadValues({}, context), {
|
|
450
|
+
result
|
|
451
|
+
})
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
const managedPlayerStateMachines = new WeakMap();
|
|
458
|
+
const usePersistentStateMachine = (options) => {
|
|
459
|
+
var _a;
|
|
460
|
+
const keyRef = React__default["default"].useRef({
|
|
461
|
+
_key: Symbol("managed-player")
|
|
462
|
+
});
|
|
463
|
+
const managedState = (_a = managedPlayerStateMachines.get(keyRef.current)) != null ? _a : new ManagedState({ middleware: options.middleware });
|
|
464
|
+
managedPlayerStateMachines.set(keyRef.current, managedState);
|
|
465
|
+
const [state, setState] = React__default["default"].useState(managedState.state);
|
|
466
|
+
React__default["default"].useEffect(() => {
|
|
467
|
+
const unsub = managedState.addListener({
|
|
468
|
+
onState: (s) => {
|
|
469
|
+
setState(s);
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
if (managedState.state === void 0) {
|
|
473
|
+
managedState.start(options);
|
|
474
|
+
}
|
|
475
|
+
return unsub;
|
|
476
|
+
}, []);
|
|
477
|
+
return { managedState, state };
|
|
478
|
+
};
|
|
479
|
+
const ManagedPlayer = (props) => {
|
|
480
|
+
var _a;
|
|
481
|
+
const { withRequestTime, RequestTimeMetricsPlugin } = useRequestTime();
|
|
482
|
+
const { state, managedState } = usePersistentStateMachine({
|
|
483
|
+
manager: props.manager,
|
|
484
|
+
middleware: { next: withRequestTime },
|
|
485
|
+
playerConfig: {
|
|
486
|
+
plugins: [...(_a = props == null ? void 0 : props.plugins) != null ? _a : [], RequestTimeMetricsPlugin],
|
|
487
|
+
player: props.player
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
React__default["default"].useEffect(() => {
|
|
491
|
+
var _a2, _b, _c;
|
|
492
|
+
if ((state == null ? void 0 : state.value) === "ended") {
|
|
493
|
+
(_a2 = props.onComplete) == null ? void 0 : _a2.call(props, state == null ? void 0 : state.context.result);
|
|
494
|
+
} else if ((state == null ? void 0 : state.value) === "error") {
|
|
495
|
+
(_b = props.onError) == null ? void 0 : _b.call(props, state == null ? void 0 : state.context.error);
|
|
496
|
+
} else if ((state == null ? void 0 : state.value) === "running") {
|
|
497
|
+
(_c = props.onStartedFlow) == null ? void 0 : _c.call(props);
|
|
498
|
+
}
|
|
499
|
+
}, [state]);
|
|
500
|
+
React__default["default"].useEffect(() => {
|
|
501
|
+
return () => {
|
|
502
|
+
var _a2, _b;
|
|
503
|
+
const playerState = state == null ? void 0 : state.context.webPlayer.player.getState();
|
|
504
|
+
if ((state == null ? void 0 : state.value) === "running" && (playerState == null ? void 0 : playerState.status) === "in-progress") {
|
|
505
|
+
(_b = (_a2 = props.manager).terminate) == null ? void 0 : _b.call(_a2, playerState.controllers.data.serialize());
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
}, [props.manager, state == null ? void 0 : state.context.webPlayer.player, state == null ? void 0 : state.value]);
|
|
509
|
+
if ((state == null ? void 0 : state.value) === "error") {
|
|
510
|
+
if (props.fallbackComponent) {
|
|
511
|
+
return /* @__PURE__ */ React__default["default"].createElement(props.fallbackComponent, {
|
|
512
|
+
reset: () => {
|
|
513
|
+
managedState.reset();
|
|
514
|
+
},
|
|
515
|
+
retry: () => {
|
|
516
|
+
managedState.restart();
|
|
517
|
+
},
|
|
518
|
+
error: state.context.error
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
if (!props.onError) {
|
|
522
|
+
throw state.context.error;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (state == null ? void 0 : state.context.webPlayer) {
|
|
526
|
+
const { Component } = state.context.webPlayer;
|
|
527
|
+
return /* @__PURE__ */ React__default["default"].createElement(Component, null);
|
|
528
|
+
}
|
|
529
|
+
return null;
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
exports.ManagedPlayer = ManagedPlayer;
|
|
533
|
+
exports.WebPlayer = WebPlayer;
|
|
534
|
+
exports.usePersistentStateMachine = usePersistentStateMachine;
|
|
535
|
+
exports.useRequestTime = useRequestTime;
|
|
536
|
+
exports.useWebPlayer = useWebPlayer;
|
|
537
|
+
Object.keys(player).forEach(function (k) {
|
|
538
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
539
|
+
enumerable: true,
|
|
540
|
+
get: function () { return player[k]; }
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
//# sourceMappingURL=index.cjs.js.map
|