xstate 6.0.0-alpha.21 → 6.0.0-alpha.22
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-550ff7f2.development.cjs.js → StateMachine-1888d034.development.cjs.js} +178 -27
- package/dist/{StateMachine-1b155670.cjs.js → StateMachine-610fe929.cjs.js} +178 -27
- package/dist/{StateMachine-e25dd61f.development.esm.js → StateMachine-9fad7a2b.development.esm.js} +174 -28
- package/dist/{StateMachine-3c17fe3e.esm.js → StateMachine-ddb5d007.esm.js} +174 -28
- package/dist/declarations/src/State.d.ts +4 -2
- package/dist/declarations/src/StateMachine.d.ts +5 -4
- package/dist/declarations/src/actions.d.ts +10 -8
- package/dist/declarations/src/actors/logic.d.ts +5 -21
- package/dist/declarations/src/createActor.d.ts +3 -1
- package/dist/declarations/src/fsm.d.ts +3 -1
- package/dist/declarations/src/index.d.ts +2 -2
- package/dist/declarations/src/stateUtils.d.ts +1 -1
- package/dist/declarations/src/system.d.ts +34 -9
- package/dist/declarations/src/transitionActions.d.ts +5 -1
- package/dist/declarations/src/types.d.ts +70 -51
- package/dist/{index-3f87408c.esm.js → index-2fe11873.esm.js} +782 -389
- package/dist/{index-ec7c7a2d.development.cjs.js → index-41c0375c.development.cjs.js} +790 -390
- package/dist/{index-e694228c.cjs.js → index-434fac23.cjs.js} +789 -389
- package/dist/{index-d39fb138.development.esm.js → index-44d8c2d1.development.esm.js} +783 -390
- package/dist/xstate-actors.cjs.js +1 -1
- package/dist/xstate-actors.development.cjs.js +1 -1
- package/dist/xstate-actors.development.esm.js +1 -1
- package/dist/xstate-actors.esm.js +1 -1
- package/dist/xstate-actors.umd.min.js +1 -1
- package/dist/xstate-actors.umd.min.js.map +1 -1
- 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.cjs.js +82 -101
- package/dist/xstate.cjs.mjs +1 -0
- package/dist/xstate.development.cjs.js +82 -101
- package/dist/xstate.development.cjs.mjs +1 -0
- package/dist/xstate.development.esm.js +80 -100
- package/dist/xstate.esm.js +80 -100
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
export { T as TimeoutError,
|
|
1
|
+
export { T as TimeoutError, H as createAsyncLogic, D as createCallbackLogic, C as createEmptyActor, F as createEventObservableLogic, I as createListenerLogic, G as createLogic, E as createObservableLogic, K as createSubscriptionLogic, J as listenerLogic, L as subscriptionLogic } from './index-44d8c2d1.development.esm.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { T as TimeoutError,
|
|
1
|
+
export { T as TimeoutError, H as createAsyncLogic, D as createCallbackLogic, C as createEmptyActor, F as createEventObservableLogic, I as createListenerLogic, G as createLogic, E as createObservableLogic, K as createSubscriptionLogic, J as listenerLogic, L as subscriptionLogic } from './index-2fe11873.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).XStateActors={})}(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.init",r="@xstate.stop";function i(t){return{type:s,input:t}}function n(t){setTimeout(()=>{throw t})}const o="function"==typeof Symbol&&Symbol.observable||"@@observable",c=new WeakMap;function a(t){const e=(t,e)=>[{status:"active",output:void 0,error:void 0,input:t},[]];return{start:(e,{self:s,system:r})=>{const i=e.input.actor;if(!i||"stopped"===i.getSnapshot().status)return;const n=t(e.input,{self:s,system:r});if(!n)return;let o,a=!1;const h=()=>{a||(a=!0,n.unsubscribe(),o?.unsubscribe())},u=s._parent;u&&(o=u.subscribe({complete:h,error:h})),c.set(s,{unsubscribe:h})},transition:(t,e,{self:s})=>e.type===r?(c.get(s)?.unsubscribe(),c.delete(s),[{...t,status:"stopped",error:void 0},[]]):[t,[]],initialTransition:e,getInitialSnapshot:(t,s)=>e(s)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}function h(t,e,s){"stopped"!==t.getSnapshot().status&&t._parent&&e._relay(t,t._parent,s())}function u(){return a(({actor:t,eventType:e,mapper:s},{self:r,system:i})=>{const n="*"!==e&&e.endsWith(".*");return t.on(n?"*":e,t=>{n&&!function(t,e){if(e===t)return!0;if("*"===e)return!0;if(!e.endsWith(".*"))return!1;const s=e.split("."),r=t.split(".");for(let t=0;t<s.length;t++){const e=s[t],i=r[t];if("*"===e){return t===s.length-1}if(e!==i)return!1}return!0}(t.type,e)||h(r,i,()=>s(t))})})}const p=u();function d(){return a(({actor:t,mappers:e},{self:s,system:r})=>{const{done:i,error:n,snapshot:o}=e;return t.subscribe({next:t=>{"done"===t.status&&i?h(s,r,()=>i(t.output)):"error"===t.status&&n?h(s,r,()=>n(t.error)):"active"===t.status&&o&&h(s,r,()=>o(t))},error:t=>{n&&h(s,r,()=>n(t))},complete:()=>{}})})}const l=d();function f(t){if(void 0!==t)return"number"==typeof t?t:function(t){const e=t.trim(),s=e.match(/^(\d+)ms$/i);if(s)return parseInt(s[1],10);const r=e.match(/^(\d*)(\.?)(\d*)s$/i);if(r){const t=r[1]?parseInt(r[1],10):0,e=!!r[2],s=r[3]?parseInt(r[3].padEnd(3,"0").slice(0,3),10):0;return 1e3*t+(e?s:0)}const i=e.match(/^P(?:(?<weeks>\d+(?:[.,]\d+)?)W)?(?:(?<days>\d+(?:[.,]\d+)?)D)?(?:T(?:(?<hours>\d+(?:[.,]\d+)?)H)?(?:(?<minutes>\d+(?:[.,]\d+)?)M)?(?:(?<seconds>\d+(?:[.,]\d+)?)S)?)?$/i);if(!i?.groups)return;const{weeks:n,days:o,hours:c,minutes:a,seconds:h}=i.groups;if(!(n||o||c||a||h))return;const u=t=>t?Number(t.replace(",",".")):0;return 7*u(n)*24*60*60*1e3+24*u(o)*60*60*1e3+60*u(c)*60*1e3+60*u(a)*1e3+1e3*u(h)}(t)}function _(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)}}function y(t,e){return`${t.sessionId}.${e}`}let v=!1;let g=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const m={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)};function b(t,e){try{t?.(e)}catch(t){n(t)}}function S(t){return"object"==typeof t&&null!==t&&"args"in t&&"exec"in t}function x(t,e){if(!t?.length)return[];const s=[];for(const r of t)S(r)?e.actionExecutor(r):s.push(r);return s}class E{constructor(t,s){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this._boundProcess=this._process.bind(this),this.mailbox=new e(this._boundProcess),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=g.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=[],this._collectedActions=[],this._collectedSent=[],this._initialEffects=void 0,this.registryKey=void 0,this.sessionId=void 0,this.system=void 0,this.trigger=void 0,this.src=void 0,this._deferred=[],this._pendingEffects=void 0;const r={...m,...s},{clock:i,logger:n,parent:o,syncSnapshot:c,id:a,registryKey:h,inspect:u}=r;this.system=o?o.system:r._systemRef?.current??function(t,e){let s=0;const r=new Map,i=new Map,n=new WeakMap,o=new Set,c={},{clock:a,logger:h}=e,u=(t,e,s,r,i)=>{o.size&&t&&(t._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:s,delay:r,id:i})},p={schedule:(t,e,s,r,i=Math.random().toString(36).slice(2))=>{u(t,e,s,r,i);const n={source:t,target:e,event:s,delay:r,id:i,startedAt:Date.now()},o=y(t,i);l._snapshot._scheduledEvents[o]=n;const h=a.setTimeout(()=>{delete c[o],delete l._snapshot._scheduledEvents[o],d(t,e,s)},r);c[o]=h},cancel:(t,e)=>{const s=y(t,e),r=c[s];delete c[s],delete l._snapshot._scheduledEvents[s],void 0!==r&&a.clearTimeout(r)},cancelAll:t=>{for(const e in l._snapshot._scheduledEvents){const s=l._snapshot._scheduledEvents[e];s.source===t&&p.cancel(t,s.id)}}},d=(t,e,s)=>{const r=e.logic;if("function"==typeof r?.isInternalEventType&&r.isInternalEventType(s.type)&&t!==e)throw new Error(`Internal event "${s.type}" cannot be sent to actor "${e.id}" from outside.`);e._lastSourceRef=t,e._send(s)},l={children:r,reverseKeyedActors:n,keyedActors:i,_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+s++,_register:(t,e)=>(r.set(t,e),t),_unregister:t=>{r.delete(t.sessionId);const e=n.get(t);void 0!==e&&(i.delete(e),n.delete(t))},get:t=>i.get(t),getAll:()=>Object.fromEntries(i.entries()),_set:(t,e)=>{const s=i.get(t);if(s&&s!==e)throw new Error(`Actor with registry key '${t}' already exists.`);i.set(t,e),n.set(e,t)},inspect:t=>{const e=_(t);return o.add(e),{unsubscribe(){o.delete(e)}}},_sendInspectionEvent:e=>{if(!o.size)return;const s={...e,rootId:t.sessionId};o.forEach(t=>t.next?.(s))},_relay:(t,e,s)=>{u(t,e,s),d(t,e,s)},scheduler:p,getSnapshot:()=>({_scheduledEvents:{...l._snapshot._scheduledEvents}}),start:()=>{const t=l._snapshot._scheduledEvents;l._snapshot._scheduledEvents={};for(const e in t){const{source:s,target:r,event:i,delay:n,id:o}=t[e];p.schedule(s,r,i,n,o)}},_clock:a,_logger:h,_timerStrategy:e.timers};return l}(this,{clock:i,logger:n,timers:r.timers}),o||!r._systemRef||r._systemRef.current||(r._systemRef.current=this.system),u&&!o&&this.system.inspect(_(u)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=s?.logger??this.system._logger,this.clock=s?.clock??this.system._clock,this._parent=o,this._syncSnapshot=c,this.options=r,this.src=r.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 non-child actor ${t.id}`);t._stop()},emit:t=>{for(const e of[this.eventListeners.get(t.type),this.eventListeners.get("*")])if(e)for(const s of e)b(s,t)},actionExecutor:t=>{const e=()=>{if(this._collectedActions.push({type:t.type,params:t.params}),!t.exec)return;const e=v;try{v=!0,t.exec()}finally{v=e}};this._processingStatus!==g.Running||this._forceDeferredActions?this._deferred.push(e):e()}},this.send=this.send.bind(this),this.trigger=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}}),h&&(this.registryKey=h,this.system._set(h,this)),this._collectedMicrosteps=[];let p=s?.snapshot??s?.state;if(p&&"object"==typeof p&&"_pendingEffects"in p){const{_pendingEffects:t,...e}=p;this._pendingEffects=t,p=e}try{if(p)this._snapshot=this.logic.restoreSnapshot?this.logic.restoreSnapshot(p,this._actorScope):p;else if(s?._inert);else{const[t,e]=this.logic.initialTransition(this.options?.input,this._actorScope);this._snapshot=t,this._initialEffects=e}}catch(t){this._snapshot={status:"error",output:void 0,error:t},this._deferred.length=0}h&&"active"!==this._snapshot.status&&this.system._unregister(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_setErrorSnapshot(t,e=this._snapshot){this._snapshot={...e,status:"error",error:t}}_recoverOrError(t,e){this._tryHandleExecutionError(t,e)||(this._setErrorSnapshot(t),this._error(t))}_tryHandleExecutionError(t,e=this._snapshot){const s=this.logic.getExecutionErrorEvent?.(e,t);if(!s)return!1;try{const[t,r]=this.logic.transition(e,s,this._actorScope);this._snapshot=t;const i=x(r,this._actorScope);return this.update(t,s),this.logic.executeEffects?.(i,this._actorScope),!0}catch{return!1}}_next(t){for(const e of this.observers)b(e.next,t)}update(t,e){this._snapshot=t;for(let e=0;e<this._deferred.length;e++){const s=this._deferred[e];try{s()}catch(e){if(this._deferred.length=0,this._tryHandleExecutionError(e,t))return;this._setErrorSnapshot(e,t);break}}switch(this._deferred.length=0,this._snapshot.status){case"active":this._next(t);break;case"done":{this._next(t),this._stopProcedure(),this._complete();const e=(s=this.id,r=this._snapshot.output,{type:`xstate.done.actor.${s}`,output:r,actorId:s});this._parent&&this.system._relay(this,this._parent,e);break}case"error":this._error(this._snapshot.error)}var s,r;this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps,actions:this._collectedActions,sent:this._collectedSent,eventType:e.type}),this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[]}_flushInitialEffects(){if(!this._initialEffects)return!0;this._forceDeferredActions=!0;try{const t=x(this._initialEffects,this._actorScope);return this.logic.executeEffects?.(t,this._actorScope),this._initialEffects=void 0,!0}catch(t){return this._initialEffects=void 0,this._deferred.length=0,this._recoverOrError(t),"active"===this._snapshot.status}finally{this._forceDeferredActions=!1}}subscribe(t,e,s){const r=_(t,e,s);if(this._processingStatus!==g.Stopped)this.observers.add(r);else switch(this._snapshot.status){case"done":b(r.complete);break;case"error":{const t=this._snapshot.error;r.error?b(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.set(t,s)),s.add(e),{unsubscribe:()=>{s.delete(e)}}}select(t,e=Object.is){return{subscribe:(s,r,i)=>{const n=_(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===g.Running)return this;if(this._processingStatus===g.Stopped)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.registryKey&&this.system._set(this.registryKey,this),this._processingStatus=g.Running;const t=i(this.options.input);this._lastSourceRef=this._parent;switch(this._snapshot.status){case"done":return this._flushInitialEffects()?(this.update(this._snapshot,t),this):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._setErrorSnapshot(t),this._error(t),this}if(!this._flushInitialEffects())return this;if(this.update(this._snapshot,t),this._pendingEffects){const t=this.options.timers??this.system._timerStrategy??"resume";for(const e of this._pendingEffects)"@xstate.raise"===e.type&&this.system.scheduler.schedule(this,this,e.event,k(t,e),e.id);this._pendingEffects=void 0}return 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)return void this._recoverOrError(s.err);let i=this._snapshot;try{const[s,r]=e;i=s,this._snapshot=i;const n=x(r,this._actorScope);this.update(i,t),this.logic.executeEffects?.(n,this._actorScope)}catch(t){return void this._recoverOrError(t,i)}t.type===r&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===g.Stopped?this:(this.mailbox.clear(),this._processingStatus===g.NotStarted?(this._processingStatus=g.Stopped,this):(this.mailbox.enqueue({type:r}),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)b(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,b(r,t)}this.observers.clear(),e&&n(t)}else this._parent||n(t);var e;this.eventListeners.clear(),this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${e=this.id}`,error:t,actorId:e})}_stopProcedure(){this._processingStatus===g.Running&&(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new e(this._boundProcess),this._processingStatus=g.Stopped,this.system._unregister(this))}_send(t){this._processingStatus!==g.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){const e=this.logic.getPersistedSnapshot(this._snapshot,t),s=this.system._snapshot._scheduledEvents;let r;const i=Date.now();for(const t in s){const e=s[t];e.source===this&&e.target===this&&(r??=[]).push({type:"@xstate.raise",event:e.event,id:e.id,delay:e.delay,startedAt:e.startedAt,elapsed:Math.max(0,i-e.startedAt)})}return r??=this._pendingEffects,r?{...e,_pendingEffects:r}:e}[o](){return this}getSnapshot(){return this._snapshot}}function k(t,e){if("function"==typeof t)return t(e);switch(t){case"restart":return e.delay;case"absolute":return Math.max(0,e.startedAt+e.delay-Date.now());default:return Math.max(0,e.delay-e.elapsed)}}const w=new WeakMap,I="xstate.logic.effect.resolve",A="xstate.logic.effect.reject",T="xstate.logic.effect.start";function R(t){let e=w.get(t);return e||(e=new Map,w.set(t,e)),e}function M(t,e){if(t?.length)for(const s of t)switch(s.type){case"emit":e.emit(s.event);break;case"sendBack":{const t=e.self._parent;t&&e.system._relay(e.self,t,s.event);break}case"raise":e.system._relay(e.self,e.self,s.event);break;case"effect":{if(!s.key){const t=s.exec();"function"==typeof t&&R(e.self).set(Symbol(),{cleanup:t});break}const t=R(e.self);if(t.has(s.key))break;const r=s.exec();t.set(s.key,{cleanup:"function"==typeof r?r:void 0});break}case"cleanupEffects":{const t=w.get(e.self);if(!t)break;for(const{cleanup:e}of t.values())e?.();w.delete(e.self);break}}}function L(t){const e=(e,s,i)=>{if("active"!==e.status)return[e,[]];if(s.type===r)return[{...e,status:"stopped",input:void 0},[{type:"cleanupEffects"}]];if(s.type===T)return[{...e,effects:{...e.effects,[s.key]:{status:"active"}}},[]];if(s.type===I)return[{...e,effects:{...e.effects,[s.key]:{status:"done",output:s.output}}},[]];if(s.type===A)return[{...e,effects:{...e.effects,[s.key]:{status:"error",error:s.error}}},[]];const n=[],o={},c={emit:t=>{n.push({type:"emit",event:t})},sendBack:t=>{n.push({type:"sendBack",event:t})},raise:t=>{n.push({type:"raise",event:t})},effect:(t,s)=>{if("string"==typeof t)return r=t,i=s,void(e.effects?.[r]||(n.push({type:"effect",key:r,exec:i}),o[r]={status:"active"}));var r,i;n.push({type:"effect",exec:t})}},a=t.run({context:e.context,event:s,input:e.input,system:i.system,self:i.self,emit:i.emit},c),h={...e,...a||{}};return("context"in e||void 0!==a?.context)&&(h.context=a?.context??e.context),(a?.effects||Object.keys(o).length)&&(h.effects={...e.effects,...a?.effects,...o}),[h,n]},s={id:t.id,config:t,transition:e,start:(t,s)=>{const[r,n]=e(t,i(t.input),s);Object.assign(t,r),M(n,s)},initialTransition:(e,s)=>{const r=function(t,e){return"function"==typeof t?t({input:e}):t}(t.context,e),i={status:"active",output:void 0,error:void 0,input:e};return void 0!==r&&(i.context=r),[{...i},[]]},getInitialSnapshot:(t,e)=>s.initialTransition(e,t)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t,executeEffects:M};return s}const P=new WeakMap;const $="xstate.observable.next",O="xstate.observable.error",j="xstate.observable.complete";const D="xstate.async.resolve",W="xstate.async.reject";class K extends Error{constructor(t){super(`Async logic timed out after ${t}.`),this.name="TimeoutError"}}const N=L({context:void 0,run:()=>{}});t.TimeoutError=K,t.createAsyncLogic=function(t){const e=t;return L({id:e.id,schemas:e.schemas,context:void 0,run:({event:t,input:r,self:i,system:n},o)=>{switch(t.type){case D:{const e=t.data??t.output;return{status:"done",output:e,input:void 0,effects:{async:{status:"done",output:e}}}}case W:{const e=t.data??t.error;return{status:"error",error:e,input:void 0,effects:{async:{status:"error",error:e}}}}}if(t.type===s)return o.effect(()=>{const t=i,s=new AbortController,c=e.timeout,a=f(c),h=void 0===a?void 0:n._clock.setTimeout(()=>{"active"===i.getSnapshot().status&&(s.abort(),n._relay(t,t,{type:W,data:new K(c)}))},a),u=()=>{void 0!==h&&n._clock.clearTimeout(h)};return Promise.resolve(e.run({input:r,system:n,self:i,signal:s.signal},{emit:o.emit,step:async(e,s)=>{const r=i.getSnapshot().effects?.[e];if("done"===r?.status)return r.output;if("error"===r?.status)throw r.error;if("active"===r?.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))})})}(i,e);n._relay(t,t,{type:T,key:e});try{const r=await s();return n._relay(t,t,{type:I,key:e,output:r}),r}catch(s){throw n._relay(t,t,{type:A,key:e,error:s}),s}}})).then(e=>{u(),"active"===i.getSnapshot().status&&n._relay(t,t,{type:D,data:e})},e=>{u(),"active"===i.getSnapshot().status&&n._relay(t,t,{type:W,data:e})}),()=>{s.abort(),u()}}),{effects:{async:{status:"active"}}}}})},t.createCallbackLogic=function(t){const e="function"==typeof t?t:t.run;return L({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,s)=>{const{event:r,input:i,self:n,system:o}=t,c=t.emit,a=P.get(n);a?.receivers?.forEach(t=>t(r)),s.effect("callback",()=>{const t={receivers:void 0,dispose:void 0};return P.set(n,t),t.dispose=e({input:i,system:o,self:n,sendBack:t=>{if("stopped"===n.getSnapshot().status)return;const e=n._parent;e&&o._relay(n,e,t)},receive:e=>{t.receivers??=new Set,t.receivers.add(e)},emit:c}),()=>{P.delete(n),t.receivers?.clear(),t.dispose?.()}})}})},t.createEmptyActor=function(){return new E(N,t);var t},t.createEventObservableLogic=function(t){const e="function"==typeof t?t:t.run,s={config:e,transition:(t,e)=>{if("active"!==t.status)return[t,[]];switch(e.type){case O:return[{...t,status:"error",error:e.data,input:void 0,_subscription:void 0},[]];case j:return[{...t,status:"done",input:void 0,_subscription:void 0},[]];case r:return t._subscription.unsubscribe(),[{...t,status:"stopped",input:void 0,_subscription:void 0},[]];default:return[t,[]]}},initialTransition:(t,e)=>[{status:"active",output:void 0,error:void 0,context:void 0,input:t,_subscription:void 0},[]],getInitialSnapshot:(t,e)=>s.initialTransition(e,t)[0],start:(t,{self:s,system:r,emit:i})=>{"done"!==t.status&&(t._subscription=e({input:t.input,system:r,self:s,emit:i}).subscribe({next:t=>{s._parent&&r._relay(s,s._parent,t)},error:t=>{r._relay(s,s,{type:O,data:t})},complete:()=>{r._relay(s,s,{type:j})}}))},getPersistedSnapshot:({_subscription:t,...e})=>e,restoreSnapshot:t=>({...t,_subscription:void 0})};return s},t.createListenerLogic=u,t.createLogic=L,t.createObservableLogic=function(t){const e="function"==typeof t?t:t.run;return L({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,s)=>{const{event:r,input:i,self:n,system:o}=t,c=t.emit;switch(r.type){case $:return{context:r.data};case O:return{status:"error",error:r.data,input:void 0,effects:{observable:{status:"error",error:r.data}}};case j:return{status:"done",input:void 0,effects:{observable:{status:"done"}}}}s.effect("observable",()=>{const t=n,s=e({input:i,system:o,self:n,emit:c}).subscribe({next:e=>{o._relay(t,t,{type:$,data:e})},error:e=>{o._relay(t,t,{type:O,data:e})},complete:()=>{o._relay(t,t,{type:j})}});return()=>s.unsubscribe()})}})},t.createSubscriptionLogic=d,t.listenerLogic=p,t.subscriptionLogic=l,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).XStateActors={})}(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.init",r="@xstate.stop",i="@xstate.terminate";function o(t){return{type:s,input:t}}function n(t){setTimeout(()=>{throw t})}const c="function"==typeof Symbol&&Symbol.observable||"@@observable";const a=new WeakMap;function h(t){const e=(t,e)=>[{status:"active",output:void 0,error:void 0,input:t},[]];return{start:(e,{self:s,system:r})=>{const i=e.input.actor;if(!i||"stopped"===i.getSnapshot().status)return;const o=t(e.input,{self:s,system:r});if(!o)return;let n,c=!1;const h=()=>{c||(c=!0,o.unsubscribe(),n?.unsubscribe())},u=s._parent;u&&(n=u.subscribe({complete:h,error:h})),a.set(s,{unsubscribe:h})},transition:(t,e,{self:s})=>e.type===r?(a.get(s)?.unsubscribe(),a.delete(s),[{...t,status:"stopped",error:void 0},[]]):[t,[]],initialTransition:e,getInitialSnapshot:(t,s)=>e(s)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}function u(t,e,s){"stopped"!==t.getSnapshot().status&&t._parent&&e._relay(t,t._parent,s())}function d(){return h(({actor:t,eventType:e,mapper:s},{self:r,system:i})=>{const o="*"!==e&&e.endsWith(".*");return t.on(o?"*":e,t=>{o&&!function(t,e){if(e===t)return!0;if("*"===e)return!0;if(!e.endsWith(".*"))return!1;const s=e.split("."),r=t.split(".");for(let t=0;t<s.length;t++){const e=s[t],i=r[t];if("*"===e){return t===s.length-1}if(e!==i)return!1}return!0}(t.type,e)||u(r,i,()=>s(t))})})}const p=d();function l(){return h(({actor:t,mappers:e},{self:s,system:r})=>{const{done:i,error:o,snapshot:n}=e;return t.subscribe({next:t=>{"done"===t.status&&i?u(s,r,()=>i(t.output)):"error"===t.status&&o?u(s,r,()=>o(t.error)):"active"===t.status&&n&&u(s,r,()=>n(t))},error:t=>{o&&u(s,r,()=>o(t))},complete:()=>{}})})}const f=l();function _(t=this.source.system){return t.emitEvent(this.source,this.event)}function y(t,e,s){return{kind:"action",exec:e,type:t,action:e,params:s,args:[]}}function v(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 m(t=this.source.system){return function(t){if("string"==typeof t)throw new Error("Only event objects may be used with sendTo")}(this.event),void 0!==this.delay?t.scheduleTimer(this.source,this.id,this.delay):t.sendEvent(this.source,this.target,this.event)}function g(t,e,s){const r=[t,e,s,{}];return{kind:"builtin",exec:m,type:"@xstate.sendTo",source:t.self,target:e,event:s,id:void 0,delay:void 0,params:void 0,args:r}}function b(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},r=[t.self,s];return{kind:"builtin",exec:v,type:i,source:t.self,actor:t.self,id:t.self.id,...s,params:void 0,args:r}}function S(t,e,[s,r]){const o="done"===s.status||"error"===s.status,n="done"===e?.status||"error"===e?.status,c=r.some(t=>"object"==typeof t&&null!==t&&"type"in t&&t.type===i);return!o||n||c?[s,r]:[s,[...r,b(t,s)]]}function x(t){if(void 0!==t)return"number"==typeof t?t:function(t){const e=t.trim(),s=e.match(/^(\d+)ms$/i);if(s)return parseInt(s[1],10);const r=e.match(/^(\d*)(\.?)(\d*)s$/i);if(r){const t=r[1]?parseInt(r[1],10):0,e=!!r[2],s=r[3]?parseInt(r[3].padEnd(3,"0").slice(0,3),10):0;return 1e3*t+(e?s:0)}const i=e.match(/^P(?:(?<weeks>\d+(?:[.,]\d+)?)W)?(?:(?<days>\d+(?:[.,]\d+)?)D)?(?:T(?:(?<hours>\d+(?:[.,]\d+)?)H)?(?:(?<minutes>\d+(?:[.,]\d+)?)M)?(?:(?<seconds>\d+(?:[.,]\d+)?)S)?)?$/i);if(!i?.groups)return;const{weeks:o,days:n,hours:c,minutes:a,seconds:h}=i.groups;if(!(o||n||c||a||h))return;const u=t=>t?Number(t.replace(",",".")):0;return 7*u(o)*24*60*60*1e3+24*u(n)*60*60*1e3+60*u(c)*60*1e3+60*u(a)*1e3+1e3*u(h)}(t)}!function(t,e){const s=(t,...s)=>{e(t,...s)};Object.assign(s,{cancel:()=>{},emit:()=>{},log:()=>{},raise:()=>{},spawn:()=>({}),sendTo:()=>{},stop:()=>{},listen:()=>({}),subscribeTo:()=>({}),...t})}({},()=>{});const E=Symbol("xstate.snapshotActorRef");function w(t,e,s=e.system){const r=function(t,e,s,r){const i=new Map(r?.children??t.children),o=new Map(r?.keyedActors??Object.entries(t.getAll()).filter(t=>!!t[1])),n=new WeakMap;let c=!0;const a={...t,children:i,keyedActors:o,reverseKeyedActors:n,_snapshot:{...r?.snapshot??t._snapshot},_bookId:()=>c?(c=!1,e.sessionId??e.id):"x:"+a._snapshot._nextActorId++,_register:(t,e)=>(i.set(t,e),t),_unregister:t=>{t.sessionId&&i.delete(t.sessionId);const e=n.get(t);void 0!==e&&o.get(e)===t&&o.delete(e),n.delete(t)},_set:(t,e)=>{const s=o.get(t);if(s&&s!==e)throw new Error(`Actor with registry key '${String(t)}' already exists.`);o.set(t,e),n.set(e,t)},get:t=>o.get(t),getAll:()=>Object.fromEntries(o),_sendInspectionEvent:()=>{}};for(const[t,e]of o)n.set(e,t);for(const t of Object.values(s)){if(!t)continue;t.sessionId&&i.set(t.sessionId,t);const e=t.registryKey;e&&(o.set(e,t),n.set(t,e))}return a}(s,e,function(t){return"children"in t&&t.children?t.children:{}}(t));Object.defineProperty(t,E,{configurable:!0,value:{actor:e,systemState:{children:r.children,keyedActors:r.keyedActors,snapshot:r._snapshot}}})}function k(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)}}function A(t,e){return`${t.sessionId}.${e}`}let T=!1;let I=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const R={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)};function O(t,e){try{t?.(e)}catch(t){n(t)}}function P(t,e){if(t?.length)for(const s of t)e.actionExecutor(s)}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._boundProcess=this._process.bind(this),this.mailbox=new e(this._boundProcess),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=I.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=[],this._collectedActions=[],this._collectedSent=[],this._initialEffects=void 0,this.registryKey=void 0,this.sessionId=void 0,this.system=void 0,this.trigger=void 0,this.src=void 0,this._deferred=[],this._restored=!1;const r={...R,...s},{clock:i,logger:o,parent:n,syncSnapshot:c,id:a,registryKey:h,inspect:u}=r;this.system=n?n.system:r._systemRef?.current??function(t,e){const s=new Map,r=new Map,i=new WeakMap,o=new Set,n={},{clock:c,logger:a}=e,h=(t,e,s,r,i)=>{o.size&&t&&(t._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:s,delay:r,id:i})},u={schedule:(t,e,s)=>{const r=A(t,e);void 0!==n[r]&&u.cancel(t,e);const i=t.getSnapshot()?.timers?.[e];if(i){const r="self"===i.target?t:i.target;h(t,r,i.event,s,e)}const o=Date.now(),a={source:t,delay:s,id:e,scheduledAt:o,dueAt:o+s},l=A(t,e);p._snapshot._scheduledTimers[l]=a;const f=c.setTimeout(()=>{delete n[l],delete p._snapshot._scheduledTimers[l],d(t,t,{type:"xstate.timer",id:e})},s);n[l]=f},cancel:(t,e)=>{const s=A(t,e),r=n[s];delete n[s],delete p._snapshot._scheduledTimers[s],void 0!==r&&c.clearTimeout(r)},cancelAll:t=>{for(const e in p._snapshot._scheduledTimers){const s=p._snapshot._scheduledTimers[e];s.source===t&&u.cancel(t,s.id)}}},d=(t,e,s)=>{const r=e.logic;if("function"==typeof r?.isInternalEventType&&r.isInternalEventType(s.type)&&t!==e)throw new Error(`Internal event "${s.type}" cannot be sent to actor "${e.id}" from outside.`);e._lastSourceRef=t,e._send(s)},p={children:s,reverseKeyedActors:i,keyedActors:r,_snapshot:{_scheduledTimers:(e?.snapshot&&e.snapshot.scheduler)??{},_nextActorId:0},_bookId:()=>"x:"+p._snapshot._nextActorId++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const e=i.get(t);void 0!==e&&(r.get(e)===t&&r.delete(e),i.delete(t))},get:t=>r.get(t),getAll:()=>Object.fromEntries(r.entries()),_set:(t,e)=>{const s=r.get(t);if(s&&s!==e)throw new Error(`Actor with registry key '${t}' already exists.`);r.set(t,e),i.set(e,t)},inspect:t=>{const e=k(t);return o.add(e),{unsubscribe(){o.delete(e)}}},_sendInspectionEvent:e=>{if(!o.size)return;const s={...e,rootId:t.sessionId};o.forEach(t=>t.next?.(s))},createActorRef:e.createActorRef,spawnActor:()=>{},startActor:t=>{t.start()},stopActor:t=>{t._stop()},terminateActor:(t,e)=>{t._terminate(e)},sendEvent:(t,e,s)=>{h(t,e,s),d(t,e,s)},emitEvent:(t,e)=>t._emit(e),scheduleTimer:(t,e,s)=>u.schedule(t,e,s),cancelTimer:(t,e)=>u.cancel(t,e),cancelAllTimers:t=>u.cancelAll(t),_relay:(t,e,s)=>p.sendEvent(t,e,s),scheduler:u,getSnapshot:()=>({_scheduledTimers:{...p._snapshot._scheduledTimers}}),start:()=>{const t=p._snapshot._scheduledTimers;p._snapshot._scheduledTimers={};for(const e in t){const{source:s,dueAt:r,id:i}=t[e];p.scheduleTimer(s,i,Math.max(0,r-Date.now()))}},_clock:c,_logger:a};return p}(this,{clock:i,logger:o,createActorRef:(t,e)=>j(t,e)}),n||!r._systemRef||r._systemRef.current||(r._systemRef.current=this.system),u&&!n&&this.system.inspect(k(u)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=s?.logger??this.system._logger,this.clock=s?.clock??this.system._clock,this._parent=n,this._syncSnapshot=c,this.options=r,this.src=r.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 non-child actor ${t.id}`);t._stop()},emit:t=>this.system.emitEvent(this,t),actionExecutor:t=>{const e=()=>{this._collectedActions.push({type:t.type,params:t.params});const e=T;try{T=!0,t.exec()}finally{T=e}};this._processingStatus!==I.Running||this._forceDeferredActions?this._deferred.push(e):e()}},this.send=this.send.bind(this),this.trigger=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}}),h&&(this.registryKey=h,this.system._set(h,this)),this._collectedMicrosteps=[];const d=s?.snapshot??s?.state;this._restored=void 0!==d;try{if(d)this._setSnapshot(this.logic.restoreSnapshot?this.logic.restoreSnapshot(d,this._actorScope):d);else if(s?._inert);else{const[t,e]=S(this._actorScope,void 0,this.logic.initialTransition(this.options?.input,this._actorScope));this._setSnapshot(t),this._initialEffects=e}}catch(t){this._setSnapshot({status:"error",output:void 0,error:t}),this._deferred.length=0}h&&"active"!==this._snapshot.status&&this.system._unregister(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_setSnapshot(t){this._snapshot=t,w(t,this)}_setErrorSnapshot(t,e=this._snapshot){this._setSnapshot({...e,status:"error",error:t})}_recoverOrError(t,e){this._tryHandleExecutionError(t,e)||(this._setErrorSnapshot(t),this._error(t))}_tryHandleExecutionError(t,e=this._snapshot){const s=this.logic.getExecutionErrorEvent?.(e,t);if(!s)return!1;try{const[t,r]=S(this._actorScope,e,this.logic.transition(e,s,this._actorScope));return this._setSnapshot(t),P(r,this._actorScope),this.update(t,s),!0}catch{return!1}}_next(t){for(const e of this.observers)O(e.next,t)}update(t,e){this._setSnapshot(t);for(let e=0;e<this._deferred.length;e++){const s=this._deferred[e];try{s()}catch(e){if(this._deferred.length=0,this._tryHandleExecutionError(e,t))return;this._setErrorSnapshot(e,t),this._error(e);break}}if(this._deferred.length=0,"active"===this._snapshot.status)this._next(t);this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps,actions:this._collectedActions,sent:this._collectedSent,eventType:e.type}),this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[]}_flushInitialEffects(){if(!this._initialEffects)return!0;this._forceDeferredActions=!0;try{return P(this._initialEffects,this._actorScope),this._initialEffects=void 0,!0}catch(t){return this._initialEffects=void 0,this._deferred.length=0,this._recoverOrError(t),"active"===this._snapshot.status}finally{this._forceDeferredActions=!1}}subscribe(t,e,s){const r=k(t,e,s);if(this._processingStatus!==I.Stopped)this.observers.add(r);else switch(this._snapshot.status){case"done":O(r.complete);break;case"error":{const t=this._snapshot.error;r.error?O(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.set(t,s)),s.add(e),{unsubscribe:()=>{s.delete(e)}}}select(t,e=Object.is){return{subscribe:(s,r,i)=>{const o=k(s,r,i);let n=t(this.getSnapshot());return this.subscribe({next:s=>{const r=t(s);e(n,r)||(n=r,o.next?.(r))},error:o.error,complete:o.complete})},get:()=>t(this.getSnapshot())}}start(){if(this._processingStatus===I.Running)return this;if(this._processingStatus===I.Stopped)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.registryKey&&this.system._set(this.registryKey,this),this._processingStatus=I.Running;const t=o(this.options.input);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,t),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.update(this._snapshot,t),this._restored){const t=this._snapshot.timers??{};for(const e of Object.values(t))this.system.scheduleTimer(this,e.id,e.delay);this._restored=!1}return this.mailbox.start(),this}_process(t){let e,s;try{e=S(this._actorScope,this._snapshot,this.logic.transition(this._snapshot,t,this._actorScope))}catch(t){s={err:t}}if(s)return void this._recoverOrError(s.err);let i=this._snapshot;try{const[s,r]=e;i=s,this._setSnapshot(i),P(r,this._actorScope),this.update(i,t)}catch(t){return void this._recoverOrError(t,i)}t.type===r&&(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)O(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,{type:`xstate.done.actor.${e}`,output:s,actorId:e})));var e,s;"error"===t.status&&this._error(t.error)}_stop(){return this._processingStatus===I.Stopped?this:(this.mailbox.clear(),this._processingStatus===I.NotStarted?(this._processingStatus=I.Stopped,this.system._unregister(this),this):(this.mailbox.enqueue({type:r}),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)O(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,O(r,t)}this.observers.clear(),e&&n(t)}else this._parent||n(t);var e;this.eventListeners.clear(),this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${e=this.id}`,error:t,actorId:e})}_stopProcedure(){this._processingStatus===I.Running&&(this.system.cancelAllTimers(this),this.mailbox.clear(),this.mailbox=new e(this._boundProcess),this._processingStatus=I.Stopped,this.system._unregister(this))}_send(t){this._processingStatus!==I.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[c](){return this}getSnapshot(){return this._snapshot}}function j(t,e){return new M(t,e)}const L=new WeakMap,$="xstate.logic.effect.resolve",K="xstate.logic.effect.reject",W="xstate.logic.effect.start";function D(t,e,s){const r=function(t){let e=L.get(t);return e||(e=new Map,L.set(t,e)),e}(t);if(void 0!==e&&r.has(e))return;const i=s();r.set(e??Symbol(),{cleanup:"function"==typeof i?i:void 0})}function N(t){const e=(e,s,i)=>{if("active"!==e.status)return[e,[]];if(s.type===r)return[{...e,status:"stopped",input:void 0},[y("xstate.logic.cleanup",()=>function(t){const e=L.get(t);if(e){for(const{cleanup:t}of e.values())t?.();L.delete(t)}}(i.self))]];if(s.type===W)return[{...e,effects:{...e.effects,[s.key]:{status:"active"}}},[]];if(s.type===$)return[{...e,effects:{...e.effects,[s.key]:{status:"done",output:s.output}}},[]];if(s.type===K)return[{...e,effects:{...e.effects,[s.key]:{status:"error",error:s.error}}},[]];const o=[],n={},c={emit:t=>{o.push(function(t,e){return{kind:"emit",exec:_,type:e.type,source:t.self,event:e,params:void 0,args:[]}}(i,t))},sendBack:t=>{const e=i.self._parent;e&&o.push(g(i,e,t))},raise:t=>{o.push(g(i,i.self,t))},effect:(t,s)=>{if("string"==typeof t)return void((t,s)=>{e.effects?.[t]||(o.push(y("xstate.logic.effect",(e=i.self.system)=>D(i.self,t,()=>s(e)),{key:t})),n[t]={status:"active"})})(t,s);const r=t;o.push(y("xstate.logic.effect",(t=i.self.system)=>D(i.self,void 0,()=>r(t))))}},a=t.run({context:e.context,event:s,input:e.input,system:i.system,self:i.self,emit:i.emit},c),h={...e,...a||{}};return("context"in e||void 0!==a?.context)&&(h.context=a?.context??e.context),(a?.effects||Object.keys(n).length)&&(h.effects={...e.effects,...a?.effects,...n}),S(i,e,[h,o])},s={id:t.id,config:t,transition:e,start:(t,s,r)=>{if(!r?.restored)return;const[i,n]=e(t,o(t.input),s);Object.assign(t,i);for(const t of n)s.actionExecutor(t)},initialTransition:(s,r)=>{const i=function(t,e){return"function"==typeof t?t({input:e}):t}(t.context,s),n={status:"active",output:void 0,error:void 0,input:s};return void 0!==i&&(n.context=i),e({...n},o(s),r)},getInitialSnapshot:(t,e)=>s.initialTransition(e,t)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t};return s}const C=new WeakMap;const H="xstate.observable.next",q="xstate.observable.error",z="xstate.observable.complete";const B="xstate.async.resolve",J="xstate.async.reject";class X extends Error{constructor(t){super(`Async logic timed out after ${t}.`),this.name="TimeoutError"}}const F=N({context:void 0,run:()=>{}});t.TimeoutError=X,t.createAsyncLogic=function(t){const e=t;return N({id:e.id,schemas:e.schemas,context:void 0,run:({event:t,input:r,self:i,system:o},n)=>{switch(t.type){case B:{const e=t.data??t.output;return{status:"done",output:e,input:void 0,effects:{async:{status:"done",output:e}}}}case J:{const e=t.data??t.error;return{status:"error",error:e,input:void 0,effects:{async:{status:"error",error:e}}}}}if(t.type===s)return n.effect((t=o)=>{const s=i,n=e=>{t.sendEvent(s,s,e)},c=new AbortController,a=e.timeout,h=x(a),u=void 0===h?void 0:o._clock.setTimeout(()=>{"active"===i.getSnapshot().status&&(c.abort(),n({type:J,data:new X(a)}))},h),d=()=>{void 0!==u&&o._clock.clearTimeout(u)},p=Promise.resolve(e.run({input:r,system:o,self:i,signal:c.signal},{emit:e=>{t.emitEvent(s,e)},step:async(t,e)=>{const s=i.getSnapshot().effects?.[t];if("done"===s?.status)return s.output;if("error"===s?.status)throw s.error;if("active"===s?.status)return function(t,e){return new Promise((s,r)=>{const i=t.subscribe(t=>{const o=t.effects?.[e];"done"===o?.status?(i.unsubscribe(),s(o.output)):"error"===o?.status&&(i.unsubscribe(),r(o.error))})})}(i,t);n({type:W,key:t});try{const s=await e();return n({type:$,key:t,output:s}),s}catch(e){throw n({type:K,key:t,error:e}),e}}}));return p.then(t=>{d(),"active"===i.getSnapshot().status&&n({type:B,data:t})},t=>{d(),"active"===i.getSnapshot().status&&n({type:J,data:t})}),()=>{c.abort(),d()}}),{effects:{async:{status:"active"}}}}})},t.createCallbackLogic=function(t){const e="function"==typeof t?t:t.run;return N({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,s)=>{const{event:r,input:i,self:o,system:n}=t,c=t.emit,a=C.get(o);a?.receivers?.forEach(t=>t(r)),s.effect("callback",()=>{const t={receivers:void 0,dispose:void 0};return C.set(o,t),t.dispose=e({input:i,system:n,self:o,sendBack:t=>{if("stopped"===o.getSnapshot().status)return;const e=o._parent;e&&n._relay(o,e,t)},receive:e=>{t.receivers??=new Set,t.receivers.add(e)},emit:c}),()=>{C.delete(o),t.receivers?.clear(),t.dispose?.()}})}})},t.createEmptyActor=function(){return j(F)},t.createEventObservableLogic=function(t){const e="function"==typeof t?t:t.run,s={config:e,transition:(t,e,s)=>{if("active"!==t.status)return[t,[]];switch(e.type){case q:return S(s,t,[{...t,status:"error",error:e.data,input:void 0,_subscription:void 0},[]]);case z:return S(s,t,[{...t,status:"done",input:void 0,_subscription:void 0},[]]);case r:return t._subscription.unsubscribe(),[{...t,status:"stopped",input:void 0,_subscription:void 0},[]];default:return[t,[]]}},initialTransition:(t,e)=>[{status:"active",output:void 0,error:void 0,context:void 0,input:t,_subscription:void 0},[]],getInitialSnapshot:(t,e)=>s.initialTransition(e,t)[0],start:(t,{self:s,system:r,emit:i})=>{"done"!==t.status&&(t._subscription=e({input:t.input,system:r,self:s,emit:t=>{i(t)}}).subscribe({next:t=>{s._parent&&r._relay(s,s._parent,t)},error:t=>{r._relay(s,s,{type:q,data:t})},complete:()=>{r._relay(s,s,{type:z})}}))},getPersistedSnapshot:({_subscription:t,...e})=>e,restoreSnapshot:t=>({...t,_subscription:void 0})};return s},t.createListenerLogic=d,t.createLogic=N,t.createObservableLogic=function(t){const e="function"==typeof t?t:t.run;return N({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,r)=>{const{event:i,input:o,self:n,system:c}=t;switch(i.type){case H:return{context:i.data};case q:return{status:"error",error:i.data,input:void 0,effects:{observable:{status:"error",error:i.data}}};case z:return{status:"done",input:void 0,effects:{observable:{status:"done"}}}}i.type===s&&r.effect((t=c)=>{const s=n,r=e=>{t.sendEvent(s,s,e)},i=e({input:o,system:c,self:n,emit:e=>{t.emitEvent(s,e)}}).subscribe({next:t=>{r({type:H,data:t})},error:t=>{r({type:q,data:t})},complete:()=>{r({type:z})}});return()=>i.unsubscribe()})}})},t.createSubscriptionLogic=l,t.listenerLogic=p,t.subscriptionLogic=f,Object.defineProperty(t,"__esModule",{value:!0})});
|
|
2
2
|
//# sourceMappingURL=xstate-actors.umd.min.js.map
|