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