xstate 5.0.0-beta.29 → 5.0.0-beta.31

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 (52) hide show
  1. package/actions/dist/xstate-actions.cjs.js +3 -3
  2. package/actions/dist/xstate-actions.development.cjs.js +3 -3
  3. package/actions/dist/xstate-actions.development.esm.js +3 -3
  4. package/actions/dist/xstate-actions.esm.js +3 -3
  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 +100 -104
  8. package/actors/dist/xstate-actors.development.cjs.js +100 -104
  9. package/actors/dist/xstate-actors.development.esm.js +100 -104
  10. package/actors/dist/xstate-actors.esm.js +100 -104
  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/Machine.d.ts +2 -2
  14. package/dist/declarations/src/State.d.ts +4 -6
  15. package/dist/declarations/src/StateMachine.d.ts +28 -21
  16. package/dist/declarations/src/StateNode.d.ts +3 -3
  17. package/dist/declarations/src/actions/stop.d.ts +1 -1
  18. package/dist/declarations/src/actors/callback.d.ts +8 -8
  19. package/dist/declarations/src/actors/index.d.ts +3 -3
  20. package/dist/declarations/src/actors/observable.d.ts +12 -13
  21. package/dist/declarations/src/actors/promise.d.ts +11 -14
  22. package/dist/declarations/src/actors/transition.d.ts +10 -7
  23. package/dist/declarations/src/index.d.ts +1 -1
  24. package/dist/declarations/src/interpreter.d.ts +4 -4
  25. package/dist/declarations/src/spawn.d.ts +3 -0
  26. package/dist/declarations/src/stateUtils.d.ts +5 -6
  27. package/dist/declarations/src/types.d.ts +74 -69
  28. package/dist/declarations/src/utils.d.ts +4 -4
  29. package/dist/{interpreter-c357bc50.cjs.js → interpreter-05e11c15.cjs.js} +11 -13
  30. package/dist/{interpreter-e2c6a579.development.cjs.js → interpreter-a2236840.development.cjs.js} +12 -14
  31. package/dist/{interpreter-498891b2.esm.js → interpreter-d5fa7ce0.esm.js} +11 -13
  32. package/dist/{interpreter-6e7909c8.development.esm.js → interpreter-e4d2487f.development.esm.js} +12 -14
  33. package/dist/{raise-59f2c242.esm.js → raise-6a68d0cc.esm.js} +61 -32
  34. package/dist/{raise-e778a828.development.esm.js → raise-6fbd4513.development.esm.js} +61 -32
  35. package/dist/{raise-03e57569.cjs.js → raise-90808d65.cjs.js} +61 -32
  36. package/dist/{raise-f751dfac.development.cjs.js → raise-b4bfe138.development.cjs.js} +61 -32
  37. package/dist/{send-f53778f6.development.cjs.js → send-4163d2af.development.cjs.js} +52 -20
  38. package/dist/{send-51717e53.cjs.js → send-72e85cc6.cjs.js} +52 -20
  39. package/dist/{send-42c83fb2.development.esm.js → send-7baeedcb.development.esm.js} +52 -20
  40. package/dist/{send-fff224db.esm.js → send-e5f0f3f6.esm.js} +52 -20
  41. package/dist/xstate.cjs.js +15 -24
  42. package/dist/xstate.development.cjs.js +18 -24
  43. package/dist/xstate.development.esm.js +21 -27
  44. package/dist/xstate.esm.js +18 -27
  45. package/dist/xstate.umd.min.js +1 -1
  46. package/dist/xstate.umd.min.js.map +1 -1
  47. package/guards/dist/xstate-guards.cjs.js +2 -2
  48. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  49. package/guards/dist/xstate-guards.development.esm.js +2 -2
  50. package/guards/dist/xstate-guards.esm.js +2 -2
  51. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  52. package/package.json +1 -1
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var send = require('../../dist/send-51717e53.cjs.js');
6
- var guards_dist_xstateGuards = require('../../dist/raise-03e57569.cjs.js');
7
- require('../../dist/interpreter-c357bc50.cjs.js');
5
+ var send = require('../../dist/send-72e85cc6.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-90808d65.cjs.js');
7
+ require('../../dist/interpreter-05e11c15.cjs.js');
8
8
  require('../../dev/dist/xstate-dev.cjs.js');
