xstate 5.0.0-beta.20 → 5.0.0-beta.21

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 (40) hide show
  1. package/actions/dist/xstate-actions.cjs.js +1 -1
  2. package/actions/dist/xstate-actions.development.cjs.js +1 -1
  3. package/actions/dist/xstate-actions.development.esm.js +1 -1
  4. package/actions/dist/xstate-actions.esm.js +1 -1
  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/{actions-b299d008.development.esm.js → actions-49f0501e.development.esm.js} +136 -63
  14. package/dist/{actions-a8a9433c.esm.js → actions-5039c951.esm.js} +134 -64
  15. package/dist/{actions-d1c41ed3.development.cjs.js → actions-a95d2e66.development.cjs.js} +136 -62
  16. package/dist/{actions-069d9805.cjs.js → actions-c619a105.cjs.js} +134 -63
  17. package/dist/declarations/src/Machine.d.ts +2 -2
  18. package/dist/declarations/src/State.d.ts +4 -7
  19. package/dist/declarations/src/StateMachine.d.ts +7 -6
  20. package/dist/declarations/src/StateNode.d.ts +3 -3
  21. package/dist/declarations/src/actions/send.d.ts +1 -1
  22. package/dist/declarations/src/actions/stop.d.ts +1 -1
  23. package/dist/declarations/src/actions.d.ts +2 -2
  24. package/dist/declarations/src/actors/callback.d.ts +4 -4
  25. package/dist/declarations/src/actors/observable.d.ts +7 -4
  26. package/dist/declarations/src/actors/promise.d.ts +4 -4
  27. package/dist/declarations/src/types.d.ts +41 -29
  28. package/dist/declarations/src/utils.d.ts +2 -2
  29. package/dist/xstate.cjs.js +11 -10
  30. package/dist/xstate.development.cjs.js +11 -10
  31. package/dist/xstate.development.esm.js +12 -11
  32. package/dist/xstate.esm.js +12 -11
  33. package/dist/xstate.umd.min.js +1 -1
  34. package/dist/xstate.umd.min.js.map +1 -1
  35. package/guards/dist/xstate-guards.cjs.js +1 -1
  36. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  37. package/guards/dist/xstate-guards.development.esm.js +1 -1
  38. package/guards/dist/xstate-guards.esm.js +1 -1
  39. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  40. package/package.json +1 -1
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var actors_dist_xstateActors = require('../../dist/actions-069d9805.cjs.js');
5
+ var actors_dist_xstateActors = require('../../dist/actions-c619a105.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.cjs.js');
7
7
 
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var actors_dist_xstateActors = require('../../dist/actions-d1c41ed3.development.cjs.js');
5
+ var actors_dist_xstateActors = require('../../dist/actions-a95d2e66.development.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.development.cjs.js');
7
7
 
8
8
 
@@ -1,2 +1,2 @@
1
- export { ah as after, x as assign, P as cancel, Q as choose, ag as constantPrefixes, y as createInitEvent, ai as done, O as doneInvoke, B as error, aj as escalate, K as forwardTo, R as log, T as pure, U as raise, J as sendParent, I as sendTo, V as stop } from '../../dist/actions-b299d008.development.esm.js';
1
+ export { ah as after, y as assign, P as cancel, Q as choose, ag as constantPrefixes, z as createInitEvent, ai as done, O as doneInvoke, aj as error, ak as escalate, K as forwardTo, R as log, T as pure, U as raise, J as sendParent, I as sendTo, V as stop } from '../../dist/actions-49f0501e.development.esm.js';
2
2
  import '../../dev/dist/xstate-dev.development.esm.js';
