xstate 5.0.0-beta.35 → 5.0.0-beta.36
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 +2 -2
- package/actions/dist/xstate-actions.development.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.esm.js +2 -2
- package/actions/dist/xstate-actions.esm.js +2 -2
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/dist/declarations/src/State.d.ts +0 -1
- package/dist/declarations/src/stateUtils.d.ts +3 -3
- package/dist/declarations/src/types.d.ts +0 -1
- package/dist/{raise-26e4d83c.development.cjs.js → raise-106ea558.development.cjs.js} +58 -62
- package/dist/{raise-cdcdf834.development.esm.js → raise-5b7ad3b7.development.esm.js} +58 -62
- package/dist/{raise-0ff57677.cjs.js → raise-c51b81a3.cjs.js} +58 -62
- package/dist/{raise-511399cc.esm.js → raise-ffe1014a.esm.js} +58 -62
- package/dist/{send-211a2a94.esm.js → send-0a7aa74e.esm.js} +2 -2
- package/dist/{send-894c4b18.development.cjs.js → send-25e70bd4.development.cjs.js} +2 -2
- package/dist/{send-19ffc568.cjs.js → send-778692de.cjs.js} +2 -2
- package/dist/{send-1de74f4d.development.esm.js → send-e93554d6.development.esm.js} +2 -2
- package/dist/xstate.cjs.js +8 -8
- package/dist/xstate.development.cjs.js +8 -8
- package/dist/xstate.development.esm.js +10 -10
- package/dist/xstate.esm.js +10 -10
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/guards/dist/xstate-guards.esm.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var send = require('../../dist/send-
|
|
6
|
-
var guards_dist_xstateGuards = require('../../dist/raise-
|
|
5
|
+
var send = require('../../dist/send-778692de.cjs.js');
|
|
6
|
+
var guards_dist_xstateGuards = require('../../dist/raise-c51b81a3.cjs.js');
|
|
7
7
|
require('../../dist/interpreter-69605bf0.cjs.js');
|
|
8
8
|
require('../../dev/dist/xstate-dev.cjs.js');
|
|
9
9
|
|
|
@@ -2,8 +2,8 @@
|
|
|
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-
|
|
5
|
+
var send = require('../../dist/send-25e70bd4.development.cjs.js');
|
|
6
|
+
var guards_dist_xstateGuards = require('../../dist/raise-106ea558.development.cjs.js');
|
|
7
7
|
require('../../dist/interpreter-d3567419.development.cjs.js');
|
|
8
8
|
require('../../dev/dist/xstate-dev.development.cjs.js');
|
|
9
9
|
|
|
@@ -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, B as stop } from '../../dist/raise-
|
|
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-e93554d6.development.esm.js';
|
|
2
|
+
export { z as cancel, A as raise, B as stop } from '../../dist/raise-5b7ad3b7.development.esm.js';
|
|
3
3
|
import '../../dist/interpreter-5c4e6634.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, B as stop } from '../../dist/raise-
|
|
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-0a7aa74e.esm.js';
|
|
2
|
+
export { z as cancel, A as raise, B as stop } from '../../dist/raise-ffe1014a.esm.js';
|
|
3
3
|
import '../../dist/interpreter-de5217bc.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)}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 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 _(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)}}let g=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const m={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 i(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=g.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={...m,...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(_(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=_(t,e,s);if(this.status!==g.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this.status===g.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=g.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===g.Stopped?this:(this.mailbox.clear(),this.status===g.NotStarted?(this.status=g.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!==g.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=g.Stopped,this.system._unregister(this),this}_send(t){this.status!==g.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 b(t,e){return new x(t,e)}const w=new WeakMap;function S(t,e,s,{sendId:n}){return[e,"function"==typeof n?n(s):n]}function E(t,e){t.self.cancel(e)}function I(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]),[V(e,{children:r}),o]}function T(t,e){e&&(e.status===g.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function k(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 k(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=>"atomic"===t.type||"final"===t.type;function R(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function j(t,e){const s=[];let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function O(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 R(t))if("history"!==e.type&&!s.has(e)){const t=N(e);for(const e of t)s.add(e)}}else N(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 P(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if($(t))return t.key}const n={};for(const t of s)n[t.key]=P(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 M(t,e){return P(t,C(O(e)))}const A=t=>"#"===t[0];function N(t){const e=function(t){const e=new Set;function s(t){if(!e.has(t))if(e.add(t),"compound"===t.type)s(t.initial.target[0]);else if("parallel"===t.type)for(const e of R(t))s(e)}return s(t),e}(t);for(const s of e)for(const n of j(s,t))e.add(n);return e}function Q(t,e){if(A(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 q(t,e){const s=e instanceof D?e.value:f(e);if("string"==typeof s)return[t,t.states[s]];const n=Object.keys(s),i=n.map((e=>Q(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,n.reduce(((e,n)=>{const i=Q(t,n);if(!i)return e;const o=q(i,s[n]);return e.concat(o)}),[]))}class D{static from(t,e={},s){if(t instanceof D)return t.context!==e?new D({value:t.value,context:e,meta:{},configuration:[],children:{},status:"active"},s):t;const n=O(q(s.root,t));return new D({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(O(q(e.root,t.value))),this.children=t.children,this.value=M(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=w.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},w.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 V(t,e={}){return new D({...t,...e},t.machine)}function W(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 h="input"in r?r.input:a.input,u=b(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=b(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!==g.Stopped)try{a.start?.()}catch(e){return void t.self.send(o(a.id,e))}})),a}}function B(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:W(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[V(e,{context:Object.assign({},e.context,r),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function J(t,e,s,{branches:n}){const i=n.find((t=>!t.guard||k(t.guard,e.context,s.event,e)))?.actions;return[e,void 0,y(i)]}function U(t,e,s,{value:n,label:i}){return[e,{value:"function"==typeof n?n(s):n,label:i}]}function z({logger:t},{value:e,label:s}){s?t(s,e):t(e)}function G(t,e,s,{get:n}){return[e,void 0,y(n({context:s.context,event:s.event}))]}function X(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?V(e,{_internalQueue:e._internalQueue.concat(c)}):e,{event:c,id:i,delay:a}]}function F(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}let H=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function K(t,e,s,{to:n,event:i,id:o,delay:r},c){const a=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 h="function"==typeof i?i(s):i;let u;if("string"==typeof r){const t=a&&a[r];u="function"==typeof t?t(s):t}else u="function"==typeof r?r(s):r;const d="function"==typeof n?n(s):n;let f;if("string"==typeof d){if(f=d===H.Parent?t?.self._parent:d===H.Internal?t?.self:d.startsWith("#_")?e.children[d.slice(2)]:c?.deferredActorIds.includes(d)?d: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:h,id:o,delay:u}]}function L(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function Y(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 Z(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=K,n.retryResolve=L,n.execute=Y,n}function tt(t,e){return Z(H.Parent,t,e)}t.assign=function(t){function e(t){}return e.type="xstate.assign",e.assignment=t,e.resolve=B,e},t.cancel=function(t){function e(t){}return e.type="xstate.cancel",e.sendId=t,e.resolve=S,e.execute=E,e},t.choose=function(t){function e(t){}return e.type="xstate.choose",e.branches=t,e.resolve=J,e},t.escalate=function(t,e){return tt((e=>({type:s,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return Z(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=U,s.execute=z,s},t.pure=function(t){function e(t){}return e.type="xstate.pure",e.get=t,e.resolve=G,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=X,s.execute=F,s},t.sendParent=tt,t.sendTo=Z,t.stop=function(t){function e(t){}return e.type="xstate.stop",e.actorRef=t,e.resolve=I,e.execute=T,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)}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 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 _(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)}}let g=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const m={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 i(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=g.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={...m,...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(_(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=_(t,e,s);if(this.status!==g.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this.status===g.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=g.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===g.Stopped?this:(this.mailbox.clear(),this.status===g.NotStarted?(this.status=g.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!==g.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=g.Stopped,this.system._unregister(this),this}_send(t){this.status!==g.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 b(t,e){return new x(t,e)}const w=new WeakMap;function S(t,e,s,{sendId:n}){return[e,"function"==typeof n?n(s):n]}function E(t,e){t.self.cancel(e)}function I(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]),[W(e,{children:r}),o]}function T(t,e){e&&(e.status===g.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function k(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 k(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=>"atomic"===t.type||"final"===t.type;function R(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function j(t,e){const s=[];let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function O(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 R(t))if("history"!==e.type&&!s.has(e)){const t=N(e);for(const e of t)s.add(e)}}else N(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 P(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if($(t))return t.key}const n={};for(const t of s)n[t.key]=P(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 M(t,e){return P(t,C(O(e)))}const A=t=>"#"===t[0];function N(t){const e=function(t){const e=new Set;function s(t){if(!e.has(t))if(e.add(t),"compound"===t.type)s(t.initial.target[0]);else if("parallel"===t.type)for(const e of R(t))s(e)}return s(t),e}(t);for(const s of e)for(const n of j(s,t))e.add(n);return e}function q(t,e){if(A(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 D(t,e){const s=e instanceof V?e.value:f(e);if("string"==typeof s)return[t,t.states[s]];const n=Object.keys(s),i=n.map((e=>q(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,n.reduce(((e,n)=>{const i=q(t,n);if(!i)return e;const o=D(i,s[n]);return e.concat(o)}),[]))}class V{static from(t,e={},s){if(t instanceof V)return t.context!==e?new V({value:t.value,context:e,meta:{},configuration:[],children:{},status:"active"},s):t;const n=O(D(s.root,t));return new V({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(O(D(e.root,t.value))),this.children=t.children,this.value=M(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=w.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},w.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 W(t,e={}){return new V({...t,...e},t.machine)}function B(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 h="input"in r?r.input:a.input,u=b(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=b(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!==g.Stopped)try{a.start?.()}catch(e){return void t.self.send(o(a.id,e))}})),a}}function J(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:B(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[W(e,{context:Object.assign({},e.context,r),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function U(t,e,s,{branches:n}){const i=n.find((t=>!t.guard||k(t.guard,e.context,s.event,e)))?.actions;return[e,void 0,y(i)]}function z(t,e,s,{value:n,label:i}){return[e,{value:"function"==typeof n?n(s):n,label:i}]}function G({logger:t},{value:e,label:s}){s?t(s,e):t(e)}function Q(t,e,s,{get:n}){return[e,void 0,y(n({context:s.context,event:s.event}))]}function X(t,e,s,{event:n,id:i,delay:o},{internalQueue:r}){const c=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 a="function"==typeof n?n(s):n;let h;if("string"==typeof o){const t=c&&c[o];h="function"==typeof t?t(s):t}else h="function"==typeof o?o(s):o;return"number"!=typeof h&&r.push(a),[e,{event:a,id:i,delay:h}]}function F(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}let H=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function K(t,e,s,{to:n,event:i,id:o,delay:r},c){const a=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 h="function"==typeof i?i(s):i;let u;if("string"==typeof r){const t=a&&a[r];u="function"==typeof t?t(s):t}else u="function"==typeof r?r(s):r;const d="function"==typeof n?n(s):n;let f;if("string"==typeof d){if(f=d===H.Parent?t?.self._parent:d===H.Internal?t?.self:d.startsWith("#_")?e.children[d.slice(2)]:c.deferredActorIds?.includes(d)?d: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:h,id:o,delay:u}]}function L(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function Y(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 Z(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=K,n.retryResolve=L,n.execute=Y,n}function tt(t,e){return Z(H.Parent,t,e)}t.assign=function(t){function e(t){}return e.type="xstate.assign",e.assignment=t,e.resolve=J,e},t.cancel=function(t){function e(t){}return e.type="xstate.cancel",e.sendId=t,e.resolve=S,e.execute=E,e},t.choose=function(t){function e(t){}return e.type="xstate.choose",e.branches=t,e.resolve=U,e},t.escalate=function(t,e){return tt((e=>({type:s,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return Z(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=z,s.execute=G,s},t.pure=function(t){function e(t){}return e.type="xstate.pure",e.get=t,e.resolve=Q,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=X,s.execute=F,s},t.sendParent=tt,t.sendTo=Z,t.stop=function(t){function e(t){}return e.type="xstate.stop",e.actorRef=t,e.resolve=I,e.execute=T,e},Object.defineProperty(t,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=xstate-actions.umd.min.js.map
|