@player-ui/external-action-plugin 0.4.0 → 0.4.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.
@@ -138,13 +138,21 @@ class ExternalActionPlugin {
138
138
  const { value: state } = toState;
139
139
  if (state.state_type === "EXTERNAL") {
140
140
  setTimeout(() => __async(this, null, function* () {
141
- var _a, _b;
141
+ const shouldTransition = (currentState2) => {
142
+ var _a, _b;
143
+ return currentState2.status === "in-progress" && ((_b = (_a = currentState2.controllers.flow.current) == null ? void 0 : _a.currentState) == null ? void 0 : _b.value) === state;
144
+ };
142
145
  const currentState = player.getState();
143
- if (currentState.status === "in-progress" && ((_b = (_a = currentState.controllers.flow.current) == null ? void 0 : _a.currentState) == null ? void 0 : _b.value) === state) {
146
+ if (shouldTransition(currentState)) {
144
147
  try {
145
148
  const transitionValue = yield this.handler(state, currentState.controllers);
146
149
  if (transitionValue !== void 0) {
147
- currentState.controllers.flow.transition(transitionValue);
150
+ const latestState = player.getState();
151
+ if (shouldTransition(latestState)) {
152
+ latestState.controllers.flow.transition(transitionValue);
153
+ } else {
154
+ player.logger.warn(`External state resolved with [${transitionValue}], but Player already navigated away from [${toState.name}]`);
155
+ }
148
156
  }
149
157
  } catch (error) {
150
158
  if (error instanceof Error) {
@@ -1 +1 @@
1
- !function(t,e){"object"===typeof exports&&"object"===typeof module?module.exports=e():"function"===typeof define&&define.amd?define([],e):"object"===typeof exports?exports.ExternalActionPlugin=e():t.ExternalActionPlugin=e()}(this,(function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"===typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";n.r(e),n.d(e,"ExternalActionPlugin",(function(){return r}));class r{constructor(t){this.name="ExternalActionPlugin",this.handler=t}apply(t){t.hooks.flowController.tap(this.name,(e=>{e.hooks.flow.tap(this.name,(e=>{e.hooks.transition.tap(this.name,((e,n)=>{const{value:r}=n;"EXTERNAL"===r.state_type&&setTimeout((()=>{return e=this,n=null,o=function*(){var e,n;const o=t.getState();if("in-progress"===o.status&&(null==(n=null==(e=o.controllers.flow.current)?void 0:e.currentState)?void 0:n.value)===r)try{const t=yield this.handler(r,o.controllers);void 0!==t&&o.controllers.flow.transition(t)}catch(i){i instanceof Error&&o.fail(i)}},new Promise(((t,r)=>{var i=t=>{try{u(o.next(t))}catch(e){r(e)}},l=t=>{try{u(o.throw(t))}catch(e){r(e)}},u=e=>e.done?t(e.value):Promise.resolve(e.value).then(i,l);u((o=o.apply(e,n)).next())}));var e,n,o}),0)}))}))}))}}}])}));
1
+ !function(t,e){"object"===typeof exports&&"object"===typeof module?module.exports=e():"function"===typeof define&&define.amd?define([],e):"object"===typeof exports?exports.ExternalActionPlugin=e():t.ExternalActionPlugin=e()}(this,(function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"===typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";n.r(e),n.d(e,"ExternalActionPlugin",(function(){return r}));class r{constructor(t){this.name="ExternalActionPlugin",this.handler=t}apply(t){t.hooks.flowController.tap(this.name,(e=>{e.hooks.flow.tap(this.name,(e=>{e.hooks.transition.tap(this.name,((e,n)=>{const{value:r}=n;"EXTERNAL"===r.state_type&&setTimeout((()=>{return e=this,o=null,i=function*(){const e=t=>{var e,n;return"in-progress"===t.status&&(null==(n=null==(e=t.controllers.flow.current)?void 0:e.currentState)?void 0:n.value)===r},o=t.getState();if(e(o))try{const i=yield this.handler(r,o.controllers);if(void 0!==i){const r=t.getState();e(r)?r.controllers.flow.transition(i):t.logger.warn(`External state resolved with [${i}], but Player already navigated away from [${n.name}]`)}}catch(i){i instanceof Error&&o.fail(i)}},new Promise(((t,n)=>{var r=t=>{try{a(i.next(t))}catch(e){n(e)}},l=t=>{try{a(i.throw(t))}catch(e){n(e)}},a=e=>e.done?t(e.value):Promise.resolve(e.value).then(r,l);a((i=i.apply(e,o)).next())}));var e,o,i}),0)}))}))}))}}}])}));
package/dist/index.cjs.js CHANGED
@@ -34,13 +34,21 @@ class ExternalActionPlugin {
34
34
  const { value: state } = toState;
35
35
  if (state.state_type === "EXTERNAL") {
36
36
  setTimeout(() => __async(this, null, function* () {
37
- var _a, _b;
37
+ const shouldTransition = (currentState2) => {
38
+ var _a, _b;
39
+ return currentState2.status === "in-progress" && ((_b = (_a = currentState2.controllers.flow.current) == null ? void 0 : _a.currentState) == null ? void 0 : _b.value) === state;
40
+ };
38
41
  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) {
42
+ if (shouldTransition(currentState)) {
40
43
  try {
41
44
  const transitionValue = yield this.handler(state, currentState.controllers);
42
45
  if (transitionValue !== void 0) {
43
- currentState.controllers.flow.transition(transitionValue);
46
+ const latestState = player.getState();
47
+ if (shouldTransition(latestState)) {
48
+ latestState.controllers.flow.transition(transitionValue);
49
+ } else {
50
+ player.logger.warn(`External state resolved with [${transitionValue}], but Player already navigated away from [${toState.name}]`);
51
+ }
44
52
  }
45
53
  } catch (error) {
46
54
  if (error instanceof Error) {
package/dist/index.esm.js CHANGED
@@ -30,13 +30,21 @@ class ExternalActionPlugin {
30
30
  const { value: state } = toState;
31
31
  if (state.state_type === "EXTERNAL") {
32
32
  setTimeout(() => __async(this, null, function* () {
33
- var _a, _b;
33
+ const shouldTransition = (currentState2) => {
34
+ var _a, _b;
35
+ return currentState2.status === "in-progress" && ((_b = (_a = currentState2.controllers.flow.current) == null ? void 0 : _a.currentState) == null ? void 0 : _b.value) === state;
36
+ };
34
37
  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) {
38
+ if (shouldTransition(currentState)) {
36
39
  try {
37
40
  const transitionValue = yield this.handler(state, currentState.controllers);
38
41
  if (transitionValue !== void 0) {
39
- currentState.controllers.flow.transition(transitionValue);
42
+ const latestState = player.getState();
43
+ if (shouldTransition(latestState)) {
44
+ latestState.controllers.flow.transition(transitionValue);
45
+ } else {
46
+ player.logger.warn(`External state resolved with [${transitionValue}], but Player already navigated away from [${toState.name}]`);
47
+ }
40
48
  }
41
49
  } catch (error) {
42
50
  if (error instanceof Error) {
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@player-ui/external-action-plugin",
3
- "version": "0.4.0",
3
+ "version": "0.4.1-next.1",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org"
7
7
  },
8
8
  "peerDependencies": {
9
- "@player-ui/player": "0.4.0"
9
+ "@player-ui/player": "0.4.1-next.1"
10
10
  },
11
11
  "dependencies": {
12
12
  "@babel/runtime": "7.15.4"
@@ -52,6 +52,14 @@
52
52
  {
53
53
  "name": "Kelly Harrop",
54
54
  "url": "https://github.com/kharrop"
55
+ },
56
+ {
57
+ "name": "Alejandro Fimbres",
58
+ "url": "https://github.com/lexfm"
59
+ },
60
+ {
61
+ "name": "Rafael Campos",
62
+ "url": "https://github.com/rafbcampos"
55
63
  }
56
64
  ],
57
65
  "bundle": "./dist/external-action-plugin.prod.js"
package/src/index.ts CHANGED
@@ -1,4 +1,9 @@
1
- import type { Player, PlayerPlugin, InProgressState } from '@player-ui/player';
1
+ import type {
2
+ Player,
3
+ PlayerPlugin,
4
+ InProgressState,
5
+ PlayerFlowState,
6
+ } from '@player-ui/player';
2
7
  import type { NavigationFlowExternalState } from '@player-ui/types';
3
8
 
4
9
  export type ExternalStateHandler = (
@@ -22,16 +27,18 @@ export class ExternalActionPlugin implements PlayerPlugin {
22
27
  flowController.hooks.flow.tap(this.name, (flow) => {
23
28
  flow.hooks.transition.tap(this.name, (fromState, toState) => {
24
29
  const { value: state } = toState;
25
-
26
30
  if (state.state_type === 'EXTERNAL') {
27
31
  setTimeout(async () => {
28
- const currentState = player.getState();
29
-
30
- if (
32
+ /** Helper for ensuring state is still current relative to external state this is handling */
33
+ const shouldTransition = (
34
+ currentState: PlayerFlowState
35
+ ): currentState is InProgressState =>
31
36
  currentState.status === 'in-progress' &&
32
37
  currentState.controllers.flow.current?.currentState?.value ===
33
- state
34
- ) {
38
+ state;
39
+
40
+ const currentState = player.getState();
41
+ if (shouldTransition(currentState)) {
35
42
  try {
36
43
  const transitionValue = await this.handler(
37
44
  state,
@@ -39,7 +46,15 @@ export class ExternalActionPlugin implements PlayerPlugin {
39
46
  );
40
47
 
41
48
  if (transitionValue !== undefined) {
42
- currentState.controllers.flow.transition(transitionValue);
49
+ // Ensure the Player is still in the same state after waiting for transitionValue
50
+ const latestState = player.getState();
51
+ if (shouldTransition(latestState)) {
52
+ latestState.controllers.flow.transition(transitionValue);
53
+ } else {
54
+ player.logger.warn(
55
+ `External state resolved with [${transitionValue}], but Player already navigated away from [${toState.name}]`
56
+ );
57
+ }
43
58
  }
44
59
  } catch (error) {
45
60
  if (error instanceof Error) {