xstate 5.0.0-beta.9 → 5.0.0

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.
Files changed (107) hide show
  1. package/README.md +10 -8
  2. package/actions/dist/xstate-actions.cjs.js +19 -5
  3. package/actions/dist/xstate-actions.cjs.mjs +3 -15
  4. package/actions/dist/xstate-actions.development.cjs.js +21 -0
  5. package/actions/dist/xstate-actions.development.cjs.mjs +13 -0
  6. package/actions/dist/xstate-actions.development.esm.js +3 -0
  7. package/actions/dist/xstate-actions.esm.js +3 -2
  8. package/actions/dist/xstate-actions.umd.min.js +1 -1
  9. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  10. package/actors/dist/xstate-actors.cjs.js +621 -4
  11. package/actors/dist/xstate-actors.cjs.mjs +1 -8
  12. package/actors/dist/xstate-actors.development.cjs.js +624 -0
  13. package/actors/dist/xstate-actors.development.cjs.mjs +8 -0
  14. package/actors/dist/xstate-actors.development.esm.js +615 -0
  15. package/actors/dist/xstate-actors.esm.js +615 -2
  16. package/actors/dist/xstate-actors.umd.min.js +1 -1
  17. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  18. package/dev/dist/xstate-dev.cjs.js +45 -4
  19. package/{dist/index-0f3fdf0c.cjs.prod.js → dev/dist/xstate-dev.development.cjs.js} +6 -7
  20. package/dev/dist/xstate-dev.development.cjs.mjs +5 -0
  21. package/{dist/index-50bd0aff.esm.js → dev/dist/xstate-dev.development.esm.js} +6 -8
  22. package/dev/dist/xstate-dev.esm.js +42 -1
  23. package/dev/dist/xstate-dev.umd.min.js +1 -1
  24. package/dev/dist/xstate-dev.umd.min.js.map +1 -1
  25. package/dist/declarations/src/SimulatedClock.d.ts +1 -1
  26. package/dist/declarations/src/State.d.ts +47 -73
  27. package/dist/declarations/src/StateMachine.d.ts +29 -57
  28. package/dist/declarations/src/StateNode.d.ts +36 -33
  29. package/dist/declarations/src/actions/assign.d.ts +11 -2
  30. package/dist/declarations/src/actions/cancel.d.ts +7 -3
  31. package/dist/declarations/src/actions/enqueueActions.d.ts +32 -0
  32. package/dist/declarations/src/actions/log.d.ts +7 -3
  33. package/dist/declarations/src/actions/raise.d.ts +7 -2
  34. package/dist/declarations/src/actions/send.d.ts +14 -36
  35. package/dist/declarations/src/actions/spawnChild.d.ts +29 -0
  36. package/dist/declarations/src/actions/stopChild.d.ts +18 -0
  37. package/dist/declarations/src/actions.d.ts +8 -48
  38. package/dist/declarations/src/actors/callback.d.ts +91 -8
  39. package/dist/declarations/src/actors/index.d.ts +6 -28
  40. package/dist/declarations/src/actors/observable.d.ts +101 -18
  41. package/dist/declarations/src/actors/promise.d.ts +80 -10
  42. package/dist/declarations/src/actors/transition.d.ts +64 -9
  43. package/dist/declarations/src/constants.d.ts +3 -0
  44. package/dist/declarations/src/createMachine.d.ts +20 -0
  45. package/dist/declarations/src/dev/index.d.ts +6 -6
  46. package/dist/declarations/src/guards.d.ts +41 -8
  47. package/dist/declarations/src/index.d.ts +18 -23
  48. package/dist/declarations/src/interpreter.d.ts +149 -41
  49. package/dist/declarations/src/setup.d.ts +51 -0
  50. package/dist/declarations/src/spawn.d.ts +23 -2
  51. package/dist/declarations/src/stateUtils.d.ts +30 -45
  52. package/dist/declarations/src/system.d.ts +26 -2
  53. package/dist/declarations/src/typegenTypes.d.ts +34 -22
  54. package/dist/declarations/src/types.d.ts +527 -669
  55. package/dist/declarations/src/utils.d.ts +15 -52
  56. package/dist/declarations/src/waitFor.d.ts +2 -2
  57. package/dist/log-22e678c5.esm.js +364 -0
  58. package/dist/log-5e226275.cjs.js +372 -0
  59. package/dist/log-641cd926.development.cjs.js +394 -0
  60. package/dist/log-f196f85f.development.esm.js +386 -0
  61. package/dist/raise-34e25c2c.cjs.js +2368 -0
  62. package/dist/raise-62704519.development.cjs.js +2422 -0
  63. package/dist/raise-89c581c4.development.esm.js +2371 -0
  64. package/dist/raise-8bc422d1.esm.js +2317 -0
  65. package/dist/xstate.cjs.js +728 -4
  66. package/dist/xstate.cjs.mjs +9 -9
  67. package/dist/xstate.development.cjs.js +737 -0
  68. package/dist/xstate.development.cjs.mjs +39 -0
  69. package/dist/xstate.development.esm.js +699 -0
  70. package/dist/xstate.esm.js +561 -770
  71. package/dist/xstate.umd.min.js +1 -1
  72. package/dist/xstate.umd.min.js.map +1 -1
  73. package/guards/dist/xstate-guards.cjs.js +12 -5
  74. package/guards/dist/xstate-guards.cjs.mjs +1 -2
  75. package/guards/dist/xstate-guards.development.cjs.js +14 -0
  76. package/guards/dist/xstate-guards.development.cjs.mjs +7 -0
  77. package/guards/dist/xstate-guards.development.esm.js +2 -0
  78. package/guards/dist/xstate-guards.esm.js +2 -2
  79. package/guards/dist/xstate-guards.umd.min.js +1 -1
  80. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  81. package/package.json +53 -1
  82. package/actions/dist/xstate-actions.cjs.dev.js +0 -32
  83. package/actions/dist/xstate-actions.cjs.prod.js +0 -32
  84. package/actions/dynamicAction.ts +0 -42
  85. package/actors/dist/xstate-actors.cjs.dev.js +0 -22
  86. package/actors/dist/xstate-actors.cjs.prod.js +0 -22
  87. package/dev/dist/xstate-dev.cjs.dev.js +0 -11
  88. package/dev/dist/xstate-dev.cjs.prod.js +0 -11
  89. package/dist/actions-b6357569.cjs.dev.js +0 -4437
  90. package/dist/actions-bd4a184d.cjs.prod.js +0 -4423
  91. package/dist/actions-de434a04.esm.js +0 -4348
  92. package/dist/declarations/actions/dynamicAction.d.ts +0 -5
  93. package/dist/declarations/src/Machine.d.ts +0 -4
  94. package/dist/declarations/src/Mailbox.d.ts +0 -12
  95. package/dist/declarations/src/actionTypes.d.ts +0 -16
  96. package/dist/declarations/src/actions/choose.d.ts +0 -3
  97. package/dist/declarations/src/actions/invoke.d.ts +0 -3
  98. package/dist/declarations/src/actions/pure.d.ts +0 -6
  99. package/dist/declarations/src/actions/stop.d.ts +0 -7
  100. package/dist/declarations/src/environment.d.ts +0 -1
  101. package/dist/declarations/src/mapState.d.ts +0 -3
  102. package/dist/declarations/src/memo.d.ts +0 -2
  103. package/dist/index-ebaab3c9.cjs.dev.js +0 -52
  104. package/dist/xstate.cjs.dev.js +0 -950
  105. package/dist/xstate.cjs.prod.js +0 -947
  106. package/guards/dist/xstate-guards.cjs.dev.js +0 -15
  107. package/guards/dist/xstate-guards.cjs.prod.js +0 -15
