xstate 5.5.1 → 5.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  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 +1 -1
  8. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  9. package/actors/dist/xstate-actors.development.esm.js +1 -1
  10. package/actors/dist/xstate-actors.esm.js +1 -1
  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/dist/declarations/src/StateMachine.d.ts +7 -2
  14. package/dist/declarations/src/createActor.d.ts +8 -2
  15. package/dist/declarations/src/createMachine.d.ts +5 -2
  16. package/dist/declarations/src/setup.d.ts +2 -1
  17. package/dist/declarations/src/types.d.ts +3 -4
  18. package/dist/declarations/src/waitFor.d.ts +3 -3
  19. package/dist/{log-da322832.development.cjs.js → log-3552abe4.development.cjs.js} +1 -1
  20. package/dist/{log-742895c6.cjs.js → log-8a4f6e5c.cjs.js} +1 -1
  21. package/dist/{log-22b3587f.esm.js → log-a8de0b28.esm.js} +1 -1
  22. package/dist/{log-285f62db.development.esm.js → log-fedd0292.development.esm.js} +1 -1
  23. package/dist/{raise-8da27ebb.development.esm.js → raise-2fdbadc6.development.esm.js} +31 -11
  24. package/dist/{raise-0e64ee6e.esm.js → raise-309d511a.esm.js} +28 -8
  25. package/dist/{raise-ad8bb7c2.cjs.js → raise-540fdaf2.cjs.js} +28 -7
  26. package/dist/{raise-7af39710.development.cjs.js → raise-d7d9caaa.development.cjs.js} +31 -10
  27. package/dist/xstate.cjs.js +14 -8
  28. package/dist/xstate.development.cjs.js +14 -8
  29. package/dist/xstate.development.esm.js +16 -10
  30. package/dist/xstate.esm.js +16 -10
  31. package/dist/xstate.umd.min.js +1 -1
  32. package/dist/xstate.umd.min.js.map +1 -1
  33. package/guards/dist/xstate-guards.cjs.js +1 -1
  34. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  35. package/guards/dist/xstate-guards.development.esm.js +1 -1
  36. package/guards/dist/xstate-guards.esm.js +1 -1
  37. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  38. package/package.json +1 -1
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var log = require('../../dist/log-742895c6.cjs.js');
6
- var guards_dist_xstateGuards = require('../../dist/raise-ad8bb7c2.cjs.js');
5
+ var log = require('../../dist/log-8a4f6e5c.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-540fdaf2.cjs.js');
7
7
  require('../../dev/dist/xstate-dev.cjs.js');
8
8
 
9
9
 
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var log = require('../../dist/log-da322832.development.cjs.js');
6
- var guards_dist_xstateGuards = require('../../dist/raise-7af39710.development.cjs.js');
5
+ var log = require('../../dist/log-3552abe4.development.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-d7d9caaa.development.cjs.js');
7
7
  require('../../dev/dist/xstate-dev.development.cjs.js');
8
8
 
9
9
 
@@ -1,3 +1,3 @@
1
- export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-285f62db.development.esm.js';
2
- export { L as cancel, M as raise, O as spawnChild, P as stop, Q as stopChild } from '../../dist/raise-8da27ebb.development.esm.js';
1
+ export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-fedd0292.development.esm.js';
2
+ export { M as cancel, O as raise, P as spawnChild, Q as stop, R as stopChild } from '../../dist/raise-2fdbadc6.development.esm.js';
3
3
  import '../../dev/dist/xstate-dev.development.esm.js';
