xstate 5.0.0-beta.50 → 5.0.0-beta.51

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 (39) hide show
  1. package/actions/dist/xstate-actions.cjs.js +11 -10
  2. package/actions/dist/xstate-actions.cjs.mjs +1 -0
  3. package/actions/dist/xstate-actions.development.cjs.js +11 -10
  4. package/actions/dist/xstate-actions.development.cjs.mjs +1 -0
  5. package/actions/dist/xstate-actions.development.esm.js +2 -2
  6. package/actions/dist/xstate-actions.esm.js +2 -2
  7. package/actions/dist/xstate-actions.umd.min.js +1 -1
  8. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  9. package/actors/dist/xstate-actors.cjs.js +1 -1
  10. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  11. package/actors/dist/xstate-actors.development.esm.js +1 -1
  12. package/actors/dist/xstate-actors.esm.js +1 -1
  13. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  14. package/dist/declarations/src/actions/enqueueActions.d.ts +32 -0
  15. package/dist/declarations/src/actions/pure.d.ts +7 -10
  16. package/dist/declarations/src/actions.d.ts +3 -2
  17. package/dist/declarations/src/utils.d.ts +1 -20
  18. package/dist/{send-2b001f05.development.esm.js → pure-296f8ebd.development.esm.js} +113 -71
  19. package/dist/{send-df1c8ef2.development.cjs.js → pure-a0f16134.development.cjs.js} +113 -70
  20. package/dist/{send-88351a33.esm.js → pure-aefddc19.esm.js} +107 -65
  21. package/dist/{send-7f3db830.cjs.js → pure-c5f1b46c.cjs.js} +107 -64
  22. package/dist/{raise-32ec7226.esm.js → raise-4742bf04.esm.js} +7 -12
  23. package/dist/{raise-dc9c2c58.development.cjs.js → raise-528386de.development.cjs.js} +6 -11
  24. package/dist/{raise-6c05c91b.development.esm.js → raise-acaa3884.development.esm.js} +7 -12
  25. package/dist/{raise-8176cd35.cjs.js → raise-d5633a02.cjs.js} +6 -11
  26. package/dist/xstate.cjs.js +13 -12
  27. package/dist/xstate.cjs.mjs +1 -0
  28. package/dist/xstate.development.cjs.js +13 -12
  29. package/dist/xstate.development.cjs.mjs +1 -0
  30. package/dist/xstate.development.esm.js +4 -4
  31. package/dist/xstate.esm.js +4 -4
  32. package/dist/xstate.umd.min.js +1 -1
  33. package/dist/xstate.umd.min.js.map +1 -1
  34. package/guards/dist/xstate-guards.cjs.js +1 -1
  35. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  36. package/guards/dist/xstate-guards.development.esm.js +1 -1
  37. package/guards/dist/xstate-guards.esm.js +1 -1
  38. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  39. package/package.json +1 -1
@@ -2,20 +2,21 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var send = require('../../dist/send-7f3db830.cjs.js');
6
- var guards_dist_xstateGuards = require('../../dist/raise-8176cd35.cjs.js');
5
+ var pure = require('../../dist/pure-c5f1b46c.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-d5633a02.cjs.js');
7
7
  require('../../dev/dist/xstate-dev.cjs.js');
8
8
 
9
9
 
10
10
 
11
- exports.assign = send.assign;
12
- exports.choose = send.choose;
13
- exports.escalate = send.escalate;
14
- exports.forwardTo = send.forwardTo;
15
- exports.log = send.log;
16
- exports.pure = send.pure;
17
- exports.sendParent = send.sendParent;
18
- exports.sendTo = send.sendTo;
11
+ exports.assign = pure.assign;
12
+ exports.choose = pure.choose;
13
+ exports.enqueueActions = pure.enqueueActions;
14
+ exports.escalate = pure.escalate;
15
+ exports.forwardTo = pure.forwardTo;
16
+ exports.log = pure.log;
17
+ exports.pure = pure.pure;
18
+ exports.sendParent = pure.sendParent;
19
+ exports.sendTo = pure.sendTo;
19
20
  exports.cancel = guards_dist_xstateGuards.cancel;
