xstate 5.24.0 → 5.25.1

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.
Files changed (52) hide show
  1. package/actions/dist/xstate-actions.cjs.js +3 -3
  2. package/actions/dist/xstate-actions.development.cjs.js +3 -3
  3. package/actions/dist/xstate-actions.development.esm.js +3 -3
  4. package/actions/dist/xstate-actions.esm.js +3 -3
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +4 -1
  8. package/actors/dist/xstate-actors.development.cjs.js +4 -1
  9. package/actors/dist/xstate-actors.development.esm.js +4 -1
  10. package/actors/dist/xstate-actors.esm.js +4 -1
  11. package/actors/dist/xstate-actors.umd.min.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dist/{StateMachine-37bc3882.esm.js → StateMachine-6c48f805.esm.js} +4 -3
  14. package/dist/{StateMachine-4f3286fa.development.cjs.js → StateMachine-6d24a98f.development.cjs.js} +4 -3
  15. package/dist/{StateMachine-d6caf9f3.development.esm.js → StateMachine-d0e98d09.development.esm.js} +4 -3
  16. package/dist/{StateMachine-30081029.cjs.js → StateMachine-fee05db8.cjs.js} +4 -3
  17. package/dist/{assign-ab9cc19e.esm.js → assign-37a2fc1e.esm.js} +1 -1
  18. package/dist/{assign-445527dc.development.esm.js → assign-541a432d.development.esm.js} +1 -1
  19. package/dist/{assign-9e730107.development.cjs.js → assign-577bb842.development.cjs.js} +1 -1
  20. package/dist/{assign-dea9f7c8.cjs.js → assign-592716a8.cjs.js} +1 -1
  21. package/dist/declarations/src/State.d.ts +5 -5
  22. package/dist/declarations/src/StateMachine.d.ts +10 -10
  23. package/dist/declarations/src/assert.d.ts +2 -4
  24. package/dist/declarations/src/setup.d.ts +13 -8
  25. package/dist/declarations/src/types.d.ts +1 -1
  26. package/dist/declarations/src/utils.d.ts +13 -0
  27. package/dist/{log-cbac1abc.development.cjs.js → log-3827c227.development.cjs.js} +2 -2
  28. package/dist/{log-ec8d4df4.cjs.js → log-9c9f917d.cjs.js} +2 -2
  29. package/dist/{log-2aa3642a.development.esm.js → log-b8ca474e.development.esm.js} +2 -2
  30. package/dist/{log-7cbae384.esm.js → log-ce14fc9a.esm.js} +2 -2
  31. package/dist/{raise-c096f887.development.esm.js → raise-13a60c49.development.esm.js} +61 -32
  32. package/dist/{raise-da5b247f.cjs.js → raise-577e4163.cjs.js} +55 -25
  33. package/dist/{raise-f777a9e8.development.cjs.js → raise-ed7c6f3d.development.cjs.js} +61 -31
  34. package/dist/{raise-9ad1c5c6.esm.js → raise-f5c7cb5b.esm.js} +55 -26
  35. package/dist/xstate.cjs.js +10 -6
  36. package/dist/xstate.development.cjs.js +10 -6
  37. package/dist/xstate.development.esm.js +14 -10
  38. package/dist/xstate.esm.js +14 -10
  39. package/dist/xstate.umd.min.js +1 -1
  40. package/dist/xstate.umd.min.js.map +1 -1
  41. package/graph/dist/xstate-graph.cjs.js +3 -3
  42. package/graph/dist/xstate-graph.development.cjs.js +3 -3
  43. package/graph/dist/xstate-graph.development.esm.js +3 -3
  44. package/graph/dist/xstate-graph.esm.js +3 -3
  45. package/graph/dist/xstate-graph.umd.min.js +1 -1
  46. package/graph/dist/xstate-graph.umd.min.js.map +1 -1
  47. package/guards/dist/xstate-guards.cjs.js +1 -1
  48. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  49. package/guards/dist/xstate-guards.development.esm.js +1 -1
  50. package/guards/dist/xstate-guards.esm.js +1 -1
  51. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  52. package/package.json +1 -1
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var guards_dist_xstateGuards = require('../../dist/raise-da5b247f.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-577e4163.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.cjs.js');
7
7
 