@@ -1,3 +1,3 @@
1
- export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-22b3587f.esm.js';
2
- export { L as cancel, M as raise, O as spawnChild, P as stop, Q as stopChild } from '../../dist/raise-0e64ee6e.esm.js';
1
+ export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-a8de0b28.esm.js';
2
+ export { M as cancel, O as raise, P as spawnChild, Q as stop, R as stopChild } from '../../dist/raise-309d511a.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",o="xstate.stop";function i(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const r=t=>{const e=i();e&&e.register(t)};function c(t,e){return{type:`xstate.error.actor.${t}`,error:e}}function a(t){setTimeout((()=>{throw t}))}const h="function"==typeof Symbol&&Symbol.observable||"@@observable";function u(t,e){return`${t.sessionId}.${e}`}let d=0;function p(t,e){const s=f(t),n=f(e);return"string"==typeof n?"string"==typeof s&&n===s:"string"==typeof s?s in n:Object.keys(s).every((t=>t in n&&p(s[t],n[t])))}function l(t){return e=t,Array.isArray(e)?t:t.split(s);var e}function f(t){if((e=t)&&"object"==typeof e&&"machine"in e&&"value"in e)return t.value;var e;if("string"!=typeof t)return t;return function(t){if(1===t.length)return t[0];const e={};let s=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)s[t[e]]=t[e+1];else{const n=s;s={},n[t[e]]=s}return e}(l(t))}function y(t,e,s){const n="object"==typeof t,o=n?t:void 0;return{next:(n?t.next:t)?.bind(o),error:(n?t.error:e)?.bind(o),complete:(n?t.complete:s)?.bind(o)}}function _(t,e){const s=e.match(/^xstate\.invoke\.(\d+)\.(.*)/);if(!s)return t.implementations.actors[e];const[,n,o]=s,i=t.getStateNodeById(o).config.invoke;return(Array.isArray(i)?i[n]:i).src}let 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._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),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:o,logger:i,parent:r,syncSnapshot:c,id:a,systemId:h,inspect:p}=n;this.system=r?r.system:function(t,e){const s=new Map,n=new Map,o=new WeakMap,i=new Set,r={},c=e.clock,a={schedule:(t,e,s,n,o=Math.random().toString(36).slice(2))=>{const i={source:t,target:e,event:s,delay:n,id:o,startedAt:Date.now()},a=u(t,o);h._snapshot._scheduledEvents[a]=i;const d=c.setTimeout((()=>{delete r[a],delete h._snapshot._scheduledEvents[a],h._relay(t,e,s)}),n);r[a]=d},cancel:(t,e)=>{const s=u(t,e),n=r[s];delete r[s],delete h._snapshot._scheduledEvents[s],c.clearTimeout(n)},cancelAll:t=>{for(const e in h._snapshot._scheduledEvents){const s=h._snapshot._scheduledEvents[e];s.source===t&&a.cancel(t,s.id)}}},h={_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+d++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const e=o.get(t);void 0!==e&&(n.delete(e),o.delete(t))},get:t=>n.get(t),_set:(t,e)=>{const s=n.get(t);if(s&&s!==e)throw new Error(`Actor with system ID '${t}' already exists.`);n.set(t,e),o.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)=>{h._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)},scheduler:a,getSnapshot:()=>({_scheduledEvents:{...h._snapshot._scheduledEvents}}),start:()=>{const t=h._snapshot._scheduledEvents;h._snapshot._scheduledEvents={};for(const e in t){const{source:s,target:n,event:o,delay:i,id:r}=t[e];a.schedule(s,n,o,i,r)}}};return h}(this,{clock:o}),p&&!r&&this.system.inspect(y(p)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=i,this.clock=o,this._parent=r,this._syncSnapshot=c,this.options=n,this.src=n.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),h&&(this._systemId=h,this.system._set(h,this)),this._initState(s?.snapshot??s?.state),h&&"active"!==this._snapshot.status&&this.system._unregister(this)}_initState(t){try{this._snapshot=t?this.logic.restoreSnapshot?this.logic.restoreSnapshot(t,this._actorScope):t:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t}}}update(t,e){let s;for(this._snapshot=t;s=this._deferred.shift();)try{s()}catch(e){this._deferred.length=0,this._snapshot={...t,status:"error",error:e}}switch(this._snapshot.status){case"active":for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}break;case"done":for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}this._stopProcedure(),this._complete(),this._doneEvent=(n=this.id,o=this._snapshot.output,{type:`xstate.done.actor.${n}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var n,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=y(t,e,s);if(this._processingStatus!==v.Stopped)this.observers.add(n);else switch(this._snapshot.status){case"done":try{n.complete?.()}catch(t){a(t)}break;case"error":{const t=this._snapshot.error;if(n.error)try{n.error(t)}catch(t){a(t)}else a(t);break}}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._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._snapshot={...this._snapshot,status:"error",error:t},this._error(t),this}return this.update(this._snapshot,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._snapshot={...this._snapshot,status:"error",error:t},void this._error(t)}this.update(e,t),t.type===o&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===v.Stopped?this:(this.mailbox.clear(),this._processingStatus===v.NotStarted?(this._processingStatus=v.Stopped,this):(this.mailbox.enqueue({type:o}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){a(t)}this.observers.clear()}_reportError(t){if(!this.observers.size)return void(this._parent||a(t));let e=!1;for(const s of this.observers){const n=s.error;e||=!n;try{n?.(t)}catch(t){a(t)}}this.observers.clear(),e&&a(t)}_error(t){this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,c(this.id,t))}_stopProcedure(){return this._processingStatus!==v.Running||(this.system.scheduler.cancelAll(this),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)}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:r)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[h](){return this}getSnapshot(){return this._snapshot}}function b(t,e){return new m(t,e)}function x(t,e,s,n,{sendId:o}){return[e,"function"==typeof o?o(s,n):o]}function S(t,e){t.defer((()=>{t.system.scheduler.cancel(t.self,e)}))}function w(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=x,e.execute=S,e}function I(t,e,s,n,{id:o,systemId:i,src:r,input:c,syncSnapshot:a}){const h="string"==typeof r?_(e.machine,r):r,u="function"==typeof o?o(s):o;let d;return h&&(d=b(h,{id:u,src:r,parent:t.self,syncSnapshot:a,systemId:i,input:"function"==typeof c?c({context:e.context,event:s.event,self:t.self}):c})),[G(e,{children:{...e.children,[u]:d}}),{id:o,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:o=!1}={}]){function i(t,e){}return i.type="snapshot.spawnChild",i.id=e,i.systemId=s,i.src=t,i.input=n,i.syncSnapshot=o,i.resolve=I,i.execute=E,i}function k(t,e,s,n,{actorRef:o}){const i="function"==typeof o?o(s,n):o,r="string"==typeof i?e.children[i]:i;let c=e.children;return r&&(c={...c},delete c[r.id]),[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 A=$;function j(t,e,s,n){const{machine:o}=n,i="function"==typeof t,r=i?t:o.implementations.guards["string"==typeof t?t:t.type];if(!i&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return j(r,e,s,n);const c={context:e,event:s},a=i||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:e,event:s}):t.params:void 0;if(!("check"in r))return r(c,a);return r.check(n,c,r)}const C=t=>"atomic"===t.type||"final"===t.type;function O(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function P(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 M(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(C(t))return t.key}const n={};for(const t of s)n[t.key]=M(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 M(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 P(s,t))e.add(n);return e}const J=function(t){return p(t,this.value)},V=function(t){return this.tags.has(t)},W=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:o,can:i,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:V,can:W,getMeta:B,toJSON:z}}({...t,...e},t.machine)}function Q(t,{machine:e,context:s},n,o){return(i,r)=>{const c=((i,r={})=>{const{systemId:c,input:a}=r;if("string"==typeof i){const h=_(e,i);if(!h)throw new Error(`Actor logic '${i}' not implemented in machine '${e.id}'`);const u=b(h,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:"function"==typeof a?a({context:s,event:n,self:t.self}):a,src:i,systemId:c});return o[u.id]=u,u}return b(i,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:r.input,src:i,systemId:c})})(i,r);return o[c.id]=c,t.defer((()=>{c._processingStatus!==v.Stopped&&c.start()})),c}}function U(t,e,s,n,{assignment:o}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},r={context:e.context,event:s.event,spawn:Q(t,e,s.event,i),self:t.self,system:t.system};let c={};if("function"==typeof o)c=o(r,n);else for(const t of Object.keys(o)){const e=o[t];c[t]="function"==typeof e?e(r,n):e}return[G(e,{context:Object.assign({},e.context,c),children:Object.keys(i).length?{...e.children,...i}: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,{event:o,id:i,delay:r},{internalQueue:c}){const a=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with raise; use raise({ type: "${o}" }) instead`);const h="function"==typeof o?o(s,n):o;let u;if("string"==typeof r){const t=a&&a[r];u="function"==typeof t?t(s,n):t}else u="function"==typeof r?r(s,n):r;return"number"!=typeof u&&c.push(h),[e,{event:h,id:i,delay:u}]}function H(t,e){const{event:s,delay:n,id:o}=e;"number"!=typeof n||t.defer((()=>{const e=t.self;t.system.scheduler.schedule(e,e,s,n,o)}))}function K(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=F,s.execute=H,s}let L=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function Y(t,e,s,n,{to:o,event:i,id:r,delay:c},a){const h=e.machine.implementations.delays;if("string"==typeof i)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${i}" }) instead`);const u="function"==typeof i?i(s,n):i;let 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 p="function"==typeof o?o(s,n):o;let l;if("string"==typeof p){if(l=p===L.Parent?t.self._parent:p===L.Internal?t.self:p.startsWith("#_")?e.children[p.slice(2)]:a.deferredActorIds?.includes(p)?p:e.children[p],!l)throw new Error(`Unable to send event to actor '${p}' from machine '${e.machine.id}'.`)}else l=p||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){t.defer((()=>{const{to:s,event:o,delay:i,id:r}=e;"number"!=typeof i?t.system._relay(t.self,s,o.type===n?c(t.self.id,o.data):o):t.system.scheduler.schedule(t.self,s,o,i,r)}))}function et(t,e,s){function n(t,e){}return n.type="xsnapshot.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=Y,n.retryResolve=Z,n.execute=tt,n}function st(t,e,s,n,{collect:o}){const i=[],r=function(t){i.push(t)};return r.assign=(...t)=>{i.push(X(...t))},r.cancel=(...t)=>{i.push(w(...t))},r.raise=(...t)=>{i.push(K(...t))},r.sendTo=(...t)=>{i.push(et(...t))},r.spawnChild=(...t)=>{i.push(T(...t))},r.stopChild=(...t)=>{i.push($(...t))},o({context:s.context,event:s.event,enqueue:r,check:t=>j(t,e.context,s.event,e),self:t.self,system:t.system}),[e,void 0,i]}function nt(t,e,s,n,{value:o,label:i}){return[e,{value:"function"==typeof o?o(s,n):o,label:i}]}function ot({logger:t},{value:e,label:s}){s?t(s,e):t(e)}t.assign=X,t.cancel=w,t.enqueueActions=function(t){function e(t,e){}return e.type="xstate.enqueueActions",e.collect=t,e.resolve=st,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=nt,s.execute=ot,s},t.raise=K,t.sendParent=function(t,e){return et(L.Parent,t,e)},t.sendTo=et,t.spawnChild=T,t.stop=A,t.stopChild=$,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="xstate.error",n="xstate.stop";function o(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const i=t=>{const e=o();e&&e.register(t)};function r(t,e){return{type:`xstate.error.actor.${t}`,error:e}}function c(t){setTimeout((()=>{throw t}))}const a="function"==typeof Symbol&&Symbol.observable||"@@observable";function h(t,e){return`${t.sessionId}.${e}`}let u=0;function d(t,e){const s=l(t),n=l(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 p(t){if(e=t,Array.isArray(e))return t;var e;let s=[],n="";for(let e=0;e<t.length;e++){switch(t.charCodeAt(e)){case 92:n+=t[e+1],e++;continue;case 46:s.push(n),n="";continue}n+=t[e]}return s.push(n),s}function l(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}(p(t))}function f(t,e,s){const n="object"==typeof t,o=n?t:void 0;return{next:(n?t.next:t)?.bind(o),error:(n?t.error:e)?.bind(o),complete:(n?t.complete:s)?.bind(o)}}function y(t,e){const s=e.match(/^xstate\.invoke\.(\d+)\.(.*)/);if(!s)return t.implementations.actors[e];const[,n,o]=s,i=t.getStateNodeById(o).config.invoke;return(Array.isArray(i)?i[n]:i).src}let _=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const v={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class g{constructor(t,s){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),this.observers=new Set,this.logger=void 0,this._processingStatus=_.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const n={...v,...s},{clock:o,logger:i,parent:r,syncSnapshot:c,id:a,systemId:d,inspect:p}=n;this.system=r?r.system:function(t,e){const s=new Map,n=new Map,o=new WeakMap,i=new Set,r={},c=e.clock,a={schedule:(t,e,s,n,o=Math.random().toString(36).slice(2))=>{const i={source:t,target:e,event:s,delay:n,id:o,startedAt:Date.now()},a=h(t,o);d._snapshot._scheduledEvents[a]=i;const u=c.setTimeout((()=>{delete r[a],delete d._snapshot._scheduledEvents[a],d._relay(t,e,s)}),n);r[a]=u},cancel:(t,e)=>{const s=h(t,e),n=r[s];delete r[s],delete d._snapshot._scheduledEvents[s],c.clearTimeout(n)},cancelAll:t=>{for(const e in d._snapshot._scheduledEvents){const s=d._snapshot._scheduledEvents[e];s.source===t&&a.cancel(t,s.id)}}},d={_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+u++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const e=o.get(t);void 0!==e&&(n.delete(e),o.delete(t))},get:t=>n.get(t),_set:(t,e)=>{const s=n.get(t);if(s&&s!==e)throw new Error(`Actor with system ID '${t}' already exists.`);n.set(t,e),o.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)=>{d._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)},scheduler:a,getSnapshot:()=>({_scheduledEvents:{...d._snapshot._scheduledEvents}}),start:()=>{const t=d._snapshot._scheduledEvents;d._snapshot._scheduledEvents={};for(const e in t){const{source:s,target:n,event:o,delay:i,id:r}=t[e];a.schedule(s,n,o,i,r)}}};return d}(this,{clock:o}),p&&!r&&this.system.inspect(f(p)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=i,this.clock=o,this._parent=r,this._syncSnapshot=c,this.options=n,this.src=n.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),d&&(this._systemId=d,this.system._set(d,this)),this._initState(s?.snapshot??s?.state),d&&"active"!==this._snapshot.status&&this.system._unregister(this)}_initState(t){try{this._snapshot=t?this.logic.restoreSnapshot?this.logic.restoreSnapshot(t,this._actorScope):t:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t}}}update(t,e){let s;for(this._snapshot=t;s=this._deferred.shift();)try{s()}catch(e){this._deferred.length=0,this._snapshot={...t,status:"error",error:e}}switch(this._snapshot.status){case"active":for(const e of this.observers)try{e.next?.(t)}catch(t){c(t)}break;case"done":for(const e of this.observers)try{e.next?.(t)}catch(t){c(t)}this._stopProcedure(),this._complete(),this._doneEvent=(n=this.id,o=this._snapshot.output,{type:`xstate.done.actor.${n}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var n,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=f(t,e,s);if(this._processingStatus!==_.Stopped)this.observers.add(n);else switch(this._snapshot.status){case"done":try{n.complete?.()}catch(t){c(t)}break;case"error":{const t=this._snapshot.error;if(n.error)try{n.error(t)}catch(t){c(t)}else c(t);break}}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this._processingStatus===_.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this._processingStatus=_.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._snapshot={...this._snapshot,status:"error",error:t},this._error(t),this}return this.update(this._snapshot,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._snapshot={...this._snapshot,status:"error",error:t},void this._error(t)}this.update(e,t),t.type===n&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===_.Stopped?this:(this.mailbox.clear(),this._processingStatus===_.NotStarted?(this._processingStatus=_.Stopped,this):(this.mailbox.enqueue({type:n}),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){c(t)}this.observers.clear()}_reportError(t){if(!this.observers.size)return void(this._parent||c(t));let e=!1;for(const s of this.observers){const n=s.error;e||=!n;try{n?.(t)}catch(t){c(t)}}this.observers.clear(),e&&c(t)}_error(t){this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,r(this.id,t))}_stopProcedure(){return this._processingStatus!==_.Running||(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new e(this._process.bind(this)),this._processingStatus=_.Stopped,this.system._unregister(this)),this}_send(t){this._processingStatus!==_.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:i)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[a](){return this}getSnapshot(){return this._snapshot}}function m(t,...[e]){return new g(t,e)}function b(t,e,s,n,{sendId:o}){return[e,"function"==typeof o?o(s,n):o]}function x(t,e){t.defer((()=>{t.system.scheduler.cancel(t.self,e)}))}function S(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=b,e.execute=x,e}function w(t,e,s,n,{id:o,systemId:i,src:r,input:c,syncSnapshot:a}){const h="string"==typeof r?y(e.machine,r):r,u="function"==typeof o?o(s):o;let d;return h&&(d=m(h,{id:u,src:r,parent:t.self,syncSnapshot:a,systemId:i,input:"function"==typeof c?c({context:e.context,event:s.event,self:t.self}):c})),[B(e,{children:{...e.children,[u]:d}}),{id:o,actorRef:d}]}function I(t,{id:e,actorRef:s}){s&&t.defer((()=>{s._processingStatus!==_.Stopped&&s.start()}))}function E(...[t,{id:e,systemId:s,input:n,syncSnapshot:o=!1}={}]){function i(t,e){}return i.type="snapshot.spawnChild",i.id=e,i.systemId=s,i.src=t,i.input=n,i.syncSnapshot=o,i.resolve=w,i.execute=I,i}function T(t,e,s,n,{actorRef:o}){const i="function"==typeof o?o(s,n):o,r="string"==typeof i?e.children[i]:i;let c=e.children;return r&&(c={...c},delete c[r.id]),[B(e,{children:c}),r]}function k(t,e){e&&(t.system._unregister(e),e._processingStatus===_.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function R(t){function e(t,e){}return e.type="xstate.stopChild",e.actorRef=t,e.resolve=T,e.execute=k,e}const $=R;function A(t,e,s,n){const{machine:o}=n,i="function"==typeof t,r=i?t:o.implementations.guards["string"==typeof t?t:t.type];if(!i&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return A(r,e,s,n);const c={context:e,event:s},a=i||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:e,event:s}):t.params:void 0;if(!("check"in r))return r(c,a);return r.check(n,c,r)}const C=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(C(t))return t.key}const n={};for(const t of s)n[t.key]=P(t,e);return n}function M(t){const e=new Map;for(const s of t)e.has(s)||e.set(s,[]),s.parent&&(e.has(s.parent)||e.set(s.parent,[]),e.get(s.parent).push(s));return e}function N(t,e){const s=function(t){const e=new Set(t),s=M(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=q(s);for(const s of t)e.add(s)}}else q(t).forEach((t=>e.add(t)));for(const t of e){let s=t.parent;for(;s;)e.add(s),s=s.parent}return e}(e);return P(t,M(s))}function q(t){const e=function(t){const e=new Set;function s(t){if(!e.has(t))if(e.add(t),"compound"===t.type)s(t.initial.target[0]);else if("parallel"===t.type)for(const e of 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)},J=function(t){return this.tags.has(t)},V=function(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))},W=function(){const{_nodes:t,tags:e,machine:s,getMeta:n,toJSON:o,can:i,hasTag:r,matches:c,...a}=this;return{...a,tags:Array.from(e)}},z=function(){return this._nodes.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})};function B(t,e={}){return function(t,e){return{status:t.status,output:t.output,error:t.error,machine:e,context:t.context,_nodes:t._nodes,value:N(e.root,t._nodes),tags:new Set(t._nodes.flatMap((t=>t.tags))),children:t.children,historyValue:t.historyValue||{},matches:D,hasTag:J,can:V,getMeta:z,toJSON:W}}({...t,...e},t.machine)}function G(t,{machine:e,context:s},n,o){return(i,r)=>{const c=((i,r={})=>{const{systemId:c,input:a}=r;if("string"==typeof i){const h=y(e,i);if(!h)throw new Error(`Actor logic '${i}' not implemented in machine '${e.id}'`);const u=m(h,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:"function"==typeof a?a({context:s,event:n,self:t.self}):a,src:i,systemId:c});return o[u.id]=u,u}return m(i,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:r.input,src:i,systemId:c})})(i,r);return o[c.id]=c,t.defer((()=>{c._processingStatus!==_.Stopped&&c.start()})),c}}function Q(t,e,s,n,{assignment:o}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},r={context:e.context,event:s.event,spawn:G(t,e,s.event,i),self:t.self,system:t.system};let c={};if("function"==typeof o)c=o(r,n);else for(const t of Object.keys(o)){const e=o[t];c[t]="function"==typeof e?e(r,n):e}return[B(e,{context:Object.assign({},e.context,c),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function U(t){function e(t,e){}return e.type="xstate.assign",e.assignment=t,e.resolve=Q,e}function X(t,e,s,n,{event:o,id:i,delay:r},{internalQueue:c}){const a=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with raise; use raise({ type: "${o}" }) instead`);const h="function"==typeof o?o(s,n):o;let u;if("string"==typeof r){const t=a&&a[r];u="function"==typeof t?t(s,n):t}else u="function"==typeof r?r(s,n):r;return"number"!=typeof u&&c.push(h),[e,{event:h,id:i,delay:u}]}function F(t,e){const{event:s,delay:n,id:o}=e;"number"!=typeof n||t.defer((()=>{const e=t.self;t.system.scheduler.schedule(e,e,s,n,o)}))}function H(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=X,s.execute=F,s}let K=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function L(t,e,s,n,{to:o,event:i,id:r,delay:c},a){const h=e.machine.implementations.delays;if("string"==typeof i)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${i}" }) instead`);const u="function"==typeof i?i(s,n):i;let 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 p="function"==typeof o?o(s,n):o;let l;if("string"==typeof p){if(l=p===K.Parent?t.self._parent:p===K.Internal?t.self:p.startsWith("#_")?e.children[p.slice(2)]:a.deferredActorIds?.includes(p)?p:e.children[p],!l)throw new Error(`Unable to send event to actor '${p}' from machine '${e.machine.id}'.`)}else l=p||t.self;return[e,{to:l,event:u,id:r,delay:d}]}function Y(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function Z(t,e){t.defer((()=>{const{to:n,event:o,delay:i,id:c}=e;"number"!=typeof i?t.system._relay(t.self,n,o.type===s?r(t.self.id,o.data):o):t.system.scheduler.schedule(t.self,n,o,i,c)}))}function tt(t,e,s){function n(t,e){}return n.type="xsnapshot.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=L,n.retryResolve=Y,n.execute=Z,n}function et(t,e,s,n,{collect:o}){const i=[],r=function(t){i.push(t)};return r.assign=(...t)=>{i.push(U(...t))},r.cancel=(...t)=>{i.push(S(...t))},r.raise=(...t)=>{i.push(H(...t))},r.sendTo=(...t)=>{i.push(tt(...t))},r.spawnChild=(...t)=>{i.push(E(...t))},r.stopChild=(...t)=>{i.push(R(...t))},o({context:s.context,event:s.event,enqueue:r,check:t=>A(t,e.context,s.event,e),self:t.self,system:t.system}),[e,void 0,i]}function st(t,e,s,n,{value:o,label:i}){return[e,{value:"function"==typeof o?o(s,n):o,label:i}]}function nt({logger:t},{value:e,label:s}){s?t(s,e):t(e)}t.assign=U,t.cancel=S,t.enqueueActions=function(t){function e(t,e){}return e.type="xstate.enqueueActions",e.collect=t,e.resolve=et,e},t.forwardTo=function(t,e){return tt(t,(({event:t})=>t),e)},t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function s(t,e){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=st,s.execute=nt,s},t.raise=H,t.sendParent=function(t,e){return tt(K.Parent,t,e)},t.sendTo=tt,t.spawnChild=E,t.stop=$,t.stopChild=R,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actions.umd.min.js.map