xstate 5.0.0-beta.10 → 5.0.0-beta.11

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 (55) hide show
  1. package/actions/dist/xstate-actions.cjs.js +29 -5
  2. package/actions/dist/{xstate-actions.cjs.dev.js → xstate-actions.development.cjs.js} +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -0
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +20 -5
  8. package/actors/dist/{xstate-actors.cjs.dev.js → xstate-actors.development.cjs.js} +2 -2
  9. package/actors/dist/xstate-actors.development.esm.js +2 -0
  10. package/actors/dist/xstate-actors.esm.js +2 -2
  11. package/actors/dist/xstate-actors.umd.min.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dev/dist/xstate-dev.cjs.js +45 -4
  14. package/{dist/index-0f3fdf0c.cjs.prod.js → dev/dist/xstate-dev.development.cjs.js} +6 -7
  15. package/{dist/index-50bd0aff.esm.js → dev/dist/xstate-dev.development.esm.js} +6 -8
  16. package/dev/dist/xstate-dev.esm.js +42 -1
  17. package/dev/dist/xstate-dev.umd.min.js +1 -1
  18. package/dev/dist/xstate-dev.umd.min.js.map +1 -1
  19. package/dist/actions-26f9aa9d.cjs.js +3388 -0
  20. package/dist/actions-acbe7aa1.development.cjs.js +3423 -0
  21. package/dist/actions-b82e841e.esm.js +3306 -0
  22. package/dist/actions-cff79077.development.esm.js +3341 -0
  23. package/dist/declarations/src/utils.d.ts +0 -1
  24. package/dist/declarations/src/waitFor.d.ts +1 -1
  25. package/dist/xstate.cjs.js +802 -4
  26. package/dist/xstate.development.cjs.js +808 -0
  27. package/dist/xstate.development.esm.js +770 -0
  28. package/dist/xstate.esm.js +543 -662
  29. package/dist/xstate.umd.min.js +1 -1
  30. package/dist/xstate.umd.min.js.map +1 -1
  31. package/guards/dist/xstate-guards.cjs.js +13 -5
  32. package/guards/dist/{xstate-guards.cjs.prod.js → xstate-guards.development.cjs.js} +2 -2
  33. package/guards/dist/xstate-guards.development.esm.js +2 -0
  34. package/guards/dist/xstate-guards.esm.js +2 -2
  35. package/guards/dist/xstate-guards.umd.min.js +1 -1
  36. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  37. package/package.json +52 -1
  38. package/actions/dist/xstate-actions.cjs.prod.js +0 -31
  39. package/actors/dist/xstate-actors.cjs.prod.js +0 -22
  40. package/dev/dist/xstate-dev.cjs.dev.js +0 -11
  41. package/dev/dist/xstate-dev.cjs.prod.js +0 -11
  42. package/dist/actions-109712d3.cjs.dev.js +0 -4357
  43. package/dist/actions-67f1d04e.esm.js +0 -4270
  44. package/dist/actions-d0262a43.cjs.prod.js +0 -4333
  45. package/dist/declarations/actions/dynamicAction.d.ts +0 -5
  46. package/dist/declarations/src/Mailbox.d.ts +0 -12
  47. package/dist/declarations/src/actions/invoke.d.ts +0 -3
  48. package/dist/declarations/src/environment.d.ts +0 -1
  49. package/dist/declarations/src/memo.d.ts +0 -2
  50. package/dist/declarations/src/spawn.d.ts +0 -2
  51. package/dist/declarations/src/system.d.ts +0 -2
  52. package/dist/index-ebaab3c9.cjs.dev.js +0 -52
  53. package/dist/xstate.cjs.dev.js +0 -933
  54. package/dist/xstate.cjs.prod.js +0 -930
  55. package/guards/dist/xstate-guards.cjs.dev.js +0 -15
