@player-ui/react 0.3.0-next.2 → 0.3.0-next.4
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 +160 -45
- package/dist/index.d.ts +107 -41
- package/dist/index.esm.js +144 -44
- package/package.json +5 -7
- package/src/app.tsx +8 -12
- package/src/asset/index.tsx +49 -0
- package/src/hooks.tsx +13 -13
- package/src/index.tsx +3 -1
- package/src/manager/managed-player.tsx +16 -16
- package/src/manager/request-time.tsx +2 -2
- package/src/manager/types.ts +4 -4
- package/src/player.tsx +51 -48
- package/src/plugins/tapstate-plugin.ts +2 -2
- package/src/utils/helpers.ts +50 -0
- package/src/utils/index.tsx +6 -0
- package/src/utils/player-context.ts +23 -0
- package/src/utils/shared-constants.tsx +15 -0
- package/src/utils/url.ts +22 -0
- package/src/utils/use-asset-props.tsx +9 -0
- package/src/utils/use-logger.ts +14 -0
- package/src/utils/desc.d.ts +0 -2
package/dist/index.esm.js
CHANGED
|
@@ -1,14 +1,113 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Player, NOT_STARTED_STATE } from '@player-ui/player';
|
|
1
|
+
import { NoopLogger, Player, NOT_STARTED_STATE } from '@player-ui/player';
|
|
3
2
|
export * from '@player-ui/player';
|
|
4
|
-
import {
|
|
5
|
-
import { ErrorBoundary } from 'react-error-boundary';
|
|
6
|
-
import { PlayerContext } from '@player-ui/react-utils';
|
|
3
|
+
import React, { useRef, useEffect, useCallback, useMemo } from 'react';
|
|
7
4
|
import { SyncWaterfallHook, AsyncParallelHook } from 'tapable-ts';
|
|
8
5
|
import { Subscribe, useSubscribedState } from '@player-ui/react-subscribe';
|
|
9
6
|
import { Registry } from '@player-ui/partial-match-registry';
|
|
7
|
+
import { ErrorBoundary } from 'react-error-boundary';
|
|
10
8
|
import { MetricsCorePluginSymbol, RequestTimeWebPlugin } from '@player-ui/metrics-plugin';
|
|
11
9
|
|
|
10
|
+
var __defProp$3 = Object.defineProperty;
|
|
11
|
+
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
12
|
+
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
13
|
+
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
14
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
15
|
+
var __spreadValues$3 = (a, b) => {
|
|
16
|
+
for (var prop in b || (b = {}))
|
|
17
|
+
if (__hasOwnProp$3.call(b, prop))
|
|
18
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
19
|
+
if (__getOwnPropSymbols$3)
|
|
20
|
+
for (var prop of __getOwnPropSymbols$3(b)) {
|
|
21
|
+
if (__propIsEnum$3.call(b, prop))
|
|
22
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
23
|
+
}
|
|
24
|
+
return a;
|
|
25
|
+
};
|
|
26
|
+
const AssetContext = React.createContext({});
|
|
27
|
+
const ReactAsset = (props) => {
|
|
28
|
+
const { registry } = React.useContext(AssetContext);
|
|
29
|
+
let unwrapped;
|
|
30
|
+
if ("type" in props && "id" in props) {
|
|
31
|
+
unwrapped = props;
|
|
32
|
+
} else if ("asset" in props) {
|
|
33
|
+
unwrapped = props.asset;
|
|
34
|
+
}
|
|
35
|
+
if (!unwrapped || typeof unwrapped !== "object" || (unwrapped == null ? void 0 : unwrapped.type) === void 0) {
|
|
36
|
+
throw Error(`Cannot determine asset type.`);
|
|
37
|
+
}
|
|
38
|
+
const Impl = registry == null ? void 0 : registry.get(unwrapped);
|
|
39
|
+
if (!Impl) {
|
|
40
|
+
throw Error(`No implementation found for id: ${unwrapped.id} type: ${unwrapped.type}`);
|
|
41
|
+
}
|
|
42
|
+
return /* @__PURE__ */ React.createElement(Impl, __spreadValues$3({}, unwrapped));
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const PlayerContext = React.createContext({});
|
|
46
|
+
const usePlayer = () => {
|
|
47
|
+
const { player } = React.useContext(PlayerContext);
|
|
48
|
+
return player;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const noopLogger = new NoopLogger();
|
|
52
|
+
function useLogger() {
|
|
53
|
+
var _a;
|
|
54
|
+
const player = usePlayer();
|
|
55
|
+
return (_a = player == null ? void 0 : player.logger) != null ? _a : noopLogger;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function useAssetProps(asset) {
|
|
59
|
+
return {
|
|
60
|
+
id: asset.id,
|
|
61
|
+
"data-asset-type": asset.type
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function trimSlashes(str) {
|
|
66
|
+
return str.replace(/^\/+|\/+$/g, "");
|
|
67
|
+
}
|
|
68
|
+
function removeEmptyValuesFromObject(obj) {
|
|
69
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
70
|
+
const value = obj[key];
|
|
71
|
+
if (value !== null && value !== void 0) {
|
|
72
|
+
acc[key] = value;
|
|
73
|
+
}
|
|
74
|
+
return acc;
|
|
75
|
+
}, {});
|
|
76
|
+
}
|
|
77
|
+
function isEmptyObject(obj) {
|
|
78
|
+
return Object.keys(obj).length === 0 && obj.constructor === Object;
|
|
79
|
+
}
|
|
80
|
+
function isFunction(maybeFn) {
|
|
81
|
+
return Boolean(maybeFn instanceof Function || typeof maybeFn === "function");
|
|
82
|
+
}
|
|
83
|
+
function callOrReturn(maybeFn, fnArgs) {
|
|
84
|
+
if (isFunction(maybeFn)) {
|
|
85
|
+
return maybeFn(fnArgs);
|
|
86
|
+
}
|
|
87
|
+
return maybeFn;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function buildUrl(url, params = {}) {
|
|
91
|
+
const baseUrl = new URL(url);
|
|
92
|
+
if (params && isEmptyObject(params)) {
|
|
93
|
+
return baseUrl.toString();
|
|
94
|
+
}
|
|
95
|
+
Object.keys(params).forEach((key) => {
|
|
96
|
+
const value = params[key];
|
|
97
|
+
baseUrl.searchParams.append(key, String(value));
|
|
98
|
+
});
|
|
99
|
+
return baseUrl.toString();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function useGetConstantByType(type, key) {
|
|
103
|
+
const player = usePlayer();
|
|
104
|
+
return player == null ? void 0 : player.constantsController.getConstants(key, type);
|
|
105
|
+
}
|
|
106
|
+
function useGetConstant(key) {
|
|
107
|
+
const player = usePlayer();
|
|
108
|
+
return player == null ? void 0 : player.constantsController.getConstants(key, "constants");
|
|
109
|
+
}
|
|
110
|
+
|
|
12
111
|
var __defProp$2 = Object.defineProperty;
|
|
13
112
|
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
14
113
|
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
@@ -25,8 +124,8 @@ var __spreadValues$2 = (a, b) => {
|
|
|
25
124
|
}
|
|
26
125
|
return a;
|
|
27
126
|
};
|
|
28
|
-
const
|
|
29
|
-
return /* @__PURE__ */ React.createElement(
|
|
127
|
+
const ReactPlayer$1 = ({ view }) => {
|
|
128
|
+
return /* @__PURE__ */ React.createElement(ReactAsset, __spreadValues$2({}, view));
|
|
30
129
|
};
|
|
31
130
|
|
|
32
131
|
class OnUpdatePlugin {
|
|
@@ -76,10 +175,10 @@ var __async$1 = (__this, __arguments, generator) => {
|
|
|
76
175
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
77
176
|
});
|
|
78
177
|
};
|
|
79
|
-
const WEB_PLAYER_VERSION = "0.3.0-next.
|
|
80
|
-
const COMMIT = "
|
|
178
|
+
const WEB_PLAYER_VERSION = "0.3.0-next.4";
|
|
179
|
+
const COMMIT = "774a3cf3b0765796901d03a311a06ee68e690a74";
|
|
81
180
|
const _window = typeof window === "undefined" ? void 0 : window;
|
|
82
|
-
class
|
|
181
|
+
class ReactPlayer {
|
|
83
182
|
constructor(options) {
|
|
84
183
|
this.assetRegistry = new Registry();
|
|
85
184
|
this.hooks = {
|
|
@@ -102,24 +201,24 @@ class WebPlayer {
|
|
|
102
201
|
const playerPlugins = plugins.filter((p) => Boolean(p.apply));
|
|
103
202
|
this.player = (_b = options == null ? void 0 : options.player) != null ? _b : new Player({ plugins: playerPlugins });
|
|
104
203
|
plugins.forEach((plugin) => {
|
|
105
|
-
if (plugin.
|
|
106
|
-
plugin.
|
|
204
|
+
if (plugin.applyReact) {
|
|
205
|
+
plugin.applyReact(this);
|
|
107
206
|
}
|
|
108
207
|
});
|
|
109
208
|
onUpdatePlugin.apply(this.player);
|
|
110
209
|
this.Component = this.hooks.webComponent.call(this.createReactComp());
|
|
111
|
-
this.
|
|
210
|
+
this.reactPlayerInfo = {
|
|
112
211
|
playerVersion: this.player.getVersion(),
|
|
113
212
|
playerCommit: this.player.getCommit(),
|
|
114
|
-
|
|
115
|
-
|
|
213
|
+
reactPlayerVersion: WEB_PLAYER_VERSION,
|
|
214
|
+
reactPlayerCommit: COMMIT
|
|
116
215
|
};
|
|
117
216
|
}
|
|
118
217
|
getPlayerVersion() {
|
|
119
|
-
return this.
|
|
218
|
+
return this.reactPlayerInfo.playerVersion;
|
|
120
219
|
}
|
|
121
220
|
getPlayerCommit() {
|
|
122
|
-
return this.
|
|
221
|
+
return this.reactPlayerInfo.playerCommit;
|
|
123
222
|
}
|
|
124
223
|
findPlugin(symbol) {
|
|
125
224
|
var _a;
|
|
@@ -127,20 +226,20 @@ class WebPlayer {
|
|
|
127
226
|
}
|
|
128
227
|
registerPlugin(plugin) {
|
|
129
228
|
var _a;
|
|
130
|
-
if (!plugin.
|
|
229
|
+
if (!plugin.applyReact)
|
|
131
230
|
return;
|
|
132
|
-
plugin.
|
|
231
|
+
plugin.applyReact(this);
|
|
133
232
|
(_a = this.options.plugins) == null ? void 0 : _a.push(plugin);
|
|
134
233
|
}
|
|
135
|
-
|
|
136
|
-
return this.
|
|
234
|
+
getReactPlayerVersion() {
|
|
235
|
+
return this.reactPlayerInfo.reactPlayerVersion;
|
|
137
236
|
}
|
|
138
|
-
|
|
139
|
-
return this.
|
|
237
|
+
getReactPlayerCommit() {
|
|
238
|
+
return this.reactPlayerInfo.reactPlayerCommit;
|
|
140
239
|
}
|
|
141
240
|
createReactComp() {
|
|
142
|
-
const ActualPlayerComp = this.hooks.playerComponent.call(
|
|
143
|
-
const
|
|
241
|
+
const ActualPlayerComp = this.hooks.playerComponent.call(ReactPlayer$1);
|
|
242
|
+
const ReactPlayerComponent = () => {
|
|
144
243
|
const view = useSubscribedState(this.viewUpdateSubscription);
|
|
145
244
|
if (this.options.suspend) {
|
|
146
245
|
this.viewUpdateSubscription.suspend();
|
|
@@ -163,7 +262,7 @@ class WebPlayer {
|
|
|
163
262
|
view
|
|
164
263
|
}))));
|
|
165
264
|
};
|
|
166
|
-
return
|
|
265
|
+
return ReactPlayerComponent;
|
|
167
266
|
}
|
|
168
267
|
setWaitForNextViewUpdate() {
|
|
169
268
|
const shouldCallResetHook = this.options.suspend && this.hooks.onBeforeViewReset.isUsed();
|
|
@@ -179,6 +278,7 @@ class WebPlayer {
|
|
|
179
278
|
}));
|
|
180
279
|
}
|
|
181
280
|
}
|
|
281
|
+
const WebPlayer = ReactPlayer;
|
|
182
282
|
|
|
183
283
|
class StateTapPlugin {
|
|
184
284
|
constructor(callback) {
|
|
@@ -192,11 +292,11 @@ class StateTapPlugin {
|
|
|
192
292
|
}
|
|
193
293
|
}
|
|
194
294
|
|
|
195
|
-
const
|
|
295
|
+
const useReactPlayer = (options) => {
|
|
196
296
|
const [playerState, setPlayerState] = React.useState(NOT_STARTED_STATE);
|
|
197
|
-
const
|
|
297
|
+
const reactPlayer = React.useMemo(() => {
|
|
198
298
|
var _a;
|
|
199
|
-
const
|
|
299
|
+
const rp = new ReactPlayer({
|
|
200
300
|
player: options == null ? void 0 : options.player,
|
|
201
301
|
plugins: [
|
|
202
302
|
...(_a = options == null ? void 0 : options.plugins) != null ? _a : [],
|
|
@@ -204,11 +304,11 @@ const useWebPlayer = (options) => {
|
|
|
204
304
|
],
|
|
205
305
|
suspend: options == null ? void 0 : options.suspend
|
|
206
306
|
});
|
|
207
|
-
return
|
|
307
|
+
return rp;
|
|
208
308
|
}, []);
|
|
209
|
-
const { player } =
|
|
309
|
+
const { player } = reactPlayer;
|
|
210
310
|
return {
|
|
211
|
-
|
|
311
|
+
reactPlayer,
|
|
212
312
|
player,
|
|
213
313
|
playerState
|
|
214
314
|
};
|
|
@@ -331,7 +431,7 @@ class ManagedState {
|
|
|
331
431
|
value: "not_started",
|
|
332
432
|
context: {
|
|
333
433
|
playerConfig,
|
|
334
|
-
|
|
434
|
+
reactPlayer: new ReactPlayer(playerConfig),
|
|
335
435
|
manager: options.manager
|
|
336
436
|
}
|
|
337
437
|
};
|
|
@@ -350,14 +450,14 @@ class ManagedState {
|
|
|
350
450
|
restart() {
|
|
351
451
|
var _a;
|
|
352
452
|
if (((_a = this.state) == null ? void 0 : _a.value) === "error") {
|
|
353
|
-
const { playerConfig, manager, prevResult,
|
|
453
|
+
const { playerConfig, manager, prevResult, reactPlayer } = this.state.context;
|
|
354
454
|
this.setState({
|
|
355
455
|
value: "completed",
|
|
356
456
|
context: {
|
|
357
457
|
playerConfig,
|
|
358
458
|
manager,
|
|
359
459
|
result: prevResult,
|
|
360
|
-
|
|
460
|
+
reactPlayer
|
|
361
461
|
}
|
|
362
462
|
});
|
|
363
463
|
} else {
|
|
@@ -370,11 +470,11 @@ class ManagedState {
|
|
|
370
470
|
this.callbacks.forEach((c) => {
|
|
371
471
|
c.onState(state);
|
|
372
472
|
});
|
|
373
|
-
const { manager,
|
|
473
|
+
const { manager, reactPlayer, playerConfig } = state.context;
|
|
374
474
|
try {
|
|
375
475
|
const nextState = yield this.processState(state, {
|
|
376
476
|
manager,
|
|
377
|
-
|
|
477
|
+
reactPlayer,
|
|
378
478
|
playerConfig
|
|
379
479
|
});
|
|
380
480
|
if (nextState) {
|
|
@@ -385,7 +485,7 @@ class ManagedState {
|
|
|
385
485
|
value: "error",
|
|
386
486
|
context: {
|
|
387
487
|
manager,
|
|
388
|
-
|
|
488
|
+
reactPlayer,
|
|
389
489
|
playerConfig,
|
|
390
490
|
error: e
|
|
391
491
|
}
|
|
@@ -431,7 +531,7 @@ class ManagedState {
|
|
|
431
531
|
context: __spreadProps(__spreadValues({}, context), {
|
|
432
532
|
flow: state.context.flow,
|
|
433
533
|
prevResult: state.context.prevResult,
|
|
434
|
-
result: state.context.
|
|
534
|
+
result: state.context.reactPlayer.start(state.context.flow)
|
|
435
535
|
})
|
|
436
536
|
};
|
|
437
537
|
}
|
|
@@ -493,12 +593,12 @@ const ManagedPlayer = (props) => {
|
|
|
493
593
|
React.useEffect(() => {
|
|
494
594
|
return () => {
|
|
495
595
|
var _a2, _b;
|
|
496
|
-
const playerState = state == null ? void 0 : state.context.
|
|
596
|
+
const playerState = state == null ? void 0 : state.context.reactPlayer.player.getState();
|
|
497
597
|
if ((state == null ? void 0 : state.value) === "running" && (playerState == null ? void 0 : playerState.status) === "in-progress") {
|
|
498
598
|
(_b = (_a2 = props.manager).terminate) == null ? void 0 : _b.call(_a2, playerState.controllers.data.serialize());
|
|
499
599
|
}
|
|
500
600
|
};
|
|
501
|
-
}, [props.manager, state == null ? void 0 : state.context.
|
|
601
|
+
}, [props.manager, state == null ? void 0 : state.context.reactPlayer.player, state == null ? void 0 : state.value]);
|
|
502
602
|
if ((state == null ? void 0 : state.value) === "error") {
|
|
503
603
|
if (props.fallbackComponent) {
|
|
504
604
|
return /* @__PURE__ */ React.createElement(props.fallbackComponent, {
|
|
@@ -515,12 +615,12 @@ const ManagedPlayer = (props) => {
|
|
|
515
615
|
throw state.context.error;
|
|
516
616
|
}
|
|
517
617
|
}
|
|
518
|
-
if (state == null ? void 0 : state.context.
|
|
519
|
-
const { Component } = state.context.
|
|
618
|
+
if (state == null ? void 0 : state.context.reactPlayer) {
|
|
619
|
+
const { Component } = state.context.reactPlayer;
|
|
520
620
|
return /* @__PURE__ */ React.createElement(Component, null);
|
|
521
621
|
}
|
|
522
622
|
return null;
|
|
523
623
|
};
|
|
524
624
|
|
|
525
|
-
export { ManagedPlayer, WebPlayer, usePersistentStateMachine,
|
|
625
|
+
export { AssetContext, ManagedPlayer, PlayerContext, ReactAsset, ReactPlayer, WebPlayer, buildUrl, callOrReturn, isEmptyObject, isFunction, removeEmptyValuesFromObject, trimSlashes, useAssetProps, useGetConstant, useGetConstantByType, useLogger, usePersistentStateMachine, usePlayer, useReactPlayer, useRequestTime };
|
|
526
626
|
//# sourceMappingURL=index.esm.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@player-ui/react",
|
|
3
|
-
"version": "0.3.0-next.
|
|
3
|
+
"version": "0.3.0-next.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -11,12 +11,10 @@
|
|
|
11
11
|
"react-dom": "^17.0.2"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@player-ui/player": "0.3.0-next.
|
|
15
|
-
"@player-ui/partial-match-registry": "0.3.0-next.
|
|
16
|
-
"@player-ui/metrics-plugin": "0.3.0-next.
|
|
17
|
-
"@player-ui/react-
|
|
18
|
-
"@player-ui/react-subscribe": "0.3.0-next.2",
|
|
19
|
-
"@player-ui/react-utils": "0.3.0-next.2",
|
|
14
|
+
"@player-ui/player": "0.3.0-next.4",
|
|
15
|
+
"@player-ui/partial-match-registry": "0.3.0-next.4",
|
|
16
|
+
"@player-ui/metrics-plugin": "0.3.0-next.4",
|
|
17
|
+
"@player-ui/react-subscribe": "0.3.0-next.4",
|
|
20
18
|
"react-error-boundary": "^3.1.3",
|
|
21
19
|
"tapable-ts": "^0.1.0",
|
|
22
20
|
"@babel/runtime": "7.15.4"
|
package/src/app.tsx
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
2
|
+
import type { View } from '@player-ui/player';
|
|
3
|
+
import { ReactAsset } from './asset';
|
|
4
4
|
|
|
5
|
-
export interface
|
|
5
|
+
export interface ReactPlayerProps {
|
|
6
6
|
/**
|
|
7
7
|
* The Content view object to render
|
|
8
8
|
*/
|
|
9
|
-
view:
|
|
9
|
+
view: View;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* The entry for the
|
|
13
|
+
* The entry for the ReactPlayer's React tree
|
|
14
14
|
*/
|
|
15
|
-
const
|
|
16
|
-
return
|
|
17
|
-
<div>
|
|
18
|
-
<Asset {...view} />
|
|
19
|
-
</div>
|
|
20
|
-
);
|
|
15
|
+
const ReactPlayer = ({ view }: ReactPlayerProps) => {
|
|
16
|
+
return <ReactAsset {...view} />;
|
|
21
17
|
};
|
|
22
18
|
|
|
23
|
-
export default
|
|
19
|
+
export default ReactPlayer;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { Asset as AssetType, AssetWrapper } from '@player-ui/player';
|
|
3
|
+
import type { Registry } from '@player-ui/partial-match-registry';
|
|
4
|
+
|
|
5
|
+
export type AssetRegistryType = Registry<React.ComponentType<any>>;
|
|
6
|
+
|
|
7
|
+
export interface ContextType {
|
|
8
|
+
/**
|
|
9
|
+
* A registry of Asset -> React Components
|
|
10
|
+
*/
|
|
11
|
+
registry?: AssetRegistryType;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const AssetContext = React.createContext<ContextType>({});
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A React Component that looks up an implementation from a registry
|
|
18
|
+
*/
|
|
19
|
+
export const ReactAsset = (
|
|
20
|
+
props: AssetType<string> | AssetWrapper<AssetType<string>>
|
|
21
|
+
) => {
|
|
22
|
+
const { registry } = React.useContext(AssetContext);
|
|
23
|
+
|
|
24
|
+
let unwrapped;
|
|
25
|
+
|
|
26
|
+
if ('type' in props && 'id' in props) {
|
|
27
|
+
unwrapped = props;
|
|
28
|
+
} else if ('asset' in props) {
|
|
29
|
+
unwrapped = (props as unknown as AssetWrapper).asset;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (
|
|
33
|
+
!unwrapped ||
|
|
34
|
+
typeof unwrapped !== 'object' ||
|
|
35
|
+
unwrapped?.type === undefined
|
|
36
|
+
) {
|
|
37
|
+
throw Error(`Cannot determine asset type.`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const Impl = registry?.get(unwrapped);
|
|
41
|
+
|
|
42
|
+
if (!Impl) {
|
|
43
|
+
throw Error(
|
|
44
|
+
`No implementation found for id: ${unwrapped.id} type: ${unwrapped.type}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return <Impl {...unwrapped} />;
|
|
49
|
+
};
|
package/src/hooks.tsx
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { Player, PlayerFlowState } from '@player-ui/player';
|
|
2
2
|
import { NOT_STARTED_STATE } from '@player-ui/player';
|
|
3
3
|
import React from 'react';
|
|
4
|
-
import type {
|
|
5
|
-
import {
|
|
4
|
+
import type { ReactPlayerOptions } from './player';
|
|
5
|
+
import { ReactPlayer } from './player';
|
|
6
6
|
import { StateTapPlugin } from './plugins/tapstate-plugin';
|
|
7
7
|
|
|
8
|
-
export interface
|
|
8
|
+
export interface UseReactPlayerReturn {
|
|
9
9
|
/** The web-player instance */
|
|
10
|
-
|
|
10
|
+
reactPlayer: ReactPlayer;
|
|
11
11
|
/** Player instance */
|
|
12
12
|
player: Player;
|
|
13
13
|
/** The state of Player */
|
|
@@ -15,17 +15,17 @@ export interface UseWebPlayerReturn {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* The `
|
|
18
|
+
* The `useReactPlayer` hook is an easy way to integrate the web-player into your React app.
|
|
19
19
|
* Simply supply your config, plugins, and an optional flow, which will be automatically started for you when changed.
|
|
20
20
|
*/
|
|
21
|
-
export const
|
|
22
|
-
options?:
|
|
23
|
-
):
|
|
21
|
+
export const useReactPlayer = (
|
|
22
|
+
options?: ReactPlayerOptions
|
|
23
|
+
): UseReactPlayerReturn => {
|
|
24
24
|
const [playerState, setPlayerState] =
|
|
25
25
|
React.useState<PlayerFlowState>(NOT_STARTED_STATE);
|
|
26
26
|
|
|
27
|
-
const
|
|
28
|
-
const
|
|
27
|
+
const reactPlayer = React.useMemo(() => {
|
|
28
|
+
const rp = new ReactPlayer({
|
|
29
29
|
player: options?.player,
|
|
30
30
|
plugins: [
|
|
31
31
|
...(options?.plugins ?? []),
|
|
@@ -34,13 +34,13 @@ export const useWebPlayer = (
|
|
|
34
34
|
suspend: options?.suspend,
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
-
return
|
|
37
|
+
return rp;
|
|
38
38
|
}, []);
|
|
39
39
|
|
|
40
|
-
const { player } =
|
|
40
|
+
const { player } = reactPlayer;
|
|
41
41
|
|
|
42
42
|
return {
|
|
43
|
-
|
|
43
|
+
reactPlayer,
|
|
44
44
|
player,
|
|
45
45
|
playerState,
|
|
46
46
|
};
|
package/src/index.tsx
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
export * from './player';
|
|
2
1
|
export * from '@player-ui/player';
|
|
2
|
+
export * from './player';
|
|
3
3
|
export * from './hooks';
|
|
4
4
|
export * from './manager/managed-player';
|
|
5
5
|
export * from './manager/request-time';
|
|
6
6
|
export * from './manager/types';
|
|
7
|
+
export * from './asset';
|
|
8
|
+
export * from './utils';
|
|
@@ -7,8 +7,8 @@ import type {
|
|
|
7
7
|
ManagedPlayerContext,
|
|
8
8
|
} from './types';
|
|
9
9
|
import { useRequestTime } from './request-time';
|
|
10
|
-
import type {
|
|
11
|
-
import {
|
|
10
|
+
import type { ReactPlayerOptions } from '../player';
|
|
11
|
+
import { ReactPlayer } from '../player';
|
|
12
12
|
|
|
13
13
|
/** noop middleware */
|
|
14
14
|
function identityMiddleware<T>(next: Promise<T>) {
|
|
@@ -58,9 +58,9 @@ class ManagedState {
|
|
|
58
58
|
manager: FlowManager;
|
|
59
59
|
|
|
60
60
|
/** the config to use when creating a player */
|
|
61
|
-
playerConfig:
|
|
61
|
+
playerConfig: ReactPlayerOptions;
|
|
62
62
|
}) {
|
|
63
|
-
const playerConfig:
|
|
63
|
+
const playerConfig: ReactPlayerOptions = {
|
|
64
64
|
...options.playerConfig,
|
|
65
65
|
suspend: true,
|
|
66
66
|
};
|
|
@@ -69,7 +69,7 @@ class ManagedState {
|
|
|
69
69
|
value: 'not_started',
|
|
70
70
|
context: {
|
|
71
71
|
playerConfig,
|
|
72
|
-
|
|
72
|
+
reactPlayer: new ReactPlayer(playerConfig),
|
|
73
73
|
manager: options.manager,
|
|
74
74
|
},
|
|
75
75
|
};
|
|
@@ -92,7 +92,7 @@ class ManagedState {
|
|
|
92
92
|
/** restart starts from the last result */
|
|
93
93
|
public restart() {
|
|
94
94
|
if (this.state?.value === 'error') {
|
|
95
|
-
const { playerConfig, manager, prevResult,
|
|
95
|
+
const { playerConfig, manager, prevResult, reactPlayer } =
|
|
96
96
|
this.state.context;
|
|
97
97
|
this.setState({
|
|
98
98
|
value: 'completed',
|
|
@@ -100,7 +100,7 @@ class ManagedState {
|
|
|
100
100
|
playerConfig,
|
|
101
101
|
manager,
|
|
102
102
|
result: prevResult,
|
|
103
|
-
|
|
103
|
+
reactPlayer,
|
|
104
104
|
},
|
|
105
105
|
});
|
|
106
106
|
} else {
|
|
@@ -114,12 +114,12 @@ class ManagedState {
|
|
|
114
114
|
c.onState(state);
|
|
115
115
|
});
|
|
116
116
|
|
|
117
|
-
const { manager,
|
|
117
|
+
const { manager, reactPlayer, playerConfig } = state.context;
|
|
118
118
|
|
|
119
119
|
try {
|
|
120
120
|
const nextState = await this.processState(state, {
|
|
121
121
|
manager,
|
|
122
|
-
|
|
122
|
+
reactPlayer,
|
|
123
123
|
playerConfig,
|
|
124
124
|
});
|
|
125
125
|
|
|
@@ -131,7 +131,7 @@ class ManagedState {
|
|
|
131
131
|
value: 'error',
|
|
132
132
|
context: {
|
|
133
133
|
manager,
|
|
134
|
-
|
|
134
|
+
reactPlayer,
|
|
135
135
|
playerConfig,
|
|
136
136
|
error: e as Error,
|
|
137
137
|
},
|
|
@@ -189,7 +189,7 @@ class ManagedState {
|
|
|
189
189
|
...context,
|
|
190
190
|
flow: state.context.flow,
|
|
191
191
|
prevResult: state.context.prevResult,
|
|
192
|
-
result: state.context.
|
|
192
|
+
result: state.context.reactPlayer.start(state.context.flow),
|
|
193
193
|
},
|
|
194
194
|
};
|
|
195
195
|
}
|
|
@@ -219,7 +219,7 @@ export const usePersistentStateMachine = (options: {
|
|
|
219
219
|
manager: FlowManager;
|
|
220
220
|
|
|
221
221
|
/** Player config */
|
|
222
|
-
playerConfig:
|
|
222
|
+
playerConfig: ReactPlayerOptions;
|
|
223
223
|
|
|
224
224
|
/** Any middleware for the manager */
|
|
225
225
|
middleware?: ManagerMiddleware;
|
|
@@ -281,13 +281,13 @@ export const ManagedPlayer = (props: ManagedPlayerProps) => {
|
|
|
281
281
|
|
|
282
282
|
React.useEffect(() => {
|
|
283
283
|
return () => {
|
|
284
|
-
const playerState = state?.context.
|
|
284
|
+
const playerState = state?.context.reactPlayer.player.getState();
|
|
285
285
|
|
|
286
286
|
if (state?.value === 'running' && playerState?.status === 'in-progress') {
|
|
287
287
|
props.manager.terminate?.(playerState.controllers.data.serialize());
|
|
288
288
|
}
|
|
289
289
|
};
|
|
290
|
-
}, [props.manager, state?.context.
|
|
290
|
+
}, [props.manager, state?.context.reactPlayer.player, state?.value]);
|
|
291
291
|
|
|
292
292
|
if (state?.value === 'error') {
|
|
293
293
|
if (props.fallbackComponent) {
|
|
@@ -309,8 +309,8 @@ export const ManagedPlayer = (props: ManagedPlayerProps) => {
|
|
|
309
309
|
}
|
|
310
310
|
}
|
|
311
311
|
|
|
312
|
-
if (state?.context.
|
|
313
|
-
const { Component } = state.context.
|
|
312
|
+
if (state?.context.reactPlayer) {
|
|
313
|
+
const { Component } = state.context.reactPlayer;
|
|
314
314
|
|
|
315
315
|
return <Component />;
|
|
316
316
|
}
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
MetricsCorePluginSymbol,
|
|
6
6
|
RequestTimeWebPlugin,
|
|
7
7
|
} from '@player-ui/metrics-plugin';
|
|
8
|
-
import type {
|
|
8
|
+
import type { ReactPlayerPlugin } from '../player';
|
|
9
9
|
|
|
10
10
|
type RequestTime = {
|
|
11
11
|
/** request start time */
|
|
@@ -45,7 +45,7 @@ export const useRequestTime = () => {
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
const RequestTimeMetricsPlugin:
|
|
48
|
+
const RequestTimeMetricsPlugin: ReactPlayerPlugin = useMemo(() => {
|
|
49
49
|
return {
|
|
50
50
|
name: 'RequestTimeMetricsPlugin',
|
|
51
51
|
apply(player: Player): void {
|
package/src/manager/types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type React from 'react';
|
|
2
2
|
import type { CompletedState, Flow, FlowResult } from '@player-ui/player';
|
|
3
|
-
import type {
|
|
3
|
+
import type { ReactPlayer, ReactPlayerOptions } from '../player';
|
|
4
4
|
|
|
5
5
|
export interface FinalState {
|
|
6
6
|
/** Mark the iteration as complete */
|
|
@@ -45,7 +45,7 @@ export interface FallbackProps {
|
|
|
45
45
|
error?: Error;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export interface ManagedPlayerProps extends
|
|
48
|
+
export interface ManagedPlayerProps extends ReactPlayerOptions {
|
|
49
49
|
/** The manager for populating the next flows */
|
|
50
50
|
manager: FlowManager;
|
|
51
51
|
|
|
@@ -67,10 +67,10 @@ export type ManagedPlayerContext = {
|
|
|
67
67
|
manager: FlowManager;
|
|
68
68
|
|
|
69
69
|
/** The web-player */
|
|
70
|
-
|
|
70
|
+
reactPlayer: ReactPlayer;
|
|
71
71
|
|
|
72
72
|
/** The config for Player */
|
|
73
|
-
playerConfig:
|
|
73
|
+
playerConfig: ReactPlayerOptions;
|
|
74
74
|
};
|
|
75
75
|
|
|
76
76
|
export type ManagedPlayerState =
|