xstate 5.0.0-beta.38 → 5.0.0-beta.39
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.
- package/actions/dist/xstate-actions.cjs.js +3 -3
- package/actions/dist/xstate-actions.development.cjs.js +3 -3
- package/actions/dist/xstate-actions.development.esm.js +3 -3
- package/actions/dist/xstate-actions.esm.js +3 -3
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/actors/dist/xstate-actors.cjs.js +60 -3
- package/actors/dist/xstate-actors.development.cjs.js +60 -3
- package/actors/dist/xstate-actors.development.esm.js +60 -3
- package/actors/dist/xstate-actors.esm.js +60 -3
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/declarations/src/StateMachine.d.ts +5 -5
- package/dist/declarations/src/actors/callback.d.ts +74 -0
- package/dist/declarations/src/actors/transition.d.ts +2 -2
- package/dist/declarations/src/interpreter.d.ts +70 -9
- package/dist/declarations/src/spawn.d.ts +2 -2
- package/dist/declarations/src/stateUtils.d.ts +4 -4
- package/dist/declarations/src/types.d.ts +7 -7
- package/dist/{interpreter-ed3f81f7.development.cjs.js → interpreter-03a5c3f5.development.cjs.js} +90 -11
- package/dist/{interpreter-c80ce92e.esm.js → interpreter-1e8c1c0c.esm.js} +90 -11
- package/dist/{interpreter-b6f22ee2.cjs.js → interpreter-5dfcd203.cjs.js} +90 -11
- package/dist/{interpreter-4005eb36.development.esm.js → interpreter-70cd9217.development.esm.js} +90 -11
- package/dist/{raise-b69a3d16.development.cjs.js → raise-17cb3d9d.development.cjs.js} +84 -81
- package/dist/{raise-7faa9b3b.cjs.js → raise-291d2181.cjs.js} +83 -80
- package/dist/{raise-c989c7fb.esm.js → raise-62de3670.esm.js} +83 -80
- package/dist/{raise-42073973.development.esm.js → raise-e044f460.development.esm.js} +84 -81
- package/dist/{send-4b616da9.esm.js → send-1249d4ac.esm.js} +22 -22
- package/dist/{send-58725522.development.cjs.js → send-33433787.development.cjs.js} +22 -22
- package/dist/{send-34160163.cjs.js → send-af152aca.cjs.js} +22 -22
- package/dist/{send-bff8c910.development.esm.js → send-f1a2a827.development.esm.js} +22 -22
- package/dist/xstate.cjs.js +17 -17
- package/dist/xstate.development.cjs.js +17 -17
- package/dist/xstate.development.esm.js +20 -20
- package/dist/xstate.esm.js +20 -20
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +2 -2
- package/guards/dist/xstate-guards.development.cjs.js +2 -2
- package/guards/dist/xstate-guards.development.esm.js +2 -2
- package/guards/dist/xstate-guards.esm.js +2 -2
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- 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-
|
|
6
|
-
var guards_dist_xstateGuards = require('../../dist/raise-
|
|
7
|
-
require('../../dist/interpreter-
|
|
5
|
+
var send = require('../../dist/send-af152aca.cjs.js');
|
|
6
|
+
var guards_dist_xstateGuards = require('../../dist/raise-291d2181.cjs.js');
|
|
7
|
+
require('../../dist/interpreter-5dfcd203.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-
|
|
6
|
-
var guards_dist_xstateGuards = require('../../dist/raise-
|
|
7
|
-
require('../../dist/interpreter-
|
|
5
|
+
var send = require('../../dist/send-33433787.development.cjs.js');
|
|
6
|
+
var guards_dist_xstateGuards = require('../../dist/raise-17cb3d9d.development.cjs.js');
|
|
7
|
+
require('../../dist/interpreter-03a5c3f5.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-
|
|
2
|
-
export { z as cancel, A as raise, C as spawn, B as stop } from '../../dist/raise-
|
|
3
|
-
import '../../dist/interpreter-
|
|
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-f1a2a827.development.esm.js';
|
|
2
|
+
export { z as cancel, A as raise, C as spawn, B as stop } from '../../dist/raise-e044f460.development.esm.js';
|
|
3
|
+
import '../../dist/interpreter-70cd9217.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-
|
|
2
|
-
export { z as cancel, A as raise, C as spawn, B as stop } from '../../dist/raise-
|
|
3
|
-
import '../../dist/interpreter-
|
|
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-1249d4ac.esm.js';
|
|
2
|
+
export { z as cancel, A as raise, C as spawn, B as stop } from '../../dist/raise-62de3670.esm.js';
|
|
3
|
+
import '../../dist/interpreter-1e8c1c0c.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=".",s="xstate.error",n="xstate.stop";class i{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}}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("object"==typeof(s=t)&&"value"in s&&"context"in s&&"event"in s)return t.value;var s;if(v(t))return l(t);if("string"!=typeof t)return t;return l(function(t){try{return v(t)?t:t.toString().split(e)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}(t))}function l(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 p(t){return[].concat(...t)}function y(t){return void 0===t?[]:function(t){return v(t)?t:[t]}(t)}function v(t){return Array.isArray(t)}function g(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 m(t,e){if(e.startsWith("xstate#")){const[,s]=e.match(/\[(\d+)\]$/),n=t.getStateNodeById(e.slice(7,-(s.length+2))).config.invoke;return{src:(Array.isArray(n)?n[s]:n).src,input:void 0}}const s=t.implementations.actors[e];return s?"transition"in s?{src:s,input:void 0}:s:void 0}let _=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const x={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class b{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 i(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 s={...x,...e},{clock:n,logger:o,parent:r,id:c,systemId:a,inspect:h}=s;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,targetRef:e,event:s}),e._send(s)}};return o}(this),h&&!r&&this.system.inspect(g(h)),a&&(this._systemId=a,this.system._set(a,this)),this.sessionId=this.system._bookId(),this.id=c??this.sessionId,this.logger=o,this.clock=n,this._parent=r,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.system._sendInspectionEvent({type:"@xstate.actor",actorRef: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,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=g(t,e,s);if(this.status!==_.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}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={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,targetRef: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._actorContext)}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._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),t.type===n&&(this._stopProcedure(),this._complete())}_stop(){return this.status===_.Stopped?this:(this.mailbox.clear(),this.status===_.NotStarted?(this.status=_.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){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.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 i(this._process.bind(this)),this.status=_.Stopped,this.system._unregister(this),this}_send(t){this.status!==_.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(){return this.logic.getPersistedState(this._state)}[h](){return this}getSnapshot(){return this._state}}function w(t,e){return new b(t,e)}const S=new WeakMap;function I(t,e,s,n,{sendId:i}){return[e,"function"==typeof i?i(s,n):i]}function E(t,e){t.self.cancel(e)}function T(t,e,s,n,{id:i,systemId:o,src:r,input:c,syncSnapshot:a}){const h="string"==typeof r?m(e.machine,r):{src:r,input:void 0},u="function"==typeof i?i(s):i;let d;if(h){const n=c||h.input;d=w(h.src,{id:u,src:"string"==typeof r?r:void 0,parent:t?.self,systemId:o,input:"function"==typeof n?n({context:e.context,event:s.event,self:t?.self}):n}),a&&d.subscribe({next:e=>{"active"===e.status&&t.self.send({type:`xstate.snapshot.${i}`,snapshot:e})},error:()=>{}})}return[U(e,{children:{...e.children,[u]:d}}),{id:i,actorRef:d}]}function k(t,{id:e,actorRef:s}){s&&t.defer((()=>{if(s.status!==_.Stopped)try{s.start?.()}catch(s){return void t.self.send(o(e,s))}}))}function $(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]),[U(e,{children:c}),r]}function R(t,e){e&&(t.system._unregister(e),e.status===_.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function j(t,e,s,n){const{machine:i}=n,o="function"==typeof t,r=o?t:i.implementations.guards["string"==typeof t?t:t.type];if(!o&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return j(r,e,s,n);const c={context:e,event:s},a=o||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:e,event:s}):t.params:void 0;if(!("check"in r))return r(c,a);return r.check(n,c,r)}const O=t=>"atomic"===t.type||"final"===t.type;function P(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function A(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 C(t){const e=new Set(t),s=new Set(t),n=N(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 P(t))if("history"!==e.type&&!s.has(e)){const t=q(e);for(const e of t)s.add(e)}}else q(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 M(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(O(t))return t.key}const n={};for(const t of s)n[t.key]=M(t,e);return n}function N(t){const e=new Map;for(const s of t)e.has(s)||e.set(s,[]),s.parent&&(e.has(s.parent)||e.set(s.parent,[]),e.get(s.parent).push(s));return e}function D(t,e){return M(t,N(C(e)))}const W=t=>"#"===t[0];function q(t){const e=function(t){const e=new Set;function s(t){if(!e.has(t))if(e.add(t),"compound"===t.type)s(t.initial.target[0]);else if("parallel"===t.type)for(const e of P(t))s(e)}return s(t),e}(t);for(const s of e)for(const n of A(s,t))e.add(n);return e}function B(t,e){if(W(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 V(t,e){const s=e instanceof J?e.value:f(e);if("string"==typeof s)return[t,t.states[s]];const n=Object.keys(s),i=n.map((e=>B(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,n.reduce(((e,n)=>{const i=B(t,n);if(!i)return e;const o=V(i,s[n]);return e.concat(o)}),[]))}class J{static from(t,e={},s){if(t instanceof J)return t.context!==e?new J({value:t.value,context:e,meta:{},configuration:[],children:{},status:"active"},s):t;const n=C(V(s.root,t));return new J({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.configuration=void 0,this.children=void 0,this.context=t.context,this.historyValue=t.historyValue||{},this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(C(V(e.root,t.value))),this.children=t.children,this.value=D(e.root,this.configuration),this.tags=new Set(p(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 d(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=S.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},S.set(t,n)),n[e]}(this,"nextEvents",(()=>[...new Set(p([...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 U(t,e={}){return new J({...t,...e},t.machine)}function z(t,{machine:e,context:s},n,i){return(r,c)=>{const a=((o,r={})=>{const{systemId:c}=r;if("string"==typeof o){const a=m(e,o);if(!a)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const h="input"in r?r.input:a.input,u=w(a.src,{id:r.id,parent:t.self,input:"function"==typeof h?h({context:s,event:n,self:t.self}):h,src:o,systemId:c});return i[u.id]=u,r.syncSnapshot&&u.subscribe({next:e=>{"active"===e.status&&t.self.send({type:`xstate.snapshot.${u.id}`,snapshot:e})},error:()=>{}}),u}{const e=w(o,{id:r.id,parent:t.self,input:r.input,src:void 0,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!==_.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:z(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[U(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||j(t.guard,e.context,s.event,e)))?.actions;return[e,void 0,y(o)]}function X(t,e,s,n,{value:i,label:o}){return[e,{value:"function"==typeof i?i(s,n):i,label:o}]}function F({logger:t},{value:e,label:s}){s?t(s,e):t(e)}function H(t,e,s,n,{get:i}){return[e,void 0,y(i({context:s.context,event:s.event}))]}function K(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 L(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}let Y=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function Z(t,e,s,n,{to:i,event:o,id:r,delay:c},a){const h=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${o}" }) instead`);const u="function"==typeof o?o(s,n):o;let d;if("string"==typeof c){const t=h&&h[c];d="function"==typeof t?t(s,n):t}else d="function"==typeof c?c(s,n):c;const f="function"==typeof i?i(s,n):i;let l;if("string"==typeof f){if(l=f===Y.Parent?t?.self._parent:f===Y.Internal?t?.self:f.startsWith("#_")?e.children[f.slice(2)]:a.deferredActorIds?.includes(f)?f:e.children[f],!l)throw new Error(`Unable to send event to actor '${f}' from machine '${e.machine.id}'.`)}else l=f||t?.self;return[e,{to:l,event:u,id:r,delay:d}]}function tt(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function et(t,e){"number"!=typeof e.delay?t.defer((()=>{const{to:n,event:i}=e;t?.system._relay(t.self,n,i.type===s?o(t.self.id,i.data):i)})):t.self.delaySend(e)}function st(t,e,s){function n(t,e){}return n.type="xstate.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=Z,n.retryResolve=tt,n.execute=et,n}function nt(t,e){return st(Y.Parent,t,e)}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=I,e.execute=E,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 nt((e=>({type:s,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return st(t,(({event:t})=>t),e)},t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function s(t,e){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=X,s.execute=F,s},t.pure=function(t){function e(t,e){}return e.type="xstate.pure",e.get=t,e.resolve=H,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=K,s.execute=L,s},t.sendParent=nt,t.sendTo=st,t.spawn=function(...[t,{id:e,systemId:s,input:n,syncSnapshot:i=!1}={}]){function o(t,e){}return o.type="xstate.spawn",o.id=e,o.systemId=s,o.src=t,o.input=n,o.syncSnapshot=i,o.resolve=T,o.execute=k,o},t.stop=function(t){function e(t,e){}return e.type="xstate.stop",e.actorRef=t,e.resolve=$,e.execute=R,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";class i{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}}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("object"==typeof(s=t)&&"value"in s&&"context"in s&&"event"in s)return t.value;var s;if(v(t))return l(t);if("string"!=typeof t)return t;return l(function(t){try{return v(t)?t:t.toString().split(e)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}(t))}function l(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 p(t){return[].concat(...t)}function y(t){return void 0===t?[]:function(t){return v(t)?t:[t]}(t)}function v(t){return Array.isArray(t)}function g(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 m(t,e){if(e.startsWith("xstate#")){const[,s]=e.match(/\[(\d+)\]$/),n=t.getStateNodeById(e.slice(7,-(s.length+2))).config.invoke;return{src:(Array.isArray(n)?n[s]:n).src,input:void 0}}const s=t.implementations.actors[e];return s?"transition"in s?{src:s,input:void 0}:s:void 0}let _=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const x={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class b{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 i(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._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 s={...x,...e},{clock:n,logger:o,parent:r,id:c,systemId:a,inspect:h}=s;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,targetRef:e,event:s}),e._send(s)}};return o}(this),h&&!r&&this.system.inspect(g(h)),this.sessionId=this.system._bookId(),this.id=c??this.sessionId,this.logger=o,this.clock=n,this._parent=r,this.options=s,this.src=s.src,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}),this._initState(),a&&"active"===this._state.status&&(this._systemId=a,this.system._set(a,this))}_initState(){this._state=this.options.state?this.logic.restoreState?this.logic.restoreState(this.options.state,this._actorScope):this.options.state: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=g(t,e,s);if(this.status!==_.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}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={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,targetRef: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===n&&(this._stopProcedure(),this._complete())}_stop(){return this.status===_.Stopped?this:(this.mailbox.clear(),this.status===_.NotStarted?(this.status=_.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){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.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 i(this._process.bind(this)),this.status=_.Stopped,this.system._unregister(this),this}_send(t){this.status!==_.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(){return this.logic.getPersistedState(this._state)}[h](){return this}getSnapshot(){return this._state}}function w(t,e){return new b(t,e)}const S=new WeakMap;function I(t,e,s,n,{sendId:i}){return[e,"function"==typeof i?i(s,n):i]}function E(t,e){t.self.cancel(e)}function T(t,e,s,n,{id:i,systemId:o,src:r,input:c,syncSnapshot:a}){const h="string"==typeof r?m(e.machine,r):{src:r,input:void 0},u="function"==typeof i?i(s):i;let d;if(h){const n=c||h.input;d=w(h.src,{id:u,src:"string"==typeof r?r:void 0,parent:t?.self,systemId:o,input:"function"==typeof n?n({context:e.context,event:s.event,self:t?.self}):n}),a&&d.subscribe({next:e=>{"active"===e.status&&t.self.send({type:`xstate.snapshot.${i}`,snapshot:e})},error:()=>{}})}return[U(e,{children:{...e.children,[u]:d}}),{id:i,actorRef:d}]}function k(t,{id:e,actorRef:s}){s&&t.defer((()=>{if(s.status!==_.Stopped)try{s.start?.()}catch(s){return void t.self.send(o(e,s))}}))}function $(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]),[U(e,{children:c}),r]}function R(t,e){e&&(t.system._unregister(e),e.status===_.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function j(t,e,s,n){const{machine:i}=n,o="function"==typeof t,r=o?t:i.implementations.guards["string"==typeof t?t:t.type];if(!o&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return j(r,e,s,n);const c={context:e,event:s},a=o||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:e,event:s}):t.params:void 0;if(!("check"in r))return r(c,a);return r.check(n,c,r)}const O=t=>"atomic"===t.type||"final"===t.type;function P(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function A(t,e){const s=[];if(e===t)return s;let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function M(t){const e=new Set(t),s=new Set(t),n=C(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 P(t))if("history"!==e.type&&!s.has(e)){const t=q(e);for(const e of t)s.add(e)}}else q(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 N(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(O(t))return t.key}const n={};for(const t of s)n[t.key]=N(t,e);return n}function C(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 D(t,e){return N(t,C(M(e)))}const W=t=>"#"===t[0];function q(t){const e=function(t){const e=new Set;function s(t){if(!e.has(t))if(e.add(t),"compound"===t.type)s(t.initial.target[0]);else if("parallel"===t.type)for(const e of P(t))s(e)}return s(t),e}(t);for(const s of e)for(const n of A(s,t))e.add(n);return e}function B(t,e){if(W(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 V(t,e){const s=e instanceof J?e.value:f(e);if("string"==typeof s)return[t,t.states[s]];const n=Object.keys(s),i=n.map((e=>B(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,n.reduce(((e,n)=>{const i=B(t,n);if(!i)return e;const o=V(i,s[n]);return e.concat(o)}),[]))}class J{static from(t,e={},s){if(t instanceof J)return t.context!==e?new J({value:t.value,context:e,meta:{},configuration:[],children:{},status:"active"},s):t;const n=M(V(s.root,t));return new J({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.configuration=void 0,this.children=void 0,this.context=t.context,this.historyValue=t.historyValue||{},this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(M(V(e.root,t.value))),this.children=t.children,this.value=D(e.root,this.configuration),this.tags=new Set(p(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 d(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=S.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},S.set(t,n)),n[e]}(this,"nextEvents",(()=>[...new Set(p([...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 U(t,e={}){return new J({...t,...e},t.machine)}function z(t,{machine:e,context:s},n,i){return(r,c)=>{const a=((o,r={})=>{const{systemId:c}=r;if("string"==typeof o){const a=m(e,o);if(!a)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const h="input"in r?r.input:a.input,u=w(a.src,{id:r.id,parent:t.self,input:"function"==typeof h?h({context:s,event:n,self:t.self}):h,src:o,systemId:c});return i[u.id]=u,r.syncSnapshot&&u.subscribe({next:e=>{"active"===e.status&&t.self.send({type:`xstate.snapshot.${u.id}`,snapshot:e})},error:()=>{}}),u}{const e=w(o,{id:r.id,parent:t.self,input:r.input,src:void 0,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!==_.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:z(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[U(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||j(t.guard,e.context,s.event,e)))?.actions;return[e,void 0,y(o)]}function X(t,e,s,n,{value:i,label:o}){return[e,{value:"function"==typeof i?i(s,n):i,label:o}]}function F({logger:t},{value:e,label:s}){s?t(s,e):t(e)}function H(t,e,s,n,{get:i}){return[e,void 0,y(i({context:s.context,event:s.event}))]}function K(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 L(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}let Y=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function Z(t,e,s,n,{to:i,event:o,id:r,delay:c},a){const h=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${o}" }) instead`);const u="function"==typeof o?o(s,n):o;let d;if("string"==typeof c){const t=h&&h[c];d="function"==typeof t?t(s,n):t}else d="function"==typeof c?c(s,n):c;const f="function"==typeof i?i(s,n):i;let l;if("string"==typeof f){if(l=f===Y.Parent?t?.self._parent:f===Y.Internal?t?.self:f.startsWith("#_")?e.children[f.slice(2)]:a.deferredActorIds?.includes(f)?f:e.children[f],!l)throw new Error(`Unable to send event to actor '${f}' from machine '${e.machine.id}'.`)}else l=f||t?.self;return[e,{to:l,event:u,id:r,delay:d}]}function tt(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function et(t,e){"number"!=typeof e.delay?t.defer((()=>{const{to:n,event:i}=e;t?.system._relay(t.self,n,i.type===s?o(t.self.id,i.data):i)})):t.self.delaySend(e)}function st(t,e,s){function n(t,e){}return n.type="xstate.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=Z,n.retryResolve=tt,n.execute=et,n}function nt(t,e){return st(Y.Parent,t,e)}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=I,e.execute=E,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 nt((e=>({type:s,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return st(t,(({event:t})=>t),e)},t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function s(t,e){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=X,s.execute=F,s},t.pure=function(t){function e(t,e){}return e.type="xstate.pure",e.get=t,e.resolve=H,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=K,s.execute=L,s},t.sendParent=nt,t.sendTo=st,t.spawn=function(...[t,{id:e,systemId:s,input:n,syncSnapshot:i=!1}={}]){function o(t,e){}return o.type="xstate.spawn",o.id=e,o.systemId=s,o.src=t,o.input=n,o.syncSnapshot=i,o.resolve=T,o.execute=k,o},t.stop=function(t){function e(t,e){}return e.type="xstate.stop",e.actorRef=t,e.resolve=$,e.execute=R,e},Object.defineProperty(t,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=xstate-actions.umd.min.js.map
|