xstate 5.3.0 → 5.4.0
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/README.md +21 -0
- package/actions/dist/xstate-actions.cjs.js +1 -1
- 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 +1 -1
- 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.development.cjs.js +1 -1
- package/actors/dist/xstate-actors.development.esm.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/declarations/src/actions/enqueueActions.d.ts +5 -5
- package/dist/declarations/src/types.d.ts +6 -4
- package/dist/{log-fb9d8790.development.esm.js → log-580765a2.development.esm.js} +5 -3
- package/dist/{log-f77d7352.cjs.js → log-a32b44b3.cjs.js} +4 -2
- package/dist/{log-0f1b9844.development.cjs.js → log-cd3d7c14.development.cjs.js} +5 -3
- package/dist/{log-6852ea2b.esm.js → log-e870aec8.esm.js} +4 -2
- package/dist/{raise-c8b9d708.development.esm.js → raise-0fc3a80c.development.esm.js} +3 -0
- package/dist/{raise-4538b891.development.cjs.js → raise-e3ff3de1.development.cjs.js} +3 -0
- package/dist/xstate.cjs.js +5 -3
- package/dist/xstate.development.cjs.js +6 -4
- package/dist/xstate.development.esm.js +8 -6
- package/dist/xstate.esm.js +6 -4
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ Get started by forking one of these templates on CodeSandbox:
|
|
|
38
38
|
<tr><th>Template</th><th></th></tr>
|
|
39
39
|
</thead>
|
|
40
40
|
<tbody>
|
|
41
|
+
|
|
41
42
|
<tr>
|
|
42
43
|
<td>
|
|
43
44
|
|
|
@@ -54,6 +55,7 @@ Get started by forking one of these templates on CodeSandbox:
|
|
|
54
55
|
|
|
55
56
|
</td>
|
|
56
57
|
</tr>
|
|
58
|
+
|
|
57
59
|
<tr>
|
|
58
60
|
<td>
|
|
59
61
|
|
|
@@ -70,6 +72,7 @@ Get started by forking one of these templates on CodeSandbox:
|
|
|
70
72
|
|
|
71
73
|
</td>
|
|
72
74
|
</tr>
|
|
75
|
+
|
|
73
76
|
<tr>
|
|
74
77
|
<td>
|
|
75
78
|
|
|
@@ -86,6 +89,24 @@ Get started by forking one of these templates on CodeSandbox:
|
|
|
86
89
|
|
|
87
90
|
</td>
|
|
88
91
|
</tr>
|
|
92
|
+
|
|
93
|
+
<tr>
|
|
94
|
+
<td>
|
|
95
|
+
|
|
96
|
+
[🧡 XState + Svelte Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/main/templates/svelte-ts)
|
|
97
|
+
|
|
98
|
+
[](https://stackblitz.com/github/statelyai/xstate/tree/main/templates/svelte-ts?file=%2Fsrc%2FfeedbackMachine.ts)
|
|
99
|
+
|
|
100
|
+
</td>
|
|
101
|
+
<td>
|
|
102
|
+
|
|
103
|
+
- [Svelte](https://svelte.dev/)
|
|
104
|
+
- XState v5
|
|
105
|
+
- TypeScript
|
|
106
|
+
|
|
107
|
+
</td>
|
|
108
|
+
</tr>
|
|
109
|
+
|
|
89
110
|
</tbody>
|
|
90
111
|
</table>
|
|
91
112
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var log = require('../../dist/log-
|
|
5
|
+
var log = require('../../dist/log-a32b44b3.cjs.js');
|
|
6
6
|
var guards_dist_xstateGuards = require('../../dist/raise-c3bbdd3a.cjs.js');
|
|
7
7
|
require('../../dev/dist/xstate-dev.cjs.js');
|
|
8
8
|
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var log = require('../../dist/log-
|
|
6
|
-
var guards_dist_xstateGuards = require('../../dist/raise-
|
|
5
|
+
var log = require('../../dist/log-cd3d7c14.development.cjs.js');
|
|
6
|
+
var guards_dist_xstateGuards = require('../../dist/raise-e3ff3de1.development.cjs.js');
|
|
7
7
|
require('../../dev/dist/xstate-dev.development.cjs.js');
|
|
8
8
|
|
|
9
9
|
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-
|
|
2
|
-
export { L as cancel, M as raise, O as spawnChild, P as stop, Q as stopChild } from '../../dist/raise-
|
|
1
|
+
export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-580765a2.development.esm.js';
|
|
2
|
+
export { L as cancel, M as raise, O as spawnChild, P as stop, Q as stopChild } from '../../dist/raise-0fc3a80c.development.esm.js';
|
|
3
3
|
import '../../dev/dist/xstate-dev.development.esm.js';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-
|
|
1
|
+
export { a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from '../../dist/log-e870aec8.esm.js';
|
|
2
2
|
export { L as cancel, M as raise, O as spawnChild, P as stop, Q as stopChild } from '../../dist/raise-7df513e7.esm.js';
|
|
3
3
|
import '../../dev/dist/xstate-dev.esm.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActions={})}(this,(function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const s=".",n="xstate.error",o="xstate.stop";function i(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const r=t=>{const e=i();e&&e.register(t)};function c(t,e){return{type:`xstate.error.actor.${t}`,error:e}}function a(t){setTimeout((()=>{throw t}))}const h="function"==typeof Symbol&&Symbol.observable||"@@observable";function u(t,e){return`${t.sessionId}.${e}`}let d=0;function p(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&&p(s[t],n[t])))}function l(t){return e=t,Array.isArray(e)?t:t.split(s);var e}function f(t){if((e=t)&&"object"==typeof e&&"machine"in e&&"value"in e)return t.value;var e;if("string"!=typeof t)return t;return function(t){if(1===t.length)return t[0];const e={};let s=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)s[t[e]]=t[e+1];else{const n=s;s={},n[t[e]]=s}return e}(l(t))}function y(t,e,s){const n="object"==typeof t,o=n?t:void 0;return{next:(n?t.next:t)?.bind(o),error:(n?t.error:e)?.bind(o),complete:(n?t.complete:s)?.bind(o)}}function _(t,e){const s=e.match(/^xstate\.invoke\.(\d+)\.(.*)/);if(!s)return t.implementations.actors[e];const[,n,o]=s,i=t.getStateNodeById(o).config.invoke;return(Array.isArray(i)?i[n]:i).src}let v=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const g={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class m{constructor(t,s){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),this.observers=new Set,this.logger=void 0,this._processingStatus=v.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const n={...g,...s},{clock:o,logger:i,parent:r,syncSnapshot:c,id:a,systemId:h,inspect:p}=n;this.system=r?r.system:function(t,e){const s=new Map,n=new Map,o=new WeakMap,i=new Set,r={},c=e.clock,a={schedule:(t,e,s,n,o=Math.random().toString(36).slice(2))=>{const i={source:t,target:e,event:s,delay:n,id:o,startedAt:Date.now()},a=u(t,o);h._snapshot._scheduledEvents[a]=i;const d=c.setTimeout((()=>{delete r[a],delete h._snapshot._scheduledEvents[a],h._relay(t,e,s)}),n);r[a]=d},cancel:(t,e)=>{const s=u(t,e),n=r[s];delete r[s],delete h._snapshot._scheduledEvents[s],c.clearTimeout(n)},cancelAll:t=>{for(const e in h._snapshot._scheduledEvents){const s=h._snapshot._scheduledEvents[e];s.source===t&&a.cancel(t,s.id)}}},h={_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+d++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const e=o.get(t);void 0!==e&&(n.delete(e),o.delete(t))},get:t=>n.get(t),_set:(t,e)=>{const s=n.get(t);if(s&&s!==e)throw new Error(`Actor with system ID '${t}' already exists.`);n.set(t,e),o.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)=>{h._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)},scheduler:a,getSnapshot:()=>({_scheduledEvents:{...h._snapshot._scheduledEvents}}),start:()=>{const t=h._snapshot._scheduledEvents;h._snapshot._scheduledEvents={};for(const e in t){const{source:s,target:n,event:o,delay:i,id:r}=t[e];a.schedule(s,n,o,i,r)}}};return h}(this,{clock:o}),p&&!r&&this.system.inspect(y(p)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=i,this.clock=o,this._parent=r,this._syncSnapshot=c,this.options=n,this.src=n.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),h&&(this._systemId=h,this.system._set(h,this)),this._initState(s?.snapshot??s?.state),h&&"active"!==this._snapshot.status&&this.system._unregister(this)}_initState(t){try{this._snapshot=t?this.logic.restoreSnapshot?this.logic.restoreSnapshot(t,this._actorScope):t:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t}}}update(t,e){let s;for(this._snapshot=t;s=this._deferred.shift();)try{s()}catch(e){this._deferred.length=0,this._snapshot={...t,status:"error",error:e}}switch(this._snapshot.status){case"active":for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}break;case"done":for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}this._stopProcedure(),this._complete(),this._doneEvent=(n=this.id,o=this._snapshot.output,{type:`xstate.done.actor.${n}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var n,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=y(t,e,s);if(this._processingStatus!==v.Stopped)this.observers.add(n);else switch(this._snapshot.status){case"done":try{n.complete?.()}catch(t){a(t)}break;case"error":{const t=this._snapshot.error;if(n.error)try{n.error(t)}catch(t){a(t)}else a(t);break}}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this._processingStatus===v.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this._processingStatus=v.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._snapshot={...this._snapshot,status:"error",error:t},this._error(t),this}return this.update(this._snapshot,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._snapshot={...this._snapshot,status:"error",error:t},void this._error(t)}this.update(e,t),t.type===o&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===v.Stopped?this:(this.mailbox.clear(),this._processingStatus===v.NotStarted?(this._processingStatus=v.Stopped,this):(this.mailbox.enqueue({type:o}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){a(t)}this.observers.clear()}_reportError(t){if(!this.observers.size)return void(this._parent||a(t));let e=!1;for(const s of this.observers){const n=s.error;e||=!n;try{n?.(t)}catch(t){a(t)}}this.observers.clear(),e&&a(t)}_error(t){this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,c(this.id,t))}_stopProcedure(){return this._processingStatus!==v.Running||(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new e(this._process.bind(this)),this._processingStatus=v.Stopped,this.system._unregister(this)),this}_send(t){this._processingStatus!==v.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:r)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[h](){return this}getSnapshot(){return this._snapshot}}function b(t,e){return new m(t,e)}function x(t,e,s,n,{sendId:o}){return[e,"function"==typeof o?o(s,n):o]}function S(t,e){t.defer((()=>{t.system.scheduler.cancel(t.self,e)}))}function w(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=x,e.execute=S,e}function I(t,e,s,n,{id:o,systemId:i,src:r,input:c,syncSnapshot:a}){const h="string"==typeof r?_(e.machine,r):r,u="function"==typeof o?o(s):o;let d;return h&&(d=b(h,{id:u,src:r,parent:t?.self,syncSnapshot:a,systemId:i,input:"function"==typeof c?c({context:e.context,event:s.event,self:t?.self}):c})),[G(e,{children:{...e.children,[u]:d}}),{id:o,actorRef:d}]}function E(t,{id:e,actorRef:s}){s&&t.defer((()=>{s._processingStatus!==v.Stopped&&s.start()}))}function T(...[t,{id:e,systemId:s,input:n,syncSnapshot:o=!1}={}]){function i(t,e){}return i.type="snapshot.spawnChild",i.id=e,i.systemId=s,i.src=t,i.input=n,i.syncSnapshot=o,i.resolve=I,i.execute=E,i}function k(t,e,s,n,{actorRef:o}){const i="function"==typeof o?o(s,n):o,r="string"==typeof i?e.children[i]:i;let c=e.children;return r&&(c={...c},delete c[r.id]),[G(e,{children:c}),r]}function R(t,e){e&&(t.system._unregister(e),e._processingStatus===v.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function $(t){function e(t,e){}return e.type="xstate.stopChild",e.actorRef=t,e.resolve=k,e.execute=R,e}const A=$;function j(t,e,s,n){const{machine:o}=n,i="function"==typeof t,r=i?t:o.implementations.guards["string"==typeof t?t:t.type];if(!i&&!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=i||"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 C=t=>"atomic"===t.type||"final"===t.type;function O(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function P(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,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(C(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 q(t,e){const s=function(t){const e=new Set(t),s=N(e);for(const t of e)if("compound"!==t.type||s.get(t)&&s.get(t).length){if("parallel"===t.type)for(const s of O(t))if("history"!==s.type&&!e.has(s)){const t=D(s);for(const s of t)e.add(s)}}else D(t).forEach((t=>e.add(t)));for(const t of e){let s=t.parent;for(;s;)e.add(s),s=s.parent}return e}(e);return M(t,N(s))}function D(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 O(t))s(e)}return s(t),e}(t);for(const s of e)for(const n of P(s,t))e.add(n);return e}const J=function(t){return p(t,this.value)},V=function(t){return this.tags.has(t)},W=function(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))},z=function(){const{_nodes:t,tags:e,machine:s,getMeta:n,toJSON:o,can:i,hasTag:r,matches:c,...a}=this;return{...a,tags:Array.from(e)}},B=function(){return this._nodes.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})};function G(t,e={}){return function(t,e){return{status:t.status,output:t.output,error:t.error,machine:e,context:t.context,_nodes:t._nodes,value:q(e.root,t._nodes),tags:new Set(t._nodes.flatMap((t=>t.tags))),children:t.children,historyValue:t.historyValue||{},matches:J,hasTag:V,can:W,getMeta:B,toJSON:z}}({...t,...e},t.machine)}function Q(t,{machine:e,context:s},n,o){return(i,r)=>{const c=((i,r={})=>{const{systemId:c,input:a}=r;if("string"==typeof i){const h=_(e,i);if(!h)throw new Error(`Actor logic '${i}' not implemented in machine '${e.id}'`);const u=b(h,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:"function"==typeof a?a({context:s,event:n,self:t.self}):a,src:i,systemId:c});return o[u.id]=u,u}return b(i,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:r.input,src:i,systemId:c})})(i,r);return o[c.id]=c,t.defer((()=>{c._processingStatus!==v.Stopped&&c.start()})),c}}function U(t,e,s,n,{assignment:o}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},r={context:e.context,event:s.event,spawn:Q(t,e,s.event,i),self:t?.self,system:t?.system};let c={};if("function"==typeof o)c=o(r,n);else for(const t of Object.keys(o)){const e=o[t];c[t]="function"==typeof e?e(r,n):e}return[G(e,{context:Object.assign({},e.context,c),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function X(t){function e(t,e){}return e.type="xstate.assign",e.assignment=t,e.resolve=U,e}function F(t,e,s,n,{event:o,id:i,delay:r},{internalQueue:c}){const a=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with raise; use raise({ type: "${o}" }) instead`);const h="function"==typeof o?o(s,n):o;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:i,delay:u}]}function H(t,e){const{event:s,delay:n,id:o}=e;"number"!=typeof n||t.defer((()=>{const e=t.self;t.system.scheduler.schedule(e,e,s,n,o)}))}function K(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=F,s.execute=H,s}let L=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function Y(t,e,s,n,{to:o,event:i,id:r,delay:c},a){const h=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 u="function"==typeof i?i(s,n):i;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 p="function"==typeof o?o(s,n):o;let l;if("string"==typeof p){if(l=p===L.Parent?t?.self._parent:p===L.Internal?t?.self:p.startsWith("#_")?e.children[p.slice(2)]:a.deferredActorIds?.includes(p)?p:e.children[p],!l)throw new Error(`Unable to send event to actor '${p}' from machine '${e.machine.id}'.`)}else l=p||t?.self;return[e,{to:l,event:u,id:r,delay:d}]}function Z(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function tt(t,e){t.defer((()=>{const{to:s,event:o,delay:i,id:r}=e;"number"!=typeof i?t.system._relay(t.self,s,o.type===n?c(t.self.id,o.data):o):t.system.scheduler.schedule(t.self,s,o,i,r)}))}function et(t,e,s){function n(t,e){}return n.type="xsnapshot.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=Y,n.retryResolve=Z,n.execute=tt,n}function st(t,e,s,n,{collect:o}){const i=[],r=function(t){i.push(t)};return r.assign=(...t)=>{i.push(X(...t))},r.cancel=(...t)=>{i.push(w(...t))},r.raise=(...t)=>{i.push(K(...t))},r.sendTo=(...t)=>{i.push(et(...t))},r.spawnChild=(...t)=>{i.push(T(...t))},r.stopChild=(...t)=>{i.push($(...t))},o({context:s.context,event:s.event,enqueue:r,check:t=>j(t,e.context,s.event,e)}),[e,void 0,i]}function nt(t,e,s,n,{value:o,label:i}){return[e,{value:"function"==typeof o?o(s,n):o,label:i}]}function ot({logger:t},{value:e,label:s}){s?t(s,e):t(e)}t.assign=X,t.cancel=w,t.enqueueActions=function(t){function e(t,e){}return e.type="xstate.enqueueActions",e.collect=t,e.resolve=st,e},t.forwardTo=function(t,e){return et(t,(({event:t})=>t),e)},t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function s(t,e){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=nt,s.execute=ot,s},t.raise=K,t.sendParent=function(t,e){return et(L.Parent,t,e)},t.sendTo=et,t.spawnChild=T,t.stop=A,t.stopChild=$,Object.defineProperty(t,"__esModule",{value:!0})}));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActions={})}(this,(function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const s=".",n="xstate.error",o="xstate.stop";function i(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const r=t=>{const e=i();e&&e.register(t)};function c(t,e){return{type:`xstate.error.actor.${t}`,error:e}}function a(t){setTimeout((()=>{throw t}))}const h="function"==typeof Symbol&&Symbol.observable||"@@observable";function u(t,e){return`${t.sessionId}.${e}`}let d=0;function p(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&&p(s[t],n[t])))}function l(t){return e=t,Array.isArray(e)?t:t.split(s);var e}function f(t){if((e=t)&&"object"==typeof e&&"machine"in e&&"value"in e)return t.value;var e;if("string"!=typeof t)return t;return function(t){if(1===t.length)return t[0];const e={};let s=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)s[t[e]]=t[e+1];else{const n=s;s={},n[t[e]]=s}return e}(l(t))}function y(t,e,s){const n="object"==typeof t,o=n?t:void 0;return{next:(n?t.next:t)?.bind(o),error:(n?t.error:e)?.bind(o),complete:(n?t.complete:s)?.bind(o)}}function _(t,e){const s=e.match(/^xstate\.invoke\.(\d+)\.(.*)/);if(!s)return t.implementations.actors[e];const[,n,o]=s,i=t.getStateNodeById(o).config.invoke;return(Array.isArray(i)?i[n]:i).src}let v=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const g={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class m{constructor(t,s){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),this.observers=new Set,this.logger=void 0,this._processingStatus=v.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const n={...g,...s},{clock:o,logger:i,parent:r,syncSnapshot:c,id:a,systemId:h,inspect:p}=n;this.system=r?r.system:function(t,e){const s=new Map,n=new Map,o=new WeakMap,i=new Set,r={},c=e.clock,a={schedule:(t,e,s,n,o=Math.random().toString(36).slice(2))=>{const i={source:t,target:e,event:s,delay:n,id:o,startedAt:Date.now()},a=u(t,o);h._snapshot._scheduledEvents[a]=i;const d=c.setTimeout((()=>{delete r[a],delete h._snapshot._scheduledEvents[a],h._relay(t,e,s)}),n);r[a]=d},cancel:(t,e)=>{const s=u(t,e),n=r[s];delete r[s],delete h._snapshot._scheduledEvents[s],c.clearTimeout(n)},cancelAll:t=>{for(const e in h._snapshot._scheduledEvents){const s=h._snapshot._scheduledEvents[e];s.source===t&&a.cancel(t,s.id)}}},h={_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+d++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const e=o.get(t);void 0!==e&&(n.delete(e),o.delete(t))},get:t=>n.get(t),_set:(t,e)=>{const s=n.get(t);if(s&&s!==e)throw new Error(`Actor with system ID '${t}' already exists.`);n.set(t,e),o.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)=>{h._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)},scheduler:a,getSnapshot:()=>({_scheduledEvents:{...h._snapshot._scheduledEvents}}),start:()=>{const t=h._snapshot._scheduledEvents;h._snapshot._scheduledEvents={};for(const e in t){const{source:s,target:n,event:o,delay:i,id:r}=t[e];a.schedule(s,n,o,i,r)}}};return h}(this,{clock:o}),p&&!r&&this.system.inspect(y(p)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=i,this.clock=o,this._parent=r,this._syncSnapshot=c,this.options=n,this.src=n.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),h&&(this._systemId=h,this.system._set(h,this)),this._initState(s?.snapshot??s?.state),h&&"active"!==this._snapshot.status&&this.system._unregister(this)}_initState(t){try{this._snapshot=t?this.logic.restoreSnapshot?this.logic.restoreSnapshot(t,this._actorScope):t:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t}}}update(t,e){let s;for(this._snapshot=t;s=this._deferred.shift();)try{s()}catch(e){this._deferred.length=0,this._snapshot={...t,status:"error",error:e}}switch(this._snapshot.status){case"active":for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}break;case"done":for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}this._stopProcedure(),this._complete(),this._doneEvent=(n=this.id,o=this._snapshot.output,{type:`xstate.done.actor.${n}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var n,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=y(t,e,s);if(this._processingStatus!==v.Stopped)this.observers.add(n);else switch(this._snapshot.status){case"done":try{n.complete?.()}catch(t){a(t)}break;case"error":{const t=this._snapshot.error;if(n.error)try{n.error(t)}catch(t){a(t)}else a(t);break}}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this._processingStatus===v.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this._processingStatus=v.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._snapshot={...this._snapshot,status:"error",error:t},this._error(t),this}return this.update(this._snapshot,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._snapshot={...this._snapshot,status:"error",error:t},void this._error(t)}this.update(e,t),t.type===o&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===v.Stopped?this:(this.mailbox.clear(),this._processingStatus===v.NotStarted?(this._processingStatus=v.Stopped,this):(this.mailbox.enqueue({type:o}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){a(t)}this.observers.clear()}_reportError(t){if(!this.observers.size)return void(this._parent||a(t));let e=!1;for(const s of this.observers){const n=s.error;e||=!n;try{n?.(t)}catch(t){a(t)}}this.observers.clear(),e&&a(t)}_error(t){this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,c(this.id,t))}_stopProcedure(){return this._processingStatus!==v.Running||(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new e(this._process.bind(this)),this._processingStatus=v.Stopped,this.system._unregister(this)),this}_send(t){this._processingStatus!==v.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:r)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[h](){return this}getSnapshot(){return this._snapshot}}function b(t,e){return new m(t,e)}function x(t,e,s,n,{sendId:o}){return[e,"function"==typeof o?o(s,n):o]}function S(t,e){t.defer((()=>{t.system.scheduler.cancel(t.self,e)}))}function w(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=x,e.execute=S,e}function I(t,e,s,n,{id:o,systemId:i,src:r,input:c,syncSnapshot:a}){const h="string"==typeof r?_(e.machine,r):r,u="function"==typeof o?o(s):o;let d;return h&&(d=b(h,{id:u,src:r,parent:t?.self,syncSnapshot:a,systemId:i,input:"function"==typeof c?c({context:e.context,event:s.event,self:t?.self}):c})),[G(e,{children:{...e.children,[u]:d}}),{id:o,actorRef:d}]}function E(t,{id:e,actorRef:s}){s&&t.defer((()=>{s._processingStatus!==v.Stopped&&s.start()}))}function T(...[t,{id:e,systemId:s,input:n,syncSnapshot:o=!1}={}]){function i(t,e){}return i.type="snapshot.spawnChild",i.id=e,i.systemId=s,i.src=t,i.input=n,i.syncSnapshot=o,i.resolve=I,i.execute=E,i}function k(t,e,s,n,{actorRef:o}){const i="function"==typeof o?o(s,n):o,r="string"==typeof i?e.children[i]:i;let c=e.children;return r&&(c={...c},delete c[r.id]),[G(e,{children:c}),r]}function R(t,e){e&&(t.system._unregister(e),e._processingStatus===v.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function $(t){function e(t,e){}return e.type="xstate.stopChild",e.actorRef=t,e.resolve=k,e.execute=R,e}const A=$;function j(t,e,s,n){const{machine:o}=n,i="function"==typeof t,r=i?t:o.implementations.guards["string"==typeof t?t:t.type];if(!i&&!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=i||"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 C=t=>"atomic"===t.type||"final"===t.type;function O(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function P(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,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(C(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 q(t,e){const s=function(t){const e=new Set(t),s=N(e);for(const t of e)if("compound"!==t.type||s.get(t)&&s.get(t).length){if("parallel"===t.type)for(const s of O(t))if("history"!==s.type&&!e.has(s)){const t=D(s);for(const s of t)e.add(s)}}else D(t).forEach((t=>e.add(t)));for(const t of e){let s=t.parent;for(;s;)e.add(s),s=s.parent}return e}(e);return M(t,N(s))}function D(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 O(t))s(e)}return s(t),e}(t);for(const s of e)for(const n of P(s,t))e.add(n);return e}const J=function(t){return p(t,this.value)},V=function(t){return this.tags.has(t)},W=function(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))},z=function(){const{_nodes:t,tags:e,machine:s,getMeta:n,toJSON:o,can:i,hasTag:r,matches:c,...a}=this;return{...a,tags:Array.from(e)}},B=function(){return this._nodes.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})};function G(t,e={}){return function(t,e){return{status:t.status,output:t.output,error:t.error,machine:e,context:t.context,_nodes:t._nodes,value:q(e.root,t._nodes),tags:new Set(t._nodes.flatMap((t=>t.tags))),children:t.children,historyValue:t.historyValue||{},matches:J,hasTag:V,can:W,getMeta:B,toJSON:z}}({...t,...e},t.machine)}function Q(t,{machine:e,context:s},n,o){return(i,r)=>{const c=((i,r={})=>{const{systemId:c,input:a}=r;if("string"==typeof i){const h=_(e,i);if(!h)throw new Error(`Actor logic '${i}' not implemented in machine '${e.id}'`);const u=b(h,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:"function"==typeof a?a({context:s,event:n,self:t.self}):a,src:i,systemId:c});return o[u.id]=u,u}return b(i,{id:r.id,parent:t.self,syncSnapshot:r.syncSnapshot,input:r.input,src:i,systemId:c})})(i,r);return o[c.id]=c,t.defer((()=>{c._processingStatus!==v.Stopped&&c.start()})),c}}function U(t,e,s,n,{assignment:o}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},r={context:e.context,event:s.event,spawn:Q(t,e,s.event,i),self:t?.self,system:t?.system};let c={};if("function"==typeof o)c=o(r,n);else for(const t of Object.keys(o)){const e=o[t];c[t]="function"==typeof e?e(r,n):e}return[G(e,{context:Object.assign({},e.context,c),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function X(t){function e(t,e){}return e.type="xstate.assign",e.assignment=t,e.resolve=U,e}function F(t,e,s,n,{event:o,id:i,delay:r},{internalQueue:c}){const a=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with raise; use raise({ type: "${o}" }) instead`);const h="function"==typeof o?o(s,n):o;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:i,delay:u}]}function H(t,e){const{event:s,delay:n,id:o}=e;"number"!=typeof n||t.defer((()=>{const e=t.self;t.system.scheduler.schedule(e,e,s,n,o)}))}function K(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=F,s.execute=H,s}let L=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function Y(t,e,s,n,{to:o,event:i,id:r,delay:c},a){const h=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 u="function"==typeof i?i(s,n):i;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 p="function"==typeof o?o(s,n):o;let l;if("string"==typeof p){if(l=p===L.Parent?t?.self._parent:p===L.Internal?t?.self:p.startsWith("#_")?e.children[p.slice(2)]:a.deferredActorIds?.includes(p)?p:e.children[p],!l)throw new Error(`Unable to send event to actor '${p}' from machine '${e.machine.id}'.`)}else l=p||t?.self;return[e,{to:l,event:u,id:r,delay:d}]}function Z(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function tt(t,e){t.defer((()=>{const{to:s,event:o,delay:i,id:r}=e;"number"!=typeof i?t.system._relay(t.self,s,o.type===n?c(t.self.id,o.data):o):t.system.scheduler.schedule(t.self,s,o,i,r)}))}function et(t,e,s){function n(t,e){}return n.type="xsnapshot.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=Y,n.retryResolve=Z,n.execute=tt,n}function st(t,e,s,n,{collect:o}){const i=[],r=function(t){i.push(t)};return r.assign=(...t)=>{i.push(X(...t))},r.cancel=(...t)=>{i.push(w(...t))},r.raise=(...t)=>{i.push(K(...t))},r.sendTo=(...t)=>{i.push(et(...t))},r.spawnChild=(...t)=>{i.push(T(...t))},r.stopChild=(...t)=>{i.push($(...t))},o({context:s.context,event:s.event,enqueue:r,check:t=>j(t,e.context,s.event,e),self:t.self,system:t.system}),[e,void 0,i]}function nt(t,e,s,n,{value:o,label:i}){return[e,{value:"function"==typeof o?o(s,n):o,label:i}]}function ot({logger:t},{value:e,label:s}){s?t(s,e):t(e)}t.assign=X,t.cancel=w,t.enqueueActions=function(t){function e(t,e){}return e.type="xstate.enqueueActions",e.collect=t,e.resolve=st,e},t.forwardTo=function(t,e){return et(t,(({event:t})=>t),e)},t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function s(t,e){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=nt,s.execute=ot,s},t.raise=K,t.sendParent=function(t,e){return et(L.Parent,t,e)},t.sendTo=et,t.spawnChild=T,t.stop=A,t.stopChild=$,Object.defineProperty(t,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=xstate-actions.umd.min.js.map
|