package/README.md CHANGED
@@ -16,12 +16,11 @@
16
16
  JavaScript and TypeScript [finite state machines](https://en.wikipedia.org/wiki/Finite-state_machine) and [statecharts](https://www.sciencedirect.com/science/article/pii/0167642387900359/pdf) for the modern web.
17
17
 
18
18
  📖 [Read the documentation](https://xstate.js.org/docs)
19
- 📑 Adheres to the [SCXML specification](https://www.w3.org/TR/scxml/).
19
+ 📑 Inspired by the [SCXML specification](https://www.w3.org/TR/scxml/).
20
20
 
21
21
  ## Packages
22
22
 
23
23
  - 🤖 `xstate` - Core finite state machine and statecharts library + interpreter
24
- - [🔬 `@xstate/fsm`](https://github.com/statelyai/xstate/tree/main/packages/xstate-fsm) - Minimal finite state machine library
25
24
  - [📉 `@xstate/graph`](https://github.com/statelyai/xstate/tree/main/packages/xstate-graph) - Graph traversal utilities for XState
26
25
  - [⚛️ `@xstate/react`](https://github.com/statelyai/xstate/tree/main/packages/xstate-react) - React hooks and utilities for using XState in React applications
27
26
  - [✅ `@xstate/test`](https://github.com/statelyai/xstate/tree/main/packages/xstate-test) - Model-based testing utilities for XState
@@ -117,8 +116,9 @@ const lightMachine = createMachine({
117
116
 
118
117
  const currentState = 'green';
119
118
 
120
- const nextState = lightMachine.transition(currentState, { type: 'TIMER' })
121
- .value;
119
+ const nextState = lightMachine.transition(currentState, {
120
+ type: 'TIMER'
121
+ }).value;
122
122
 
123
123
  // => 'yellow'
124
124
  ```
@@ -172,8 +172,9 @@ const lightMachine = createMachine({
172
172
 
173
173
  const currentState = 'yellow';
174
174
 
175
- const nextState = lightMachine.transition(currentState, { type: 'TIMER' })
176
- .value;
175
+ const nextState = lightMachine.transition(currentState, {
176
+ type: 'TIMER'
177
+ }).value;
177
178
  // => {
178
179
  // red: 'walk'
179
180
  // }
@@ -263,8 +264,9 @@ const wordMachine = createMachine({
263
264
  }
264
265
  });
265
266
 
266
- const boldState = wordMachine.transition('bold.off', { type: 'TOGGLE_BOLD' })
267
- .value;
267
+ const boldState = wordMachine.transition('bold.off', {
268
+ type: 'TOGGLE_BOLD'
269
+ }).value;
268
270
 
269
271
  // {
270
272
  // bold: 'on',
@@ -1,7 +1,21 @@
1
1
  'use strict';
2
2
 
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./xstate-actions.cjs.prod.js");
5
- } else {
6
- module.exports = require("./xstate-actions.cjs.dev.js");
7
- }
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var log = require('../../dist/log-5e226275.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-34e25c2c.cjs.js');
7
+ require('../../dev/dist/xstate-dev.cjs.js');
8
+
9
+
10
+
11
+ exports.assign = log.assign;
12
+ exports.enqueueActions = log.enqueueActions;
13
+ exports.forwardTo = log.forwardTo;
14
+ exports.log = log.log;
15
+ exports.sendParent = log.sendParent;
16
+ exports.sendTo = log.sendTo;
17
+ exports.cancel = guards_dist_xstateGuards.cancel;
18
+ exports.raise = guards_dist_xstateGuards.raise;
19
+ exports.spawnChild = guards_dist_xstateGuards.spawnChild;
20
+ exports.stop = guards_dist_xstateGuards.stop;
21
+ exports.stopChild = guards_dist_xstateGuards.stopChild;
@@ -1,25 +1,13 @@
1
1
  export {
2
- actionTypes,
3
- after,
4
2
  assign,
5
3
  cancel,
6
- choose,
7
- createInitEvent,
8
- done,
9
- doneInvoke,
10
- error,
11
- escalate,
4
+ enqueueActions,
12
5
  forwardTo,
13
- initEvent,
14
6
  log,
15
- pure,
16
7
  raise,
17
- resolveActionObject,
18
- respond,
19
- send,
20
8
  sendParent,
21
9
  sendTo,
10
+ spawnChild,
22
11
  stop,
23
- toActionObject,
24
- toActionObjects
12
+ stopChild
25
13
  } from "./xstate-actions.cjs.js";
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var log = require('../../dist/log-641cd926.development.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-62704519.development.cjs.js');
7
+ require('../../dev/dist/xstate-dev.development.cjs.js');
8
+
9
+
10
+
11
+ exports.assign = log.assign;
12
+ exports.enqueueActions = log.enqueueActions;
13
+ exports.forwardTo = log.forwardTo;
14
+ exports.log = log.log;
15
+ exports.sendParent = log.sendParent;
16
+ exports.sendTo = log.sendTo;
17
+ exports.cancel = guards_dist_xstateGuards.cancel;
18
+ exports.raise = guards_dist_xstateGuards.raise;
19
+ exports.spawnChild = guards_dist_xstateGuards.spawnChild;
20
+ exports.stop = guards_dist_xstateGuards.stop;
21
+ exports.stopChild = guards_dist_xstateGuards.stopChild;
@@ -0,0 +1,13 @@
1
+ export {
2
+ assign,
3
+ cancel,
4
+ enqueueActions,
5
+ forwardTo,
6
+ log,
7
+ raise,
8
+ sendParent,
9
+ sendTo,
10
+ spawnChild,
11
+ stop,
12
+ stopChild
13
+ } from "./xstate-actions.development.cjs.js";
@@ -0,0 +1,3 @@
1
+ export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-f196f85f.development.esm.js';
2
+ export { L as cancel, M as raise, O as spawnChild, P as stop, Q as stopChild } from '../../dist/raise-89c581c4.development.esm.js';
3
+ import '../../dev/dist/xstate-dev.development.esm.js';
@@ -1,2 +1,3 @@
1
- export { at as actionTypes, aw as after, a0 as assign, a1 as cancel, a2 as choose, x as createInitEvent, ax as done, $ as doneInvoke, K as error, aA as escalate, X as forwardTo, T as initEvent, a3 as log, a4 as pure, a5 as raise, au as resolveActionObject, az as respond, ay as send, W as sendParent, V as sendTo, a6 as stop, av as toActionObject, j as toActionObjects } from '../../dist/actions-de434a04.esm.js';
2
- import '../../dist/index-50bd0aff.esm.js';
1
+ export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-22e678c5.esm.js';
2
+ export { L as cancel, M as raise, O as spawnChild, P as stop, Q as stopChild } from '../../dist/raise-8bc422d1.esm.js';
3
+ import '../../dev/dist/xstate-dev.esm.js';
@@ -1,2 +1,2 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActions={})}(this,(function(t){"use strict";var e,n;!function(t){t.Stop="xstate.stop",t.Raise="xstate.raise",t.Send="xstate.send",t.Cancel="xstate.cancel",t.Assign="xstate.assign",t.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.Log="xstate.log",t.Init="xstate.init",t.Invoke="xstate.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t.Pure="xstate.pure",t.Choose="xstate.choose"}(e||(e={})),function(t){t.Parent="#_parent",t.Internal="#_internal"}(n||(n={}));var r=e.Stop,i=e.Raise,o=e.Send,a=e.Cancel,s=e.Assign,u=e.After,c=e.DoneState,l=e.Log,f=e.Init,d=e.Invoke,v=e.ErrorExecution,h=e.ErrorPlatform,p=e.ErrorCustom,y=e.Choose,m=e.Pure,g=Object.freeze({__proto__:null,stop:r,raise:i,send:o,cancel:a,assign:s,after:u,doneState:c,log:l,init:f,invoke:d,errorExecution:v,errorPlatform:h,error:p,choose:y,pure:m});function b(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}function _(t,e,n){return(e=b(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function x(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function w(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?x(Object(n),!0).forEach((function(e){_(t,e,n[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):x(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}function S(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function k(t,e){if(t){if("string"==typeof t)return S(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?S(t,e):void 0}}function E(t){return function(t){if(Array.isArray(t))return S(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||k(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function O(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=k(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0,i=function(){};return{s:i,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,s=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return a=t.done,t},e:function(t){s=!0,o=t},f:function(){try{a||null==n.return||n.return()}finally{if(s)throw o}}}}function j(t){return j="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},j(t)}var I=!0;function P(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:".",r=T(t,n),i=T(e,n);return $(i)?!!$(r)&&i===r:$(r)?r in i:Object.keys(r).every((function(t){return t in i&&P(r[t],i[t])}))}function T(t,e){if("object"===j(n=t)&&"value"in n&&"context"in n&&"event"in n&&"_event"in n)return t.value;var n;if(N(t))return A(t);if("string"!=typeof t)return t;var r=function(t,e){try{return N(t)?t:t.toString().split(e)}catch(e){throw new Error("'".concat(t,"' is not a valid state path."))}}(t,e);return A(r)}function A(t){if(1===t.length)return t[0];for(var e={},n=e,r=0;r<t.length-1;r++)r===t.length-2?n[t[r]]=t[r+1]:(n[t[r]]={},n=n[t[r]]);return e}function C(t){var e;return(e=[]).concat.apply(e,E(t))}function D(t){return void 0===t?[]:function(t){return N(t)?t:[t]}(t)}var M=function(){};function N(t){return Array.isArray(t)}function R(t){return"function"==typeof t}function $(t){return"string"==typeof t}function q(t,e){return function(t){return"$$type"in t&&"scxml"===t.$$type}(t)?t:w({name:t.type,data:t,$$type:"scxml",type:"external"},e)}function W(t){return t?"transition"in t?{src:t,input:void 0}:t:void 0}function z(t,e){return{type:t.type,params:t.params,resolve:e}}function B(t){return"object"===j(t)&&null!==t&&"resolve"in t}function J(t,e){return z({type:o,params:{to:e?e.to:void 0,delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:R(t)?t.name:t.type}},(function(r,i){var a,s=i.actorContext,u=i.state,c={to:e?e.to:void 0,delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:R(t)?t.name:t.type},l={context:u.context,event:r.data,_event:r,self:null!==(a=null==s?void 0:s.self)&&void 0!==a?a:null,system:null==s?void 0:s.system},f=u.machine.options.delays;if("string"==typeof t)throw new Error('Only event objects may be used with sendTo; use sendTo({ type: "'.concat(t,'" }) instead'));var d,v=q(R(t)?t(l):t);if($(c.delay)){var h=f&&f[c.delay];d=R(h)?h(l):h}else d=R(c.delay)?c.delay(l):c.delay;var y,m=R(c.to)?c.to(l):c.to;if("string"==typeof m){if(!(y=m===n.Parent?null==s?void 0:s.self._parent:m===n.Internal?null==s?void 0:s.self:m.startsWith("#_")?u.children[m.slice(2)]:u.children[m]))throw new Error("Unable to send event to actor '".concat(m,"' from machine '").concat(u.machine.id,"'."))}else y=m||(null==s?void 0:s.self);var g={type:o,params:w(w({},c),{},{to:y,_event:v,event:v.data,delay:d,internal:m===n.Internal}),execute:function(t){var e=g;if("number"!=typeof e.params.delay){var n=e.params.to,r=e.params._event;t.defer((function(){var e=t.self,i=w(w({},r),{},{name:r.name===p?"".concat(jt(e.id)):r.name,origin:e});n.send(i)}))}else t.self.delaySend(e)}};return[u,g]}))}function Q(t,e){return J(t,w(w({},e),{},{to:n.Parent}))}function U(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function V(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,b(r.key),r)}}function L(t,e,n){return e&&V(t.prototype,e),n&&V(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}I||(M=function(t,e){var n=t instanceof Error?t:void 0;if((n||!t)&&void 0!==console){var r=["Warning: ".concat(e)];n&&r.push(n),console.warn.apply(console,r)}});var G=function(){function t(e){U(this,t),this._process=e,_(this,"_active",!1),_(this,"_current",null),_(this,"_last",null)}return L(t,[{key:"start",value:function(){this._active=!0,this.flush()}},{key:"clear",value:function(){this._current&&(this._current.next=null,this._last=this._current)}},{key:"prepend",value:function(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}},{key:"enqueue",value:function(t){var e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}},{key:"flush",value:function(){for(;this._current;){var t=this._current;this._process(t.value),t===this._current&&(this._current=this._current.next)}this._last=null}}]),t}(),X="function"==typeof Symbol&&Symbol.observable||"@@observable",F="xstate.stop";function H(){var t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}var K,Y=function(t){var e=H();e&&e.register(t)};!function(t){t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped"}(K||(K={}));var Z={deferEvents:!0,clock:{setTimeout:function(t){function e(e,n){return t.apply(this,arguments)}return e.toString=function(){return t.toString()},e}((function(t,e){return setTimeout(t,e)})),clearTimeout:function(t){function e(e){return t.apply(this,arguments)}return e.toString=function(){return t.toString()},e}((function(t){return clearTimeout(t)}))},logger:console.log.bind(console),devTools:!1},tt=function(){function t(e,n){var r,i=this;U(this,t),this.behavior=e,_(this,"_state",void 0),_(this,"clock",void 0),_(this,"options",void 0),_(this,"id",void 0),_(this,"mailbox",new G(this._process.bind(this))),_(this,"delayedEventsMap",{}),_(this,"observers",new Set),_(this,"logger",void 0),_(this,"status",K.NotStarted),_(this,"_parent",void 0),_(this,"ref",void 0),_(this,"_actorContext",void 0),_(this,"_systemId",void 0),_(this,"sessionId",void 0),_(this,"system",void 0),_(this,"_doneEvent",void 0),_(this,"src",void 0),_(this,"_deferred",[]);var o,a,s,u,c=w(w({},Z),n),l=c.clock,f=c.logger,d=c.parent,v=c.id,h=c.systemId;this.system=null!==(r=null==d?void 0:d.system)&&void 0!==r?r:(o=0,a=new Map,s=new Map,u=new WeakMap,{_bookId:function(){return"x:".concat(o++)},_register:function(t,e){return a.set(t,e),t},_unregister:function(t){a.delete(t.sessionId);var e=u.get(t);void 0!==e&&(s.delete(e),u.delete(t))},get:function(t){return s.get(t)},_set:function(t,e){var n=s.get(t);if(n&&n!==e)throw new Error("Actor with system ID '".concat(t,"' already exists."));s.set(t,e),u.set(e,t)}}),h&&(this._systemId=h,this.system._set(h,this)),this.sessionId=this.system._bookId(),this.id=null!=v?v:this.sessionId,this.logger=f,this.clock=l,this._parent=d,this.options=c,this.src=c.src,this.ref=this,this._actorContext={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:function(t){i._deferred.push(t)},system:this.system,stopChild:function(t){if(t._parent!==i)throw new Error("Cannot stop child actor ".concat(t.id," of ").concat(i.id," because it is not a child"));t._stop()}},this.send=this.send.bind(this),this._initState()}return L(t,[{key:"_initState",value:function(){var t;this._state=this.options.state?this.behavior.restoreState?this.behavior.restoreState(this.options.state,this._actorContext):this.options.state:this.behavior.getInitialState(this._actorContext,null===(t=this.options)||void 0===t?void 0:t.input)}},{key:"update",value:function(t){var e,n,r,i;this._state=t;for(var o,a=this.getSnapshot();o=this._deferred.shift();)o(t);var s,u=O(this.observers);try{for(u.s();!(s=u.n()).done;){var c,l=s.value;null===(c=l.next)||void 0===c||c.call(l,a)}}catch(t){u.e(t)}finally{u.f()}var f=null===(e=(n=this.behavior).getStatus)||void 0===e?void 0:e.call(n,t);switch(null==f?void 0:f.status){case"done":this._stopProcedure(),this._doneEvent=Ot(this.id,f.data),null===(r=this._parent)||void 0===r||r.send(q(this._doneEvent,{origin:this,invokeid:this.id})),this._complete();break;case"error":this._stopProcedure(),null===(i=this._parent)||void 0===i||i.send(q(jt(this.id,f.data),{origin:this})),this._error(f.data)}}},{key:"subscribe",value:function(t,e,n){var r,i=this,o=function(t,e,n){var r=function(){},i="object"===j(t),o=i?t:null;return{next:((i?t.next:t)||r).bind(o),error:((i?t.error:e)||r).bind(o),complete:((i?t.complete:n)||r).bind(o)}}(t,e,n);(this.observers.add(o),this.status===K.Stopped)&&(null===(r=o.complete)||void 0===r||r.call(o),this.observers.delete(o));return{unsubscribe:function(){i.observers.delete(o)}}}},{key:"onDone",value:function(t){var e=this;return this.status===K.Stopped&&this._doneEvent?t(this._doneEvent):this.observers.add({complete:function(){e._doneEvent&&t(e._doneEvent)}}),this}},{key:"start",value:function(){return this.status===K.Running||(this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=K.Running,this.behavior.start&&this.behavior.start(this._state,this._actorContext),this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start()),this}},{key:"_process",value:function(t){try{var e=this.behavior.transition(this._state,t,this._actorContext);this.update(e),t.name===F&&(this._stopProcedure(),this._complete())}catch(t){if(!(this.observers.size>0))throw t;this.observers.forEach((function(e){var n;null===(n=e.error)||void 0===n||n.call(e,t)})),this.stop()}}},{key:"_stop",value:function(){return this.status===K.Stopped?this:(this.mailbox.clear(),this.status===K.NotStarted?(this.status=K.Stopped,this):(this.mailbox.enqueue(q({type:F})),this))}},{key:"stop",value:function(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}},{key:"_complete",value:function(){var t,e=O(this.observers);try{for(e.s();!(t=e.n()).done;){var n,r=t.value;null===(n=r.complete)||void 0===n||n.call(r)}}catch(t){e.e(t)}finally{e.f()}this.observers.clear()}},{key:"_error",value:function(t){var e,n=O(this.observers);try{for(n.s();!(e=n.n()).done;){var r,i=e.value;null===(r=i.error)||void 0===r||r.call(i,t)}}catch(t){n.e(t)}finally{n.f()}this.observers.clear()}},{key:"_stopProcedure",value:function(){if(this.status!==K.Running)return this;for(var t=0,e=Object.keys(this.delayedEventsMap);t<e.length;t++){var n=e[t];this.clock.clearTimeout(this.delayedEventsMap[n])}return this.mailbox.clear(),this.mailbox=new G(this._process.bind(this)),this.status=K.Stopped,this.system._unregister(this),this}},{key:"send",value:function(t){if("string"==typeof t)throw new Error('Only event objects may be sent to actors; use .send({ type: "'.concat(t,'" }) instead'));var e=q(t);if(this.status!==K.Stopped){if(this.status!==K.Running&&!this.options.deferEvents)throw new Error('Event "'.concat(e.name,'" was sent to uninitialized actor "').concat(this.id,'". Make sure .start() is called for this actor, or set { deferEvents: true } in the actor options.\nEvent: ').concat(JSON.stringify(e.data)));this.mailbox.enqueue(e)}}},{key:"delaySend",value:function(t){var e=this;this.delayedEventsMap[t.params.id]=this.clock.setTimeout((function(){"to"in t.params&&t.params.to?t.params.to.send(t.params._event):e.send(t.params._event)}),t.params.delay)}},{key:"cancel",value:function(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}},{key:"attachDevTools",value:function(){var t=this.options.devTools;t&&("function"==typeof t?t:Y)(this)}},{key:"toJSON",value:function(){return{id:this.id}}},{key:"getPersistedState",value:function(){var t,e;return null===(t=(e=this.behavior).getPersistedState)||void 0===t?void 0:t.call(e,this._state)}},{key:X,value:function(){return this}},{key:"getSnapshot",value:function(){return this.behavior.getSnapshot?this.behavior.getSnapshot(this._state):this._state}}]),t}();function et(t,e){return new tt(t,e)}var nt=function(t){return{context:t.context,event:t.event}};function rt(t,e){if(null==t)return{};var n,r,i=function(t,e){if(null==t)return{};var n,r,i={},o=Object.keys(t);for(r=0;r<o.length;r++)n=o[r],e.indexOf(n)>=0||(i[n]=t[n]);return i}(t,e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);for(r=0;r<o.length;r++)n=o[r],e.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(t,n)&&(i[n]=t[n])}return i}var it=new WeakMap;function ot(t,e,n,r){var i,o,a,s=r.machine,u=null!==(i=null==s||null===(o=s.options)||void 0===o||null===(a=o.guards)||void 0===a?void 0:a[t.type])&&void 0!==i?i:t.predicate;if(!u)throw new Error("Guard '".concat(t.type,"' is not implemented.'."));return u({context:e,event:n.data,state:r,guard:t,_event:n,evaluate:ot})}function at(t,e){var n;return $(t)?{type:t,predicate:(null==e?void 0:e(t))||void 0,params:{type:t}}:R(t)?{type:t.name,predicate:t,params:{type:t.name,name:t.name}}:{type:t.type,params:t.params||t,children:null===(n=t.children)||void 0===n?void 0:n.map((function(t){return at(t,e)})),predicate:(null==e?void 0:e(t.type))||t.predicate}}var st=function(t){return"atomic"===t.type||"final"===t.type};function ut(t){return Object.values(t.states).filter((function(t){return"history"!==t.type}))}function ct(t,e){for(var n=[],r=t.parent;r&&r!==e;)n.push(r),r=r.parent;return n}function lt(t){var e,n=new Set(t),r=new Set(t),i=dt(r),o=O(n);try{for(o.s();!(e=o.n()).done;){var a=e.value;if("compound"!==a.type||i.get(a)&&i.get(a).length){if("parallel"===a.type){var s,u=O(ut(a));try{for(u.s();!(s=u.n()).done;){var c=s.value;if("history"!==c.type&&!r.has(c))for(var l=0,f=pt(c);l<f.length;l++){var d=f[l];r.add(d)}}}catch(t){u.e(t)}finally{u.f()}}}else pt(a).forEach((function(t){return r.add(t)}))}}catch(t){o.e(t)}finally{o.f()}var v,h=O(r);try{for(h.s();!(v=h.n()).done;)for(var p=v.value.parent;p;)r.add(p),p=p.parent}catch(t){h.e(t)}finally{h.f()}return r}function ft(t,e){var n=e.get(t);if(!n)return{};if("compound"===t.type){var r=n[0];if(!r)return{};if(st(r))return r.key}var i,o={},a=O(n);try{for(a.s();!(i=a.n()).done;){var s=i.value;o[s.key]=ft(s,e)}}catch(t){a.e(t)}finally{a.f()}return o}function dt(t){var e,n=new Map,r=O(t);try{for(r.s();!(e=r.n()).done;){var i=e.value;n.has(i)||n.set(i,[]),i.parent&&(n.has(i.parent)||n.set(i.parent,[]),n.get(i.parent).push(i))}}catch(t){r.e(t)}finally{r.f()}return n}function vt(t,e){return ft(t,dt(lt(e)))}var ht=function(t){return"#"===t[0]};function pt(t){var e=new Set;return function n(r){if(!e.has(r))if(e.add(r),"compound"===r.type){var i,o=O(r.initial.target);try{for(o.s();!(i=o.n()).done;){var a,s=i.value,u=O(ct(s,t));try{for(u.s();!(a=u.n()).done;){var c=a.value;e.add(c)}}catch(t){u.e(t)}finally{u.f()}n(s)}}catch(t){o.e(t)}finally{o.f()}}else if("parallel"===r.type){var l,f=O(ut(r));try{for(f.s();!(l=f.n()).done;){n(l.value)}}catch(t){f.e(t)}finally{f.f()}}}(t),E(e)}function yt(t,e){if(ht(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error("Unable to retrieve child state '".concat(e,"' from '").concat(t.id,"'; no child states exist."));var n=t.states[e];if(!n)throw new Error("Child state '".concat(e,"' does not exist on '").concat(t.id,"'"));return n}function mt(t,e){var n=e instanceof bt?e.value:T(e,t.machine.delimiter);if($(n))return[t,t.states[n]];var r=Object.keys(n),i=r.map((function(e){return yt(t,e)})).filter(Boolean);return[t.machine.root,t].concat(i,r.reduce((function(e,r){var i=yt(t,r);if(!i)return e;var o=mt(i,n[r]);return e.concat(o)}),[]))}var gt=["configuration","transitions","tags","machine"],bt=function(){function t(e,n){var r,i,o,a;U(this,t),this.machine=n,_(this,"tags",void 0),_(this,"value",void 0),_(this,"done",void 0),_(this,"output",void 0),_(this,"context",void 0),_(this,"historyValue",{}),_(this,"actions",[]),_(this,"event",void 0),_(this,"_internalQueue",void 0),_(this,"_event",void 0),_(this,"_initial",!1),_(this,"changed",void 0),_(this,"configuration",void 0),_(this,"transitions",void 0),_(this,"children",void 0),this.context=e.context,this._event=e._event,this._internalQueue=null!==(r=e._internalQueue)&&void 0!==r?r:[],this.event=this._event.data,this.historyValue=e.historyValue||{},this.actions=null!==(i=e.actions)&&void 0!==i?i:[],this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=null!==(o=e.configuration)&&void 0!==o?o:Array.from(lt(mt(n.root,e.value))),this.transitions=e.transitions,this.children=e.children,this.value=vt(n.root,this.configuration),this.tags=new Set(C(this.configuration.map((function(t){return t.tags})))),this.done=null!==(a=e.done)&&void 0!==a&&a,this.output=e.output}return L(t,[{key:"toStrings",value:function(){var t=this,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.value,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:".";if($(e))return[e];var r=Object.keys(e);return r.concat.apply(r,E(r.map((function(r){return t.toStrings(e[r],n).map((function(t){return r+n+t}))}))))}},{key:"toJSON",value:function(){this.configuration,this.transitions;var t=this.tags;return this.machine,w(w({},rt(this,gt)),{},{tags:Array.from(t),meta:this.meta})}},{key:"matches",value:function(t){return P(t,this.value)}},{key:"hasTag",value:function(t){return this.tags.has(t)}},{key:"can",value:function(t){I&&M(!!this.machine,"state.can(...) used outside of a machine-created State object; this will always return false.");var e=this.machine.getTransitionData(this,q(t));return!(null==e||!e.length)&&e.some((function(t){return void 0!==t.target||t.actions.length}))}},{key:"nextEvents",get:function(){var t,e,n,r,i=this;return t=this,e="nextEvents",n=function(){return E(new Set(C(E(i.configuration.map((function(t){return t.ownEvents}))))))},(r=it.get(t))?e in r||(r[e]=n()):(r=_({},e,n()),it.set(t,r)),r[e]}},{key:"meta",get:function(){return this.configuration.reduce((function(t,e){return void 0!==e.meta&&(t[e.id]=e.meta),t}),{})}}],[{key:"from",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2?arguments[2]:void 0;if(e instanceof t)return e.context!==n?new t({value:e.value,context:n,_event:e._event,actions:[],meta:{},configuration:[],transitions:[],children:{}},r):e;var i=It({}),o=lt(mt(r.root,e));return new t({value:e,context:n,_event:i,actions:[],meta:void 0,configuration:Array.from(o),transitions:[],children:{}},r)}}]),t}();function _t(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return new bt(w(w({},t),e),t.machine)}function xt(t){return z({type:d,params:t},(function(e,n){var r,i,o=n.state,a=n.actorContext,s=d,u=t.id,c=t.src;if((i=c)&&"object"===j(i)&&"function"==typeof i.send)r={type:s,params:w(w({},t),{},{ref:c})};else{var l=W(o.machine.options.actors[c]);if(l){var f="input"in t?t.input:l.input,v=et(l.src,{id:u,src:c,parent:null==a?void 0:a.self,systemId:t.systemId,input:"function"==typeof f?f({context:o.context,event:e.data,self:null==a?void 0:a.self}):f});r={type:s,params:w(w({},t),{},{ref:v})}}else r={type:s,params:t}}var h=r.params.ref,p=_t(o,{children:w(w({},o.children),{},_({},u,h))});return r.execute=function(t){var e=t.self,n=r.params,i=n.id;n.ref?t.defer((function(){if(h.status!==K.Stopped)try{var t;null===(t=h.start)||void 0===t||t.call(h)}catch(t){return void e.send(jt(i,t))}})):I||M(!1,"Actor type '".concat(r.params.src,"' not found in machine '").concat(t.id,"'."))},[p,r]}))}function wt(t,e,n,r,i){return function(o){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if($(o)){var s=W(e.options.actors[o]);if(s){var u,c=null!==(u=a.id)&&void 0!==u?u:"anon",l="input"in a?a.input:s.input,f=et(s.src,{id:c,parent:t,input:"function"==typeof l?l({context:n,event:r.data,self:t}):l});return i.push(xt({id:f.id,src:f,ref:f,meta:void 0,input:l})),f}throw new Error("Behavior '".concat(o,"' not implemented in machine '").concat(e.id,"'"))}var d=et(o,{id:a.id||"anonymous",parent:t,input:a.input});return i.push(xt({src:d,ref:d,id:d.id,meta:void 0,input:a.input})),d}}var St=q({type:f});function kt(t){if(B(t))return t;if("string"==typeof t)return{type:t,params:{}};if("function"==typeof t){var e="xstate.function";return z({type:e,params:{}},(function(n,r){var i=r.state,o={type:e,params:{function:t},execute:function(e){return t({context:i.context,event:n.data,action:o,_event:n,self:e.self,system:e.system})}};return[i,o]}))}return t}var Et=function(t){return t?(N(t)?t:[t]).map(kt):[]};function Ot(t,n){var r="".concat(e.DoneInvoke,".").concat(t),i={type:r,output:n,toString:function(){return r}};return i}function jt(t,n){var r="".concat(e.ErrorPlatform,".").concat(t),i={type:r,data:n,toString:function(){return r}};return i}function It(t){return q({type:f,input:t})}t.actionTypes=g,t.after=function(t,n){var r=n?"#".concat(n):"";return"".concat(e.After,"(").concat(t,")").concat(r)},t.assign=function(t){return z({type:s,params:{assignment:t}},(function(e,n){var r,i=n.state,o=n.action,a=n.actorContext,u=[];if(!i.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");var c={context:i.context,event:e.data,action:o,_event:e,spawn:wt(null==a?void 0:a.self,i.machine,i.context,e,u),self:null!==(r=null==a?void 0:a.self)&&void 0!==r?r:{},system:null==a?void 0:a.system},l={};if(R(t))l=t(c);else for(var f=0,d=Object.keys(t);f<d.length;f++){var v=d[f],h=t[v];l[v]=R(h)?h(c):h}var p=Object.assign({},i.context,l);return[_t(i,{context:p}),{type:s,params:{context:p,actions:u}}]}))},t.cancel=function(t){return z({type:a,params:{sendId:t}},(function(e,n){var r,i=n.state,o=n.actorContext,a=R(t)?t({context:i.context,event:e.data,_event:e,self:null!==(r=null==o?void 0:o.self)&&void 0!==r?r:{},system:null==o?void 0:o.system}):t;return[i,{type:"xstate.cancel",params:{sendId:a},execute:function(t){t.self.cancel(a)}}]}))},t.choose=function(t){return z({type:y,params:{guards:t}},(function(e,n){var r,i=n.state,o=null===(r=t.find((function(t){var n=t.guard&&at(t.guard,(function(t){return i.machine.options.guards[t]}));return!n||ot(n,i.context,e,i)})))||void 0===r?void 0:r.actions;return[i,{type:y,params:{actions:Et(o)}}]}))},t.createInitEvent=It,t.done=function(t,n){var r="".concat(e.DoneState,".").concat(t),i={type:r,output:n,toString:function(){return r}};return i},t.doneInvoke=Ot,t.error=jt,t.escalate=function(t,e){return Q((function(e){return{type:p,data:R(t)?t(e):t}}),w(w({},e),{},{to:n.Parent}))},t.forwardTo=function(t,e){if("production"!==process.env.NODE_END&&(!t||"function"==typeof t)){var n=t;t=function(){var t="function"==typeof n?n.apply(void 0,arguments):n;if(!t)throw new Error("Attempted to forward event to undefined actor. This risks an infinite loop in the sender.");return t}}return J((function(t){return t.event}),w(w({},e),{},{to:t}))},t.initEvent=St,t.log=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:nt,e=arguments.length>1?arguments[1]:void 0;return z({type:l,params:{label:e,expr:t}},(function(n,r){var i,o=r.state,a=r.actorContext,s="function"==typeof t?t({context:o.context,event:n.data,_event:n,self:null!==(i=null==a?void 0:a.self)&&void 0!==i?i:{},system:null==a?void 0:a.system}):t;return[o,{type:"xstate.log",params:{label:e,value:s},execute:function(t){var n,r;e?null===(n=t.logger)||void 0===n||n.call(t,e,s):null===(r=t.logger)||void 0===r||r.call(t,s)}}]}))},t.pure=function(t){return z({type:m,params:{get:t}},(function(e,n){var r,i=n.state;return[i,{type:m,params:{actions:null!==(r=D(Et(t({context:i.context,event:e.data}))))&&void 0!==r?r:[]}}]}))},t.raise=function(t,e){return z({type:i,params:{delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:"function"==typeof t?t.name:t.type}},(function(n,r){var o,a=r.state,s=r.actorContext,u={delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:"function"==typeof t?t.name:t.type},c={context:a.context,event:n.data,_event:n,self:null!==(o=null==s?void 0:s.self)&&void 0!==o?o:{},system:null==s?void 0:s.system},l=a.machine.options.delays;if("string"==typeof t)throw new Error('Only event objects may be used with raise; use raise({ type: "'.concat(t,'" }) instead'));var f,d=q("function"==typeof t?t(c):t);if("string"==typeof u.delay){var v=l&&l[u.delay];f="function"==typeof v?v(c):v}else f="function"==typeof u.delay?u.delay(c):u.delay;var h={type:i,params:w(w({},u),{},{_event:d,event:d.data,delay:f}),execute:function(t){"number"!=typeof h.params.delay||t.self.delaySend(h)}};return[a,h]}))},t.resolveActionObject=function(t,e){if(B(t))return t;var n,r=e[t.type];return"function"==typeof r?z({type:"xstate.function",params:null!==(n=t.params)&&void 0!==n?n:{}},(function(e,n){var i=n.state,o={type:t.type,params:t.params,execute:function(t){return r({context:i.context,event:e.data,action:o,_event:e,system:t.system,self:t.self})}};return[i,o]})):r||t},t.respond=function(t,e){return J(t,w(w({},e),{},{to:function(t){return t._event.origin}}))},t.send=J,t.sendParent=Q,t.sendTo=function(t,e,n){return J(e,w(w({},n),{},{to:t}))},t.stop=function(t){var e=t;return z({type:r,params:{actor:e}},(function(t,n){var r=n.state,i=R(e)?e({context:r.context,event:t.data}):e,o="string"==typeof i?r.children[i]:i;return[r,{type:"xstate.stop",params:{actor:o},execute:function(t){o&&(o.status===K.Running?t.defer((function(){t.stopChild(o)})):t.stopChild(o))}}]}))},t.toActionObject=kt,t.toActionObjects=Et,Object.defineProperty(t,"__esModule",{value:!0})}));
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActions={})}(this,(function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const s=".",n="xstate.error",o="xstate.stop";function i(t,e){return{type:`xstate.error.actor.${t}`,error:e}}function r(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const c=t=>{const e=r();e&&e.register(t)};function a(t){setTimeout((()=>{throw t}))}const h="function"==typeof Symbol&&Symbol.observable||"@@observable";let u=0;function p(t,e){const s=f(t),n=f(e);return"string"==typeof n?"string"==typeof s&&n===s:"string"==typeof s?s in n:Object.keys(s).every((t=>t in n&&p(s[t],n[t])))}function d(t){return e=t,Array.isArray(e)?t:t.split(s);var e}function f(t){if((e=t)&&"object"==typeof e&&"machine"in e&&"value"in e)return t.value;var e;if("string"!=typeof t)return t;return function(t){if(1===t.length)return t[0];const e={};let s=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)s[t[e]]=t[e+1];else{const n=s;s={},n[t[e]]=s}return e}(d(t))}function l(t,e,s){const n="object"==typeof t,o=n?t:void 0;return{next:(n?t.next:t)?.bind(o),error:(n?t.error:e)?.bind(o),complete:(n?t.complete:s)?.bind(o)}}function y(t,e){const s=e.match(/^xstate\.invoke\.(\d+)\.(.*)/);if(!s)return t.implementations.actors[e];const[,n,o]=s,i=t.getStateNodeById(o).config.invoke;return(Array.isArray(i)?i[n]:i).src}let _=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const v={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class g{constructor(t,s){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this._processingStatus=_.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const n={...v,...s},{clock:o,logger:i,parent:r,syncSnapshot:c,id:a,systemId:h,inspect:p}=n;this.system=r?.system??function(t){const e=new Map,s=new Map,n=new WeakMap,o=new Set,i={_bookId:()=>"x:"+u++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const o=n.get(t);void 0!==o&&(s.delete(o),n.delete(t))},get:t=>s.get(t),_set:(t,e)=>{const o=s.get(t);if(o&&o!==e)throw new Error(`Actor with system ID '${t}' already exists.`);s.set(t,e),n.set(e,t)},inspect:t=>{o.add(t)},_sendInspectionEvent:e=>{const s={...e,rootId:t.sessionId};o.forEach((t=>t.next?.(s)))},_relay:(t,e,s)=>{i._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)}};return i}(this),p&&!r&&this.system.inspect(l(p)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=i,this.clock=o,this._parent=r,this._syncSnapshot=c,this.options=n,this.src=n.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),h&&(this._systemId=h,this.system._set(h,this)),this._initState(s?.snapshot??s?.state),h&&"active"!==this._snapshot.status&&this.system._unregister(this)}_initState(t){try{this._snapshot=t?this.logic.restoreSnapshot?this.logic.restoreSnapshot(t,this._actorScope):t:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t}}}update(t,e){let s;for(this._snapshot=t;s=this._deferred.shift();)try{s()}catch(e){this._deferred.length=0,this._snapshot={...t,status:"error",error:e}}switch(this._snapshot.status){case"active":for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}break;case"done":for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}this._stopProcedure(),this._complete(),this._doneEvent=(n=this.id,o=this._snapshot.output,{type:`xstate.done.actor.${n}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var n,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=l(t,e,s);if(this._processingStatus!==_.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this._processingStatus===_.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this._processingStatus=_.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._snapshot={...this._snapshot,status:"error",error:t},this._error(t),this}return this.update(this._snapshot,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._snapshot={...this._snapshot,status:"error",error:t},void this._error(t)}this.update(e,t),t.type===o&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===_.Stopped?this:(this.mailbox.clear(),this._processingStatus===_.NotStarted?(this._processingStatus=_.Stopped,this):(this.mailbox.enqueue({type:o}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){a(t)}this.observers.clear()}_reportError(t){if(!this.observers.size)return void(this._parent||a(t));let e=!1;for(const s of this.observers){const n=s.error;e||=!n;try{n?.(t)}catch(t){a(t)}}this.observers.clear(),e&&a(t)}_error(t){this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,i(this.id,t))}_stopProcedure(){if(this._processingStatus!==_.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new e(this._process.bind(this)),this._processingStatus=_.Stopped,this.system._unregister(this),this}_send(t){this._processingStatus!==_.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}delaySend(t){const{event:e,id:s,delay:n}=t,o=this.clock.setTimeout((()=>{this.system._relay(this,t.to??this,e)}),n);s&&(this.delayedEventsMap[s]=o)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:c)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[h](){return this}getSnapshot(){return this._snapshot}}function m(t,e){return new g(t,e)}function x(t,e,s,n,{sendId:o}){return[e,"function"==typeof o?o(s,n):o]}function b(t,e){t.self.cancel(e)}function S(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=x,e.execute=b,e}function w(t,e,s,n,{id:o,systemId:i,src:r,input:c,syncSnapshot:a}){const h="string"==typeof r?y(e.machine,r):r,u="function"==typeof o?o(s):o;let p;return h&&(p=m(h,{id:u,src:r,parent:t?.self,syncSnapshot:a,systemId:i,input:"function"==typeof c?c({context:e.context,event:s.event,self:t?.self}):c})),[B(e,{children:{...e.children,[u]:p}}),{id:o,actorRef:p}]}function I(t,{id:e,actorRef:s}){s&&t.defer((()=>{s._processingStatus!==_.Stopped&&s.start()}))}function E(...[t,{id:e,systemId:s,input:n,syncSnapshot:o=!1}={}]){function i(t,e){}return i.type="snapshot.spawnChild",i.id=e,i.systemId=s,i.src=t,i.input=n,i.syncSnapshot=o,i.resolve=w,i.execute=I,i}function T(t,e,s,n,{actorRef:o}){const i="function"==typeof o?o(s,n):o,r="string"==typeof i?e.children[i]:i;let c=e.children;return r&&(c={...c},delete c[r.id]),[B(e,{children:c}),r]}function k(t,e){e&&(t.system._unregister(e),e._processingStatus===_.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function R(t){function e(t,e){}return e.type="xstate.stopChild",e.actorRef=t,e.resolve=T,e.execute=k,e}const $=R;function M(t,e,s,n){const{machine:o}=n,i="function"==typeof t,r=i?t:o.implementations.guards["string"==typeof t?t:t.type];if(!i&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return M(r,e,s,n);const c={context:e,event:s},a=i||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:e,event:s}):t.params:void 0;if(!("check"in r))return r(c,a);return r.check(n,c,r)}const j=t=>"atomic"===t.type||"final"===t.type;function A(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function O(t,e){const s=[];if(e===t)return s;let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function C(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(j(t))return t.key}const n={};for(const t of s)n[t.key]=C(t,e);return n}function P(t){const e=new Map;for(const s of t)e.has(s)||e.set(s,[]),s.parent&&(e.has(s.parent)||e.set(s.parent,[]),e.get(s.parent).push(s));return e}function N(t,e){const s=function(t){const e=new Set(t),s=P(e);for(const t of e)if("compound"!==t.type||s.get(t)&&s.get(t).length){if("parallel"===t.type)for(const s of A(t))if("history"!==s.type&&!e.has(s)){const t=q(s);for(const s of t)e.add(s)}}else q(t).forEach((t=>e.add(t)));for(const t of e){let s=t.parent;for(;s;)e.add(s),s=s.parent}return e}(e);return C(t,P(s))}function q(t){const e=function(t){const e=new Set;function s(t){if(!e.has(t))if(e.add(t),"compound"===t.type)s(t.initial.target[0]);else if("parallel"===t.type)for(const e of A(t))s(e)}return s(t),e}(t);for(const s of e)for(const n of O(s,t))e.add(n);return e}const D=function(t){return p(t,this.value)},J=function(t){return this.tags.has(t)},V=function(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))},W=function(){const{_nodes:t,tags:e,machine:s,getMeta:n,toJSON:o,can:i,hasTag:r,matches:c,...a}=this;return{...a,tags:Array.from(e)}},z=function(){return this._nodes.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})};function B(t,e={}){return function(t,e){return{status:t.status,output:t.output,error:t.error,machine:e,context:t.context,_nodes:t._nodes,value:N(e.root,t._nodes),tags:new Set(t._nodes.flatMap((t=>t.tags))),children:t.children,historyValue:t.historyValue||{},matches:D,hasTag:J,can:V,getMeta:z,toJSON:W}}({...t,...e},t.machine)}function G(t,{machine:e,context:s},n,o){return(i,r)=>{const c=((i,r={})=>{const{systemId:c,input:a}=r;if("string"==typeof i){const h=y(e,i);if(!h)throw new Error(`Actor logic '${i}' not implemented in machine '${e.id}'`);const u=m(h,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:"function"==typeof a?a({context:s,event:n,self:t.self}):a,src:i,systemId:c});return o[u.id]=u,u}return m(i,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:r.input,src:i,systemId:c})})(i,r);return o[c.id]=c,t.defer((()=>{c._processingStatus!==_.Stopped&&c.start()})),c}}function Q(t,e,s,n,{assignment:o}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},r={context:e.context,event:s.event,spawn:G(t,e,s.event,i),self:t?.self,system:t?.system};let c={};if("function"==typeof o)c=o(r,n);else for(const t of Object.keys(o)){const e=o[t];c[t]="function"==typeof e?e(r,n):e}return[B(e,{context:Object.assign({},e.context,c),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function U(t){function e(t,e){}return e.type="xstate.assign",e.assignment=t,e.resolve=Q,e}function X(t,e,s,n,{event:o,id:i,delay:r},{internalQueue:c}){const a=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with raise; use raise({ type: "${o}" }) instead`);const h="function"==typeof o?o(s,n):o;let u;if("string"==typeof r){const t=a&&a[r];u="function"==typeof t?t(s,n):t}else u="function"==typeof r?r(s,n):r;return"number"!=typeof u&&c.push(h),[e,{event:h,id:i,delay:u}]}function F(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}function H(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=X,s.execute=F,s}let K=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function L(t,e,s,n,{to:o,event:i,id:r,delay:c},a){const h=e.machine.implementations.delays;if("string"==typeof i)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${i}" }) instead`);const u="function"==typeof i?i(s,n):i;let p;if("string"==typeof c){const t=h&&h[c];p="function"==typeof t?t(s,n):t}else p="function"==typeof c?c(s,n):c;const d="function"==typeof o?o(s,n):o;let f;if("string"==typeof d){if(f=d===K.Parent?t?.self._parent:d===K.Internal?t?.self:d.startsWith("#_")?e.children[d.slice(2)]:a.deferredActorIds?.includes(d)?d:e.children[d],!f)throw new Error(`Unable to send event to actor '${d}' from machine '${e.machine.id}'.`)}else f=d||t?.self;return[e,{to:f,event:u,id:r,delay:p}]}function Y(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function Z(t,e){"number"!=typeof e.delay?t.defer((()=>{const{to:s,event:o}=e;t?.system._relay(t.self,s,o.type===n?i(t.self.id,o.data):o)})):t.self.delaySend(e)}function tt(t,e,s){function n(t,e){}return n.type="xsnapshot.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=L,n.retryResolve=Y,n.execute=Z,n}function et(t,e,s,n,{collect:o}){const i=[],r=function(t){i.push(t)};return r.assign=(...t)=>{i.push(U(...t))},r.cancel=(...t)=>{i.push(S(...t))},r.raise=(...t)=>{i.push(H(...t))},r.sendTo=(...t)=>{i.push(tt(...t))},r.spawnChild=(...t)=>{i.push(E(...t))},r.stopChild=(...t)=>{i.push(R(...t))},o({context:s.context,event:s.event,enqueue:r,check:t=>M(t,e.context,s.event,e)}),[e,void 0,i]}function st(t,e,s,n,{value:o,label:i}){return[e,{value:"function"==typeof o?o(s,n):o,label:i}]}function nt({logger:t},{value:e,label:s}){s?t(s,e):t(e)}t.assign=U,t.cancel=S,t.enqueueActions=function(t){function e(t,e){}return e.type="xstate.enqueueActions",e.collect=t,e.resolve=et,e},t.forwardTo=function(t,e){return tt(t,(({event:t})=>t),e)},t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function s(t,e){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=st,s.execute=nt,s},t.raise=H,t.sendParent=function(t,e){return tt(K.Parent,t,e)},t.sendTo=tt,t.spawnChild=E,t.stop=$,t.stopChild=R,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actions.umd.min.js.map