xstate 6.0.0-alpha.49 → 6.0.0-alpha.50
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/dist/{StateMachine-0adb3a70.cjs.js → StateMachine-1e6ee14a.cjs.js} +18 -6
- package/dist/{StateMachine-29450e6d.development.cjs.js → StateMachine-70df737c.development.cjs.js} +18 -6
- package/dist/{StateMachine-2af31c11.esm.js → StateMachine-8cea8974.esm.js} +18 -6
- package/dist/{StateMachine-faf50c5c.development.esm.js → StateMachine-bc28e207.development.esm.js} +18 -6
- package/dist/{createActor-b4dd05ae.cjs.js → createActor-27fd819f.cjs.js} +80 -10
- package/dist/{createActor-88709f74.development.esm.js → createActor-74e185ee.development.esm.js} +80 -11
- package/dist/{createActor-268165d4.esm.js → createActor-ad3a1abe.esm.js} +80 -11
- package/dist/{createActor-a1e0dffb.development.cjs.js → createActor-ea9a560c.development.cjs.js} +80 -10
- package/dist/declarations/src/effectDescriptor.d.ts +9 -0
- package/dist/declarations/src/index.d.ts +1 -1
- package/dist/declarations/src/inspection.d.ts +24 -7
- package/dist/declarations/src/runtimeHelpers.d.ts +6 -3
- package/dist/declarations/src/system.d.ts +38 -3
- package/dist/declarations/src/types.d.ts +35 -2
- package/dist/{effectDescriptor-7ff6802f.development.cjs.js → effectDescriptor-16b6f3b4.cjs.js} +12 -1
- package/dist/{effectDescriptor-39d22100.development.esm.js → effectDescriptor-38598bd3.development.esm.js} +12 -1
- package/dist/{effectDescriptor-be348e50.cjs.js → effectDescriptor-40ca302f.development.cjs.js} +12 -1
- package/dist/{effectDescriptor-30c9a744.esm.js → effectDescriptor-7dac431a.esm.js} +12 -1
- package/dist/{fsm-e18a7cb4.development.esm.js → fsm-25f10f50.development.esm.js} +1 -1
- package/dist/{fsm-c1316948.development.cjs.js → fsm-697ffd89.development.cjs.js} +1 -1
- package/dist/{fsm-7a2e9697.cjs.js → fsm-7adaf0ea.cjs.js} +1 -1
- package/dist/{fsm-b804b3e9.esm.js → fsm-dc1bb88b.esm.js} +1 -1
- package/dist/{types-fbabaeee.cjs.js → types-a14db10c.development.cjs.js} +6 -1
- package/dist/{types-3c3455f7.esm.js → types-a313e7e2.esm.js} +6 -1
- package/dist/{types-b3fea5bd.development.cjs.js → types-bc22b358.cjs.js} +6 -1
- package/dist/{types-adda3842.development.esm.js → types-c37714eb.development.esm.js} +6 -1
- package/dist/xstate-actors.cjs.js +12 -3
- package/dist/xstate-actors.development.cjs.js +12 -3
- package/dist/xstate-actors.development.esm.js +13 -4
- package/dist/xstate-actors.esm.js +13 -4
- package/dist/xstate-actors.umd.min.js +1 -1
- package/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/xstate-durable.cjs.js +2 -2
- package/dist/xstate-durable.development.cjs.js +2 -2
- package/dist/xstate-durable.development.esm.js +2 -2
- package/dist/xstate-durable.esm.js +2 -2
- package/dist/xstate-durable.umd.min.js +1 -1
- package/dist/xstate-durable.umd.min.js.map +1 -1
- package/dist/xstate-fsm.cjs.js +2 -2
- package/dist/xstate-fsm.development.cjs.js +2 -2
- package/dist/xstate-fsm.development.esm.js +2 -2
- package/dist/xstate-fsm.esm.js +2 -2
- package/dist/xstate-graph.cjs.js +2 -2
- package/dist/xstate-graph.development.cjs.js +2 -2
- package/dist/xstate-graph.development.esm.js +2 -2
- package/dist/xstate-graph.esm.js +2 -2
- package/dist/xstate-graph.umd.min.js +1 -1
- package/dist/xstate-graph.umd.min.js.map +1 -1
- package/dist/xstate-scxml.cjs.js +3 -3
- package/dist/xstate-scxml.development.cjs.js +3 -3
- package/dist/xstate-scxml.development.esm.js +3 -3
- package/dist/xstate-scxml.esm.js +3 -3
- package/dist/xstate-scxml.umd.min.js +1 -1
- package/dist/xstate-scxml.umd.min.js.map +1 -1
- package/dist/xstate-validation.cjs.js +1 -1
- package/dist/xstate-validation.development.cjs.js +1 -1
- package/dist/xstate-validation.development.esm.js +1 -1
- package/dist/xstate-validation.esm.js +1 -1
- package/dist/xstate.cjs.js +5 -5
- package/dist/xstate.development.cjs.js +5 -5
- package/dist/xstate.development.esm.js +8 -8
- package/dist/xstate.esm.js +8 -8
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.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 effectDescriptor = require('./effectDescriptor-
|
|
6
|
-
var createActor = require('./createActor-
|
|
5
|
+
var effectDescriptor = require('./effectDescriptor-16b6f3b4.cjs.js');
|
|
6
|
+
var createActor = require('./createActor-27fd819f.cjs.js');
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* An event addressed to a durable execution's root actor, captured while
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var effectDescriptor = require('./effectDescriptor-
|
|
6
|
-
var createActor = require('./createActor-
|
|
5
|
+
var effectDescriptor = require('./effectDescriptor-40ca302f.development.cjs.js');
|
|
6
|
+
var createActor = require('./createActor-ea9a560c.development.cjs.js');
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* An event addressed to a durable execution's root actor, captured while
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { i as initialTransition, t as transition, c as getEffectDescriptor } from './effectDescriptor-
|
|
2
|
-
import {
|
|
1
|
+
import { i as initialTransition, t as transition, c as getEffectDescriptor } from './effectDescriptor-38598bd3.development.esm.js';
|
|
2
|
+
import { aD as encodeAddressSegment, aE as getRootActorId, aF as RUNTIME_OPERATIONS, aG as withSystemInspector, aH as getSnapshotActorRef, aI as withExecutionIdentity, u as deliverEvent } from './createActor-74e185ee.development.esm.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* An event addressed to a durable execution's root actor, captured while
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { i as initialTransition, t as transition, c as getEffectDescriptor } from './effectDescriptor-
|
|
2
|
-
import {
|
|
1
|
+
import { i as initialTransition, t as transition, c as getEffectDescriptor } from './effectDescriptor-7dac431a.esm.js';
|
|
2
|
+
import { aD as encodeAddressSegment, aE as getRootActorId, aF as RUNTIME_OPERATIONS, aG as withSystemInspector, aH as getSnapshotActorRef, aI as withExecutionIdentity, u as deliverEvent } from './createActor-ad3a1abe.esm.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* An event addressed to a durable execution's root actor, captured while
|
|
@@ -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).XStateDurable={})}(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="@xstate.stop",r="@xstate.terminate";function i(t){return{type:"@xstate.init",input:t}}function n(t){setTimeout(()=>{throw t})}const o="function"==typeof Symbol&&Symbol.observable||"@@observable",c=Symbol();function a(t=this.actor.system){const e="done"===this.status?{status:"done",output:this.output,error:void 0}:{status:"error",output:void 0,error:this.error};return t.terminateActor(this.actor,e)}function h(t,e){if("done"!==e.status&&"error"!==e.status)throw new Error("Cannot terminate an active or stopped actor");const s="done"===e.status?{status:"done",output:e.output,error:void 0}:{status:"error",output:void 0,error:e.error},i=[t.self,s];return{kind:"builtin",exec:a,type:r,source:t.self,actor:t.self,id:t.self.id,...s,params:void 0,args:i}}function d(t,e,[s,i]){const n="done"===s.status||"error"===s.status,o="done"===e?.status||"error"===e?.status,c=i.some(t=>"object"==typeof t&&null!==t&&"type"in t&&t.type===r);return!n||o||c?[s,i]:[s,[...i,h(t,s)]]}const u=()=>{},l=()=>({});let p;function _(){return void 0!==p}function f(){return p}function v(t,e){if(!t)return e();const s=p;p=t;try{return e()}finally{p=s}}!function(t,e){const s=(t,...s)=>{e(t,...s)};Object.assign(s,{cancel:u,emit:u,log:u,raise:u,spawn:l,sendTo:u,stop:u,listen:l,subscribeTo:l,...t})}({},u);const y=new WeakMap,m=new WeakMap,g=new Map;function b(t){const e=t._peekKeyedActors;return e?e.call(t)??g:t.keyedActors}function S(t){t._snapshotVersion++}function x(t){return t._snapshotVersion}function w(t,e,s){const r=_(),i=function(t,e){if(e){const t=new Map(e.children);return e.root?.sessionId&&t.set(e.root.sessionId,e.root),t}return new Map(t.children)}(t,s),n=new Map(s?.keyedActors??b(t)),o=new WeakMap,c=Object.assign(Object.create(t),{children:i,keyedActors:n,reverseKeyedActors:o,_snapshot:{...s?.snapshot??t._snapshot},_snapshotVersion:s?.sourceVersion??t._snapshotVersion,_register:(t,e)=>(i.set(t,e),S(c),t),_unregister:t=>{t.sessionId&&i.delete(t.sessionId);const e=o.get(t);void 0!==e&&n.get(e)===t&&n.delete(e),o.delete(t),S(c)},_set:(t,e)=>{const s=n.get(t);if(s&&s!==e)throw new Error(`Actor with registry key '${String(t)}' already exists.`);n.set(t,e),o.set(e,t),S(c)},get:t=>n.get(t),getAll:()=>Object.fromEntries(n),_hasInspectionObservers:r?()=>t._hasInspectionObservers?.()??!1:()=>!1,_sendInspectionEvent:r?e=>t._sendInspectionEvent(e):()=>{}});for(const[t,e]of n)e&&o.set(e,t);for(const t of Object.values(e)){if(!t)continue;t.sessionId&&i.set(t.sessionId,t);const e=t.registryKey;e&&(n.set(e,t),o.set(t,e))}return c}function A(t){const e=y.get(t);if(e)return e;const s=m.get(t);if(!s)return;const r=s();return y.set(t,r),m.delete(t),r}function E(t,e,s=e.system,r){m.delete(t);const i=x(s),n=r?A(r):void 0,o=n?.actor===e?n:A(t);if(o?.actor===e&&o.systemState.sourceSystem===s&&o.systemState.sourceVersion===i)return void y.set(t,o);const c=function(t){const e=t._getRootActor?.(),s=t._peekChildren?.();if(!e&&!s){if(t._getRootActor&&t._peekChildren)return{};const e=new Map(t.children);return e.size?{children:e}:{}}return s?.size?{children:new Map(t.children)}:e?{root:e}:{}}(s);let a=c.children;const h=b(s);let d=h.size?new Map(h):g;for(const e of Object.values(function(t){return"children"in t&&t.children?t.children:{}}(t))){if(!e)continue;e.sessionId&&(a??=new Map).set(e.sessionId,e);const t=e.registryKey;t&&(d===g&&(d=new Map),d.set(t,e))}y.set(t,{actor:e,systemState:{...c,children:a,keyedActors:d,snapshot:{...s._snapshot},sourceSystem:s,sourceVersion:i}})}function I(t,e,s=!0){const r=O(t);return r?(r.snapshot=e,r.materialized&&(r.materialized.self._snapshot=e)):t.self._snapshot=e,s&&e&&"object"==typeof e&&E(e,t.self),e}function k(t,e){I(t,e,!1);const s=O(t);R.set(e,t);const r=()=>(E(e,t.self,t.system),A(e));return s&&(s.materialized?s.identityProvider=r:s.identityProvider??=r),function(t,e){y.delete(t),m.set(t,e)}(e,r),e}const T=Symbol(),R=new WeakMap;function O(t){return t[T]}function M(t){return O(t).materialize()}const j={[c]:!0,get _parent(){const t=O(this);return t.parentKnown?t.parent:M(this).self._parent},get self(){return M(this).self},get defer(){return M(this).defer},get id(){return M(this).id},get logger(){return M(this).logger},get sessionId(){return M(this).sessionId},get stopChild(){return M(this).stopChild},get system(){return M(this).system},get emit(){return M(this).emit},get actionExecutor(){return M(this).actionExecutor}};function P(t,e,s,r,i){const n=e?.(),o=i??n?.actor,c=o?.system,a=o&&c?w(c,s,i?void 0:n?.systemState):void 0,h=(d=t,u={_inert:!0,...o?{id:o.id,_sessionId:o.sessionId}:{},...a?{_systemRef:{current:a}}:{}},new at(d,u));var d,u;return o?._parent&&(h._parent=o._parent,h._address=void 0),r&&(h._snapshot=r),Object.create(h._actorScope,{defer:{value:()=>{}},stopChild:{value:t=>t._stop()},actionExecutor:{value:()=>{}}})}function $(t,e,s,r){const i=r??(e&&"object"==typeof e?R.get(e):void 0),n=i?O(i):void 0,o=e&&"object"==typeof e?function(t){return y.get(t)}(e):void 0,c=n?.identityProvider??(e&&"object"==typeof e?function(t){const e=y.get(t);return e?()=>e:m.get(t)}(e):void 0),a={snapshot:e,sourceRef:c,sourceChildren:(h=e,h&&"object"==typeof h&&"machine"in h&&"value"in h?e.children:{}),identityProvider:n?.identityProvider??c,parent:s?._parent??n?.parent??o?.actor._parent,parentKnown:!(!s&&!n?.parentKnown&&!o&&e)};var h;if(a.materialize=()=>a.materialized??=P(t,a.sourceRef,a.sourceChildren,a.snapshot,s),!a.identityProvider&&!e){const e={},r=a.sourceRef,i=a.sourceChildren;let n,o;const c=()=>n??=P(t,r,i,void 0,s);a.materialize=()=>{const t=c();return void 0!==a.snapshot&&(t.self._snapshot=a.snapshot),a.materialized=t},a.identityProvider=()=>{if(o)return o;const t=c();return E(e,t.self,t.system),o=A(e)}}const d=Object.create(j);return d[T]=a,d}function z(t,e,s){const r="object"==typeof t,i=r?t:void 0;return{next:(r?t.next:t)?.bind(i),error:(r?t.error:e)?.bind(i),complete:(r?t.complete:s)?.bind(i)}}async function K(t,e,s,r=e=>{t.system.sendEvent(t,t,e)}){const i=function(t,e){return t.getSnapshot().effects?.[e]}(t,e);if("done"===i?.status)return i.output;if("error"===i?.status)throw i.error;if("active"===i?.status)return function(t,e){return new Promise((s,r)=>{const i=t.subscribe(t=>{const n=t.effects?.[e];"done"===n?.status?(i.unsubscribe(),s(n.output)):"error"===n?.status&&(i.unsubscribe(),r(n.error))})})}(t,e);r({type:"xstate.logic.effect.start",key:e});try{const t=await s();return r({type:"xstate.logic.effect.resolve",key:e,output:t}),t}catch(t){throw r({type:"xstate.logic.effect.reject",key:e,error:t}),t}}function C(t,e,s){L(t,e,s);const r=e;r._lastSourceRef=t,r._send(s)}function L(t,e,s){const r=e;if(t!==e&&r.logic?.isInternalEventType?.(s.type))throw new Error(`Internal event "${s.type}" cannot be sent to actor "${e.id}" from outside.`)}let D;Object.freeze({});let V=0;const N=new Error("Transition effect"),q=[];function W(){return D??=function(){let t;try{t=globalThis.crypto}catch{}if(t?.getRandomValues)try{const e=new Uint32Array(4);return t.getRandomValues(e),Array.from(e,t=>t.toString(36).padStart(7,"0")).join("")}catch{}if(t?.randomUUID)try{return t.randomUUID().replaceAll("-","")}catch{}return`xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`}(),`${D}:${(V++).toString(36)}`}let U;function F(t,e){if(!t)return e();const s=U;U=t;try{return e()}finally{U=s}}function H(t){if("string"==typeof t)return t;const e=t?.id;return"string"==typeof e&&e.length&&"(machine)"!==e?e:"x"}function J(t){return t.includes("/")||t.includes("%")?t.replaceAll("%","%25").replaceAll("/","%2F"):t}function X(t,e){return`${t?t.address:""}|${e}`}function B(t,e,s){const r=t._snapshot._nextActorIds;(r[e]??0)>=s||(t._snapshot._nextActorIds={...r,[e]:s},S(t))}function G(t,e,s){if(void 0!==e){const r=function(t){const e=t.lastIndexOf(":");if(e<=0||e===t.length-1)return;const s=Number(t.slice(e+1));return Number.isSafeInteger(s)&&s>=0?{prefix:t.slice(0,e),index:s}:void 0}(e);return r?B(t,X(s?.parent,r.prefix),r.index+1):s?.parent||B(t,X(void 0,e),1),e}const r=H(s?.src),i=X(s?.parent,r),n=t._snapshot._nextActorIds[i]??0;return B(t,i,n+1),s?.parent||"x"===r||0!==n?`${r}:${n}`:r}const Q=["spawnActor","startActor","stopActor","terminateActor","emitEvent","scheduleTimer","cancelTimer","cancelAllTimers","deadLetter"],Y=Object.freeze({});function Z(t,e){return`${t.sessionId}.${e}`}class tt{get children(){const t=this._children??=new Map;return this._getRootActor()&&t.set(this._rootActor.sessionId,this._rootActor),t}set children(t){this._children=t}_getRootActor(){return this._rootActor._isRunning()?this._rootActor:void 0}_peekChildren(){return this._children}get keyedActors(){return this._keyedActors??=new Map}set keyedActors(t){this._keyedActors=t}get reverseKeyedActors(){return this._reverseKeyedActors??=new WeakMap}set reverseKeyedActors(t){this._reverseKeyedActors=t}_peekKeyedActors(){return this._keyedActors}constructor(t,e){this._rootActor=t,this._identity=U??{systemId:W(),nextSessionId:0},this._snapshot=void 0,this._snapshotVersion=0,this.scheduler=this,this._clock=void 0,this._logger=void 0,this.createActorRef=void 0;const s="object"==typeof e.snapshot&&null!==e.snapshot?e.snapshot:void 0;this._clock=e.clock,this._logger=e.logger,this.createActorRef=e.createActorRef;const r=f();r&&this.inspect(r),this._snapshot={_scheduledTimers:s?.scheduler??Y,_nextActorIds:{}}}_recordSent(t,e,s,r,i){if(!this._inspectionObservers?.size||!t)return;const n=t;(n._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:s,delay:r,id:i})}schedule(t,e,s){const r=Z(t,e);void 0!==this._timerMap?.[r]&&this.cancel(t,e);const i=t.getSnapshot()?.timers?.[e];if(i){const r="self"===i.target?t:i.target;this._recordSent(t,r,i.event,s,e)}const n=this._clock.now?.()??Date.now(),o={source:t,delay:s,id:e,scheduledAt:n,dueAt:n+s},c=Z(t,e);this._snapshot._scheduledTimers===Y&&(this._snapshot._scheduledTimers={}),this._snapshot._scheduledTimers[c]=o,S(this);const a=this._clock.setTimeout(()=>{this._timerMap&&delete this._timerMap[c],delete this._snapshot._scheduledTimers[c],S(this),this._deliver(t,t,{type:"xstate.timer",id:e})},s);(this._timerMap??={})[c]=a}cancel(t,e){const s=Z(t,e),r=this._timerMap?.[s];this._timerMap&&delete this._timerMap[s],this._snapshot._scheduledTimers!==Y&&delete this._snapshot._scheduledTimers[s],S(this),void 0!==r&&this._clock.clearTimeout(r)}cancelAll(t){for(const e in this._snapshot._scheduledTimers){const s=this._snapshot._scheduledTimers[e];s.source===t&&this.cancel(t,s.id)}}_deliver(t,e,s){C(t,e,s)}_register(t,e){return e===this._rootActor?this._children?.set(t,e):(this._children??=new Map).set(t,e),S(this),t}_unregister(t){let e;t===this._rootActor?(e=void 0!==this._getRootActor(),e=void 0!==t.sessionId&&(this._children?.delete(t.sessionId)??!1)||e):e=void 0!==t.sessionId&&(this._children?.delete(t.sessionId)??!1);const s=this._reverseKeyedActors?.get(t);void 0!==s&&(this._keyedActors?.get(s)===t&&(this._keyedActors.delete(s),e=!0),this._reverseKeyedActors?.delete(t)),e&&S(this)}get(t){return this._keyedActors?.get(t)}getAll(){return Object.fromEntries(this._keyedActors?.entries()??[])}_set(t,e){const s=this._keyedActors?.get(t);if(s&&s!==e)throw new Error(`Actor with registry key '${t}' already exists.`);(this._keyedActors??=new Map).set(t,e),(this._reverseKeyedActors??=new WeakMap).set(e,t),s!==e&&S(this)}inspect(t){const e=z(t);return(this._inspectionObservers??=new Set).add(e),{unsubscribe:()=>{this._inspectionObservers?.delete(e)}}}_hasInspectionObservers(){return!!this._inspectionObservers?.size}_sendInspectionEvent(t){if(!this._inspectionObservers?.size)return;const e={...t,rootId:this._rootActor.sessionId};this._inspectionObservers.forEach(t=>t.next?.(e))}spawnActor(t,e){return this.runtime?.spawnActor?.(t,e)}startActor(t){const e=this.runtime?.startActor;if(e)return e(t);t.start()}stopActor(t){const e=this.runtime?.stopActor;if(e)return e(t);!function(t){t._stop()}(t)}terminateActor(t,e){const s=this.runtime?.terminateActor;if(s)return s(t,e);!function(t,e){t._terminate(e)}(t,e)}sendEvent(t,e,s){L(t,e,s),this._recordSent(t,e,s);const r=this.runtime?.sendEvent;if(r)return r(t,e,s);this._deliver(t,e,s)}emitEvent(t,e){const s=this.runtime?.emitEvent;if(s)return s(t,e);t._emit(e)}deadLetter(t,e,s,r){this._sendInspectionEvent({type:"@xstate.deadletter",actorRef:e,sourceRef:t,event:s,reason:r});const i=this.runtime?.deadLetter;if(i)return i(t,e,s,r)}runStep(t,e,s){const r=this.runtime?.runStep;return r?r(t,e,s):K(t,e,s)}runLogic(t,e){const s=this.runtime?.runLogic;return s?s(t,e):e()}scheduleTimer(t,e,s){const r=this.runtime?.scheduleTimer;if(r){const i=t.getSnapshot()?.timers?.[e];if(i){const r="self"===i.target?t:i.target;this._recordSent(t,r,i.event,s,e)}return r(t,e,s)}this.schedule(t,e,s)}cancelTimer(t,e){const s=this.runtime?.cancelTimer;if(s)return s(t,e);this.cancel(t,e)}cancelAllTimers(t){const e=this.runtime?.cancelAllTimers;if(e)return e(t);this.cancelAll(t)}_relay(t,e,s){if(q.length&&q.includes(e))throw N;return this.sendEvent(t,e,s)}getSnapshot(){return{_scheduledTimers:{...this._snapshot._scheduledTimers}}}start(){const t=this._snapshot._scheduledTimers;let e=!0;for(const s in t){e&&(this._snapshot._scheduledTimers={},e=!1);const{source:r,dueAt:i,id:n}=t[s];this.scheduleTimer(r,n,Math.max(0,i-(this._clock.now?.()??Date.now())))}}}let et=!1;const st=Object.freeze([]);let rt=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const it=Object.freeze({clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)});function nt(t,e){try{t?.(e)}catch(t){n(t)}}function ot(t,e){if(t?.length)for(const s of t)e.actionExecutor(s)}function ct(t,e){return new at(t,e)}class at{get address(){return this._address??=this._parent?`${this._parent.address}/${J(this.id)}`:J(this.id)}get trigger(){return this._trigger??=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}})}get send(){return this._boundSend??=this._sendPublic.bind(this)}constructor(t,e){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this._boundProcess=void 0,this.mailbox=void 0,this._mailboxStarted=!1,this.observers=void 0,this.eventListeners=void 0,this.logger=void 0,this._processingStatus=rt.NotStarted,this._forceDeferredActions=!1,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._lastSourceRef=void 0,this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0,this._initialEffects=void 0,this.registryKey=void 0,this.sessionId=void 0,this._address=void 0,this.system=void 0,this._trigger=void 0,this._boundSend=void 0,this.src=void 0,this._deferred=void 0,this._restored=!1;const s=e?{...it,...e}:it,{clock:r,logger:i,parent:n,syncSnapshot:o,id:c,registryKey:a,inspect:h}=s;var u;this.system=n?n.system:s._systemRef?.current??function(t,e){return new tt(t,e)}(this,{clock:r,logger:i,snapshot:s.snapshot??s.state,createActorRef:ct}),n||!s._systemRef||s._systemRef.current||(s._systemRef.current=this.system),h&&!n&&this.system.inspect(z(h)),this.sessionId=s._sessionId??`${(u=this.system)._identity.systemId}:${u._identity.nextSessionId++}`,this.id=G(this.system,c,{parent:n,src:s.src??t}),this.logger=e?.logger??this.system._logger,this.clock=e?.clock??this.system._clock,this._parent=n,this._syncSnapshot=o,this.options=s,this.src=s.src??t,this.ref=this,this._actorScope=this,a&&(this.registryKey=a,this.system._set(a,this)),this._collectedMicrosteps=void 0;const l=e?.snapshot??e?.state;this._restored=void 0!==l;try{if(l)this._setSnapshot(this.logic.restoreSnapshot?this.logic.restoreSnapshot(l,this._actorScope):l);else if(e?._inert);else{const[t,e]=d(this._actorScope,void 0,this.logic.initialTransition(this.options?.input,this._actorScope));this._setSnapshot(t),this._initialEffects=e.length?e:void 0}}catch(t){this._setSnapshot({status:"error",output:void 0,error:t}),this._deferred&&(this._deferred.length=0)}a&&"active"!==this._snapshot.status&&this.system._unregister(this),(this.system._hasInspectionObservers?.()??1)&&this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_getLogger(){return this.logger}get self(){return this}get defer(){const t=t=>this._defer(t);return Object.defineProperty(this,"defer",{value:t}),t}get stopChild(){const t=t=>this._stopChild(t);return Object.defineProperty(this,"stopChild",{value:t}),t}get emit(){const t=t=>this.system.emitEvent(this,t);return Object.defineProperty(this,"emit",{value:t}),t}get actionExecutor(){const t=t=>this._executeAction(t);return Object.defineProperty(this,"actionExecutor",{value:t}),t}_isRunning(){return this._processingStatus===rt.Running}_defer(t){(this._deferred??=[]).push(t)}_stopChild(t){if(t._parent!==this)throw new Error(`Cannot stop non-child actor ${t.id}`);t._stop()}_executeAction(t){const e=()=>{(this.system._hasInspectionObservers?.()??1)&&(this._collectedActions??=[]).push({type:t.type,params:t.params});const e=et;try{et=!0,t.exec()}finally{et=e}};this._processingStatus!==rt.Running||this._forceDeferredActions?(this._deferred??=[]).push(e):e()}_setSnapshot(t){const e=this._snapshot;this._snapshot=t,E(t,this,this.system,e)}_setErrorSnapshot(t,e=this._snapshot){this._setSnapshot({...e,status:"error",error:t})}_recoverOrError(t,e){return!!this._tryHandleExecutionError(t,e)||(this._setErrorSnapshot(t),this._error(t),!1)}_tryHandleExecutionError(t,e=this._snapshot){const s=this.logic.getExecutionErrorEvent?.(e,t);if(!s)return!1;try{const[t,r]=d(this._actorScope,e,this.logic.transition(e,s,this._actorScope));return this._setSnapshot(t),ot(r,this._actorScope),this.update(t,s),!0}catch{return!1}}_next(t){for(const e of this.observers??st)nt(e.next,t)}update(t,e){this._setSnapshot(t);const s=this._deferred;for(let e=0;e<(s?.length??0);e++){const r=s[e];try{r()}catch(e){if(s.length=0,this._tryHandleExecutionError(e,t))return;this._setErrorSnapshot(e,t),this._error(e);break}}if(s&&(s.length=0),"active"===this._snapshot.status)this._next(t);this._inspectTransition(this._snapshot,e)}_inspectTransition(t,e){(this.system._hasInspectionObservers?.()??1)&&this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps??st,actions:this._collectedActions??st,sent:this._collectedSent??st,eventType:e.type}),this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0}_flushInitialEffects(){if(!this._initialEffects?.length)return!0;this._forceDeferredActions=!0;try{return ot(this._initialEffects,this._actorScope),this._initialEffects=void 0,!0}catch(t){return this._initialEffects=void 0,this._deferred&&(this._deferred.length=0),this._recoverOrError(t),"active"===this._snapshot.status}finally{this._forceDeferredActions=!1}}subscribe(t,e,s){const r=z(t,e,s);if(this._processingStatus!==rt.Stopped)(this.observers??=new Set).add(r);else switch(this._snapshot.status){case"done":nt(r.complete);break;case"error":{const t=this._snapshot.error;r.error?nt(r.error,t):n(t);break}}return{unsubscribe:()=>{this.observers?.delete(r)}}}on(t,e){let s=this.eventListeners?.get(t);return s||(s=new Set,(this.eventListeners??=new Map).set(t,s)),s.add(e),{unsubscribe:()=>{s.delete(e)}}}select(t,e=Object.is){return{subscribe:(s,r,i)=>{const n=z(s,r,i);let o=t(this.getSnapshot());return this.subscribe({next:s=>{const r=t(s);e(o,r)||(o=r,n.next?.(r))},error:n.error,complete:n.complete})},get:()=>t(this.getSnapshot())}}start(){if(this._processingStatus===rt.Running)return this;if(this._processingStatus===rt.Stopped)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:"xstate.snapshot.actor",actorId:this.id,sessionId:this.sessionId,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.registryKey&&this.system._set(this.registryKey,this),this._processingStatus=rt.Running,this._lastSourceRef=this._parent;switch(this._snapshot.status){case"done":return this._restored?(this._next(this._snapshot),this._stopProcedure(),this._complete(),this):this._flushInitialEffects()?(this.update(this._snapshot,i(this.options.input)),this):this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),!this._flushInitialEffects())return this;if(this.logic.start)try{this.logic.start(this._snapshot,this._actorScope,{restored:this._restored})}catch(t){return this._setErrorSnapshot(t),this._error(t),this}if(this._restored||this._deferred?.length||this.observers?.size||(this.system._hasInspectionObservers?.()??1)?this.update(this._snapshot,i(this.options.input)):(function(t,e,s){const r=y.get(t),i=x(s);return r?.actor===e&&r.systemState.sourceSystem===s&&r.systemState.sourceVersion+1===i&&s._getRootActor?.()===e&&!s._peekChildren?.()&&(r.systemState.root=e,r.systemState.sourceVersion=i,!0)}(this._snapshot,this,this.system)||this._setSnapshot(this._snapshot),this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0),this._restored){const t=this._snapshot.timers??{},e=!this.system._clock.now,s=Date.now();for(const r of Object.values(t)){const t=e&&void 0!==r.startedAt?Math.min(r.delay,Math.max(0,r.startedAt+r.delay-s)):r.delay;this.system.scheduleTimer(this,r.id,t)}this._restored=!1}return this._mailboxStarted=!0,this.mailbox?.start(),this}_process(t){let e,r;try{e=d(this._actorScope,this._snapshot,this.logic.transition(this._snapshot,t,this._actorScope))}catch(t){r={err:t}}if(r)return this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0,void(this._recoverOrError(r.err)||this._inspectTransition(this._snapshot,t));if(!e)return;let i=this._snapshot;try{const[s,r]=e;i=s,this._setSnapshot(i),ot(r,this._actorScope),this.update(i,t)}catch(e){return void(this._recoverOrError(e,i)||this._inspectTransition(this._snapshot,t))}t.type===s&&(this._stopProcedure(),this._complete())}_emit(t){for(const e of[this.eventListeners?.get(t.type),this.eventListeners?.get("*")])if(e)for(const s of e)nt(s,t)}_terminate(t){if("done"===t.status)return this._next(this._snapshot),this._stopProcedure(),this._complete(),void(this._parent&&this.system._relay(this,this._parent,(e=this.id,s=t.output,r=this.sessionId,{type:"xstate.done.actor",output:s,actorId:e,sessionId:r})));var e,s,r;"error"===t.status&&this._error(t.error)}_stop(){return this._processingStatus===rt.Stopped?this:(this.mailbox?.clear(),this._processingStatus===rt.NotStarted?(this._processingStatus=rt.Stopped,this.system._unregister(this),this):(this._send({type:s}),this.system._unregister(this),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??st)nt(t.complete);this.observers?.clear(),this.eventListeners?.clear()}_error(t){if(this._stopProcedure(),this.observers?.size){let e=!1;for(const s of this.observers){const r=s.error;e||=!r,nt(r,t)}this.observers.clear(),e&&n(t)}else this._parent||n(t);var e,s,r;this.eventListeners?.clear(),this._parent&&this.system._relay(this,this._parent,(e=this.id,s=t,r=this.sessionId,{type:"xstate.error.actor",error:s,actorId:e,sessionId:r}))}_stopProcedure(){this._processingStatus===rt.Running&&(this.system.cancelAllTimers(this),this.mailbox?.clear(),this.mailbox=void 0,this._boundProcess=void 0,this._mailboxStarted=!1,this._processingStatus=rt.Stopped,this.system._unregister(this))}_send(t){if(this._processingStatus===rt.Stopped)return void this.system.deadLetter(this._lastSourceRef,this,t,"stopped");let s=this.mailbox;s||(this._boundProcess??=this._process.bind(this),s=this.mailbox=new e(this._boundProcess),this._mailboxStarted&&s.start()),s.enqueue(t)}_sendPublic(t){this.system._relay(void 0,this,t)}toJSON(){return{xstate$type:"actorRef",id:this.id,address:this.address,src:"string"==typeof this.src?this.src:void 0}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[o](){return this}getSnapshot(){return this._snapshot}}function ht(t,e,s){if(!_())return;const r=t.self;r?.system._hasInspectionObservers?.()&&r._inspectTransition(e,s)}Object.freeze({status:"active",output:void 0,error:void 0});class dt extends Error{constructor(){super("Durable execution was stopped"),this.name="DurableExecutionCancelledError"}}class ut extends Error{constructor(){super("run() can only start a fresh durable execution; resume checkpoints with transition() and the persisted snapshot"),this.name="DurableExecutionResumeError"}}t.DurableExecutionCancelledError=dt,t.DurableExecutionResumeError=ut,t.createDurable=function(t,e,s){let r=e.transitionIndex??0;const n=r;let o=0===r?void 0:r-1;if(!Number.isSafeInteger(r)||r<0)throw new RangeError("transitionIndex must be a non-negative safe integer");const c=t=>{const e=r++;return o=e,t.map((t,s)=>{const r={id:`${e}:${s}`,transitionIndex:e,effectIndex:s,effect:t};let i;return Object.defineProperty(r,"descriptor",{enumerable:!0,get:()=>i??=function(t){if("action"===t.kind)return{kind:"action",type:t.type,params:t.params};if("emit"===t.kind)return{kind:"emit",type:t.type,source:t.source.address,event:t.event};switch(t.type){case"@xstate.spawn":return{kind:"builtin",type:"@xstate.spawn",source:t.source?.address,actor:t.actor.address,id:t.id,src:H(t.src),input:t.input};case"@xstate.start":return{kind:"builtin",type:"@xstate.start",source:t.source?.address,actor:t.actor.address,id:t.id};case"@xstate.raise":return{kind:"builtin",type:"@xstate.raise",source:t.source.address,event:t.event,id:t.id,delay:t.delay};case"@xstate.sendTo":return{kind:"builtin",type:"@xstate.sendTo",source:t.source.address,target:t.target.address,incarnation:(e=t.target,!0===e._remote?t.target.sessionId:void 0),event:t.event,id:t.id,delay:t.delay};case"@xstate.cancel":return{kind:"builtin",type:"@xstate.cancel",source:t.source.address,id:t.id};case"@xstate.stop":return{kind:"builtin",type:"@xstate.stop",source:t.source.address,actor:t.actor.address,id:t.id};case"@xstate.terminate":return{kind:"builtin",type:"@xstate.terminate",source:t.source.address,actor:t.actor.address,id:t.id,status:t.status,output:t.output,error:t.error}}var e}(t)}),r})},a=J(function(t){const e=H(t);return"x"===e?"x:0":e}(t)),h=t.id??a,u=t.version,l=e.executionId?{systemId:e.executionId,nextSessionId:0}:void 0,p={};for(const t of[...Q,"sendEvent","runStep","runLogic"]){const s=e[t];s&&(p[t]=s.bind(e))}const _=Object.keys(p).length>0||!!e.enqueueRootEvent;let f;const y=[];let m=Promise.resolve();const g=t=>f?((t,e)=>{const s=m.then(e).then(()=>{});return m=s.catch(()=>{}),((t,e)=>(t.pending.add(e),e.catch(e=>{t.failures.push(e)}).then(()=>t.pending.delete(e)),e))(t,s)})(f,t):t(),b=async t=>{for(;t.pending.size;)await Promise.allSettled([...t.pending])};function S(t,s,r=!1){const i={};for(const e of Q){const s=t[e]??(r?(...t)=>("spawnActor"===e?t[1]:t[0]).system[e](...t):void 0);s&&(i[e]=(...t)=>g(()=>s(...t)))}return t.runStep&&(i.runStep=t.runStep),t.runLogic&&(i.runLogic=t.runLogic),i.sendEvent=(r,i,n)=>{if(!f&&i.address===a&&!t.sendEvent&&e.enqueueRootEvent)return g(()=>e.enqueueRootEvent(r,n));if(!f&&i.address===a&&!t.sendEvent)throw new Error(`A root-addressed event ("${n.type}") was produced while the durable loop was parked, but the adapter has no enqueueRootEvent or sendEvent to receive it. Implement enqueueRootEvent to place root-addressed events in the host's mailbox.`);if(f&&i.address===a)return void f.rootEvents.push({event:n,source:r});const o=t.sendEvent;if(o)return g(()=>o(r,i,n));if(s)return g(()=>C(r,i,n));throw new TypeError(`The durable runtime does not support the sendEvent operation (target '${i.address}')`)},i}const x=_?S(p,!0):void 0;function w(t,e){if(t.address===e)return t;if(!e.startsWith(`${t.address}/`))return;const s=t.getSnapshot().children;for(const t of Object.values(s??{})){const s=t&&w(t,e);if(s)return s}}const E=s?.inspect;function T(t){E&&!t._hasInspectionObservers?.()&&t.inspect(E)}function R(t){if(x||E){const e=A(t)?.actor;e&&(x&&(e.system.runtime=x),T(e.system));const s=t.children;if(s)for(const t of Object.values(s))t&&t.system!==e?.system&&(x&&(t.system.runtime=x),T(t.system))}return t}const O={rootAddress:a,machineId:h,machineVersion:u,get nextTransitionIndex(){return r},initialTransition(...e){const[s,r]=v(E,()=>F(l,()=>function(t,...[e]){const s=$(t),[r,n]=d(s,void 0,t.initialTransition(e,s));I(s,r,!1);const o=k(s,r);return ht(s,o,i(e)),[o,n]}(t,...e)));return[R(s),c(r)]},transition(e,s){const[r,i]=v(E,()=>F(l,()=>function(t,e,s){const r=$(t,e);I(r,e,!1);const[i,n]=d(r,e,t.transition(e,s,r));I(r,i,!1);const o=i===e?i:k(r,i);return ht(r,o,s),[o,n]}(t,e,s)));return[R(r),c(i)]},getActorRef(t,e){const s=A(t)?.actor;return s&&void 0!==e?w(s,e):s},async executeEffects(t){if(f)throw new Error("executeEffects calls must not overlap: await the previous call before starting the next batch.");const s={rootEvents:[],failures:[],pending:new Set};f=s;const r=_?void 0:{};try{for(const s of t){const{effect:t}=s,i={id:s.id,transitionIndex:s.transitionIndex,effectIndex:s.effectIndex},n=e.runtime?.(i,t),o=n?S({...p,...n},_,!0):r;"action"===t.kind?await e.executeAction(t,i,o??x??{}):await t.exec(o)}await(async t=>{if(await b(t),t.failures.length)throw t.failures[0]})(s),y.push(...s.rootEvents)}catch(t){throw await b(s),t}finally{f=void 0}},async waitForEvent(){if(void 0===o)throw new Error("Cannot wait for an event before the first transition");const t=y.shift();return t?t.event:e.waitForEvent({id:`event:${o}`,transitionIndex:o})},async run(...t){if(0!==n||r!==n)throw new ut;let[e,s]=O.initialTransition(...t);for(await O.executeEffects(s);"active"===e.status;){const t=await O.waitForEvent();[e,s]=O.transition(e,t),await O.executeEffects(s)}const i=e;if("done"===i.status)return i.output;if("error"===i.status)throw i.error;throw new dt}};return O},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).XStateDurable={})}(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="@xstate.stop",r="@xstate.terminate";function i(t){return{type:"@xstate.init",input:t}}function n(t){setTimeout(()=>{throw t})}const o="function"==typeof Symbol&&Symbol.observable||"@@observable",c=Symbol();function a(t=this.actor.system){const e="done"===this.status?{status:"done",output:this.output,error:void 0}:{status:"error",output:void 0,error:this.error};return t.terminateActor(this.actor,e)}function h(t,e){if("done"!==e.status&&"error"!==e.status)throw new Error("Cannot terminate an active or stopped actor");const s="done"===e.status?{status:"done",output:e.output,error:void 0}:{status:"error",output:void 0,error:e.error},i=[t.self,s];return{kind:"builtin",exec:a,type:r,source:t.self,actor:t.self,id:t.self.id,...s,params:void 0,args:i}}function d(t,e,[s,i]){const n="done"===s.status||"error"===s.status,o="done"===e?.status||"error"===e?.status,c=i.some(t=>"object"==typeof t&&null!==t&&"type"in t&&t.type===r);return!n||o||c?[s,i]:[s,[...i,h(t,s)]]}const u=()=>{},l=()=>({});let p;function _(){return void 0!==p}function f(){return p}function v(t,e){if(!t)return e();const s=p;p=t;try{return e()}finally{p=s}}!function(t,e){const s=(t,...s)=>{e(t,...s)};Object.assign(s,{cancel:u,emit:u,log:u,raise:u,spawn:l,sendTo:u,stop:u,listen:l,subscribeTo:l,...t})}({},u);const y=new WeakMap,m=new WeakMap,g=new Map;function b(t){const e=t._peekKeyedActors;return e?e.call(t)??g:t.keyedActors}function S(t){t._snapshotVersion++}function x(t){return t._snapshotVersion}function w(t,e,s){const r=_(),i=function(t,e){if(e){const t=new Map(e.children);return e.root?.sessionId&&t.set(e.root.sessionId,e.root),t}return new Map(t.children)}(t,s),n=new Map(s?.keyedActors??b(t)),o=new WeakMap,c=Object.assign(Object.create(t),{children:i,keyedActors:n,reverseKeyedActors:o,_snapshot:{...s?.snapshot??t._snapshot},_snapshotVersion:s?.sourceVersion??t._snapshotVersion,_register:(t,e)=>(i.set(t,e),S(c),t),_unregister:t=>{t.sessionId&&i.delete(t.sessionId);const e=o.get(t);void 0!==e&&n.get(e)===t&&n.delete(e),o.delete(t),S(c)},_set:(t,e)=>{const s=n.get(t);if(s&&s!==e)throw new Error(`Actor with registry key '${String(t)}' already exists.`);n.set(t,e),o.set(e,t),S(c)},get:t=>n.get(t),getAll:()=>Object.fromEntries(n),_hasInspectionObservers:r?()=>t._hasInspectionObservers?.()??!1:()=>!1,_sendInspectionEvent:r?e=>t._sendInspectionEvent(e):()=>{}});for(const[t,e]of n)e&&o.set(e,t);for(const t of Object.values(e)){if(!t)continue;t.sessionId&&i.set(t.sessionId,t);const e=t.registryKey;e&&(n.set(e,t),o.set(t,e))}return c}function E(t){const e=y.get(t);if(e)return e;const s=m.get(t);if(!s)return;const r=s();return y.set(t,r),m.delete(t),r}function A(t,e,s=e.system,r){m.delete(t);const i=x(s),n=r?E(r):void 0,o=n?.actor===e?n:E(t);if(o?.actor===e&&o.systemState.sourceSystem===s&&o.systemState.sourceVersion===i)return void y.set(t,o);const c=function(t){const e=t._getRootActor?.(),s=t._peekChildren?.();if(!e&&!s){if(t._getRootActor&&t._peekChildren)return{};const e=new Map(t.children);return e.size?{children:e}:{}}return s?.size?{children:new Map(t.children)}:e?{root:e}:{}}(s);let a=c.children;const h=b(s);let d=h.size?new Map(h):g;for(const e of Object.values(function(t){return"children"in t&&t.children?t.children:{}}(t))){if(!e)continue;e.sessionId&&(a??=new Map).set(e.sessionId,e);const t=e.registryKey;t&&(d===g&&(d=new Map),d.set(t,e))}y.set(t,{actor:e,systemState:{...c,children:a,keyedActors:d,snapshot:{...s._snapshot},sourceSystem:s,sourceVersion:i}})}function I(t,e,s=!0){const r=O(t);return r?(r.snapshot=e,r.materialized&&(r.materialized.self._snapshot=e)):t.self._snapshot=e,s&&e&&"object"==typeof e&&A(e,t.self),e}function k(t,e){I(t,e,!1);const s=O(t);T.set(e,t);const r=()=>(A(e,t.self,t.system),E(e));return s&&(s.materialized?s.identityProvider=r:s.identityProvider??=r),function(t,e){y.delete(t),m.set(t,e)}(e,r),e}const R=Symbol(),T=new WeakMap;function O(t){return t[R]}function j(t){return O(t).materialize()}const M={[c]:!0,get _parent(){const t=O(this);return t.parentKnown?t.parent:j(this).self._parent},get self(){return j(this).self},get defer(){return j(this).defer},get id(){return j(this).id},get logger(){return j(this).logger},get sessionId(){return j(this).sessionId},get stopChild(){return j(this).stopChild},get system(){return j(this).system},get emit(){return j(this).emit},get actionExecutor(){return j(this).actionExecutor}};function P(t,e,s,r,i){const n=e?.(),o=i??n?.actor,c=o?.system,a=o&&c?w(c,s,i?void 0:n?.systemState):void 0,h=(d=t,u={_inert:!0,...o?{id:o.id,_sessionId:o.sessionId}:{},...a?{_systemRef:{current:a}}:{}},new at(d,u));var d,u;return o?._parent&&(h._parent=o._parent,h._address=void 0),r&&(h._snapshot=r),Object.create(h._actorScope,{defer:{value:()=>{}},stopChild:{value:t=>t._stop()},actionExecutor:{value:()=>{}}})}function $(t,e,s,r){const i=r??(e&&"object"==typeof e?T.get(e):void 0),n=i?O(i):void 0,o=e&&"object"==typeof e?function(t){return y.get(t)}(e):void 0,c=n?.identityProvider??(e&&"object"==typeof e?function(t){const e=y.get(t);return e?()=>e:m.get(t)}(e):void 0),a={snapshot:e,sourceRef:c,sourceChildren:(h=e,h&&"object"==typeof h&&"machine"in h&&"value"in h?e.children:{}),identityProvider:n?.identityProvider??c,parent:s?._parent??n?.parent??o?.actor._parent,parentKnown:!(!s&&!n?.parentKnown&&!o&&e)};var h;if(a.materialize=()=>a.materialized??=P(t,a.sourceRef,a.sourceChildren,a.snapshot,s),!a.identityProvider&&!e){const e={},r=a.sourceRef,i=a.sourceChildren;let n,o;const c=()=>n??=P(t,r,i,void 0,s);a.materialize=()=>{const t=c();return void 0!==a.snapshot&&(t.self._snapshot=a.snapshot),a.materialized=t},a.identityProvider=()=>{if(o)return o;const t=c();return A(e,t.self,t.system),o=E(e)}}const d=Object.create(M);return d[R]=a,d}function z(t,e,s){const r="object"==typeof t,i=r?t:void 0;return{next:(r?t.next:t)?.bind(i),error:(r?t.error:e)?.bind(i),complete:(r?t.complete:s)?.bind(i)}}async function L(t,e,s,r=e=>{t.system.sendEvent(t,t,e)}){const i=function(t,e){return t.getSnapshot().effects?.[e]}(t,e);if("done"===i?.status)return i.output;if("error"===i?.status)throw i.error;if("active"===i?.status)return function(t,e){return new Promise((s,r)=>{const i=t.subscribe(t=>{const n=t.effects?.[e];"done"===n?.status?(i.unsubscribe(),s(n.output)):"error"===n?.status&&(i.unsubscribe(),r(n.error))})})}(t,e);r({type:"xstate.logic.effect.start",key:e});try{const t=await s();return r({type:"xstate.logic.effect.resolve",key:e,output:t}),t}catch(t){throw r({type:"xstate.logic.effect.reject",key:e,error:t}),t}}function K(t,e,s){if(C(t,e,s))return;const r=e;r._lastSourceRef=t,r._send(s)}function C(t,e,s){const r=function(t,e,s){const r=e;if(t!==e&&r.logic?.isInternalEventType?.(s.type))return new Error(`Internal event "${s.type}" cannot be sent to actor "${e.id}" from outside.`)}(t,e,s);return!!r&&(e.system.deadLetter(t,e,s,"internalEvent",{error:r}),!0)}let D;Object.freeze({});let V=0;const N=new Error("Transition effect"),q=[];function W(){return D??=function(){let t;try{t=globalThis.crypto}catch{}if(t?.getRandomValues)try{const e=new Uint32Array(4);return t.getRandomValues(e),Array.from(e,t=>t.toString(36).padStart(7,"0")).join("")}catch{}if(t?.randomUUID)try{return t.randomUUID().replaceAll("-","")}catch{}return`xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`}(),`${D}:${(V++).toString(36)}`}let U;function F(t,e){if(!t)return e();const s=U;U=t;try{return e()}finally{U=s}}function H(t){if("string"==typeof t)return t;const e=t?.id;return"string"==typeof e&&e.length&&"(machine)"!==e?e:"x"}function J(t){return t.includes("/")||t.includes("%")?t.replaceAll("%","%25").replaceAll("/","%2F"):t}function X(t,e){return`${t?t.address:""}|${e}`}function B(t,e,s){const r=t._snapshot._nextActorIds;(r[e]??0)>=s||(t._snapshot._nextActorIds={...r,[e]:s},S(t))}function G(t,e,s){if(void 0!==e){const r=function(t){const e=t.lastIndexOf(":");if(e<=0||e===t.length-1)return;const s=Number(t.slice(e+1));return Number.isSafeInteger(s)&&s>=0?{prefix:t.slice(0,e),index:s}:void 0}(e);return r?B(t,X(s?.parent,r.prefix),r.index+1):s?.parent||B(t,X(void 0,e),1),e}const r=H(s?.src),i=X(s?.parent,r),n=t._snapshot._nextActorIds[i]??0;return B(t,i,n+1),s?.parent||"x"===r||0!==n?`${r}:${n}`:r}const Q=["spawnActor","startActor","stopActor","terminateActor","emitEvent","scheduleTimer","cancelTimer","cancelAllTimers","deadLetter"],Y=Object.freeze({});function Z(t,e){return`${t.sessionId}.${e}`}class tt{get children(){const t=this._children??=new Map;return this._getRootActor()&&t.set(this._rootActor.sessionId,this._rootActor),t}set children(t){this._children=t}_getRootActor(){return this._rootActor._isRunning()?this._rootActor:void 0}_peekChildren(){return this._children}get keyedActors(){return this._keyedActors??=new Map}set keyedActors(t){this._keyedActors=t}get reverseKeyedActors(){return this._reverseKeyedActors??=new WeakMap}set reverseKeyedActors(t){this._reverseKeyedActors=t}_peekKeyedActors(){return this._keyedActors}constructor(t,e){this._rootActor=t,this._identity=U??{systemId:W(),nextSessionId:0},this._snapshot=void 0,this._snapshotVersion=0,this.scheduler=this,this._clock=void 0,this._logger=void 0,this.createActorRef=void 0,this._onRejectedEvent=e.onRejectedEvent;const s="object"==typeof e.snapshot&&null!==e.snapshot?e.snapshot:void 0;this._clock=e.clock,this._logger=e.logger,this.createActorRef=e.createActorRef;const r=f();r&&this.inspect(r),this._snapshot={_scheduledTimers:s?.scheduler??Y,_nextActorIds:{}}}_recordSent(t,e,s,r,i){if(!this._inspectionObservers?.size||!t)return;const n=t;(n._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:s,delay:r,id:i})}schedule(t,e,s){const r=Z(t,e);void 0!==this._timerMap?.[r]&&this.cancel(t,e);const i=t.getSnapshot()?.timers?.[e];if(i){const r="self"===i.target?t:i.target;this._recordSent(t,r,i.event,s,e)}const n=this._clock.now?.()??Date.now(),o={source:t,delay:s,id:e,scheduledAt:n,dueAt:n+s},c=Z(t,e);this._snapshot._scheduledTimers===Y&&(this._snapshot._scheduledTimers={}),this._snapshot._scheduledTimers[c]=o,S(this);const a=this._clock.setTimeout(()=>{this._timerMap&&delete this._timerMap[c],delete this._snapshot._scheduledTimers[c],S(this),this._deliver(t,t,{type:"xstate.timer",id:e})},s);(this._timerMap??={})[c]=a}cancel(t,e){const s=Z(t,e),r=this._timerMap?.[s];this._timerMap&&delete this._timerMap[s],this._snapshot._scheduledTimers!==Y&&delete this._snapshot._scheduledTimers[s],S(this),void 0!==r&&this._clock.clearTimeout(r)}cancelAll(t){for(const e in this._snapshot._scheduledTimers){const s=this._snapshot._scheduledTimers[e];s.source===t&&this.cancel(t,s.id)}}_deliver(t,e,s){K(t,e,s)}_register(t,e){return e===this._rootActor?this._children?.set(t,e):(this._children??=new Map).set(t,e),S(this),t}_unregister(t){let e;t===this._rootActor?(e=void 0!==this._getRootActor(),e=void 0!==t.sessionId&&(this._children?.delete(t.sessionId)??!1)||e):e=void 0!==t.sessionId&&(this._children?.delete(t.sessionId)??!1);const s=this._reverseKeyedActors?.get(t);void 0!==s&&(this._keyedActors?.get(s)===t&&(this._keyedActors.delete(s),e=!0),this._reverseKeyedActors?.delete(t)),e&&S(this)}get(t){return this._keyedActors?.get(t)}getAll(){return Object.fromEntries(this._keyedActors?.entries()??[])}_set(t,e){const s=this._keyedActors?.get(t);if(s&&s!==e)throw new Error(`Actor with registry key '${t}' already exists.`);(this._keyedActors??=new Map).set(t,e),(this._reverseKeyedActors??=new WeakMap).set(e,t),s!==e&&S(this)}inspect(t){const e=z(t);return(this._inspectionObservers??=new Set).add(e),{unsubscribe:()=>{this._inspectionObservers?.delete(e)}}}_hasInspectionObservers(){return!!this._inspectionObservers?.size}_sendInspectionEvent(t){if(!this._inspectionObservers?.size)return;const e={...t,rootId:this._rootActor.sessionId};this._inspectionObservers.forEach(t=>t.next?.(e))}spawnActor(t,e){return this.runtime?.spawnActor?.(t,e)}startActor(t){const e=this.runtime?.startActor;if(e)return e(t);t.start()}stopActor(t){const e=this.runtime?.stopActor;if(e)return e(t);!function(t){t._stop()}(t)}terminateActor(t,e){const s=this.runtime?.terminateActor;if(s)return s(t,e);!function(t,e){t._terminate(e)}(t,e)}sendEvent(t,e,s){if(C(t,e,s))return;this._recordSent(t,e,s);const r=this.runtime?.sendEvent;if(r)return r(t,e,s);this._deliver(t,e,s)}emitEvent(t,e){const s=this.runtime?.emitEvent;if(s)return s(t,e);t._emit(e)}deadLetter(t,e,s,r,i){this._sendInspectionEvent({type:"@xstate.deadletter",actorRef:e,sourceRef:t,event:s,reason:r,issues:i?.issues,error:i?.error}),this._onRejectedEvent?.({event:s,targetRef:e,targetId:e.id,sourceRef:t,eventOrigin:t?"actor":"external",reason:r,issues:i?.issues,error:i?.error});const n=this.runtime?.deadLetter;if(n)return n(t,e,s,r,i)}runStep(t,e,s){const r=this.runtime?.runStep;return r?r(t,e,s):L(t,e,s)}runLogic(t,e){const s=this.runtime?.runLogic;return s?s(t,e):e()}scheduleTimer(t,e,s){const r=this.runtime?.scheduleTimer;if(r){const i=t.getSnapshot()?.timers?.[e];if(i){const r="self"===i.target?t:i.target;this._recordSent(t,r,i.event,s,e)}return r(t,e,s)}this.schedule(t,e,s)}cancelTimer(t,e){const s=this.runtime?.cancelTimer;if(s)return s(t,e);this.cancel(t,e)}cancelAllTimers(t){const e=this.runtime?.cancelAllTimers;if(e)return e(t);this.cancelAll(t)}_relay(t,e,s){if(q.length&&q.includes(e))throw N;return this.sendEvent(t,e,s)}getSnapshot(){return{_scheduledTimers:{...this._snapshot._scheduledTimers}}}start(){const t=this._snapshot._scheduledTimers;let e=!0;for(const s in t){e&&(this._snapshot._scheduledTimers={},e=!1);const{source:r,dueAt:i,id:n}=t[s];this.scheduleTimer(r,n,Math.max(0,i-(this._clock.now?.()??Date.now())))}}}let et=!1;const st=Object.freeze([]);let rt=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const it=Object.freeze({clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)});function nt(t,e){try{t?.(e)}catch(t){n(t)}}function ot(t,e){if(t?.length)for(const s of t)e.actionExecutor(s)}function ct(t,e){return new at(t,e)}class at{get address(){return this._address??=this._parent?`${this._parent.address}/${J(this.id)}`:J(this.id)}get trigger(){return this._trigger??=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}})}get send(){return this._boundSend??=this._sendPublic.bind(this)}constructor(t,e){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this._boundProcess=void 0,this.mailbox=void 0,this._mailboxStarted=!1,this.observers=void 0,this.eventListeners=void 0,this.logger=void 0,this._processingStatus=rt.NotStarted,this._forceDeferredActions=!1,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._lastSourceRef=void 0,this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0,this._initialEffects=void 0,this.registryKey=void 0,this.sessionId=void 0,this._address=void 0,this.system=void 0,this._trigger=void 0,this._boundSend=void 0,this.src=void 0,this._deferred=void 0,this._restored=!1;const s=e?{...it,...e}:it,{clock:r,logger:i,parent:n,syncSnapshot:o,id:c,registryKey:a,inspect:h}=s;var u;this.system=n?n.system:s._systemRef?.current??function(t,e){return new tt(t,e)}(this,{clock:r,logger:i,snapshot:s.snapshot??s.state,createActorRef:ct,onRejectedEvent:s.onRejectedEvent}),n||!s._systemRef||s._systemRef.current||(s._systemRef.current=this.system),h&&!n&&this.system.inspect(z(h)),this.sessionId=s._sessionId??`${(u=this.system)._identity.systemId}:${u._identity.nextSessionId++}`,this.id=G(this.system,c,{parent:n,src:s.src??t}),this.logger=e?.logger??this.system._logger,this.clock=e?.clock??this.system._clock,this._parent=n,this._syncSnapshot=o,this.options=s,this.src=s.src??t,this.ref=this,this._actorScope=this,a&&(this.registryKey=a,this.system._set(a,this)),this._collectedMicrosteps=void 0;const l=e?.snapshot??e?.state;this._restored=void 0!==l;try{if(l)this._setSnapshot(this.logic.restoreSnapshot?this.logic.restoreSnapshot(l,this._actorScope):l);else if(e?._inert);else{const[t,e]=d(this._actorScope,void 0,this.logic.initialTransition(this.options?.input,this._actorScope));this._setSnapshot(t),this._initialEffects=e.length?e:void 0}}catch(t){this._setSnapshot({status:"error",output:void 0,error:t}),this._deferred&&(this._deferred.length=0)}a&&"active"!==this._snapshot.status&&this.system._unregister(this),(this.system._hasInspectionObservers?.()??1)&&this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_getLogger(){return this.logger}get self(){return this}get defer(){const t=t=>this._defer(t);return Object.defineProperty(this,"defer",{value:t}),t}get stopChild(){const t=t=>this._stopChild(t);return Object.defineProperty(this,"stopChild",{value:t}),t}get emit(){const t=t=>this.system.emitEvent(this,t);return Object.defineProperty(this,"emit",{value:t}),t}get actionExecutor(){const t=t=>this._executeAction(t);return Object.defineProperty(this,"actionExecutor",{value:t}),t}_isRunning(){return this._processingStatus===rt.Running}_defer(t){(this._deferred??=[]).push(t)}_stopChild(t){if(t._parent!==this)throw new Error(`Cannot stop non-child actor ${t.id}`);t._stop()}_executeAction(t){const e=()=>{(this.system._hasInspectionObservers?.()??1)&&(this._collectedActions??=[]).push({type:t.type,params:t.params});const e=et;try{et=!0,t.exec()}finally{et=e}};this._processingStatus!==rt.Running||this._forceDeferredActions?(this._deferred??=[]).push(e):e()}_setSnapshot(t){const e=this._snapshot;this._snapshot=t,A(t,this,this.system,e)}_setErrorSnapshot(t,e=this._snapshot){this._setSnapshot({...e,status:"error",error:t})}_recoverOrError(t,e){return!!this._tryHandleExecutionError(t,e)||(this._setErrorSnapshot(t),this._error(t),!1)}_tryHandleExecutionError(t,e=this._snapshot){const s=this.logic.getExecutionErrorEvent?.(e,t);if(!s)return!1;try{const[t,r]=d(this._actorScope,e,this.logic.transition(e,s,this._actorScope));return this._setSnapshot(t),ot(r,this._actorScope),this.update(t,s),!0}catch{return!1}}_next(t){for(const e of this.observers??st)nt(e.next,t)}update(t,e){this._setSnapshot(t);const s=this._deferred;for(let e=0;e<(s?.length??0);e++){const r=s[e];try{r()}catch(e){if(s.length=0,this._tryHandleExecutionError(e,t))return;this._setErrorSnapshot(e,t),this._error(e);break}}if(s&&(s.length=0),"active"===this._snapshot.status)this._next(t);this._inspectTransition(this._snapshot,e)}_inspectTransition(t,e){(this.system._hasInspectionObservers?.()??1)&&this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps??st,actions:this._collectedActions??st,sent:this._collectedSent??st,eventType:e.type}),this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0}_flushInitialEffects(){if(!this._initialEffects?.length)return!0;this._forceDeferredActions=!0;try{return ot(this._initialEffects,this._actorScope),this._initialEffects=void 0,!0}catch(t){return this._initialEffects=void 0,this._deferred&&(this._deferred.length=0),this._recoverOrError(t),"active"===this._snapshot.status}finally{this._forceDeferredActions=!1}}subscribe(t,e,s){const r=z(t,e,s);if(this._processingStatus!==rt.Stopped)(this.observers??=new Set).add(r);else switch(this._snapshot.status){case"done":nt(r.complete);break;case"error":{const t=this._snapshot.error;r.error?nt(r.error,t):n(t);break}}return{unsubscribe:()=>{this.observers?.delete(r)}}}on(t,e){let s=this.eventListeners?.get(t);return s||(s=new Set,(this.eventListeners??=new Map).set(t,s)),s.add(e),{unsubscribe:()=>{s.delete(e)}}}select(t,e=Object.is){return{subscribe:(s,r,i)=>{const n=z(s,r,i);let o=t(this.getSnapshot());return this.subscribe({next:s=>{const r=t(s);e(o,r)||(o=r,n.next?.(r))},error:n.error,complete:n.complete})},get:()=>t(this.getSnapshot())}}start(){if(this._processingStatus===rt.Running)return this;if(this._processingStatus===rt.Stopped)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:"xstate.snapshot.actor",actorId:this.id,sessionId:this.sessionId,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.registryKey&&this.system._set(this.registryKey,this),this._processingStatus=rt.Running,this._lastSourceRef=this._parent;switch(this._snapshot.status){case"done":return this._restored?(this._next(this._snapshot),this._stopProcedure(),this._complete(),this):this._flushInitialEffects()?(this.update(this._snapshot,i(this.options.input)),this):this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),!this._flushInitialEffects())return this;if(this.logic.start)try{this.logic.start(this._snapshot,this._actorScope,{restored:this._restored})}catch(t){return this._setErrorSnapshot(t),this._error(t),this}if(this._restored||this._deferred?.length||this.observers?.size||(this.system._hasInspectionObservers?.()??1)?this.update(this._snapshot,i(this.options.input)):(function(t,e,s){const r=y.get(t),i=x(s);return r?.actor===e&&r.systemState.sourceSystem===s&&r.systemState.sourceVersion+1===i&&s._getRootActor?.()===e&&!s._peekChildren?.()&&(r.systemState.root=e,r.systemState.sourceVersion=i,!0)}(this._snapshot,this,this.system)||this._setSnapshot(this._snapshot),this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0),this._restored){const t=this._snapshot.timers??{},e=!this.system._clock.now,s=Date.now();for(const r of Object.values(t)){const t=e&&void 0!==r.startedAt?Math.min(r.delay,Math.max(0,r.startedAt+r.delay-s)):r.delay;this.system.scheduleTimer(this,r.id,t)}this._restored=!1}return this._mailboxStarted=!0,this.mailbox?.start(),this}_process(t){let e,r;try{e=d(this._actorScope,this._snapshot,this.logic.transition(this._snapshot,t,this._actorScope))}catch(t){r={err:t}}if(r)return this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0,void(this._recoverOrError(r.err)||this._inspectTransition(this._snapshot,t));if(!e)return;let i=this._snapshot;try{const[s,r]=e;i=s,this._setSnapshot(i),ot(r,this._actorScope),this.update(i,t)}catch(e){return void(this._recoverOrError(e,i)||this._inspectTransition(this._snapshot,t))}t.type===s&&(this._stopProcedure(),this._complete())}_emit(t){for(const e of[this.eventListeners?.get(t.type),this.eventListeners?.get("*")])if(e)for(const s of e)nt(s,t)}_terminate(t){if("done"===t.status)return this._next(this._snapshot),this._stopProcedure(),this._complete(),void(this._parent&&this.system._relay(this,this._parent,(e=this.id,s=t.output,r=this.sessionId,{type:"xstate.done.actor",output:s,actorId:e,sessionId:r})));var e,s,r;"error"===t.status&&this._error(t.error)}_stop(){return this._processingStatus===rt.Stopped?this:(this.mailbox?.clear(),this._processingStatus===rt.NotStarted?(this._processingStatus=rt.Stopped,this.system._unregister(this),this):(this._send({type:s}),this.system._unregister(this),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??st)nt(t.complete);this.observers?.clear(),this.eventListeners?.clear()}_error(t){if(this._stopProcedure(),this.observers?.size){let e=!1;for(const s of this.observers){const r=s.error;e||=!r,nt(r,t)}this.observers.clear(),e&&n(t)}else this._parent||n(t);var e,s,r;this.eventListeners?.clear(),this._parent&&this.system._relay(this,this._parent,(e=this.id,s=t,r=this.sessionId,{type:"xstate.error.actor",error:s,actorId:e,sessionId:r}))}_stopProcedure(){this._processingStatus===rt.Running&&(this.system.cancelAllTimers(this),this.mailbox?.clear(),this.mailbox=void 0,this._boundProcess=void 0,this._mailboxStarted=!1,this._processingStatus=rt.Stopped,this.system._unregister(this))}_send(t){if(this._processingStatus===rt.Stopped)return void this.system.deadLetter(this._lastSourceRef,this,t,"stopped");let s=this.mailbox;s||(this._boundProcess??=this._process.bind(this),s=this.mailbox=new e(this._boundProcess),this._mailboxStarted&&s.start()),s.enqueue(t)}_sendPublic(t){this.system._relay(void 0,this,t)}toJSON(){return{xstate$type:"actorRef",id:this.id,address:this.address,src:"string"==typeof this.src?this.src:void 0}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[o](){return this}getSnapshot(){return this._snapshot}}function ht(t,e,s){if(!_())return;const r=t.self;r?.system._hasInspectionObservers?.()&&r._inspectTransition(e,s)}Object.freeze({status:"active",output:void 0,error:void 0});class dt extends Error{constructor(){super("Durable execution was stopped"),this.name="DurableExecutionCancelledError"}}class ut extends Error{constructor(){super("run() can only start a fresh durable execution; resume checkpoints with transition() and the persisted snapshot"),this.name="DurableExecutionResumeError"}}t.DurableExecutionCancelledError=dt,t.DurableExecutionResumeError=ut,t.createDurable=function(t,e,s){let r=e.transitionIndex??0;const n=r;let o=0===r?void 0:r-1;if(!Number.isSafeInteger(r)||r<0)throw new RangeError("transitionIndex must be a non-negative safe integer");const c=t=>{const e=r++;return o=e,t.map((t,s)=>{const r={id:`${e}:${s}`,transitionIndex:e,effectIndex:s,effect:t};let i;return Object.defineProperty(r,"descriptor",{enumerable:!0,get:()=>i??=function(t){if("action"===t.kind)return{kind:"action",type:t.type,params:t.params};if("emit"===t.kind)return{kind:"emit",type:t.type,source:t.source.address,event:t.event};switch(t.type){case"@xstate.spawn":return{kind:"builtin",type:"@xstate.spawn",source:t.source?.address,actor:t.actor.address,id:t.id,src:H(t.src),input:t.input};case"@xstate.start":return{kind:"builtin",type:"@xstate.start",source:t.source?.address,actor:t.actor.address,id:t.id};case"@xstate.raise":return{kind:"builtin",type:"@xstate.raise",source:t.source.address,event:t.event,id:t.id,delay:t.delay};case"@xstate.sendTo":return{kind:"builtin",type:"@xstate.sendTo",source:t.source.address,target:t.target.address,incarnation:(e=t.target,!0===e._remote?t.target.sessionId:void 0),event:t.event,id:t.id,delay:t.delay};case"@xstate.cancel":return{kind:"builtin",type:"@xstate.cancel",source:t.source.address,id:t.id};case"@xstate.stop":return{kind:"builtin",type:"@xstate.stop",source:t.source.address,actor:t.actor.address,id:t.id};case"@xstate.terminate":return{kind:"builtin",type:"@xstate.terminate",source:t.source.address,actor:t.actor.address,id:t.id,status:t.status,output:t.output,error:t.error};case"@xstate.deadLetter":return{kind:"builtin",type:"@xstate.deadLetter",source:t.source?.address,target:t.target.address,event:t.event,reason:t.reason,issues:t.detail?.issues,error:t.detail?.error}}var e}(t)}),r})},a=J(function(t){const e=H(t);return"x"===e?"x:0":e}(t)),h=t.id??a,u=t.version,l=e.executionId?{systemId:e.executionId,nextSessionId:0}:void 0,p={};for(const t of[...Q,"sendEvent","runStep","runLogic"]){const s=e[t];s&&(p[t]=s.bind(e))}const _=Object.keys(p).length>0||!!e.enqueueRootEvent;let f;const y=[];let m=Promise.resolve();const g=t=>f?((t,e)=>{const s=m.then(e).then(()=>{});return m=s.catch(()=>{}),((t,e)=>(t.pending.add(e),e.catch(e=>{t.failures.push(e)}).then(()=>t.pending.delete(e)),e))(t,s)})(f,t):t(),b=async t=>{for(;t.pending.size;)await Promise.allSettled([...t.pending])};function S(t,s,r=!1){const i={};for(const e of Q){const s=t[e]??(r?(...t)=>("spawnActor"===e?t[1]:t[0]).system[e](...t):void 0);s&&(i[e]=(...t)=>g(()=>s(...t)))}return t.runStep&&(i.runStep=t.runStep),t.runLogic&&(i.runLogic=t.runLogic),i.sendEvent=(r,i,n)=>{if(!f&&i.address===a&&!t.sendEvent&&e.enqueueRootEvent)return g(()=>e.enqueueRootEvent(r,n));if(!f&&i.address===a&&!t.sendEvent)throw new Error(`A root-addressed event ("${n.type}") was produced while the durable loop was parked, but the adapter has no enqueueRootEvent or sendEvent to receive it. Implement enqueueRootEvent to place root-addressed events in the host's mailbox.`);if(f&&i.address===a)return void f.rootEvents.push({event:n,source:r});const o=t.sendEvent;if(o)return g(()=>o(r,i,n));if(s)return g(()=>K(r,i,n));throw new TypeError(`The durable runtime does not support the sendEvent operation (target '${i.address}')`)},i}const x=_?S(p,!0):void 0;function w(t,e){if(t.address===e)return t;if(!e.startsWith(`${t.address}/`))return;const s=t.getSnapshot().children;for(const t of Object.values(s??{})){const s=t&&w(t,e);if(s)return s}}const A=s?.inspect;function R(t){A&&!t._hasInspectionObservers?.()&&t.inspect(A)}function T(t){if(x||A){const e=E(t)?.actor;e&&(x&&(e.system.runtime=x),R(e.system));const s=t.children;if(s)for(const t of Object.values(s))t&&t.system!==e?.system&&(x&&(t.system.runtime=x),R(t.system))}return t}const O={rootAddress:a,machineId:h,machineVersion:u,get nextTransitionIndex(){return r},initialTransition(...e){const[s,r]=v(A,()=>F(l,()=>function(t,...[e]){const s=$(t),[r,n]=d(s,void 0,t.initialTransition(e,s));I(s,r,!1);const o=k(s,r);return ht(s,o,i(e)),[o,n]}(t,...e)));return[T(s),c(r)]},transition(e,s){const[r,i]=v(A,()=>F(l,()=>function(t,e,s){const r=$(t,e);I(r,e,!1);const[i,n]=d(r,e,t.transition(e,s,r));I(r,i,!1);const o=i===e?i:k(r,i);return ht(r,o,s),[o,n]}(t,e,s)));return[T(r),c(i)]},getActorRef(t,e){const s=E(t)?.actor;return s&&void 0!==e?w(s,e):s},async executeEffects(t){if(f)throw new Error("executeEffects calls must not overlap: await the previous call before starting the next batch.");const s={rootEvents:[],failures:[],pending:new Set};f=s;const r=_?void 0:{};try{for(const s of t){const{effect:t}=s,i={id:s.id,transitionIndex:s.transitionIndex,effectIndex:s.effectIndex},n=e.runtime?.(i,t),o=n?S({...p,...n},_,!0):r;"action"===t.kind?await e.executeAction(t,i,o??x??{}):await t.exec(o)}await(async t=>{if(await b(t),t.failures.length)throw t.failures[0]})(s),y.push(...s.rootEvents)}catch(t){throw await b(s),t}finally{f=void 0}},async waitForEvent(){if(void 0===o)throw new Error("Cannot wait for an event before the first transition");const t=y.shift();return t?t.event:e.waitForEvent({id:`event:${o}`,transitionIndex:o})},async run(...t){if(0!==n||r!==n)throw new ut;let[e,s]=O.initialTransition(...t);for(await O.executeEffects(s);"active"===e.status;){const t=await O.waitForEvent();[e,s]=O.transition(e,t),await O.executeEffects(s)}const i=e;if("done"===i.status)return i.output;if("error"===i.status)throw i.error;throw new dt}};return O},Object.defineProperty(t,"__esModule",{value:!0})});
|
|
2
2
|
//# sourceMappingURL=xstate-durable.umd.min.js.map
|