9
9
 
10
10
 
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var send = require('../../dist/send-f53778f6.development.cjs.js');
6
- var guards_dist_xstateGuards = require('../../dist/raise-f751dfac.development.cjs.js');
7
- require('../../dist/interpreter-e2c6a579.development.cjs.js');
5
+ var send = require('../../dist/send-4163d2af.development.cjs.js');
6
+ var guards_dist_xstateGuards = require('../../dist/raise-b4bfe138.development.cjs.js');
7
+ require('../../dist/interpreter-a2236840.development.cjs.js');
8
8
  require('../../dev/dist/xstate-dev.development.cjs.js');
9
9
 
10
10
 
@@ -1,4 +1,4 @@
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-42c83fb2.development.esm.js';
2
- export { z as cancel, A as raise, B as stop } from '../../dist/raise-e778a828.development.esm.js';
3
- import '../../dist/interpreter-6e7909c8.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-7baeedcb.development.esm.js';
2
+ export { z as cancel, A as raise, B as stop } from '../../dist/raise-6fbd4513.development.esm.js';
3
+ import '../../dist/interpreter-e4d2487f.development.esm.js';
4
4
  import '../../dev/dist/xstate-dev.development.esm.js';
@@ -1,4 +1,4 @@
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-fff224db.esm.js';
2
- export { z as cancel, A as raise, B as stop } from '../../dist/raise-59f2c242.esm.js';
3
- import '../../dist/interpreter-498891b2.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-e5f0f3f6.esm.js';
2
+ export { z as cancel, A as raise, B as stop } from '../../dist/raise-6a68d0cc.esm.js';
3
+ import '../../dist/interpreter-d5fa7ce0.esm.js';
4
4
  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";const e=".",n="xstate.error",s="xstate.stop",i=new WeakMap;function o(t,e){return{type:`xstate.error.actor.${t}`,data:e}}function r(t,e,n,{sendId:s}){return[e,"function"==typeof s?s(n):s]}function c(t,e){t.self.cancel(e)}class a{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),t===this._current&&(this._current=this._current.next)}this._last=null}}function u(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const h=t=>{const e=u();e&&e.register(t)};function d(t){setTimeout((()=>{throw t}))}const f="function"==typeof Symbol&&Symbol.observable||"@@observable";function l(t,e){const n=p(t),s=p(e);return"string"==typeof s?"string"==typeof n&&s===n:"string"==typeof n?n in s:Object.keys(n).every((t=>t in s&&l(n[t],s[t])))}function p(t){if("object"==typeof(n=t)&&"value"in n&&"context"in n&&"event"in n)return t.value;var n;if(m(t))return y(t);if("string"!=typeof t)return t;return y(function(t){try{return m(t)?t:t.toString().split(e)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}(t))}function y(t){if(1===t.length)return t[0];const e={};let n=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)n[t[e]]=t[e+1];else{const s=n;n={},s[t[e]]=n}return e}function g(t){return[].concat(...t)}function v(t){return void 0===t?[]:function(t){return m(t)?t:[t]}(t)}function m(t){return Array.isArray(t)}let _=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const b={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class x{constructor(t,e){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new a(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=_.NotStarted,this._parent=void 0,this.ref=void 0,this._actorContext=void 0,this._systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const n={...b,...e},{clock:s,logger:i,parent:o,id:r,systemId:c}=n;this.system=o?.system??function(){let t=0;const e=new Map,n=new Map,s=new WeakMap;return{_bookId:()=>"x:"+t++,_register:(t,n)=>(e.set(t,n),t),_unregister:t=>{e.delete(t.sessionId);const i=s.get(t);void 0!==i&&(n.delete(i),s.delete(t))},get:t=>n.get(t),_set:(t,e)=>{const i=n.get(t);if(i&&i!==e)throw new Error(`Actor with system ID '${t}' already exists.`);n.set(t,e),s.set(e,t)}}}(),c&&(this._systemId=c,this.system._set(c,this)),this.sessionId=this.system._bookId(),this.id=r??this.sessionId,this.logger=i,this.clock=s,this._parent=o,this.options=n,this.src=n.src,this.ref=this,this._actorContext={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()}},this.send=this.send.bind(this),this._initState()}_initState(){this._state=this.options.state?this.logic.restoreState?this.logic.restoreState(this.options.state,this._actorContext):this.options.state:this.logic.getInitialState(this._actorContext,this.options?.input)}update(t){this._state=t;const e=this.getSnapshot();let n;for(;n=this._deferred.shift();)n();for(const t of this.observers)try{t.next?.(e)}catch(t){d(t)}const s=this.logic.getStatus?.(t);switch(s?.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(i=this.id,r=s.data,{type:`xstate.done.actor.${i}`,output:r}),this._parent?.send(this._doneEvent);break;case"error":this._stopProcedure(),this._error(s.data),this._parent?.send(o(this.id,s.data))}var i,r}subscribe(t,e,n){const s=function(t,e,n){const s="object"==typeof t,i=s?t:void 0;return{next:(s?t.next:t)?.bind(i),error:(s?t.error:e)?.bind(i),complete:(s?t.complete:n)?.bind(i)}}(t,e,n);if(this.status!==_.Stopped)this.observers.add(s);else try{s.complete?.()}catch(t){d(t)}return{unsubscribe:()=>{this.observers.delete(s)}}}start(){if(this.status===_.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=_.Running;const t=this.logic.getStatus?.(this._state);switch(t?.status){case"done":this.update(this._state);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorContext)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(o(this.id,t)),this}return this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,n;try{e=this.logic.transition(this._state,t,this._actorContext)}catch(t){n={err:t}}if(n){const{err:t}=n;return this._stopProcedure(),this._error(t),void this._parent?.send(o(this.id,t))}this.update(e),t.type===s&&(this._stopProcedure(),this._complete())}_stop(){return this.status===_.Stopped?this:(this.mailbox.clear(),this.status===_.NotStarted?(this.status=_.Stopped,this):(this.mailbox.enqueue({type:s}),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){d(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||d(t));let e=!1;for(const n of this.observers){const s=n.error;e||=!s;try{s?.(t)}catch(t){d(t)}}this.observers.clear(),e&&d(t)}_stopProcedure(){if(this.status!==_.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new a(this._process.bind(this)),this.status=_.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);this.status!==_.Stopped&&this.mailbox.enqueue(t)}delaySend({event:t,id:e,delay:n,to:s}){const i=this.clock.setTimeout((()=>{s?s.send(t):this.send(t)}),n);e&&(this.delayedEventsMap[e]=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:h)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[f](){return this}getSnapshot(){return this.logic.getSnapshot?this.logic.getSnapshot(this._state):this._state}}function w(t,e){return new x(t,e)}function S(t,e,n,{actorRef:s}){const i="function"==typeof s?s(n):s,o="string"==typeof i?e.children[i]:i;let r=e.children;return o&&(r={...r},delete r[o.id]),[q(e,{children:r}),o]}function E(t,e){e&&(e.status===_.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function I(t,e,n,s){const{machine:i}=s,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 I(r,e,n,s);const c={context:e,event:n,guard:o?void 0:"string"==typeof t?{type:t}:"function"==typeof t.params?{type:t.type,params:t.params({context:e,event:n})}:t};if(!("check"in r))return r(c);return r.check(s,c,r)}const T=t=>"atomic"===t.type||"final"===t.type;function k(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function $(t,e){const n=[];let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function j(t){const e=new Set(t),n=new Set(t),s=P(n);for(const t of e)if("compound"!==t.type||s.get(t)&&s.get(t).length){if("parallel"===t.type)for(const e of k(t))if("history"!==e.type&&!n.has(e))for(const t of A(e))n.add(t)}else A(t).forEach((t=>n.add(t)));for(const t of n){let e=t.parent;for(;e;)n.add(e),e=e.parent}return n}function O(t,e){const n=e.get(t);if(!n)return{};if("compound"===t.type){const t=n[0];if(!t)return{};if(T(t))return t.key}const s={};for(const t of n)s[t.key]=O(t,e);return s}function P(t){const e=new Map;for(const n of t)e.has(n)||e.set(n,[]),n.parent&&(e.has(n.parent)||e.set(n.parent,[]),e.get(n.parent).push(n));return e}function C(t,e){return O(t,P(j(e)))}const M=t=>"#"===t[0];function A(t){const e=new Set;return function n(s){if(!e.has(s))if(e.add(s),"compound"===s.type)for(const i of s.initial.target){for(const n of $(i,t))e.add(n);n(i)}else if("parallel"===s.type)for(const t of k(s))n(t)}(t),[...e]}function R(t,e){if(M(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);const n=t.states[e];if(!n)throw new Error(`Child state '${e}' does not exist on '${t.id}'`);return n}function N(t,e){const n=e instanceof Q?e.value:p(e);if("string"==typeof n)return[t,t.states[n]];const s=Object.keys(n),i=s.map((e=>R(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,s.reduce(((e,s)=>{const i=R(t,s);if(!i)return e;const o=N(i,n[s]);return e.concat(o)}),[]))}class Q{static from(t,e={},n){if(t instanceof Q)return t.context!==e?new Q({value:t.value,context:e,meta:{},configuration:[],children:{}},n):t;const s=j(N(n.root,t));return new Q({value:t,context:e,meta:void 0,configuration:Array.from(s),children:{}},n)}constructor(t,e){this.machine=e,this.tags=void 0,this.value=void 0,this.done=void 0,this.output=void 0,this.error=void 0,this.context=void 0,this.historyValue={},this._internalQueue=void 0,this.configuration=void 0,this.children=void 0,this.context=t.context,this._internalQueue=t._internalQueue??[],this.historyValue=t.historyValue||{},this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(j(N(e.root,t.value))),this.children=t.children,this.value=C(e.root,this.configuration),this.tags=new Set(g(this.configuration.map((t=>t.tags)))),this.done=t.done??!1,this.output=t.output,this.error=t.error}toStrings(t=this.value){if("string"==typeof t)return[t];const n=Object.keys(t);return n.concat(...n.map((n=>this.toStrings(t[n]).map((t=>n+e+t)))))}toJSON(){const{configuration:t,tags:e,machine:n,...s}=this;return{...s,tags:Array.from(e),meta:this.meta}}matches(t){return l(t,this.value)}hasTag(t){return this.tags.has(t)}can(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))}get nextEvents(){return function(t,e,n){let s=i.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},i.set(t,s)),s[e]}(this,"nextEvents",(()=>[...new Set(g([...this.configuration.map((t=>t.ownEvents))]))]))}get meta(){return this.configuration.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})}}function q(t,e={}){return new Q({...t,...e},t.machine)}function D(t,{machine:e,context:n},s,i){return(r,c)=>{const a=((o,r={})=>{const{systemId:c}=r;if("string"==typeof o){const a=function(t){return t?"transition"in t?{src:t,input:void 0}:t:void 0}(e.implementations.actors[o]);if(!a)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const u="input"in r?r.input:a.input,h=w(a.src,{id:r.id,parent:t.self,input:"function"==typeof u?u({context:n,event:s,self:t.self}):u,systemId:c});return i[h.id]=h,h}return w(o,{id:r.id,parent:t.self,input:r.input,systemId:c})})(r,c);return i[a.id]=a,t.defer((()=>{if(a.status!==_.Stopped)try{a.start?.()}catch(e){return void t.self.send(o(a.id,e))}})),a}}function V(t,e,n,{assignment:s}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},o={context:e.context,event:n.event,action:n.action,spawn:D(t,e,n.event,i),self:t?.self,system:t?.system};let r={};if("function"==typeof s)r=s(o);else for(const t of Object.keys(s)){const e=s[t];r[t]="function"==typeof e?e(o):e}return[q(e,{context:Object.assign({},e.context,r),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function W(t,e,n,{branches:s}){const i=s.find((t=>!t.guard||I(t.guard,e.context,n.event,e)))?.actions;return[e,void 0,v(i)]}function B(t,e,n,{value:s,label:i}){return[e,{value:"function"==typeof s?s(n):s,label:i}]}function J({logger:t},{value:e,label:n}){n?t(n,e):t(e)}function U(t,e,n,{get:s}){return[e,void 0,v(s({context:n.context,event:n.event}))]}function z(t,e,n,{event:s,id:i,delay:o}){const r=e.machine.implementations.delays;if("string"==typeof s)throw new Error(`Only event objects may be used with raise; use raise({ type: "${s}" }) instead`);const c="function"==typeof s?s(n):s;let a;if("string"==typeof o){const t=r&&r[o];a="function"==typeof t?t(n):t}else a="function"==typeof o?o(n):o;return["number"!=typeof a?q(e,{_internalQueue:e._internalQueue.concat(c)}):e,{event:c,id:i,delay:a}]}function G(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}let X=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function F(t,e,n,{to:s,event:i,id:o,delay:r}){const c=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 a="function"==typeof i?i(n):i;let u;if("string"==typeof r){const t=c&&c[r];u="function"==typeof t?t(n):t}else u="function"==typeof r?r(n):r;const h="function"==typeof s?s(n):s;let d;if("string"==typeof h){if(d=h===X.Parent?t?.self._parent:h===X.Internal?t?.self:h.startsWith("#_")?e.children[h.slice(2)]:e.children[h],!d)throw new Error(`Unable to send event to actor '${h}' from machine '${e.machine.id}'.`)}else d=h||t?.self;return[e,{to:d,event:a,id:o,delay:u}]}function H(t,e){if("number"==typeof e.delay)return void t.self.delaySend(e);const{to:s,event:i}=e;t.defer((()=>{s.send(i.type===n?o(t.self.id,i.data):i)}))}function K(t,e,n){function s(t){}return s.type="xstate.sendTo",s.to=t,s.event=e,s.id=n?.id,s.delay=n?.delay,s.resolve=F,s.execute=H,s}function L(t,e){return K(X.Parent,t,e)}t.assign=function(t){function e(t){}return e.type="xstate.assign",e.assignment=t,e.resolve=V,e},t.cancel=function(t){function e(t){}return e.type="xstate.cancel",e.sendId=t,e.resolve=r,e.execute=c,e},t.choose=function(t){function e(t){}return e.type="xstate.choose",e.branches=t,e.resolve=W,e},t.escalate=function(t,e){return L((e=>({type:n,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return K(t,(({event:t})=>t),e)},t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function n(t){}return n.type="xstate.log",n.value=t,n.label=e,n.resolve=B,n.execute=J,n},t.pure=function(t){function e(t){}return e.type="xstate.pure",e.get=t,e.resolve=U,e},t.raise=function(t,e){function n(t){}return n.type="xstate.raise",n.event=t,n.id=e?.id,n.delay=e?.delay,n.resolve=z,n.execute=G,n},t.sendParent=L,t.sendTo=K,t.stop=function(t){function e(t){}return e.type="xstate.stop",e.actorRef=t,e.resolve=S,e.execute=E,e},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";const e=".",s="xstate.error",n="xstate.stop",i=new WeakMap;function o(t,e){return{type:`xstate.error.actor.${t}`,data:e}}function r(t,e,s,{sendId:n}){return[e,"function"==typeof n?n(s):n]}function c(t,e){t.self.cancel(e)}class a{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),t===this._current&&(this._current=this._current.next)}this._last=null}}function u(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const h=t=>{const e=u();e&&e.register(t)};function d(t){setTimeout((()=>{throw t}))}const f="function"==typeof Symbol&&Symbol.observable||"@@observable";function l(t,e){const s=p(t),n=p(e);return"string"==typeof n?"string"==typeof s&&n===s:"string"==typeof s?s in n:Object.keys(s).every((t=>t in n&&l(s[t],n[t])))}function p(t){if("object"==typeof(s=t)&&"value"in s&&"context"in s&&"event"in s)return t.value;var s;if(_(t))return y(t);if("string"!=typeof t)return t;return y(function(t){try{return _(t)?t:t.toString().split(e)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}(t))}function y(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 v(t){return[].concat(...t)}function g(t){return void 0===t?[]:function(t){return _(t)?t:[t]}(t)}function _(t){return Array.isArray(t)}let m=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const b={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class x{constructor(t,e){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new a(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=m.NotStarted,this._parent=void 0,this.ref=void 0,this._actorContext=void 0,this._systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const s={...b,...e},{clock:n,logger:i,parent:o,id:r,systemId:c}=s;this.system=o?.system??function(){let t=0;const e=new Map,s=new Map,n=new WeakMap;return{_bookId:()=>"x:"+t++,_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)}}}(),c&&(this._systemId=c,this.system._set(c,this)),this.sessionId=this.system._bookId(),this.id=r??this.sessionId,this.logger=i,this.clock=n,this._parent=o,this.options=s,this.src=s.src,this.ref=this,this._actorContext={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()}},this.send=this.send.bind(this),this._initState()}_initState(){this._state=this.options.state?this.logic.restoreState?this.logic.restoreState(this.options.state,this._actorContext):this.options.state:this.logic.getInitialState(this._actorContext,this.options?.input)}update(t){let e;for(this._state=t;e=this._deferred.shift();)e();for(const e of this.observers)try{e.next?.(t)}catch(t){d(t)}switch(this._state.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(s=this.id,n=this._state.output,{type:`xstate.done.actor.${s}`,output:n}),this._parent?.send(this._doneEvent);break;case"error":this._stopProcedure(),this._error(this._state.error),this._parent?.send(o(this.id,this._state.error))}var s,n}subscribe(t,e,s){const n=function(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)}}(t,e,s);if(this.status!==m.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){d(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this.status===m.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=m.Running;switch(this._state.status){case"done":this.update(this._state);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorContext)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(o(this.id,t)),this}return this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._state,t,this._actorContext)}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.type===n&&(this._stopProcedure(),this._complete())}_stop(){return this.status===m.Stopped?this:(this.mailbox.clear(),this.status===m.NotStarted?(this.status=m.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){d(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||d(t));let e=!1;for(const s of this.observers){const n=s.error;e||=!n;try{n?.(t)}catch(t){d(t)}}this.observers.clear(),e&&d(t)}_stopProcedure(){if(this.status!==m.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new a(this._process.bind(this)),this.status=m.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);this.status!==m.Stopped&&this.mailbox.enqueue(t)}delaySend({event:t,id:e,delay:s,to:n}){const i=this.clock.setTimeout((()=>{n?n.send(t):this.send(t)}),s);e&&(this.delayedEventsMap[e]=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:h)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[f](){return this}getSnapshot(){return this._state}}function w(t,e){return new x(t,e)}function S(t,e,s,{actorRef:n}){const i="function"==typeof n?n(s):n,o="string"==typeof i?e.children[i]:i;let r=e.children;return o&&(r={...r},delete r[o.id]),[q(e,{children:r}),o]}function E(t,e){e&&(e.status===m.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function I(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 I(r,e,s,n);const c={context:e,event:s,guard:o?void 0:"string"==typeof t?{type:t}:"function"==typeof t.params?{type:t.type,params:t.params({context:e,event:s})}:t};if(!("check"in r))return r(c);return r.check(n,c,r)}const T=t=>"atomic"===t.type||"final"===t.type;function k(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function $(t,e){const s=[];let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function j(t){const e=new Set(t),s=new Set(t),n=P(s);for(const t of e)if("compound"!==t.type||n.get(t)&&n.get(t).length){if("parallel"===t.type)for(const e of k(t))if("history"!==e.type&&!s.has(e))for(const t of A(e))s.add(t)}else A(t).forEach((t=>s.add(t)));for(const t of s){let e=t.parent;for(;e;)s.add(e),e=e.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(T(t))return t.key}const n={};for(const t of s)n[t.key]=O(t,e);return n}function P(t){const e=new Map;for(const s of t)e.has(s)||e.set(s,[]),s.parent&&(e.has(s.parent)||e.set(s.parent,[]),e.get(s.parent).push(s));return e}function C(t,e){return O(t,P(j(e)))}const M=t=>"#"===t[0];function A(t){const e=new Set;return function s(n){if(!e.has(n))if(e.add(n),"compound"===n.type)for(const i of n.initial.target){for(const s of $(i,t))e.add(s);s(i)}else if("parallel"===n.type)for(const t of k(n))s(t)}(t),[...e]}function R(t,e){if(M(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);const s=t.states[e];if(!s)throw new Error(`Child state '${e}' does not exist on '${t.id}'`);return s}function N(t,e){const s=e instanceof Q?e.value:p(e);if("string"==typeof s)return[t,t.states[s]];const n=Object.keys(s),i=n.map((e=>R(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,n.reduce(((e,n)=>{const i=R(t,n);if(!i)return e;const o=N(i,s[n]);return e.concat(o)}),[]))}class Q{static from(t,e={},s){if(t instanceof Q)return t.context!==e?new Q({value:t.value,context:e,meta:{},configuration:[],children:{},status:"active"},s):t;const n=j(N(s.root,t));return new Q({value:t,context:e,meta:void 0,configuration:Array.from(n),children:{},status:"active"},s)}constructor(t,e){this.machine=e,this.tags=void 0,this.value=void 0,this.status=void 0,this.error=void 0,this.context=void 0,this.historyValue={},this._internalQueue=void 0,this.configuration=void 0,this.children=void 0,this.context=t.context,this._internalQueue=t._internalQueue??[],this.historyValue=t.historyValue||{},this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(j(N(e.root,t.value))),this.children=t.children,this.value=C(e.root,this.configuration),this.tags=new Set(v(this.configuration.map((t=>t.tags)))),this.status=t.status,this.output=t.output,this.error=t.error}toStrings(t=this.value){if("string"==typeof t)return[t];const s=Object.keys(t);return s.concat(...s.map((s=>this.toStrings(t[s]).map((t=>s+e+t)))))}toJSON(){const{configuration:t,tags:e,machine:s,...n}=this;return{...n,tags:Array.from(e),meta:this.meta}}matches(t){return l(t,this.value)}hasTag(t){return this.tags.has(t)}can(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))}get nextEvents(){return function(t,e,s){let n=i.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},i.set(t,n)),n[e]}(this,"nextEvents",(()=>[...new Set(v([...this.configuration.map((t=>t.ownEvents))]))]))}get meta(){return this.configuration.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})}}function q(t,e={}){return new Q({...t,...e},t.machine)}function D(t,{machine:e,context:s},n,i){return(r,c)=>{const a=((o,r={})=>{const{systemId:c}=r;if("string"==typeof o){const a=function(t){return t?"transition"in t?{src:t,input:void 0}:t:void 0}(e.implementations.actors[o]);if(!a)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const u="input"in r?r.input:a.input,h=w(a.src,{id:r.id,parent:t.self,input:"function"==typeof u?u({context:s,event:n,self:t.self}):u,systemId:c});return i[h.id]=h,r.syncSnapshot&&h.subscribe({next:e=>{"active"===e.status&&t.self.send({type:`xstate.snapshot.${h.id}`,snapshot:e})},error:()=>{}}),h}{const e=w(o,{id:r.id,parent:t.self,input:r.input,systemId:c});return r.syncSnapshot&&e.subscribe({next:s=>{"active"===s.status&&t.self.send({type:`xstate.snapshot.${e.id}`,snapshot:s,id:e.id})},error:()=>{}}),e}})(r,c);return i[a.id]=a,t.defer((()=>{if(a.status!==m.Stopped)try{a.start?.()}catch(e){return void t.self.send(o(a.id,e))}})),a}}function V(t,e,s,{assignment:n}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},o={context:e.context,event:s.event,action:s.action,spawn:D(t,e,s.event,i),self:t?.self,system:t?.system};let r={};if("function"==typeof n)r=n(o);else for(const t of Object.keys(n)){const e=n[t];r[t]="function"==typeof e?e(o):e}return[q(e,{context:Object.assign({},e.context,r),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function W(t,e,s,{branches:n}){const i=n.find((t=>!t.guard||I(t.guard,e.context,s.event,e)))?.actions;return[e,void 0,g(i)]}function B(t,e,s,{value:n,label:i}){return[e,{value:"function"==typeof n?n(s):n,label:i}]}function J({logger:t},{value:e,label:s}){s?t(s,e):t(e)}function U(t,e,s,{get:n}){return[e,void 0,g(n({context:s.context,event:s.event}))]}function z(t,e,s,{event:n,id:i,delay:o}){const r=e.machine.implementations.delays;if("string"==typeof n)throw new Error(`Only event objects may be used with raise; use raise({ type: "${n}" }) instead`);const c="function"==typeof n?n(s):n;let a;if("string"==typeof o){const t=r&&r[o];a="function"==typeof t?t(s):t}else a="function"==typeof o?o(s):o;return["number"!=typeof a?q(e,{_internalQueue:e._internalQueue.concat(c)}):e,{event:c,id:i,delay:a}]}function G(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}let X=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function F(t,e,s,{to:n,event:i,id:o,delay:r}){const c=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 a="function"==typeof i?i(s):i;let u;if("string"==typeof r){const t=c&&c[r];u="function"==typeof t?t(s):t}else u="function"==typeof r?r(s):r;const h="function"==typeof n?n(s):n;let d;if("string"==typeof h){if(d=h===X.Parent?t?.self._parent:h===X.Internal?t?.self:h.startsWith("#_")?e.children[h.slice(2)]:e.children[h],!d)throw new Error(`Unable to send event to actor '${h}' from machine '${e.machine.id}'.`)}else d=h||t?.self;return[e,{to:d,event:a,id:o,delay:u}]}function H(t,e){if("number"==typeof e.delay)return void t.self.delaySend(e);const{to:n,event:i}=e;t.defer((()=>{n.send(i.type===s?o(t.self.id,i.data):i)}))}function K(t,e,s){function n(t){}return n.type="xstate.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=F,n.execute=H,n}function L(t,e){return K(X.Parent,t,e)}t.assign=function(t){function e(t){}return e.type="xstate.assign",e.assignment=t,e.resolve=V,e},t.cancel=function(t){function e(t){}return e.type="xstate.cancel",e.sendId=t,e.resolve=r,e.execute=c,e},t.choose=function(t){function e(t){}return e.type="xstate.choose",e.branches=t,e.resolve=W,e},t.escalate=function(t,e){return L((e=>({type:s,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return K(t,(({event:t})=>t),e)},t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function s(t){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=B,s.execute=J,s},t.pure=function(t){function e(t){}return e.type="xstate.pure",e.get=t,e.resolve=U,e},t.raise=function(t,e){function s(t){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=z,s.execute=G,s},t.sendParent=L,t.sendTo=K,t.stop=function(t){function e(t){}return e.type="xstate.stop",e.actorRef=t,e.resolve=S,e.execute=E,e},Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actions.umd.min.js.map