@player-ui/external-action-plugin 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.
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var __async = (__this, __arguments, generator) => {
6
+ return new Promise((resolve, reject) => {
7
+ var fulfilled = (value) => {
8
+ try {
9
+ step(generator.next(value));
10
+ } catch (e) {
11
+ reject(e);
12
+ }
13
+ };
14
+ var rejected = (value) => {
15
+ try {
16
+ step(generator.throw(value));
17
+ } catch (e) {
18
+ reject(e);
19
+ }
20
+ };
21
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
22
+ step((generator = generator.apply(__this, __arguments)).next());
23
+ });
24
+ };
25
+ class ExternalActionPlugin {
26
+ constructor(handler) {
27
+ this.name = "ExternalActionPlugin";
28
+ this.handler = handler;
29
+ }
30
+ apply(player) {
31
+ player.hooks.flowController.tap(this.name, (flowController) => {
32
+ flowController.hooks.flow.tap(this.name, (flow) => {
33
+ flow.hooks.transition.tap(this.name, (fromState, toState) => {
34
+ const { value: state } = toState;
35
+ if (state.state_type === "EXTERNAL") {
36
+ setTimeout(() => __async(this, null, function* () {
37
+ var _a, _b;
38
+ const currentState = player.getState();
39
+ if (currentState.status === "in-progress" && ((_b = (_a = currentState.controllers.flow.current) == null ? void 0 : _a.currentState) == null ? void 0 : _b.value) === state) {
40
+ try {
41
+ const transitionValue = yield this.handler(state, currentState.controllers);
42
+ if (transitionValue !== void 0) {
43
+ currentState.controllers.flow.transition(transitionValue);
44
+ }
45
+ } catch (error) {
46
+ if (error instanceof Error) {
47
+ currentState.fail(error);
48
+ }
49
+ }
50
+ }
51
+ }), 0);
52
+ }
53
+ });
54
+ });
55
+ });
56
+ }
57
+ }
58
+
59
+ exports.ExternalActionPlugin = ExternalActionPlugin;
60
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1,15 @@
1
+ import { InProgressState, PlayerPlugin, Player } from '@player-ui/player';
2
+ import { NavigationFlowExternalState } from '@player-ui/types';
3
+
4
+ declare type ExternalStateHandler = (state: NavigationFlowExternalState, options: InProgressState['controllers']) => string | undefined | Promise<string | undefined>;
5
+ /**
6
+ * A plugin to handle external actions states
7
+ */
8
+ declare class ExternalActionPlugin implements PlayerPlugin {
9
+ name: string;
10
+ private handler;
11
+ constructor(handler: ExternalStateHandler);
12
+ apply(player: Player): void;
13
+ }
14
+
15
+ export { ExternalActionPlugin, ExternalStateHandler };
@@ -0,0 +1,56 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ class ExternalActionPlugin {
22
+ constructor(handler) {
23
+ this.name = "ExternalActionPlugin";
24
+ this.handler = handler;
25
+ }
26
+ apply(player) {
27
+ player.hooks.flowController.tap(this.name, (flowController) => {
28
+ flowController.hooks.flow.tap(this.name, (flow) => {
29
+ flow.hooks.transition.tap(this.name, (fromState, toState) => {
30
+ const { value: state } = toState;
31
+ if (state.state_type === "EXTERNAL") {
32
+ setTimeout(() => __async(this, null, function* () {
33
+ var _a, _b;
34
+ const currentState = player.getState();
35
+ if (currentState.status === "in-progress" && ((_b = (_a = currentState.controllers.flow.current) == null ? void 0 : _a.currentState) == null ? void 0 : _b.value) === state) {
36
+ try {
37
+ const transitionValue = yield this.handler(state, currentState.controllers);
38
+ if (transitionValue !== void 0) {
39
+ currentState.controllers.flow.transition(transitionValue);
40
+ }
41
+ } catch (error) {
42
+ if (error instanceof Error) {
43
+ currentState.fail(error);
44
+ }
45
+ }
46
+ }
47
+ }), 0);
48
+ }
49
+ });
50
+ });
51
+ });
52
+ }
53
+ }
54
+
55
+ export { ExternalActionPlugin };
56
+ //# sourceMappingURL=index.esm.js.map
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@player-ui/external-action-plugin",
3
+ "version": "0.0.1-next.1",
4
+ "private": false,
5
+ "publishConfig": {
6
+ "registry": "https://registry.npmjs.org"
7
+ },
8
+ "peerDependencies": {
9
+ "@player-ui/binding-grammar": "0.0.1-next.1"
10
+ },
11
+ "dependencies": {
12
+ "@babel/runtime": "7.15.4"
13
+ },
14
+ "main": "dist/index.cjs.js",
15
+ "module": "dist/index.esm.js",
16
+ "typings": "dist/index.d.ts"
17
+ }
package/src/index.ts ADDED
@@ -0,0 +1,56 @@
1
+ import type { Player, PlayerPlugin, InProgressState } from '@player-ui/player';
2
+ import type { NavigationFlowExternalState } from '@player-ui/types';
3
+
4
+ export type ExternalStateHandler = (
5
+ state: NavigationFlowExternalState,
6
+ options: InProgressState['controllers']
7
+ ) => string | undefined | Promise<string | undefined>;
8
+
9
+ /**
10
+ * A plugin to handle external actions states
11
+ */
12
+ export class ExternalActionPlugin implements PlayerPlugin {
13
+ name = 'ExternalActionPlugin';
14
+ private handler: ExternalStateHandler;
15
+
16
+ constructor(handler: ExternalStateHandler) {
17
+ this.handler = handler;
18
+ }
19
+
20
+ apply(player: Player) {
21
+ player.hooks.flowController.tap(this.name, (flowController) => {
22
+ flowController.hooks.flow.tap(this.name, (flow) => {
23
+ flow.hooks.transition.tap(this.name, (fromState, toState) => {
24
+ const { value: state } = toState;
25
+
26
+ if (state.state_type === 'EXTERNAL') {
27
+ setTimeout(async () => {
28
+ const currentState = player.getState();
29
+
30
+ if (
31
+ currentState.status === 'in-progress' &&
32
+ currentState.controllers.flow.current?.currentState?.value ===
33
+ state
34
+ ) {
35
+ try {
36
+ const transitionValue = await this.handler(
37
+ state,
38
+ currentState.controllers
39
+ );
40
+
41
+ if (transitionValue !== undefined) {
42
+ currentState.controllers.flow.transition(transitionValue);
43
+ }
44
+ } catch (error) {
45
+ if (error instanceof Error) {
46
+ currentState.fail(error);
47
+ }
48
+ }
49
+ }
50
+ }, 0);
51
+ }
52
+ });
53
+ });
54
+ });
55
+ }
56
+ }