@@ -1,2 +1,2 @@
1
- export { ah as after, x as assign, P as cancel, Q as choose, ag as constantPrefixes, y as createInitEvent, ai as done, O as doneInvoke, B as error, aj as escalate, K as forwardTo, R as log, T as pure, U as raise, J as sendParent, I as sendTo, V as stop } from '../../dist/actions-a8a9433c.esm.js';
1
+ export { ah as after, y as assign, P as cancel, Q as choose, ag as constantPrefixes, z as createInitEvent, ai as done, O as doneInvoke, aj as error, ak as escalate, K as forwardTo, R as log, T as pure, U as raise, J as sendParent, I as sendTo, V as stop } from '../../dist/actions-5039c951.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
@@ -1,2 +1,2 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActions={})}(this,(function(t){"use strict";let e=function(t){return t.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t}({}),n=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});const s=e.After,i=e.DoneState,o=e.ErrorExecution,r=e.ErrorPlatform,a=e.ErrorCustom;var c=Object.freeze({__proto__:null,after:s,doneState:i,errorExecution:o,errorPlatform:r,error:a});const u=".";function h(t,e,s,{to:i,event:o,id:r,delay:a}){const c=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):o;let h;if("string"==typeof a){const t=c&&c[a];h="function"==typeof t?t(s):t}else h="function"==typeof a?a(s):a;const f="function"==typeof i?i(s):i;let d;if("string"==typeof f){if(d=f===n.Parent?t?.self._parent:f===n.Internal?t?.self:f.startsWith("#_")?e.children[f.slice(2)]:e.children[f],!d)throw new Error(`Unable to send event to actor '${f}' from machine '${e.machine.id}'.`)}else d=f||t?.self;return[e,{to:d,event:u,id:r,delay:h}]}function f(t,e){if("number"==typeof e.delay)return void t.self.delaySend(e);const{to:n,event:s}=e;t.defer((()=>{n.send(s.type===a?{type:`${it(t.self.id)}`,data:s.data}:s)}))}function d(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=h,s.execute=f,s}function l(t,e){return d(n.Parent,t,e)}const p=new WeakMap;function y(t,e,n,{sendId:s}){return[e,"function"==typeof s?s(n):s]}function v(t,e){t.self.cancel(e)}class m{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),t===this._current&&(this._current=this._current.next)}this._last=null}}const g="function"==typeof Symbol&&Symbol.observable||"@@observable";function _(t,e){const n=b(t),s=b(e);return"string"==typeof s?"string"==typeof n&&s===n:"string"==typeof n?n in s:Object.keys(n).every((t=>t in s&&_(n[t],s[t])))}function b(t){if("object"==typeof(e=t)&&"value"in e&&"context"in e&&"event"in e)return t.value;var e;if(E(t))return x(t);if("string"!=typeof t)return t;return x(function(t){try{return E(t)?t:t.toString().split(u)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}(t))}function x(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 w(t){return[].concat(...t)}function S(t){return void 0===t?[]:function(t){return E(t)?t:[t]}(t)}function E(t){return Array.isArray(t)}const $="xstate.stop";function I(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const k=t=>{const e=I();e&&e.register(t)};let T=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const j={deferEvents:!0,clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class O{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 m(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=T.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={...j,...e},{clock:s,logger:i,parent:o,id:r,systemId:a}=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)}}}(),a&&(this._systemId=a,this.system._set(a,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)t.next?.(e);const s=this.logic.getStatus?.(t);switch(s?.status){case"done":this._stopProcedure(),this._doneEvent=st(this.id,s.data),this._parent?.send(this._doneEvent),this._complete();break;case"error":this._stopProcedure(),this._parent?.send(it(this.id,s.data)),this._error(s.data)}}subscribe(t,e,n){const s=function(t,e,n){const s=()=>{},i="object"==typeof t,o=i?t:null;return{next:((i?t.next:t)||s).bind(o),error:((i?t.error:e)||s).bind(o),complete:((i?t.complete:n)||s).bind(o)}}(t,e,n);return this.observers.add(s),this.status===T.Stopped&&(s.complete?.(),this.observers.delete(s)),{unsubscribe:()=>{this.observers.delete(s)}}}start(){return this.status===T.Running||(this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=T.Running,this.logic.start&&this.logic.start(this._state,this._actorContext),this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start()),this}_process(t){try{const e=this.logic.transition(this._state,t,this._actorContext);this.update(e),t.type===$&&(this._stopProcedure(),this._complete())}catch(t){if(!(this.observers.size>0))throw t;this.observers.forEach((e=>{e.error?.(t)})),this.stop()}}_stop(){return this.status===T.Stopped?this:(this.mailbox.clear(),this.status===T.NotStarted?(this.status=T.Stopped,this):(this.mailbox.enqueue({type:$}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)t.complete?.();this.observers.clear()}_error(t){for(const e of this.observers)e.error?.(t);this.observers.clear()}_stopProcedure(){if(this.status!==T.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new m(this._process.bind(this)),this.status=T.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);if(this.status!==T.Stopped){if(this.status!==T.Running&&!this.options.deferEvents)throw new Error(`Event "${t.type}" was sent to uninitialized actor "${this.id}". Make sure .start() is called for this actor, or set { deferEvents: true } in the actor options.\nEvent: ${JSON.stringify(t)}`);this.mailbox.enqueue(t)}}delaySend({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:k)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[g](){return this}getSnapshot(){return this.logic.getSnapshot?this.logic.getSnapshot(this._state):this._state}}function P(t,e){return new O(t,e)}function C(t,e,n,s){const{machine:i}=s,o=i?.implementations?.guards?.[t.type]??t.predicate;if(!o)throw new Error(`Guard '${t.type}' is not implemented.'.`);return o({context:e,event:n,state:s,guard:t,evaluate:C})}function M(t,e){if("string"==typeof t){const n=e?.(t);return"function"==typeof n?{type:t,predicate:n,params:{type:t}}:n||{type:t,params:{type:t}}}if("function"==typeof t)return{type:t.name,predicate:t,params:{type:t.name,name:t.name}};const n=e?.(t.type);return"function"==typeof n?{type:t.type,params:t.params||t,children:t.children?.map((t=>M(t,e))),predicate:e?.(t.type)||t.predicate}:n||{type:t.type,params:t.params||t,children:t.children?.map((t=>M(t,e))),predicate:t.predicate}}const A=t=>"atomic"===t.type||"final"===t.type;function D(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function R(t,e){const n=[];let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function N(t){const e=new Set(t),n=new Set(t),s=q(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 D(t))if("history"!==e.type&&!n.has(e))for(const t of V(e))n.add(t)}else V(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 Q(t,e){const n=e.get(t);if(!n)return{};if("compound"===t.type){const t=n[0];if(!t)return{};if(A(t))return t.key}const s={};for(const t of n)s[t.key]=Q(t,e);return s}function q(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 z(t,e){return Q(t,q(N(e)))}const J=t=>"#"===t[0];function V(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 R(i,t))e.add(n);n(i)}else if("parallel"===s.type)for(const t of D(s))n(t)}(t),[...e]}function W(t,e){if(J(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 B(t,e){const n=e instanceof U?e.value:b(e);if("string"==typeof n)return[t,t.states[n]];const s=Object.keys(n),i=s.map((e=>W(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,s.reduce(((e,s)=>{const i=W(t,s);if(!i)return e;const o=B(i,n[s]);return e.concat(o)}),[]))}class U{static from(t,e={},n){if(t instanceof U)return t.context!==e?new U({value:t.value,context:e,meta:{},configuration:[],children:{}},n):t;const s=N(B(n.root,t));return new U({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.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(N(B(e.root,t.value))),this.children=t.children,this.value=z(e.root,this.configuration),this.tags=new Set(w(this.configuration.map((t=>t.tags)))),this.done=t.done??!1,this.output=t.output}toStrings(t=this.value){if("string"==typeof t)return[t];const e=Object.keys(t);return e.concat(...e.map((e=>this.toStrings(t[e]).map((t=>e+u+t)))))}toJSON(){const{configuration:t,tags:e,machine:n,...s}=this;return{...s,tags:Array.from(e),meta:this.meta}}matches(t){return _(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=p.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},p.set(t,s)),s[e]}(this,"nextEvents",(()=>[...new Set(w([...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 G(t,e={}){return new U({...t,...e},t.machine)}function X(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]),[G(e,{children:r}),o]}function F(t,e){e&&(e.status===T.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function H(t,e,n,{value:s,label:i}){return[e,{value:"function"==typeof s?s(n):s,label:i}]}function K({logger:t},{value:e,label:n}){n?t(n,e):t(e)}function L(t,{machine:e,context:n},s,i){return(o,r)=>{const a=((o,r={})=>{const{systemId:a}=r;if("string"==typeof o){const c=function(t){return t?"transition"in t?{src:t,input:void 0}:t:void 0}(e.implementations.actors[o]);if(!c)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const u="input"in r?r.input:c.input,h=P(c.src,{id:r.id,parent:t.self,input:"function"==typeof u?u({context:n,event:s,self:t.self}):u,systemId:a});return i[h.id]=h,h}return P(o,{id:r.id,parent:t.self,input:r.input,systemId:a})})(o,r);return i[a.id]=a,t.defer((()=>{if(a.status!==T.Stopped)try{a.start?.()}catch(e){return void t.self.send(it(a.id,e))}})),a}}function Y(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:L(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[G(e,{context:Object.assign({},e.context,r),children:Object.keys(i).length?{...e.children,...i}:e.children})]}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 a="function"==typeof s?s(n):s;let c;if("string"==typeof o){const t=r&&r[o];c="function"==typeof t?t(n):t}else c="function"==typeof o?o(n):o;return["number"!=typeof c?G(e,{_internalQueue:e._internalQueue.concat(a)}):e,{event:a,id:i,delay:c}]}function tt(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}function et(t,e,n,{branches:s}){const i=s.find((t=>{const s=t.guard&&M(t.guard,(t=>e.machine.implementations.guards[t]));return!s||C(s,e.context,n.event,e)}))?.actions;return[e,void 0,S(i)]}function nt(t,e,n,{get:s}){return[e,void 0,S(s({context:e.context,event:n.event}))]}function st(t,n){const s=`${e.DoneInvoke}.${t}`,i={type:s,output:n,toString:()=>s};return i}function it(t,n){const s=`${e.ErrorPlatform}.${t}`,i={type:s,data:n,toString:()=>s};return i}t.after=function(t,n){const s=n?`#${n}`:"";return`${e.After}(${t})${s}`},t.assign=function(t){function e(t){}return e.type="xstate.assign",e.assignment=t,e.resolve=Y,e},t.cancel=function(t){function e(t){}return e.type="xstate.cancel",e.sendId=t,e.resolve=y,e.execute=v,e},t.choose=function(t){function e(t){}return e.type="xstate.choose",e.branches=t,e.resolve=et,e},t.constantPrefixes=c,t.createInitEvent=function(t){return{type:"xstate.init",input:t}},t.done=function(t,n){const s=`${e.DoneState}.${t}`,i={type:s,output:n,toString:()=>s};return i},t.doneInvoke=st,t.error=it,t.escalate=function(t,e){return l((e=>({type:a,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return d(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=H,n.execute=K,n},t.pure=function(t){function e(t){}return e.type="xstate.pure",e.get=t,e.resolve=nt,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=tt,n},t.sendParent=l,t.sendTo=d,t.stop=function(t){function e(t){}return e.type="xstate.stop",e.actorRef=t,e.resolve=X,e.execute=F,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";let e=function(t){return t.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t}({}),n=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});const s=e.After,i=e.DoneState,o=e.ErrorExecution,r=e.ErrorPlatform,c=e.ErrorCustom;var a=Object.freeze({__proto__:null,after:s,doneState:i,errorExecution:o,errorPlatform:r,error:c});const u=".";function h(t,e,s,{to:i,event:o,id:r,delay:c}){const a=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):o;let h;if("string"==typeof c){const t=a&&a[c];h="function"==typeof t?t(s):t}else h="function"==typeof c?c(s):c;const d="function"==typeof i?i(s):i;let f;if("string"==typeof d){if(f=d===n.Parent?t?.self._parent:d===n.Internal?t?.self:d.startsWith("#_")?e.children[d.slice(2)]:e.children[d],!f)throw new Error(`Unable to send event to actor '${d}' from machine '${e.machine.id}'.`)}else f=d||t?.self;return[e,{to:f,event:u,id:r,delay:h}]}function d(t,e){if("number"==typeof e.delay)return void t.self.delaySend(e);const{to:n,event:s}=e;t.defer((()=>{n.send(s.type===c?{type:`${ot(t.self.id)}`,data:s.data}:s)}))}function f(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=h,s.execute=d,s}function l(t,e){return f(n.Parent,t,e)}const p=new WeakMap;function y(t,e,n,{sendId:s}){return[e,"function"==typeof s?s(n):s]}function v(t,e){t.self.cancel(e)}class m{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),t===this._current&&(this._current=this._current.next)}this._last=null}}const g="function"==typeof Symbol&&Symbol.observable||"@@observable";function _(t,e){const n=x(t),s=x(e);return"string"==typeof s?"string"==typeof n&&s===n:"string"==typeof n?n in s:Object.keys(n).every((t=>t in s&&_(n[t],s[t])))}function x(t){if("object"==typeof(e=t)&&"value"in e&&"context"in e&&"event"in e)return t.value;var e;if(E(t))return b(t);if("string"!=typeof t)return t;return b(function(t){try{return E(t)?t:t.toString().split(u)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}(t))}function b(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 w(t){return[].concat(...t)}function S(t){return void 0===t?[]:function(t){return E(t)?t:[t]}(t)}function E(t){return Array.isArray(t)}const $="xstate.stop";function I(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const k=t=>{const e=I();e&&e.register(t)};function T(t){setTimeout((()=>{throw t}))}let P=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const j={deferEvents:!0,clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class O{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 m(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=P.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={...j,...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){T(t)}const s=this.logic.getStatus?.(t);switch(s?.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=it(this.id,s.data),this._parent?.send(this._doneEvent);break;case"error":this._stopProcedure(),this._error(s.data),this._parent?.send(ot(this.id,s.data))}}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!==P.Stopped)this.observers.add(s);else try{s.complete?.()}catch(t){T(t)}return{unsubscribe:()=>{this.observers.delete(s)}}}start(){if(this.status===P.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=P.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(ot(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(ot(this.id,t))}this.update(e),t.type===$&&(this._stopProcedure(),this._complete())}_stop(){return this.status===P.Stopped?this:(this.mailbox.clear(),this.status===P.NotStarted?(this.status=P.Stopped,this):(this.mailbox.enqueue({type:$}),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){T(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||T(t));let e=!1;for(const n of this.observers){const s=n.error;e||=!s;try{s?.(t)}catch(t){T(t)}}this.observers.clear(),e&&T(t)}_stopProcedure(){if(this.status!==P.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new m(this._process.bind(this)),this.status=P.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);if(this.status!==P.Stopped){if(this.status!==P.Running&&!this.options.deferEvents)throw new Error(`Event "${t.type}" was sent to uninitialized actor "${this.id}". Make sure .start() is called for this actor, or set { deferEvents: true } in the actor options.\nEvent: ${JSON.stringify(t)}`);this.mailbox.enqueue(t)}}delaySend({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:k)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[g](){return this}getSnapshot(){return this.logic.getSnapshot?this.logic.getSnapshot(this._state):this._state}}function C(t,e){return new O(t,e)}function M(t,e,n,s){const{machine:i}=s,o=i?.implementations?.guards?.[t.type]??t.predicate;if(!o)throw new Error(`Guard '${t.type}' is not implemented.'.`);return o({context:e,event:n,state:s,guard:t,evaluate:M})}function A(t,e){if("string"==typeof t){const n=e?.(t);return"function"==typeof n?{type:t,predicate:n,params:{type:t}}:n||{type:t,params:{type:t}}}if("function"==typeof t)return{type:t.name,predicate:t,params:{type:t.name,name:t.name}};const n=e?.(t.type);return"function"==typeof n?{type:t.type,params:t.params||t,children:t.children?.map((t=>A(t,e))),predicate:e?.(t.type)||t.predicate}:n||{type:t.type,params:t.params||t,children:t.children?.map((t=>A(t,e))),predicate:t.predicate}}const D=t=>"atomic"===t.type||"final"===t.type;function R(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function N(t,e){const n=[];let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function Q(t){const e=new Set(t),n=new Set(t),s=z(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 R(t))if("history"!==e.type&&!n.has(e))for(const t of W(e))n.add(t)}else W(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 q(t,e){const n=e.get(t);if(!n)return{};if("compound"===t.type){const t=n[0];if(!t)return{};if(D(t))return t.key}const s={};for(const t of n)s[t.key]=q(t,e);return s}function z(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 J(t,e){return q(t,z(Q(e)))}const V=t=>"#"===t[0];function W(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 N(i,t))e.add(n);n(i)}else if("parallel"===s.type)for(const t of R(s))n(t)}(t),[...e]}function B(t,e){if(V(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 U(t,e){const n=e instanceof G?e.value:x(e);if("string"==typeof n)return[t,t.states[n]];const s=Object.keys(n),i=s.map((e=>B(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,s.reduce(((e,s)=>{const i=B(t,s);if(!i)return e;const o=U(i,n[s]);return e.concat(o)}),[]))}class G{static from(t,e={},n){if(t instanceof G)return t.context!==e?new G({value:t.value,context:e,meta:{},configuration:[],children:{}},n):t;const s=Q(U(n.root,t));return new G({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(Q(U(e.root,t.value))),this.children=t.children,this.value=J(e.root,this.configuration),this.tags=new Set(w(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 e=Object.keys(t);return e.concat(...e.map((e=>this.toStrings(t[e]).map((t=>e+u+t)))))}toJSON(){const{configuration:t,tags:e,machine:n,...s}=this;return{...s,tags:Array.from(e),meta:this.meta}}matches(t){return _(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=p.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},p.set(t,s)),s[e]}(this,"nextEvents",(()=>[...new Set(w([...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 X(t,e={}){return new G({...t,...e},t.machine)}function F(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]),[X(e,{children:r}),o]}function H(t,e){e&&(e.status===P.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function K(t,e,n,{value:s,label:i}){return[e,{value:"function"==typeof s?s(n):s,label:i}]}function L({logger:t},{value:e,label:n}){n?t(n,e):t(e)}function Y(t,{machine:e,context:n},s,i){return(o,r)=>{const c=((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=C(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 C(o,{id:r.id,parent:t.self,input:r.input,systemId:c})})(o,r);return i[c.id]=c,t.defer((()=>{if(c.status!==P.Stopped)try{c.start?.()}catch(e){return void t.self.send(ot(c.id,e))}})),c}}function Z(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:Y(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[X(e,{context:Object.assign({},e.context,r),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function tt(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?X(e,{_internalQueue:e._internalQueue.concat(c)}):e,{event:c,id:i,delay:a}]}function et(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}function nt(t,e,n,{branches:s}){const i=s.find((t=>{const s=t.guard&&A(t.guard,(t=>e.machine.implementations.guards[t]));return!s||M(s,e.context,n.event,e)}))?.actions;return[e,void 0,S(i)]}function st(t,e,n,{get:s}){return[e,void 0,S(s({context:e.context,event:n.event}))]}function it(t,n){const s=`${e.DoneInvoke}.${t}`,i={type:s,output:n,toString:()=>s};return i}function ot(t,n){const s=`${e.ErrorPlatform}.${t}`,i={type:s,data:n,toString:()=>s};return i}t.after=function(t,n){const s=n?`#${n}`:"";return`${e.After}(${t})${s}`},t.assign=function(t){function e(t){}return e.type="xstate.assign",e.assignment=t,e.resolve=Z,e},t.cancel=function(t){function e(t){}return e.type="xstate.cancel",e.sendId=t,e.resolve=y,e.execute=v,e},t.choose=function(t){function e(t){}return e.type="xstate.choose",e.branches=t,e.resolve=nt,e},t.constantPrefixes=a,t.createInitEvent=function(t){return{type:"xstate.init",input:t}},t.done=function(t,n){const s=`${e.DoneState}.${t}`,i={type:s,output:n,toString:()=>s};return i},t.doneInvoke=it,t.error=ot,t.escalate=function(t,e){return l((e=>({type:c,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return f(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=K,n.execute=L,n},t.pure=function(t){function e(t){}return e.type="xstate.pure",e.get=t,e.resolve=st,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=tt,n.execute=et,n},t.sendParent=l,t.sendTo=f,t.stop=function(t){function e(t){}return e.type="xstate.stop",e.actorRef=t,e.resolve=F,e.execute=H,e},Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actions.umd.min.js.map