@@ -1,7 +1,31 @@
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 actors_dist_xstateActors = require('../../dist/actions-26f9aa9d.cjs.js');
6
+ require('../../dev/dist/xstate-dev.cjs.js');
7
+
8
+
9
+
10
+ exports.actionTypes = actors_dist_xstateActors.actionTypes;
11
+ exports.after = actors_dist_xstateActors.after;
12
+ exports.assign = actors_dist_xstateActors.assign;
13
+ exports.cancel = actors_dist_xstateActors.cancel;
14
+ exports.choose = actors_dist_xstateActors.choose;
15
+ exports.createInitEvent = actors_dist_xstateActors.createInitEvent;
16
+ exports.done = actors_dist_xstateActors.done;
17
+ exports.doneInvoke = actors_dist_xstateActors.doneInvoke;
18
+ exports.error = actors_dist_xstateActors.error;
19
+ exports.escalate = actors_dist_xstateActors.escalate;
20
+ exports.forwardTo = actors_dist_xstateActors.forwardTo;
21
+ exports.initEvent = actors_dist_xstateActors.initEvent;
22
+ exports.log = actors_dist_xstateActors.log;
23
+ exports.pure = actors_dist_xstateActors.pure;
24
+ exports.raise = actors_dist_xstateActors.raise;
25
+ exports.resolveActionObject = actors_dist_xstateActors.resolveActionObject;
26
+ exports.send = actors_dist_xstateActors.send;
27
+ exports.sendParent = actors_dist_xstateActors.sendParent;
28
+ exports.sendTo = actors_dist_xstateActors.sendTo;
29
+ exports.stop = actors_dist_xstateActors.stop;
30
+ exports.toActionObject = actors_dist_xstateActors.toActionObject;
31
+ exports.toActionObjects = actors_dist_xstateActors.toActionObjects;
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var actors_dist_xstateActors = require('../../dist/actions-109712d3.cjs.dev.js');
6
- require('../../dist/index-ebaab3c9.cjs.dev.js');
5
+ var actors_dist_xstateActors = require('../../dist/actions-acbe7aa1.development.cjs.js');
6
+ require('../../dev/dist/xstate-dev.development.cjs.js');
7
7
 
8
8
 
9
9
 