20
21
  exports.raise = guards_dist_xstateGuards.raise;
21
22
  exports.spawnChild = guards_dist_xstateGuards.spawnChild;
@@ -2,6 +2,7 @@ export {
2
2
  assign,
3
3
  cancel,
4
4
  choose,
5
+ enqueueActions,
5
6
  escalate,
6
7
  forwardTo,
7
8
  log,
@@ -2,20 +2,21 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var send = require('../../dist/send-df1c8ef2.development.cjs.js');
6
- var guards_dist_xstateGuards = require('../../dist/raise-dc9c2c58.development.cjs.js');
5
+ var pure = require('../../dist/pure-a0f16134.development.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-528386de.development.cjs.js');
7
7
  require('../../dev/dist/xstate-dev.development.cjs.js');
8
8
 
9
9
 
10
10
 
11
- exports.assign = send.assign;
12
- exports.choose = send.choose;
13
- exports.escalate = send.escalate;
14
- exports.forwardTo = send.forwardTo;
15
- exports.log = send.log;
16
- exports.pure = send.pure;
17
- exports.sendParent = send.sendParent;
18
- exports.sendTo = send.sendTo;
11
+ exports.assign = pure.assign;
12
+ exports.choose = pure.choose;
13
+ exports.enqueueActions = pure.enqueueActions;
14
+ exports.escalate = pure.escalate;
15
+ exports.forwardTo = pure.forwardTo;
16
+ exports.log = pure.log;
17
+ exports.pure = pure.pure;
18
+ exports.sendParent = pure.sendParent;
19
+ exports.sendTo = pure.sendTo;
19
20
  exports.cancel = guards_dist_xstateGuards.cancel;
20
21
  exports.raise = guards_dist_xstateGuards.raise;
21
22
  exports.spawnChild = guards_dist_xstateGuards.spawnChild;
@@ -2,6 +2,7 @@ export {
2
2
  assign,
3
3
  cancel,
4
4
  choose,
5
+ enqueueActions,
5
6
  escalate,
6
7
  forwardTo,
7
8
  log,
@@ -1,3 +1,3 @@
1
- export { a as assign, c as choose, e as escalate, f as forwardTo, l as log, p as pure, s as sendParent, b as sendTo } from '../../dist/send-2b001f05.development.esm.js';
2
- export { L as cancel, M as raise, Q as spawnChild, O as stop, P as stopChild } from '../../dist/raise-6c05c91b.development.esm.js';
1
+ export { a as assign, c as choose, e as enqueueActions, b as escalate, f as forwardTo, l as log, p as pure, s as sendParent, d as sendTo } from '../../dist/pure-296f8ebd.development.esm.js';
2
+ export { L as cancel, M as raise, O as spawnChild, P as stop, Q as stopChild } from '../../dist/raise-acaa3884.development.esm.js';
3
3
  import '../../dev/dist/xstate-dev.development.esm.js';
@@ -1,3 +1,3 @@
1
- export { a as assign, c as choose, e as escalate, f as forwardTo, l as log, p as pure, s as sendParent, b as sendTo } from '../../dist/send-88351a33.esm.js';
2
- export { L as cancel, M as raise, Q as spawnChild, O as stop, P as stopChild } from '../../dist/raise-32ec7226.esm.js';
1
+ export { a as assign, c as choose, e as enqueueActions, b as escalate, f as forwardTo, l as log, p as pure, s as sendParent, d as sendTo } from '../../dist/pure-aefddc19.esm.js';
2
+ export { L as cancel, M as raise, O as spawnChild, P as stop, Q as stopChild } from '../../dist/raise-4742bf04.esm.js';
3
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";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",i="xstate.stop";function o(t,e){return{type:`xstate.error.actor.${t}`,data: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 d(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&&d(s[t],n[t])))}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}(function(t){try{return p(t)?t:t.split(s)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}(t))}function l(t){return void 0===t?[]:function(t){return p(t)?t:[t]}(t)}function p(t){return Array.isArray(t)}function y(t,e,s){const n="object"==typeof t,i=n?t:void 0;return{next:(n?t.next:t)?.bind(i),error:(n?t.error:e)?.bind(i),complete:(n?t.complete:s)?.bind(i)}}function _(t,e){if(e.startsWith("xstate#")){const[,s]=e.match(/\[(\d+)\]$/),n=t.getStateNodeById(e.slice(7,-(s.length+2))).config.invoke;return(Array.isArray(n)?n[s]:n).src}return t.implementations.actors[e]}let v=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const g={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class m{constructor(t,s){this.logic=t,this._state=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=v.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={...g,...s},{clock:i,logger:o,parent:r,syncSnapshot:c,id:a,systemId:h,inspect:d}=n;this.system=r?.system??function(t){const e=new Map,s=new Map,n=new WeakMap,i=new Set,o={_bookId:()=>"x:"+u++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const i=n.get(t);void 0!==i&&(s.delete(i),n.delete(t))},get:t=>s.get(t),_set:(t,e)=>{const i=s.get(t);if(i&&i!==e)throw new Error(`Actor with system ID '${t}' already exists.`);s.set(t,e),n.set(e,t)},inspect:t=>{i.add(t)},_sendInspectionEvent:e=>{const s={...e,rootId:t.sessionId};i.forEach((t=>t.next?.(s)))},_relay:(t,e,s)=>{o._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)}};return o}(this),d&&!r&&this.system.inspect(y(d)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=o,this.clock=i,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?.state),h&&"active"!==this._state.status&&this.system._unregister(this)}_initState(t){try{this._state=t?this.logic.restoreState?this.logic.restoreState(t,this._actorScope):t:this.logic.getInitialState(this._actorScope,this.options?.input)}catch(t){this._state={status:"error",output:void 0,error:t}}}update(t,e){let s;for(this._state=t;s=this._deferred.shift();)try{s()}catch(e){this._deferred.length=0,this._state={...t,status:"error",error:e}}switch(this._state.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,i=this._state.output,{type:`xstate.done.actor.${n}`,output:i}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._state.error)}var n,i;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=y(t,e,s);if(this._processingStatus!==v.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this._processingStatus===v.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=v.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._state.status){case"done":return this.update(this._state,t),this;case"error":return this._error(this._state.error),this}if(this.logic.start)try{this.logic.start(this._state,this._actorScope)}catch(t){return this._state={...this._state,status:"error",error:t},this._error(t),this}return this.update(this._state,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._state,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._state={...this._state,status:"error",error:t},void this._error(t)}this.update(e,t),t.type===i&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===v.Stopped?this:(this.mailbox.clear(),this._processingStatus===v.NotStarted?(this._processingStatus=v.Stopped,this):(this.mailbox.enqueue({type:i}),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,o(this.id,t))}_stopProcedure(){if(this._processingStatus!==v.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=v.Stopped,this.system._unregister(this),this}_send(t){this._processingStatus!==v.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}delaySend(t){const{event:e,id:s,delay:n}=t,i=this.clock.setTimeout((()=>{this.system._relay(this,t.to??this,e)}),n);s&&(this.delayedEventsMap[s]=i)}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}}getPersistedState(t){return this.logic.getPersistedState(this._state,t)}[h](){return this}getSnapshot(){return this._state}}function x(t,e){return new m(t,e)}function b(t,e,s,n,{sendId:i}){return[e,"function"==typeof i?i(s,n):i]}function S(t,e){t.self.cancel(e)}function w(t,e,s,n,{id:i,systemId:o,src:r,input:c,syncSnapshot:a}){const h="string"==typeof r?_(e.machine,r):r,u="function"==typeof i?i(s):i;let d;return h&&(d=x(h,{id:u,src:r,parent:t?.self,syncSnapshot:a,systemId:o,input:"function"==typeof c?c({context:e.context,event:s.event,self:t?.self}):c})),[z(e,{children:{...e.children,[u]:d}}),{id:i,actorRef:d}]}function I(t,{id:e,actorRef:s}){s&&t.defer((()=>{s._processingStatus!==v.Stopped&&s.start()}))}function E(t,e,s,n,{actorRef:i}){const o="function"==typeof i?i(s,n):i,r="string"==typeof o?e.children[o]:o;let c=e.children;return r&&(c={...c},delete c[r.id]),[z(e,{children:c}),r]}function T(t,e){e&&(t.system._unregister(e),e._processingStatus===v.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function k(t){function e(t,e){}return e.type="xstate.stopChild",e.actorRef=t,e.resolve=E,e.execute=T,e}const R=k;function $(t,e,s,n){const{machine:i}=n,o="function"==typeof t,r=o?t:i.implementations.guards["string"==typeof t?t:t.type];if(!o&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return $(r,e,s,n);const c={context:e,event:s},a=o||"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 M=t=>"atomic"===t.type||"final"===t.type;function j(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 P(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(M(t))return t.key}const n={};for(const t of s)n[t.key]=P(t,e);return n}function A(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 C(t,e){const s=function(t){const e=new Set(t),s=A(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 j(t))if("history"!==s.type&&!e.has(s)){const t=N(s);for(const s of t)e.add(s)}}else N(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 P(t,A(s))}function N(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 j(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 d(t,this.value)},q=function(t){return this.tags.has(t)},J=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:i,can:o,hasTag:r,matches:c,...a}=this;return{...a,tags:Array.from(e)}},V=function(){return this._nodes.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})};function z(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:C(e.root,t._nodes),tags:new Set(t._nodes.flatMap((t=>t.tags))),children:t.children,historyValue:t.historyValue||{},matches:D,hasTag:q,can:J,getMeta:V,toJSON:W}}({...t,...e},t.machine)}function B(t,{machine:e,context:s},n,i){return(o,r)=>{const c=((o,r={})=>{const{systemId:c,input:a}=r;if("string"==typeof o){const h=_(e,o);if(!h)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const u=x(h,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:"function"==typeof a?a({context:s,event:n,self:t.self}):a,src:o,systemId:c});return i[u.id]=u,u}return x(o,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:r.input,src:o,systemId:c})})(o,r);return i[c.id]=c,t.defer((()=>{c._processingStatus!==v.Stopped&&c.start()})),c}}function G(t,e,s,n,{assignment:i}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const o={},r={context:e.context,event:s.event,spawn:B(t,e,s.event,o),self:t?.self,system:t?.system};let c={};if("function"==typeof i)c=i(r,n);else for(const t of Object.keys(i)){const e=i[t];c[t]="function"==typeof e?e(r,n):e}return[z(e,{context:Object.assign({},e.context,c),children:Object.keys(o).length?{...e.children,...o}:e.children})]}function Q(t,e,s,n,{branches:i}){const o=i.find((t=>!t.guard||$(t.guard,e.context,s.event,e)))?.actions;return[e,void 0,l(o)]}function U(t,e,s,n,{value:i,label:o}){return[e,{value:"function"==typeof i?i(s,n):i,label:o}]}function X({logger:t},{value:e,label:s}){s?t(s,e):t(e)}function F(t,e,s,n,{get:i}){return[e,void 0,l(i({context:s.context,event:s.event}))]}function H(t,e,s,n,{event:i,id:o,delay:r},{internalQueue:c}){const a=e.machine.implementations.delays;if("string"==typeof i)throw new Error(`Only event objects may be used with raise; use raise({ type: "${i}" }) instead`);const h="function"==typeof i?i(s,n):i;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:o,delay:u}]}function K(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}let L=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function Y(t,e,s,n,{to:i,event:o,id:r,delay:c},a){const h=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${o}" }) instead`);const u="function"==typeof o?o(s,n):o;let d;if("string"==typeof c){const t=h&&h[c];d="function"==typeof t?t(s,n):t}else d="function"==typeof c?c(s,n):c;const f="function"==typeof i?i(s,n):i;let l;if("string"==typeof f){if(l=f===L.Parent?t?.self._parent:f===L.Internal?t?.self:f.startsWith("#_")?e.children[f.slice(2)]:a.deferredActorIds?.includes(f)?f:e.children[f],!l)throw new Error(`Unable to send event to actor '${f}' from machine '${e.machine.id}'.`)}else l=f||t?.self;return[e,{to:l,event:u,id:r,delay:d}]}function Z(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function tt(t,e){"number"!=typeof e.delay?t.defer((()=>{const{to:s,event:i}=e;t?.system._relay(t.self,s,i.type===n?o(t.self.id,i.data):i)})):t.self.delaySend(e)}function et(t,e,s){function n(t,e){}return n.type="xstate.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=Y,n.retryResolve=Z,n.execute=tt,n}function st(t,e){return et(L.Parent,t,e)}t.assign=function(t){function e(t,e){}return e.type="xstate.assign",e.assignment=t,e.resolve=G,e},t.cancel=function(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=b,e.execute=S,e},t.choose=function(t){function e(t,e){}return e.type="xstate.choose",e.branches=t,e.resolve=Q,e},t.escalate=function(t,e){return st((e=>({type:n,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return et(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=U,s.execute=X,s},t.pure=function(t){function e(t,e){}return e.type="xstate.pure",e.get=t,e.resolve=F,e},t.raise=function(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=H,s.execute=K,s},t.sendParent=st,t.sendTo=et,t.spawnChild=function(...[t,{id:e,systemId:s,input:n,syncSnapshot:i=!1}={}]){function o(t,e){}return o.type="xstate.spawnChild",o.id=e,o.systemId=s,o.src=t,o.input=n,o.syncSnapshot=i,o.resolve=w,o.execute=I,o},t.stop=R,t.stopChild=k,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",i="xstate.stop";function o(t,e){return{type:`xstate.error.actor.${t}`,data: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 d(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&&d(s[t],n[t])))}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;var n;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}(p(n=t)?n:n.split(s))}function l(t){return void 0===t?[]:function(t){return p(t)?t:[t]}(t)}function p(t){return Array.isArray(t)}function y(t,e,s){const n="object"==typeof t,i=n?t:void 0;return{next:(n?t.next:t)?.bind(i),error:(n?t.error:e)?.bind(i),complete:(n?t.complete:s)?.bind(i)}}function _(t,e){if(e.startsWith("xstate#")){const[,s]=e.match(/\[(\d+)\]$/),n=t.getStateNodeById(e.slice(7,-(s.length+2))).config.invoke;return(Array.isArray(n)?n[s]:n).src}return t.implementations.actors[e]}let v=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const g={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class m{constructor(t,s){this.logic=t,this._state=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=v.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={...g,...s},{clock:i,logger:o,parent:r,syncSnapshot:c,id:a,systemId:h,inspect:d}=n;this.system=r?.system??function(t){const e=new Map,s=new Map,n=new WeakMap,i=new Set,o={_bookId:()=>"x:"+u++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const i=n.get(t);void 0!==i&&(s.delete(i),n.delete(t))},get:t=>s.get(t),_set:(t,e)=>{const i=s.get(t);if(i&&i!==e)throw new Error(`Actor with system ID '${t}' already exists.`);s.set(t,e),n.set(e,t)},inspect:t=>{i.add(t)},_sendInspectionEvent:e=>{const s={...e,rootId:t.sessionId};i.forEach((t=>t.next?.(s)))},_relay:(t,e,s)=>{o._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)}};return o}(this),d&&!r&&this.system.inspect(y(d)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=o,this.clock=i,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?.state),h&&"active"!==this._state.status&&this.system._unregister(this)}_initState(t){try{this._state=t?this.logic.restoreState?this.logic.restoreState(t,this._actorScope):t:this.logic.getInitialState(this._actorScope,this.options?.input)}catch(t){this._state={status:"error",output:void 0,error:t}}}update(t,e){let s;for(this._state=t;s=this._deferred.shift();)try{s()}catch(e){this._deferred.length=0,this._state={...t,status:"error",error:e}}switch(this._state.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,i=this._state.output,{type:`xstate.done.actor.${n}`,output:i}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._state.error)}var n,i;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=y(t,e,s);if(this._processingStatus!==v.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this._processingStatus===v.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=v.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._state.status){case"done":return this.update(this._state,t),this;case"error":return this._error(this._state.error),this}if(this.logic.start)try{this.logic.start(this._state,this._actorScope)}catch(t){return this._state={...this._state,status:"error",error:t},this._error(t),this}return this.update(this._state,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._state,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._state={...this._state,status:"error",error:t},void this._error(t)}this.update(e,t),t.type===i&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===v.Stopped?this:(this.mailbox.clear(),this._processingStatus===v.NotStarted?(this._processingStatus=v.Stopped,this):(this.mailbox.enqueue({type:i}),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,o(this.id,t))}_stopProcedure(){if(this._processingStatus!==v.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=v.Stopped,this.system._unregister(this),this}_send(t){this._processingStatus!==v.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}delaySend(t){const{event:e,id:s,delay:n}=t,i=this.clock.setTimeout((()=>{this.system._relay(this,t.to??this,e)}),n);s&&(this.delayedEventsMap[s]=i)}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}}getPersistedState(t){return this.logic.getPersistedState(this._state,t)}[h](){return this}getSnapshot(){return this._state}}function x(t,e){return new m(t,e)}function b(t,e,s,n,{sendId:i}){return[e,"function"==typeof i?i(s,n):i]}function S(t,e){t.self.cancel(e)}function w(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=b,e.execute=S,e}function I(t,e,s,n,{id:i,systemId:o,src:r,input:c,syncSnapshot:a}){const h="string"==typeof r?_(e.machine,r):r,u="function"==typeof i?i(s):i;let d;return h&&(d=x(h,{id:u,src:r,parent:t?.self,syncSnapshot:a,systemId:o,input:"function"==typeof c?c({context:e.context,event:s.event,self:t?.self}):c})),[G(e,{children:{...e.children,[u]:d}}),{id:i,actorRef:d}]}function E(t,{id:e,actorRef:s}){s&&t.defer((()=>{s._processingStatus!==v.Stopped&&s.start()}))}function T(...[t,{id:e,systemId:s,input:n,syncSnapshot:i=!1}={}]){function o(t,e){}return o.type="xstate.spawnChild",o.id=e,o.systemId=s,o.src=t,o.input=n,o.syncSnapshot=i,o.resolve=I,o.execute=E,o}function k(t,e,s,n,{actorRef:i}){const o="function"==typeof i?i(s,n):i,r="string"==typeof o?e.children[o]:o;let c=e.children;return r&&(c={...c},delete c[r.id]),[G(e,{children:c}),r]}function R(t,e){e&&(t.system._unregister(e),e._processingStatus===v.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function $(t){function e(t,e){}return e.type="xstate.stopChild",e.actorRef=t,e.resolve=k,e.execute=R,e}const M=$;function j(t,e,s,n){const{machine:i}=n,o="function"==typeof t,r=o?t:i.implementations.guards["string"==typeof t?t:t.type];if(!o&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return j(r,e,s,n);const c={context:e,event:s},a=o||"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 A=t=>"atomic"===t.type||"final"===t.type;function O(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function C(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 P(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(A(t))return t.key}const n={};for(const t of s)n[t.key]=P(t,e);return n}function N(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 q(t,e){const s=function(t){const e=new Set(t),s=N(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 O(t))if("history"!==s.type&&!e.has(s)){const t=D(s);for(const s of t)e.add(s)}}else D(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 P(t,N(s))}function D(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 O(t))s(e)}return s(t),e}(t);for(const s of e)for(const n of C(s,t))e.add(n);return e}const J=function(t){return d(t,this.value)},W=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))},z=function(){const{_nodes:t,tags:e,machine:s,getMeta:n,toJSON:i,can:o,hasTag:r,matches:c,...a}=this;return{...a,tags:Array.from(e)}},B=function(){return this._nodes.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})};function G(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:q(e.root,t._nodes),tags:new Set(t._nodes.flatMap((t=>t.tags))),children:t.children,historyValue:t.historyValue||{},matches:J,hasTag:W,can:V,getMeta:B,toJSON:z}}({...t,...e},t.machine)}function Q(t,{machine:e,context:s},n,i){return(o,r)=>{const c=((o,r={})=>{const{systemId:c,input:a}=r;if("string"==typeof o){const h=_(e,o);if(!h)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const u=x(h,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:"function"==typeof a?a({context:s,event:n,self:t.self}):a,src:o,systemId:c});return i[u.id]=u,u}return x(o,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:r.input,src:o,systemId:c})})(o,r);return i[c.id]=c,t.defer((()=>{c._processingStatus!==v.Stopped&&c.start()})),c}}function U(t,e,s,n,{assignment:i}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const o={},r={context:e.context,event:s.event,spawn:Q(t,e,s.event,o),self:t?.self,system:t?.system};let c={};if("function"==typeof i)c=i(r,n);else for(const t of Object.keys(i)){const e=i[t];c[t]="function"==typeof e?e(r,n):e}return[G(e,{context:Object.assign({},e.context,c),children:Object.keys(o).length?{...e.children,...o}:e.children})]}function X(t){function e(t,e){}return e.type="xstate.assign",e.assignment=t,e.resolve=U,e}function F(t,e,s,n,{branches:i}){const o=i.find((t=>!t.guard||j(t.guard,e.context,s.event,e)))?.actions;return[e,void 0,l(o)]}function H(t,e,s,n,{event:i,id:o,delay:r},{internalQueue:c}){const a=e.machine.implementations.delays;if("string"==typeof i)throw new Error(`Only event objects may be used with raise; use raise({ type: "${i}" }) instead`);const h="function"==typeof i?i(s,n):i;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:o,delay:u}]}function K(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}function L(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=H,s.execute=K,s}let Y=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function Z(t,e,s,n,{to:i,event:o,id:r,delay:c},a){const h=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${o}" }) instead`);const u="function"==typeof o?o(s,n):o;let d;if("string"==typeof c){const t=h&&h[c];d="function"==typeof t?t(s,n):t}else d="function"==typeof c?c(s,n):c;const f="function"==typeof i?i(s,n):i;let l;if("string"==typeof f){if(l=f===Y.Parent?t?.self._parent:f===Y.Internal?t?.self:f.startsWith("#_")?e.children[f.slice(2)]:a.deferredActorIds?.includes(f)?f:e.children[f],!l)throw new Error(`Unable to send event to actor '${f}' from machine '${e.machine.id}'.`)}else l=f||t?.self;return[e,{to:l,event:u,id:r,delay:d}]}function tt(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function et(t,e){"number"!=typeof e.delay?t.defer((()=>{const{to:s,event:i}=e;t?.system._relay(t.self,s,i.type===n?o(t.self.id,i.data):i)})):t.self.delaySend(e)}function st(t,e,s){function n(t,e){}return n.type="xstate.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=Z,n.retryResolve=tt,n.execute=et,n}function nt(t,e){return st(Y.Parent,t,e)}function it(t,e,s,n,{collect:i}){const o=[],r=function(t){o.push(t)};return r.assign=(...t)=>{o.push(X(...t))},r.cancel=(...t)=>{o.push(w(...t))},r.raise=(...t)=>{o.push(L(...t))},r.sendTo=(...t)=>{o.push(st(...t))},r.spawnChild=(...t)=>{o.push(T(...t))},r.stopChild=(...t)=>{o.push($(...t))},i({context:s.context,event:s.event,enqueue:r,check:t=>j(t,e.context,s.event,e)}),[e,void 0,o]}function ot(t){function e(t,e){}return e.type="xstate.enqueueActions",e.collect=t,e.resolve=it,e}function rt(t,e,s,n,{value:i,label:o}){return[e,{value:"function"==typeof i?i(s,n):i,label:o}]}function ct({logger:t},{value:e,label:s}){s?t(s,e):t(e)}t.assign=X,t.cancel=w,t.choose=function(t){function e(t,e){}return e.type="xstate.choose",e.branches=t,e.resolve=F,e},t.enqueueActions=ot,t.escalate=function(t,e){return nt((e=>({type:n,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return st(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=rt,s.execute=ct,s},t.pure=function(t){return ot((({context:e,event:s,enqueue:n})=>{l(t({context:e,event:s})).forEach(n)}))},t.raise=L,t.sendParent=nt,t.sendTo=st,t.spawnChild=T,t.stop=M,t.stopChild=$,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actions.umd.min.js.map