xstate 5.0.0-beta.48 → 5.0.0-beta.50

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 (45) 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 +53 -74
  8. package/actors/dist/xstate-actors.development.cjs.js +53 -74
  9. package/actors/dist/xstate-actors.development.esm.js +53 -74
  10. package/actors/dist/xstate-actors.esm.js +53 -74
  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/State.d.ts +20 -17
  14. package/dist/declarations/src/StateMachine.d.ts +13 -14
  15. package/dist/declarations/src/actors/callback.d.ts +6 -9
  16. package/dist/declarations/src/actors/index.d.ts +3 -3
  17. package/dist/declarations/src/actors/observable.d.ts +5 -5
  18. package/dist/declarations/src/actors/promise.d.ts +6 -6
  19. package/dist/declarations/src/actors/transition.d.ts +3 -3
  20. package/dist/declarations/src/createMachine.d.ts +17 -2
  21. package/dist/declarations/src/interpreter.d.ts +2 -2
  22. package/dist/declarations/src/setup.d.ts +19 -3
  23. package/dist/declarations/src/stateUtils.d.ts +1 -1
  24. package/dist/declarations/src/types.d.ts +19 -13
  25. package/dist/declarations/src/utils.d.ts +0 -1
  26. package/dist/{raise-84fd7a92.esm.js → raise-32ec7226.esm.js} +94 -33
  27. package/dist/{raise-286581d5.development.esm.js → raise-6c05c91b.development.esm.js} +94 -33
  28. package/dist/{raise-cd0dde81.cjs.js → raise-8176cd35.cjs.js} +93 -33
  29. package/dist/{raise-0eafc1df.development.cjs.js → raise-dc9c2c58.development.cjs.js} +93 -33
  30. package/dist/{send-f0a3179c.development.esm.js → send-2b001f05.development.esm.js} +2 -7
  31. package/dist/{send-355ba004.cjs.js → send-7f3db830.cjs.js} +2 -7
  32. package/dist/{send-ae491737.esm.js → send-88351a33.esm.js} +2 -7
  33. package/dist/{send-32a63473.development.cjs.js → send-df1c8ef2.development.cjs.js} +2 -7
  34. package/dist/xstate.cjs.js +11 -20
  35. package/dist/xstate.development.cjs.js +11 -20
  36. package/dist/xstate.development.esm.js +13 -22
  37. package/dist/xstate.esm.js +13 -22
  38. package/dist/xstate.umd.min.js +1 -1
  39. package/dist/xstate.umd.min.js.map +1 -1
  40. package/guards/dist/xstate-guards.cjs.js +1 -1
  41. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  42. package/guards/dist/xstate-guards.development.esm.js +1 -1
  43. package/guards/dist/xstate-guards.esm.js +1 -1
  44. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  45. package/package.json +1 -1
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var send = require('../../dist/send-355ba004.cjs.js');
6
- var guards_dist_xstateGuards = require('../../dist/raise-cd0dde81.cjs.js');
5
+ var send = require('../../dist/send-7f3db830.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-8176cd35.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 send = require('../../dist/send-32a63473.development.cjs.js');
6
- var guards_dist_xstateGuards = require('../../dist/raise-0eafc1df.development.cjs.js');
5
+ var send = require('../../dist/send-df1c8ef2.development.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-dc9c2c58.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, c as choose, e as escalate, f as forwardTo, l as log, p as pure, s as sendParent, b as sendTo } from '../../dist/send-f0a3179c.development.esm.js';
2
- export { O as cancel, P as raise, T as spawnChild, Q as stop, R as stopChild } from '../../dist/raise-286581d5.development.esm.js';
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';
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-ae491737.esm.js';
2
- export { O as cancel, P as raise, T as spawnChild, Q as stop, R as stopChild } from '../../dist/raise-84fd7a92.esm.js';
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';
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 l(t)?t:t.split(s)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}(t))}function p(t){return void 0===t?[]:function(t){return l(t)?t:[t]}(t)}function l(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){this._state=t?this.logic.restoreState?this.logic.restoreState(t,this._actorScope):t:this.logic.getInitialState(this._actorScope,this.options?.input)}update(t,e){let s;for(this._state=t;s=this._deferred.shift();)s();for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}switch(this._state.status){case"done":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._stopProcedure(),this._error(this._state.error),this._parent&&this.system._relay(this,this._parent,o(this.id,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._parent.send({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":this.update(this._state,t);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorScope)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(o(this.id,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._stopProcedure(),this._error(t),void this._parent?.send(o(this.id,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()}_error(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)}_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((()=>{if(s._processingStatus!==v.Stopped)try{s.start?.()}catch(s){return void t.self.send(o(e,s))}}))}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 P(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function j(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 O(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]=O(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 P(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 O(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 P(t))s(e)}return s(t),e}(t);for(const s of e)for(const n of j(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((s=t._nodes.map((t=>t.tags)),[].concat(...s))),children:t.children,historyValue:t.historyValue||{},matches:D,hasTag:q,can:J,getMeta:V,toJSON:W};var s}({...t,...e},t.machine)}function B(t,{machine:e,context:s},n,i){return(r,c)=>{const a=((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})})(r,c);return i[a.id]=a,t.defer((()=>{if(a._processingStatus!==v.Stopped)try{a.start?.()}catch(e){return void t.self.send(o(a.id,e))}})),a}}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,p(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,p(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 p;if("string"==typeof f){if(p=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],!p)throw new Error(`Unable to send event to actor '${f}' from machine '${e.machine.id}'.`)}else p=f||t?.self;return[e,{to:p,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;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})}));
2
2
  //# sourceMappingURL=xstate-actions.umd.min.js.map