8
8
  /**
@@ -306,6 +306,8 @@ function fromCallback(callback) {
306
306
  status: 'stopped',
307
307
  error: undefined
308
308
  };
309
+ instanceStates.delete(actorScope.self);
310
+ callbackState.receivers?.clear();
309
311
  callbackState.dispose?.();
310
312
  return state;
311
313
  }
@@ -766,6 +768,7 @@ function fromPromise(promiseCreator) {
766
768
  case guards_dist_xstateGuards.XSTATE_STOP:
767
769
  {
768
770
  controllerMap.get(scope.self)?.abort();
771
+ controllerMap.delete(scope.self);
769
772
  return {
770
773
  ...state,
771
774
  status: 'stopped',
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var guards_dist_xstateGuards = require('../../dist/raise-f777a9e8.development.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-ed7c6f3d.development.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.development.cjs.js');
7
7
 
8
8
  /**
@@ -306,6 +306,8 @@ function fromCallback(callback) {
306
306
  status: 'stopped',
307
307
  error: undefined
308
308
  };
309
+ instanceStates.delete(actorScope.self);
310
+ callbackState.receivers?.clear();
309
311
  callbackState.dispose?.();
310
312
  return state;
311
313
  }
@@ -766,6 +768,7 @@ function fromPromise(promiseCreator) {
766
768
  case guards_dist_xstateGuards.XSTATE_STOP:
767
769
  {
768
770
  controllerMap.get(scope.self)?.abort();
771
+ controllerMap.delete(scope.self);
769
772
  return {
770
773
  ...state,
771
774
  status: 'stopped',
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, c as createActor } from '../../dist/raise-c096f887.development.esm.js';
1
+ import { X as XSTATE_STOP, c as createActor } from '../../dist/raise-13a60c49.development.esm.js';
2
2
  import '../../dev/dist/xstate-dev.development.esm.js';
3
3
 
4
4
  /**
@@ -302,6 +302,8 @@ function fromCallback(callback) {
302
302
  status: 'stopped',
303
303
  error: undefined
304
304
  };
305
+ instanceStates.delete(actorScope.self);
306
+ callbackState.receivers?.clear();
305
307
  callbackState.dispose?.();
306
308
  return state;
307
309
  }
@@ -762,6 +764,7 @@ function fromPromise(promiseCreator) {
762
764
  case XSTATE_STOP:
763
765
  {
764
766
  controllerMap.get(scope.self)?.abort();
767
+ controllerMap.delete(scope.self);
765
768
  return {
766
769
  ...state,
767
770
  status: 'stopped',
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, c as createActor } from '../../dist/raise-9ad1c5c6.esm.js';
1
+ import { X as XSTATE_STOP, c as createActor } from '../../dist/raise-f5c7cb5b.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
3
3
 
4
4
  /**
@@ -302,6 +302,8 @@ function fromCallback(callback) {
302
302
  status: 'stopped',
303
303
  error: undefined
304
304
  };
305
+ instanceStates.delete(actorScope.self);
306
+ callbackState.receivers?.clear();
305
307
  callbackState.dispose?.();
306
308
  return state;
307
309
  }
@@ -762,6 +764,7 @@ function fromPromise(promiseCreator) {
762
764
  case XSTATE_STOP:
763
765
  {
764
766
  controllerMap.get(scope.self)?.abort();
767
+ controllerMap.delete(scope.self);
765
768
  return {
766
769
  ...state,
767
770
  status: 'stopped',
@@ -1,2 +1,2 @@
1
- !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActors={})}(this,function(t){"use strict";class s{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 s={value:t,next:null};if(this._current)return this._last.next=s,void(this._last=s);this._current=s,this._last=s,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const e="xstate.stop";function i(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const r=t=>{const s=i();s&&s.register(t)};function o(t){setTimeout(()=>{throw t})}const n="function"==typeof Symbol&&Symbol.observable||"@@observable";function a(t,s,e){const i="object"==typeof t,r=i?t:void 0;return{next:(i?t.next:t)?.bind(r),error:(i?t.error:s)?.bind(r),complete:(i?t.complete:e)?.bind(r)}}function c(t,s){return`${t.sessionId}.${s}`}let h=0;let p=!1;let u=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const d={clock:{setTimeout:(t,s)=>setTimeout(t,s),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class l{constructor(t,e){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new s(this._process.bind(this)),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=u.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this.systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const i={...d,...e},{clock:r,logger:n,parent:l,syncSnapshot:_,id:v,systemId:f,inspect:y}=i;this.system=l?l.system:function(t,s){const e=new Map,i=new Map,r=new WeakMap,o=new Set,n={},{clock:p,logger:u}=s,d={schedule:(t,s,e,i,r=Math.random().toString(36).slice(2))=>{const o={source:t,target:s,event:e,delay:i,id:r,startedAt:Date.now()},a=c(t,r);l._snapshot._scheduledEvents[a]=o;const h=p.setTimeout(()=>{delete n[a],delete l._snapshot._scheduledEvents[a],l._relay(t,s,e)},i);n[a]=h},cancel:(t,s)=>{const e=c(t,s),i=n[e];delete n[e],delete l._snapshot._scheduledEvents[e],void 0!==i&&p.clearTimeout(i)},cancelAll:t=>{for(const s in l._snapshot._scheduledEvents){const e=l._snapshot._scheduledEvents[s];e.source===t&&d.cancel(t,e.id)}}},l={_snapshot:{_scheduledEvents:(s?.snapshot&&s.snapshot.scheduler)??{}},_bookId:()=>"x:"+h++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const s=r.get(t);void 0!==s&&(i.delete(s),r.delete(t))},get:t=>i.get(t),getAll:()=>Object.fromEntries(i.entries()),_set:(t,s)=>{const e=i.get(t);if(e&&e!==s)throw new Error(`Actor with system ID '${t}' already exists.`);i.set(t,s),r.set(s,t)},inspect:t=>{const s=a(t);return o.add(s),{unsubscribe(){o.delete(s)}}},_sendInspectionEvent:s=>{if(!o.size)return;const e={...s,rootId:t.sessionId};o.forEach(t=>t.next?.(e))},_relay:(t,s,e)=>{l._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:s,event:e}),s._send(e)},scheduler:d,getSnapshot:()=>({_scheduledEvents:{...l._snapshot._scheduledEvents}}),start:()=>{const t=l._snapshot._scheduledEvents;l._snapshot._scheduledEvents={};for(const s in t){const{source:e,target:i,event:r,delay:o,id:n}=t[s];d.schedule(e,i,r,o,n)}},_clock:p,_logger:u};return l}(this,{clock:r,logger:n}),y&&!l&&this.system.inspect(a(y)),this.sessionId=this.system._bookId(),this.id=v??this.sessionId,this.logger=e?.logger??this.system._logger,this.clock=e?.clock??this.system._clock,this._parent=l,this._syncSnapshot=_,this.options=i,this.src=i.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()},emit:t=>{const s=this.eventListeners.get(t.type),e=this.eventListeners.get("*");if(!s&&!e)return;const i=[...s?s.values():[],...e?e.values():[]];for(const s of i)try{s(t)}catch(t){o(t)}},actionExecutor:t=>{const s=()=>{if(this._actorScope.system._sendInspectionEvent({type:"@xstate.action",actorRef:this,action:{type:t.type,params:t.params}}),!t.exec)return;const s=p;try{p=!0,t.exec(t.info,t.params)}finally{p=s}};this._processingStatus===u.Running?s():this._deferred.push(s)}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),f&&(this.systemId=f,this.system._set(f,this)),this._initState(e?.snapshot??e?.state),f&&"active"!==this._snapshot.status&&this.system._unregister(this)}_initState(t){try{this._snapshot=t?this.logic.restoreSnapshot?this.logic.restoreSnapshot(t,this._actorScope):t:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t}}}update(t,s){let e;for(this._snapshot=t;e=this._deferred.shift();)try{e()}catch(s){this._deferred.length=0,this._snapshot={...t,status:"error",error:s}}switch(this._snapshot.status){case"active":for(const s of this.observers)try{s.next?.(t)}catch(t){o(t)}break;case"done":for(const s of this.observers)try{s.next?.(t)}catch(t){o(t)}this._stopProcedure(),this._complete(),this._doneEvent=(i=this.id,r=this._snapshot.output,{type:`xstate.done.actor.${i}`,output:r,actorId:i}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var i,r;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:s,snapshot:t})}subscribe(t,s,e){const i=a(t,s,e);if(this._processingStatus!==u.Stopped)this.observers.add(i);else switch(this._snapshot.status){case"done":try{i.complete?.()}catch(t){o(t)}break;case"error":{const t=this._snapshot.error;if(i.error)try{i.error(t)}catch(t){o(t)}else o(t);break}}return{unsubscribe:()=>{this.observers.delete(i)}}}on(t,s){let e=this.eventListeners.get(t);e||(e=new Set,this.eventListeners.set(t,e));const i=s.bind(void 0);return e.add(i),{unsubscribe:()=>{e.delete(i)}}}start(){if(this._processingStatus===u.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.systemId&&this.system._set(this.systemId,this),this._processingStatus=u.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._snapshot={...this._snapshot,status:"error",error:t},this._error(t),this}return this.update(this._snapshot,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let s,i;try{s=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){i={err:t}}if(i){const{err:t}=i;return this._snapshot={...this._snapshot,status:"error",error:t},void this._error(t)}this.update(s,t),t.type===e&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===u.Stopped?this:(this.mailbox.clear(),this._processingStatus===u.NotStarted?(this._processingStatus=u.Stopped,this):(this.mailbox.enqueue({type:e}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){o(t)}this.observers.clear()}_reportError(t){if(!this.observers.size)return void(this._parent||o(t));let s=!1;for(const e of this.observers){const i=e.error;s||=!i;try{i?.(t)}catch(t){o(t)}}this.observers.clear(),s&&o(t)}_error(t){var s;this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${s=this.id}`,error:t,actorId:s})}_stopProcedure(){return this._processingStatus!==u.Running||(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new s(this._process.bind(this)),this._processingStatus=u.Stopped,this.system._unregister(this)),this}_send(t){this._processingStatus!==u.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:r)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[n](){return this}getSnapshot(){return this._snapshot}}function _(t,s){return{config:t,transition:(s,e,i)=>({...s,context:t(s.context,e,i)}),getInitialSnapshot:(t,e)=>({status:"active",output:void 0,error:void 0,context:"function"==typeof s?s({input:e}):s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}const v=new WeakMap;const f="xstate.observable.next",y="xstate.observable.error",g="xstate.observable.complete";const b="xstate.promise.resolve",m="xstate.promise.reject",S=new WeakMap;const x=_(t=>{},void 0);t.createEmptyActor=function(){return function(t,...[s]){return new l(t,s)}(x)},t.fromCallback=function(t){return{config:t,start:(s,e)=>{const{self:i,system:r,emit:o}=e,n={receivers:void 0,dispose:void 0};v.set(i,n),n.dispose=t({input:s.input,system:r,self:i,sendBack:t=>{"stopped"!==i.getSnapshot().status&&i._parent&&r._relay(i,i._parent,t)},receive:t=>{n.receivers??=new Set,n.receivers.add(t)},emit:o})},transition:(t,s,i)=>{const r=v.get(i.self);return s.type===e?(t={...t,status:"stopped",error:void 0},r.dispose?.(),t):(r.receivers?.forEach(t=>t(s)),t)},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}},t.fromEventObservable=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case y:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case g:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(s,{self:e,system:i,emit:r})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e,emit:r}).subscribe({next:t=>{e._parent&&i._relay(e,e._parent,t)},error:t=>{i._relay(e,e,{type:y,data:t})},complete:()=>{i._relay(e,e,{type:g})}}))},getPersistedSnapshot:({_subscription:t,...s})=>s,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case f:return{...t,context:s.data};case y:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case g:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(s,{self:e,system:i,emit:r})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e,emit:r}).subscribe({next:t=>{i._relay(e,e,{type:f,data:t})},error:t=>{i._relay(e,e,{type:y,data:t})},complete:()=>{i._relay(e,e,{type:g})}}))},getPersistedSnapshot:({_subscription:t,...s})=>s,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,s,i)=>{if("active"!==t.status)return t;switch(s.type){case b:{const e=s.data;return{...t,status:"done",output:e,input:void 0}}case m:return{...t,status:"error",error:s.data,input:void 0};case e:return S.get(i.self)?.abort(),{...t,status:"stopped",input:void 0};default:return t}},start:(s,{self:e,system:i,emit:r})=>{if("active"!==s.status)return;const o=new AbortController;S.set(e,o);Promise.resolve(t({input:s.input,system:i,self:e,signal:o.signal,emit:r})).then(t=>{"active"===e.getSnapshot().status&&(S.delete(e),i._relay(e,e,{type:b,data:t}))},t=>{"active"===e.getSnapshot().status&&(S.delete(e),i._relay(e,e,{type:m,data:t}))})},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}},t.fromTransition=_,Object.defineProperty(t,"__esModule",{value:!0})});
1
+ !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActors={})}(this,function(t){"use strict";class s{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 s={value:t,next:null};if(this._current)return this._last.next=s,void(this._last=s);this._current=s,this._last=s,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const e="xstate.stop";function i(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const r=t=>{const s=i();s&&s.register(t)};function o(t){setTimeout(()=>{throw t})}const n="function"==typeof Symbol&&Symbol.observable||"@@observable";function a(t,s,e){const i="object"==typeof t,r=i?t:void 0;return{next:(i?t.next:t)?.bind(r),error:(i?t.error:s)?.bind(r),complete:(i?t.complete:e)?.bind(r)}}function c(t,s){return`${t.sessionId}.${s}`}let h=0;let p=!1;let u=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const d={clock:{setTimeout:(t,s)=>setTimeout(t,s),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class l{constructor(t,e){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new s(this._process.bind(this)),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=u.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this.systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const i={...d,...e},{clock:r,logger:n,parent:l,syncSnapshot:_,id:v,systemId:f,inspect:y}=i;this.system=l?l.system:function(t,s){const e=new Map,i=new Map,r=new WeakMap,o=new Set,n={},{clock:p,logger:u}=s,d={schedule:(t,s,e,i,r=Math.random().toString(36).slice(2))=>{const o={source:t,target:s,event:e,delay:i,id:r,startedAt:Date.now()},a=c(t,r);l._snapshot._scheduledEvents[a]=o;const h=p.setTimeout(()=>{delete n[a],delete l._snapshot._scheduledEvents[a],l._relay(t,s,e)},i);n[a]=h},cancel:(t,s)=>{const e=c(t,s),i=n[e];delete n[e],delete l._snapshot._scheduledEvents[e],void 0!==i&&p.clearTimeout(i)},cancelAll:t=>{for(const s in l._snapshot._scheduledEvents){const e=l._snapshot._scheduledEvents[s];e.source===t&&d.cancel(t,e.id)}}},l={_snapshot:{_scheduledEvents:(s?.snapshot&&s.snapshot.scheduler)??{}},_bookId:()=>"x:"+h++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const s=r.get(t);void 0!==s&&(i.delete(s),r.delete(t))},get:t=>i.get(t),getAll:()=>Object.fromEntries(i.entries()),_set:(t,s)=>{const e=i.get(t);if(e&&e!==s)throw new Error(`Actor with system ID '${t}' already exists.`);i.set(t,s),r.set(s,t)},inspect:t=>{const s=a(t);return o.add(s),{unsubscribe(){o.delete(s)}}},_sendInspectionEvent:s=>{if(!o.size)return;const e={...s,rootId:t.sessionId};o.forEach(t=>t.next?.(e))},_relay:(t,s,e)=>{l._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:s,event:e}),s._send(e)},scheduler:d,getSnapshot:()=>({_scheduledEvents:{...l._snapshot._scheduledEvents}}),start:()=>{const t=l._snapshot._scheduledEvents;l._snapshot._scheduledEvents={};for(const s in t){const{source:e,target:i,event:r,delay:o,id:n}=t[s];d.schedule(e,i,r,o,n)}},_clock:p,_logger:u};return l}(this,{clock:r,logger:n}),y&&!l&&this.system.inspect(a(y)),this.sessionId=this.system._bookId(),this.id=v??this.sessionId,this.logger=e?.logger??this.system._logger,this.clock=e?.clock??this.system._clock,this._parent=l,this._syncSnapshot=_,this.options=i,this.src=i.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()},emit:t=>{const s=this.eventListeners.get(t.type),e=this.eventListeners.get("*");if(!s&&!e)return;const i=[...s?s.values():[],...e?e.values():[]];for(const s of i)try{s(t)}catch(t){o(t)}},actionExecutor:t=>{const s=()=>{if(this._actorScope.system._sendInspectionEvent({type:"@xstate.action",actorRef:this,action:{type:t.type,params:t.params}}),!t.exec)return;const s=p;try{p=!0,t.exec(t.info,t.params)}finally{p=s}};this._processingStatus===u.Running?s():this._deferred.push(s)}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),f&&(this.systemId=f,this.system._set(f,this)),this._initState(e?.snapshot??e?.state),f&&"active"!==this._snapshot.status&&this.system._unregister(this)}_initState(t){try{this._snapshot=t?this.logic.restoreSnapshot?this.logic.restoreSnapshot(t,this._actorScope):t:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t}}}update(t,s){let e;for(this._snapshot=t;e=this._deferred.shift();)try{e()}catch(s){this._deferred.length=0,this._snapshot={...t,status:"error",error:s}}switch(this._snapshot.status){case"active":for(const s of this.observers)try{s.next?.(t)}catch(t){o(t)}break;case"done":for(const s of this.observers)try{s.next?.(t)}catch(t){o(t)}this._stopProcedure(),this._complete(),this._doneEvent=(i=this.id,r=this._snapshot.output,{type:`xstate.done.actor.${i}`,output:r,actorId:i}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var i,r;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:s,snapshot:t})}subscribe(t,s,e){const i=a(t,s,e);if(this._processingStatus!==u.Stopped)this.observers.add(i);else switch(this._snapshot.status){case"done":try{i.complete?.()}catch(t){o(t)}break;case"error":{const t=this._snapshot.error;if(i.error)try{i.error(t)}catch(t){o(t)}else o(t);break}}return{unsubscribe:()=>{this.observers.delete(i)}}}on(t,s){let e=this.eventListeners.get(t);e||(e=new Set,this.eventListeners.set(t,e));const i=s.bind(void 0);return e.add(i),{unsubscribe:()=>{e.delete(i)}}}start(){if(this._processingStatus===u.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.systemId&&this.system._set(this.systemId,this),this._processingStatus=u.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._snapshot={...this._snapshot,status:"error",error:t},this._error(t),this}return this.update(this._snapshot,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let s,i;try{s=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){i={err:t}}if(i){const{err:t}=i;return this._snapshot={...this._snapshot,status:"error",error:t},void this._error(t)}this.update(s,t),t.type===e&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===u.Stopped?this:(this.mailbox.clear(),this._processingStatus===u.NotStarted?(this._processingStatus=u.Stopped,this):(this.mailbox.enqueue({type:e}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){o(t)}this.observers.clear(),this.eventListeners.clear()}_reportError(t){if(!this.observers.size)return this._parent||o(t),void this.eventListeners.clear();let s=!1;for(const e of this.observers){const i=e.error;s||=!i;try{i?.(t)}catch(t){o(t)}}this.observers.clear(),this.eventListeners.clear(),s&&o(t)}_error(t){var s;this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${s=this.id}`,error:t,actorId:s})}_stopProcedure(){return this._processingStatus!==u.Running||(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new s(this._process.bind(this)),this._processingStatus=u.Stopped,this.system._unregister(this)),this}_send(t){this._processingStatus!==u.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:r)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[n](){return this}getSnapshot(){return this._snapshot}}function _(t,s){return{config:t,transition:(s,e,i)=>({...s,context:t(s.context,e,i)}),getInitialSnapshot:(t,e)=>({status:"active",output:void 0,error:void 0,context:"function"==typeof s?s({input:e}):s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}const v=new WeakMap;const f="xstate.observable.next",y="xstate.observable.error",g="xstate.observable.complete";const b="xstate.promise.resolve",m="xstate.promise.reject",S=new WeakMap;const x=_(t=>{},void 0);t.createEmptyActor=function(){return function(t,...[s]){return new l(t,s)}(x)},t.fromCallback=function(t){return{config:t,start:(s,e)=>{const{self:i,system:r,emit:o}=e,n={receivers:void 0,dispose:void 0};v.set(i,n),n.dispose=t({input:s.input,system:r,self:i,sendBack:t=>{"stopped"!==i.getSnapshot().status&&i._parent&&r._relay(i,i._parent,t)},receive:t=>{n.receivers??=new Set,n.receivers.add(t)},emit:o})},transition:(t,s,i)=>{const r=v.get(i.self);return s.type===e?(t={...t,status:"stopped",error:void 0},v.delete(i.self),r.receivers?.clear(),r.dispose?.(),t):(r.receivers?.forEach(t=>t(s)),t)},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}},t.fromEventObservable=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case y:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case g:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(s,{self:e,system:i,emit:r})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e,emit:r}).subscribe({next:t=>{e._parent&&i._relay(e,e._parent,t)},error:t=>{i._relay(e,e,{type:y,data:t})},complete:()=>{i._relay(e,e,{type:g})}}))},getPersistedSnapshot:({_subscription:t,...s})=>s,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case f:return{...t,context:s.data};case y:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case g:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(s,{self:e,system:i,emit:r})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e,emit:r}).subscribe({next:t=>{i._relay(e,e,{type:f,data:t})},error:t=>{i._relay(e,e,{type:y,data:t})},complete:()=>{i._relay(e,e,{type:g})}}))},getPersistedSnapshot:({_subscription:t,...s})=>s,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,s,i)=>{if("active"!==t.status)return t;switch(s.type){case b:{const e=s.data;return{...t,status:"done",output:e,input:void 0}}case m:return{...t,status:"error",error:s.data,input:void 0};case e:return S.get(i.self)?.abort(),S.delete(i.self),{...t,status:"stopped",input:void 0};default:return t}},start:(s,{self:e,system:i,emit:r})=>{if("active"!==s.status)return;const o=new AbortController;S.set(e,o);Promise.resolve(t({input:s.input,system:i,self:e,signal:o.signal,emit:r})).then(t=>{"active"===e.getSnapshot().status&&(S.delete(e),i._relay(e,e,{type:b,data:t}))},t=>{"active"===e.getSnapshot().status&&(S.delete(e),i._relay(e,e,{type:m,data:t}))})},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}},t.fromTransition=_,Object.defineProperty(t,"__esModule",{value:!0})});
2
2
  //# sourceMappingURL=xstate-actors.umd.min.js.map