@@ -0,0 +1,2 @@
1
+ export { ak as actionTypes, an as after, T as assign, U as cancel, V as choose, A as createInitEvent, ao as done, R as doneInvoke, D as error, aq as escalate, O as forwardTo, J as initEvent, W as log, X as pure, Y as raise, al as resolveActionObject, ap as send, M as sendParent, L as sendTo, Z as stop, am as toActionObject, t as toActionObjects } from '../../dist/actions-cff79077.development.esm.js';
2
+ import '../../dev/dist/xstate-dev.development.esm.js';
@@ -1,2 +1,2 @@
1
- export { as as actionTypes, av as after, $ as assign, a0 as cancel, a1 as choose, G as createInitEvent, aw as done, Z as doneInvoke, J as error, ay as escalate, W as forwardTo, R as initEvent, a2 as log, a3 as pure, a4 as raise, at as resolveActionObject, ax as send, V as sendParent, U as sendTo, a5 as stop, au as toActionObject, j as toActionObjects } from '../../dist/actions-67f1d04e.esm.js';
2
- import '../../dist/index-50bd0aff.esm.js';
1
+ export { ak as actionTypes, an as after, T as assign, U as cancel, V as choose, A as createInitEvent, ao as done, R as doneInvoke, D as error, aq as escalate, O as forwardTo, J as initEvent, W as log, X as pure, Y as raise, al as resolveActionObject, ap as send, M as sendParent, L as sendTo, Z as stop, am as toActionObject, t as toActionObjects } from '../../dist/actions-b82e841e.esm.js';
2
+ 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 x(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 _(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 S(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?_(Object(n),!0).forEach((function(e){x(t,e,n[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):_(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}function w(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 w(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)?w(t,e):void 0}}function E(t){return function(t){if(Array.isArray(t))return w(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=C(t,n),i=C(e,n);return q(i)?!!q(r)&&i===r:q(r)?r in i:Object.keys(r).every((function(t){return t in i&&P(r[t],i[t])}))}function C(t,e){if("object"===j(n=t)&&"value"in n&&"context"in n&&"event"in n)return t.value;var n;if(N(t))return T(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 T(r)}function T(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 A(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 q(t){return"string"==typeof t}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,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=R(t)?t(l):t;if(q(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:S(S({},c),{},{to:y,event:v,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(){n.send(r.type===p?{type:"".concat(Ot(t.self.id)),data:r.data}:e.params.event)}))}else t.self.delaySend(e)}};return[u,g]}))}function Q(t,e){return J(t,S(S({},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,x(this,"_active",!1),x(this,"_current",null),x(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",$="xstate.stop";function F(){var t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}var H,K=function(t){var e=F();e&&e.register(t)};!function(t){t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped"}(H||(H={}));var Y={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},Z=function(){function t(e,n){var r,i=this;U(this,t),this.behavior=e,x(this,"_state",void 0),x(this,"clock",void 0),x(this,"options",void 0),x(this,"id",void 0),x(this,"mailbox",new G(this._process.bind(this))),x(this,"delayedEventsMap",{}),x(this,"observers",new Set),x(this,"logger",void 0),x(this,"status",H.NotStarted),x(this,"_parent",void 0),x(this,"ref",void 0),x(this,"_actorContext",void 0),x(this,"_systemId",void 0),x(this,"sessionId",void 0),x(this,"system",void 0),x(this,"_doneEvent",void 0),x(this,"src",void 0),x(this,"_deferred",[]);var o,a,s,u,c=S(S({},Y),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=Et(this.id,f.data),null===(r=this._parent)||void 0===r||r.send(this._doneEvent),this._complete();break;case"error":this._stopProcedure(),null===(i=this._parent)||void 0===i||i.send(Ot(this.id,f.data)),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===H.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===H.Stopped&&this._doneEvent?t(this._doneEvent):this.observers.add({complete:function(){e._doneEvent&&t(e._doneEvent)}}),this}},{key:"start",value:function(){return this.status===H.Running||(this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=H.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.type===$&&(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===H.Stopped?this:(this.mailbox.clear(),this.status===H.NotStarted?(this.status=H.Stopped,this):(this.mailbox.enqueue({type:$}),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!==H.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=H.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'));if(this.status!==H.Stopped){if(this.status!==H.Running&&!this.options.deferEvents)throw new Error('Event "'.concat(t.type,'" 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(t)));this.mailbox.enqueue(t)}}},{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:K)(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 tt(t,e){return new Z(t,e)}var et=function(t){return{context:t.context,event:t.event}};function nt(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 rt=new WeakMap;function it(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,state:r,guard:t,evaluate:it})}function ot(t,e){var n;return q(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 ot(t,e)})),predicate:(null==e?void 0:e(t.type))||t.predicate}}var at=function(t){return"atomic"===t.type||"final"===t.type};function st(t){return Object.values(t.states).filter((function(t){return"history"!==t.type}))}function ut(t,e){for(var n=[],r=t.parent;r&&r!==e;)n.push(r),r=r.parent;return n}function ct(t){var e,n=new Set(t),r=new Set(t),i=ft(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(st(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=ht(c);l<f.length;l++){var d=f[l];r.add(d)}}}catch(t){u.e(t)}finally{u.f()}}}else ht(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 lt(t,e){var n=e.get(t);if(!n)return{};if("compound"===t.type){var r=n[0];if(!r)return{};if(at(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]=lt(s,e)}}catch(t){a.e(t)}finally{a.f()}return o}function ft(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 dt(t,e){return lt(t,ft(ct(e)))}var vt=function(t){return"#"===t[0]};function ht(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(ut(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(st(r));try{for(f.s();!(l=f.n()).done;){n(l.value)}}catch(t){f.e(t)}finally{f.f()}}}(t),E(e)}function pt(t,e){if(vt(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 yt(t,e){var n=e instanceof gt?e.value:C(e,t.machine.delimiter);if(q(n))return[t,t.states[n]];var r=Object.keys(n),i=r.map((function(e){return pt(t,e)})).filter(Boolean);return[t.machine.root,t].concat(i,r.reduce((function(e,r){var i=pt(t,r);if(!i)return e;var o=yt(i,n[r]);return e.concat(o)}),[]))}var mt=["configuration","transitions","tags","machine"],gt=function(){function t(e,n){var r,i,o,a;U(this,t),this.machine=n,x(this,"tags",void 0),x(this,"value",void 0),x(this,"done",void 0),x(this,"output",void 0),x(this,"context",void 0),x(this,"historyValue",{}),x(this,"actions",[]),x(this,"event",void 0),x(this,"_internalQueue",void 0),x(this,"_initial",!1),x(this,"changed",void 0),x(this,"configuration",void 0),x(this,"transitions",void 0),x(this,"children",void 0),this.context=e.context,this._internalQueue=null!==(r=e._internalQueue)&&void 0!==r?r:[],this.event=e.event,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(ct(yt(n.root,e.value))),this.transitions=e.transitions,this.children=e.children,this.value=dt(n.root,this.configuration),this.tags=new Set(A(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(q(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,S(S({},nt(this,mt)),{},{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,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(A(E(i.configuration.map((function(t){return t.ownEvents}))))))},(r=rt.get(t))?e in r||(r[e]=n()):(r=x({},e,n()),rt.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=jt({}),o=ct(yt(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 bt(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return new gt(S(S({},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:S(S({},t),{},{ref:c})};else{var l=W(o.machine.options.actors[c]);if(l){var f="input"in t?t.input:l.input,v=tt(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,self:null==a?void 0:a.self}):f});r={type:s,params:S(S({},t),{},{ref:v})}}else r={type:s,params:t}}var h=r.params.ref,p=bt(o,{children:S(S({},o.children),{},x({},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!==H.Stopped)try{var t;null===(t=h.start)||void 0===t||t.call(h)}catch(t){return void e.send(Ot(i,t))}})):I||M(!1,"Actor type '".concat(r.params.src,"' not found in machine '").concat(t.id,"'."))},[p,r]}))}function _t(t,e,n,r,i){return function(o){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(q(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=tt(s.src,{id:c,parent:t,input:"function"==typeof l?l({context:n,event:r,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=tt(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={type:f};function wt(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,action:o,self:e.self,system:e.system})}};return[i,o]}))}return t}var kt=function(t){return t?(N(t)?t:[t]).map(wt):[]};function Et(t,n){var r="".concat(e.DoneInvoke,".").concat(t),i={type:r,output:n,toString:function(){return r}};return i}function Ot(t,n){var r="".concat(e.ErrorPlatform,".").concat(t),i={type:r,data:n,toString:function(){return r}};return i}function jt(t){return{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,action:o,spawn:_t(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[bt(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,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&&ot(t.guard,(function(t){return i.machine.options.guards[t]}));return!n||it(n,i.context,e,i)})))||void 0===r?void 0:r.actions;return[i,{type:y,params:{actions:kt(o)}}]}))},t.createInitEvent=jt,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=Et,t.error=Ot,t.escalate=function(t,e){return Q((function(e){return{type:p,data:R(t)?t(e):t}}),S(S({},e),{},{to:n.Parent}))},t.forwardTo=function(t,e){return J((function(t){return t.event}),S(S({},e),{},{to:t}))},t.initEvent=St,t.log=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:et,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,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(kt(t({context:i.context,event:e}))))&&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,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="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:S(S({},u),{},{event:d,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,action:o,system:t.system,self:t.self})}};return[i,o]})):r||t},t.send=J,t.sendParent=Q,t.sendTo=function(t,e,n){return J(e,S(S({},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}):e,o="string"==typeof i?r.children[i]:i;return[r,{type:"xstate.stop",params:{actor:o},execute:function(t){o&&(o.status===H.Running?t.defer((function(){t.stopChild(o)})):t.stopChild(o))}}]}))},t.toActionObject=wt,t.toActionObjects=kt,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";let e=function(t){return 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",t}({}),n=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});const s=e.Stop,o=e.Raise,i=e.Send,r=e.Cancel,a=e.Assign,c=e.After,u=e.DoneState,h=e.Log,d=e.Init,p=e.Invoke,f=e.ErrorExecution,l=e.ErrorPlatform,y=e.ErrorCustom,m=e.Choose,v=e.Pure;var g=Object.freeze({__proto__:null,stop:s,raise:o,send:i,cancel:r,assign:a,after:c,doneState:u,log:h,init:d,invoke:p,errorExecution:f,errorPlatform:l,error:y,choose:m,pure:v});function x(t,e,n="."){const s=_(t,n),o=_(e,n);return $(o)?!!$(s)&&o===s:$(s)?s in o:Object.keys(s).every((t=>t in o&&x(s[t],o[t])))}function _(t,e){if("object"==typeof(n=t)&&"value"in n&&"context"in n&&"event"in n)return t.value;var n;if(E(t))return b(t);if("string"!=typeof t)return t;const s=function(t,e){try{return E(t)?t:t.toString().split(e)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}(t,e);return b(s)}function b(t){if(1===t.length)return t[0];const e={};let n=e;for(let e=0;e<t.length-1;e++)e===t.length-2?n[t[e]]=t[e+1]:(n[t[e]]={},n=n[t[e]]);return e}function S(t){return[].concat(...t)}function w(t){return void 0===t?[]:function(t){return E(t)?t:[t]}(t)}function E(t){return Array.isArray(t)}function I(t){return"function"==typeof t}function $(t){return"string"==typeof t}function k(t){return t?"transition"in t?{src:t,input:void 0}:t:void 0}function C(t,e){return{type:t.type,params:t.params,resolve:e}}function T(t){return"object"==typeof t&&null!==t&&"resolve"in t}function j(t,e){return C({type:i,params:{to:e?e.to:void 0,delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:I(t)?t.name:t.type}},((s,{actorContext:o,state:r})=>{const a={to:e?e.to:void 0,delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:I(t)?t.name:t.type},c={context:r.context,event:s,self:o?.self??null,system:o?.system},u=r.machine.options.delays;if("string"==typeof t)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${t}" }) instead`);const h=I(t)?t(c):t;let d;if($(a.delay)){const t=u&&u[a.delay];d=I(t)?t(c):t}else d=I(a.delay)?a.delay(c):a.delay;const p=I(a.to)?a.to(c):a.to;let f;if("string"==typeof p){if(f=p===n.Parent?o?.self._parent:p===n.Internal?o?.self:p.startsWith("#_")?r.children[p.slice(2)]:r.children[p],!f)throw new Error(`Unable to send event to actor '${p}' from machine '${r.machine.id}'.`)}else f=p||o?.self;const l={type:i,params:{...a,to:f,event:h,delay:d,internal:p===n.Internal},execute:t=>{const e=l;if("number"!=typeof e.params.delay){const n=e.params.to,s=e.params.event;t.defer((()=>{n.send(s.type===y?{type:`${ht(t.self.id)}`,data:s.data}:e.params.event)}))}else t.self.delaySend(e)}};return[r,l]}))}function O(t,e){return j(t,{...e,to:n.Parent})}class P{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)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}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),t===this._current&&(this._current=this._current.next)}this._last=null}}const A="function"==typeof Symbol&&Symbol.observable||"@@observable",M="xstate.stop";function D(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const R=t=>{const e=D();e&&e.register(t)};let N=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const q={deferEvents:!0,clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class z{constructor(t,e){this.behavior=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new P(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=N.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=[];const n={...q,...e},{clock:s,logger:o,parent:i,id:r,systemId:a}=n;this.system=i?.system??function(){let t=0;const e=new Map,n=new Map,s=new WeakMap;return{_bookId:()=>"x:"+t++,_register:(t,n)=>(e.set(t,n),t),_unregister:t=>{e.delete(t.sessionId);const o=s.get(t);void 0!==o&&(n.delete(o),s.delete(t))},get:t=>n.get(t),_set:(t,e)=>{const o=n.get(t);if(o&&o!==e)throw new Error(`Actor with system ID '${t}' already exists.`);n.set(t,e),s.set(e,t)}}}(),a&&(this._systemId=a,this.system._set(a,this)),this.sessionId=this.system._bookId(),this.id=r??this.sessionId,this.logger=o,this.clock=s,this._parent=i,this.options=n,this.src=n.src,this.ref=this,this._actorContext={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._initState()}_initState(){this._state=this.options.state?this.behavior.restoreState?this.behavior.restoreState(this.options.state,this._actorContext):this.options.state:this.behavior.getInitialState(this._actorContext,this.options?.input)}update(t){this._state=t;const e=this.getSnapshot();let n;for(;n=this._deferred.shift();)n(t);for(const t of this.observers)t.next?.(e);const s=this.behavior.getStatus?.(t);switch(s?.status){case"done":this._stopProcedure(),this._doneEvent=ut(this.id,s.data),this._parent?.send(this._doneEvent),this._complete();break;case"error":this._stopProcedure(),this._parent?.send(ht(this.id,s.data)),this._error(s.data)}}subscribe(t,e,n){const s=function(t,e,n){const s=()=>{},o="object"==typeof t,i=o?t:null;return{next:((o?t.next:t)||s).bind(i),error:((o?t.error:e)||s).bind(i),complete:((o?t.complete:n)||s).bind(i)}}(t,e,n);return this.observers.add(s),this.status===N.Stopped&&(s.complete?.(),this.observers.delete(s)),{unsubscribe:()=>{this.observers.delete(s)}}}onDone(t){return this.status===N.Stopped&&this._doneEvent?t(this._doneEvent):this.observers.add({complete:()=>{this._doneEvent&&t(this._doneEvent)}}),this}start(){return this.status===N.Running||(this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=N.Running,this.behavior.start&&this.behavior.start(this._state,this._actorContext),this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start()),this}_process(t){try{const e=this.behavior.transition(this._state,t,this._actorContext);this.update(e),t.type===M&&(this._stopProcedure(),this._complete())}catch(t){if(!(this.observers.size>0))throw t;this.observers.forEach((e=>{e.error?.(t)})),this.stop()}}_stop(){return this.status===N.Stopped?this:(this.mailbox.clear(),this.status===N.NotStarted?(this.status=N.Stopped,this):(this.mailbox.enqueue({type:M}),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)t.complete?.();this.observers.clear()}_error(t){for(const e of this.observers)e.error?.(t);this.observers.clear()}_stopProcedure(){if(this.status!==N.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new P(this._process.bind(this)),this.status=N.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);if(this.status!==N.Stopped){if(this.status!==N.Running&&!this.options.deferEvents)throw new Error(`Event "${t.type}" was sent to uninitialized actor "${this.id}". Make sure .start() is called for this actor, or set { deferEvents: true } in the actor options.\nEvent: ${JSON.stringify(t)}`);this.mailbox.enqueue(t)}}delaySend(t){this.delayedEventsMap[t.params.id]=this.clock.setTimeout((()=>{"to"in t.params&&t.params.to?t.params.to.send(t.params.event):this.send(t.params.event)}),t.params.delay)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:R)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.behavior.getPersistedState?.(this._state)}[A](){return this}getSnapshot(){return this.behavior.getSnapshot?this.behavior.getSnapshot(this._state):this._state}}function B(t,e){return new z(t,e)}const J=({context:t,event:e})=>({context:t,event:e});const Q=new WeakMap;function V(t,e,n,s){const{machine:o}=s,i=o?.options?.guards?.[t.type]??t.predicate;if(!i)throw new Error(`Guard '${t.type}' is not implemented.'.`);return i({context:e,event:n,state:s,guard:t,evaluate:V})}function W(t,e){return $(t)?{type:t,predicate:e?.(t)||void 0,params:{type:t}}:I(t)?{type:t.name,predicate:t,params:{type:t.name,name:t.name}}:{type:t.type,params:t.params||t,children:t.children?.map((t=>W(t,e))),predicate:e?.(t.type)||t.predicate}}const L=t=>"atomic"===t.type||"final"===t.type;function U(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function G(t,e){const n=[];let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function X(t){const e=new Set(t),n=new Set(t),s=H(n);for(const t of e)if("compound"!==t.type||s.get(t)&&s.get(t).length){if("parallel"===t.type)for(const e of U(t))if("history"!==e.type&&!n.has(e))for(const t of Z(e))n.add(t)}else Z(t).forEach((t=>n.add(t)));for(const t of n){let e=t.parent;for(;e;)n.add(e),e=e.parent}return n}function F(t,e){const n=e.get(t);if(!n)return{};if("compound"===t.type){const t=n[0];if(!t)return{};if(L(t))return t.key}const s={};for(const t of n)s[t.key]=F(t,e);return s}function H(t){const e=new Map;for(const n of t)e.has(n)||e.set(n,[]),n.parent&&(e.has(n.parent)||e.set(n.parent,[]),e.get(n.parent).push(n));return e}function K(t,e){return F(t,H(X(e)))}const Y=t=>"#"===t[0];function Z(t){const e=new Set;return function n(s){if(!e.has(s))if(e.add(s),"compound"===s.type)for(const o of s.initial.target){for(const n of G(o,t))e.add(n);n(o)}else if("parallel"===s.type)for(const t of U(s))n(t)}(t),[...e]}function tt(t,e){if(Y(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);const n=t.states[e];if(!n)throw new Error(`Child state '${e}' does not exist on '${t.id}'`);return n}function et(t,e){const n=e instanceof nt?e.value:_(e,t.machine.delimiter);if($(n))return[t,t.states[n]];const s=Object.keys(n),o=s.map((e=>tt(t,e))).filter(Boolean);return[t.machine.root,t].concat(o,s.reduce(((e,s)=>{const o=tt(t,s);if(!o)return e;const i=et(o,n[s]);return e.concat(i)}),[]))}class nt{static from(t,e={},n){if(t instanceof nt)return t.context!==e?new nt({value:t.value,context:e,event:t.event,actions:[],meta:{},configuration:[],transitions:[],children:{}},n):t;const s=dt({}),o=X(et(n.root,t));return new nt({value:t,context:e,event:s,actions:[],meta:void 0,configuration:Array.from(o),transitions:[],children:{}},n)}constructor(t,e){this.machine=e,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._initial=!1,this.changed=void 0,this.configuration=void 0,this.transitions=void 0,this.children=void 0,this.context=t.context,this._internalQueue=t._internalQueue??[],this.event=t.event,this.historyValue=t.historyValue||{},this.actions=t.actions??[],this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(X(et(e.root,t.value))),this.transitions=t.transitions,this.children=t.children,this.value=K(e.root,this.configuration),this.tags=new Set(S(this.configuration.map((t=>t.tags)))),this.done=t.done??!1,this.output=t.output}toStrings(t=this.value,e="."){if($(t))return[t];const n=Object.keys(t);return n.concat(...n.map((n=>this.toStrings(t[n],e).map((t=>n+e+t)))))}toJSON(){const{configuration:t,transitions:e,tags:n,machine:s,...o}=this;return{...o,tags:Array.from(n),meta:this.meta}}matches(t){return x(t,this.value)}hasTag(t){return this.tags.has(t)}can(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))}get nextEvents(){return function(t,e,n){let s=Q.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},Q.set(t,s)),s[e]}(this,"nextEvents",(()=>[...new Set(S([...this.configuration.map((t=>t.ownEvents))]))]))}get meta(){return this.configuration.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})}}function st(t,e={}){return new nt({...t,...e},t.machine)}function ot(t){return C({type:p,params:t},((e,{state:n,actorContext:s})=>{const o=p,{id:i,src:r}=t;let a;if((c=r)&&"object"==typeof c&&"function"==typeof c.send)a={type:o,params:{...t,ref:r}};else{const c=k(n.machine.options.actors[r]);if(c){const u="input"in t?t.input:c.input,h=B(c.src,{id:i,src:r,parent:s?.self,systemId:t.systemId,input:"function"==typeof u?u({context:n.context,event:e,self:s?.self}):u});a={type:o,params:{...t,ref:h}}}else a={type:o,params:t}}var c;const u=a.params.ref,h=st(n,{children:{...n.children,[i]:u}});return a.execute=t=>{const e=t.self,{id:n,ref:s}=a.params;s&&t.defer((()=>{if(u.status!==N.Stopped)try{u.start?.()}catch(t){return void e.send(ht(n,t))}}))},[h,a]}))}function it(t,e,n,s,o){return(i,r={})=>{if($(i)){const a=k(e.options.actors[i]);if(a){const e=r.id??"anon",i="input"in r?r.input:a.input,c=B(a.src,{id:e,parent:t,input:"function"==typeof i?i({context:n,event:s,self:t}):i});return o.push(ot({id:c.id,src:c,ref:c,meta:void 0,input:i})),c}throw new Error(`Behavior '${i}' not implemented in machine '${e.id}'`)}{const e=B(i,{id:r.id||"anonymous",parent:t,input:r.input});return o.push(ot({src:e,ref:e,id:e.id,meta:void 0,input:r.input})),e}}}const rt={type:d};function at(t){if(T(t))return t;if("string"==typeof t)return{type:t,params:{}};if("function"==typeof t){const e="xstate.function";return C({type:e,params:{}},((n,{state:s})=>{const o={type:e,params:{function:t},execute:e=>t({context:s.context,event:n,action:o,self:e.self,system:e.system})};return[s,o]}))}return t}const ct=t=>{if(!t)return[];return(E(t)?t:[t]).map(at)};function ut(t,n){const s=`${e.DoneInvoke}.${t}`,o={type:s,output:n,toString:()=>s};return o}function ht(t,n){const s=`${e.ErrorPlatform}.${t}`,o={type:s,data:n,toString:()=>s};return o}function dt(t){return{type:d,input:t}}t.actionTypes=g,t.after=function(t,n){const s=n?`#${n}`:"";return`${e.After}(${t})${s}`},t.assign=function(t){return C({type:a,params:{assignment:t}},((e,{state:n,action:s,actorContext:o})=>{const i=[];if(!n.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const r={context:n.context,event:e,action:s,spawn:it(o?.self,n.machine,n.context,e,i),self:o?.self??{},system:o?.system};let c={};if(I(t))c=t(r);else for(const e of Object.keys(t)){const n=t[e];c[e]=I(n)?n(r):n}const u=Object.assign({},n.context,c);return[st(n,{context:u}),{type:a,params:{context:u,actions:i}}]}))},t.cancel=function(t){return C({type:r,params:{sendId:t}},((e,{state:n,actorContext:s})=>{const o=I(t)?t({context:n.context,event:e,self:s?.self??{},system:s?.system}):t;return[n,{type:"xstate.cancel",params:{sendId:o},execute:t=>{t.self.cancel(o)}}]}))},t.choose=function(t){return C({type:m,params:{guards:t}},((e,{state:n})=>{const s=t.find((t=>{const s=t.guard&&W(t.guard,(t=>n.machine.options.guards[t]));return!s||V(s,n.context,e,n)}))?.actions;return[n,{type:m,params:{actions:ct(s)}}]}))},t.createInitEvent=dt,t.done=function(t,n){const s=`${e.DoneState}.${t}`,o={type:s,output:n,toString:()=>s};return o},t.doneInvoke=ut,t.error=ht,t.escalate=function(t,e){return O((e=>({type:y,data:I(t)?t(e):t})),{...e,to:n.Parent})},t.forwardTo=function(t,e){return j((({event:t})=>t),{...e,to:t})},t.initEvent=rt,t.log=function(t=J,e){return C({type:h,params:{label:e,expr:t}},((n,{state:s,actorContext:o})=>{const i="function"==typeof t?t({context:s.context,event:n,self:o?.self??{},system:o?.system}):t;return[s,{type:"xstate.log",params:{label:e,value:i},execute:t=>{e?t.logger?.(e,i):t.logger?.(i)}}]}))},t.pure=function(t){return C({type:v,params:{get:t}},((e,{state:n})=>[n,{type:v,params:{actions:w(ct(t({context:n.context,event:e})))??[]}}]))},t.raise=function(t,e){return C({type:o,params:{delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:"function"==typeof t?t.name:t.type}},((n,{state:s,actorContext:i})=>{const r={delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:"function"==typeof t?t.name:t.type},a={context:s.context,event:n,self:i?.self??{},system:i?.system},c=s.machine.options.delays;if("string"==typeof t)throw new Error(`Only event objects may be used with raise; use raise({ type: "${t}" }) instead`);const u="function"==typeof t?t(a):t;let h;if("string"==typeof r.delay){const t=c&&c[r.delay];h="function"==typeof t?t(a):t}else h="function"==typeof r.delay?r.delay(a):r.delay;const d={type:o,params:{...r,event:u,delay:h},execute:t=>{"number"!=typeof d.params.delay||t.self.delaySend(d)}};return[s,d]}))},t.resolveActionObject=function(t,e){if(T(t))return t;const n=e[t.type];return"function"==typeof n?C({type:"xstate.function",params:t.params??{}},((e,{state:s})=>{const o={type:t.type,params:t.params,execute:t=>n({context:s.context,event:e,action:o,system:t.system,self:t.self})};return[s,o]})):n||t},t.send=j,t.sendParent=O,t.sendTo=function(t,e,n){return j(e,{...n,to:t})},t.stop=function(t){const e=t;return C({type:s,params:{actor:e}},((t,{state:n})=>{const s=I(e)?e({context:n.context,event:t}):e,o="string"==typeof s?n.children[s]:s;return[n,{type:"xstate.stop",params:{actor:o},execute:t=>{o&&(o.status===N.Running?t.defer((()=>{t.stopChild(o)})):t.stopChild(o))}}]}))},t.toActionObject=at,t.toActionObjects=ct,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actions.umd.min.js.map