xstate 5.11.0 → 5.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  5. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  6. package/actors/dist/xstate-actors.cjs.js +18 -8
  7. package/actors/dist/xstate-actors.development.cjs.js +18 -8
  8. package/actors/dist/xstate-actors.development.esm.js +18 -8
  9. package/actors/dist/xstate-actors.esm.js +18 -8
  10. package/actors/dist/xstate-actors.umd.min.js +1 -1
  11. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  12. package/dist/declarations/src/State.d.ts +12 -11
  13. package/dist/declarations/src/StateMachine.d.ts +16 -13
  14. package/dist/declarations/src/StateNode.d.ts +5 -1
  15. package/dist/declarations/src/actors/promise.d.ts +1 -44
  16. package/dist/declarations/src/createMachine.d.ts +5 -4
  17. package/dist/declarations/src/setup.d.ts +4 -4
  18. package/dist/declarations/src/stateUtils.d.ts +5 -4
  19. package/dist/declarations/src/types.d.ts +76 -54
  20. package/dist/{log-6380a394.development.cjs.js → log-12c20f1f.development.cjs.js} +10 -1
  21. package/dist/{log-11d3f771.cjs.js → log-5f8c5877.cjs.js} +1 -1
  22. package/dist/{log-cd22d72c.esm.js → log-86c89339.esm.js} +1 -1
  23. package/dist/{log-f64de12b.development.esm.js → log-bbab35a7.development.esm.js} +10 -1
  24. package/dist/{raise-040ba012.esm.js → raise-29904799.esm.js} +7 -1
  25. package/dist/{raise-03a786f4.development.esm.js → raise-2b7e2072.development.esm.js} +11 -2
  26. package/dist/{raise-52a00e8f.development.cjs.js → raise-c417eb4e.development.cjs.js} +10 -1
  27. package/dist/{raise-40b1a1f5.cjs.js → raise-f5b7c8e2.cjs.js} +7 -1
  28. package/dist/xstate.cjs.js +2 -2
  29. package/dist/xstate.development.cjs.js +2 -2
  30. package/dist/xstate.development.esm.js +4 -4
  31. package/dist/xstate.esm.js +4 -4
  32. package/dist/xstate.umd.min.js +1 -1
  33. package/dist/xstate.umd.min.js.map +1 -1
  34. package/guards/dist/xstate-guards.cjs.js +1 -1
  35. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  36. package/guards/dist/xstate-guards.development.esm.js +1 -1
  37. package/guards/dist/xstate-guards.esm.js +1 -1
  38. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  39. 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-40b1a1f5.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-f5b7c8e2.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.cjs.js');
7
7
 
8
8
  /**
@@ -529,10 +529,12 @@ const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
529
529
  * // }
530
530
  * ```
531
531
  */
532
+
533
+ const controllerMap = new WeakMap();
532
534
  function fromPromise(promiseCreator) {
533
535
  const logic = {
534
536
  config: promiseCreator,
535
- transition: (state, event) => {
537
+ transition: (state, event, scope) => {
536
538
  if (state.status !== 'active') {
537
539
  return state;
538
540
  }
@@ -555,11 +557,14 @@ function fromPromise(promiseCreator) {
555
557
  input: undefined
556
558
  };
557
559
  case guards_dist_xstateGuards.XSTATE_STOP:
558
- return {
559
- ...state,
560
- status: 'stopped',
561
- input: undefined
562
- };
560
+ {
561
+ controllerMap.get(scope.self)?.abort();
562
+ return {
563
+ ...state,
564
+ status: 'stopped',
565
+ input: undefined
566
+ };
567
+ }
563
568
  default:
564
569
  return state;
565
570
  }
@@ -573,15 +578,19 @@ function fromPromise(promiseCreator) {
573
578
  if (state.status !== 'active') {
574
579
  return;
575
580
  }
581
+ const controller = new AbortController();
582
+ controllerMap.set(self, controller);
576
583
  const resolvedPromise = Promise.resolve(promiseCreator({
577
584
  input: state.input,
578
585
  system,
579
- self
586
+ self,
587
+ signal: controller.signal
580
588
  }));
581
589
  resolvedPromise.then(response => {
582
590
  if (self.getSnapshot().status !== 'active') {
583
591
  return;
584
592
  }
593
+ controllerMap.delete(self);
585
594
  system._relay(self, self, {
586
595
  type: XSTATE_PROMISE_RESOLVE,
587
596
  data: response
@@ -590,6 +599,7 @@ function fromPromise(promiseCreator) {
590
599
  if (self.getSnapshot().status !== 'active') {
591
600
  return;
592
601
  }
602
+ controllerMap.delete(self);
593
603
  system._relay(self, self, {
594
604
  type: XSTATE_PROMISE_REJECT,
595
605
  data: errorData
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var guards_dist_xstateGuards = require('../../dist/raise-52a00e8f.development.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-c417eb4e.development.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.development.cjs.js');
7
7
 
8
8
  /**
@@ -529,10 +529,12 @@ const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
529
529
  * // }
530
530
  * ```
531
531
  */
532
+
533
+ const controllerMap = new WeakMap();
532
534
  function fromPromise(promiseCreator) {
533
535
  const logic = {
534
536
  config: promiseCreator,
535
- transition: (state, event) => {
537
+ transition: (state, event, scope) => {
536
538
  if (state.status !== 'active') {
537
539
  return state;
538
540
  }
@@ -555,11 +557,14 @@ function fromPromise(promiseCreator) {
555
557
  input: undefined
556
558
  };
557
559
  case guards_dist_xstateGuards.XSTATE_STOP:
558
- return {
559
- ...state,
560
- status: 'stopped',
561
- input: undefined
562
- };
560
+ {
561
+ controllerMap.get(scope.self)?.abort();
562
+ return {
563
+ ...state,
564
+ status: 'stopped',
565
+ input: undefined
566
+ };
567
+ }
563
568
  default:
564
569
  return state;
565
570
  }
@@ -573,15 +578,19 @@ function fromPromise(promiseCreator) {
573
578
  if (state.status !== 'active') {
574
579
  return;
575
580
  }
581
+ const controller = new AbortController();
582
+ controllerMap.set(self, controller);
576
583
  const resolvedPromise = Promise.resolve(promiseCreator({
577
584
  input: state.input,
578
585
  system,
579
- self
586
+ self,
587
+ signal: controller.signal
580
588
  }));
581
589
  resolvedPromise.then(response => {
582
590
  if (self.getSnapshot().status !== 'active') {
583
591
  return;
584
592
  }
593
+ controllerMap.delete(self);
585
594
  system._relay(self, self, {
586
595
  type: XSTATE_PROMISE_RESOLVE,
587
596
  data: response
@@ -590,6 +599,7 @@ function fromPromise(promiseCreator) {
590
599
  if (self.getSnapshot().status !== 'active') {
591
600
  return;
592
601
  }
602
+ controllerMap.delete(self);
593
603
  system._relay(self, self, {
594
604
  type: XSTATE_PROMISE_REJECT,
595
605
  data: errorData
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-03a786f4.development.esm.js';
1
+ import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-2b7e2072.development.esm.js';
2
2
  import '../../dev/dist/xstate-dev.development.esm.js';
3
3
 
4
4
  /**
@@ -525,10 +525,12 @@ const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
525
525
  * // }
526
526
  * ```
527
527
  */
528
+
529
+ const controllerMap = new WeakMap();
528
530
  function fromPromise(promiseCreator) {
529
531
  const logic = {
530
532
  config: promiseCreator,
531
- transition: (state, event) => {
533
+ transition: (state, event, scope) => {
532
534
  if (state.status !== 'active') {
533
535
  return state;
534
536
  }
@@ -551,11 +553,14 @@ function fromPromise(promiseCreator) {
551
553
  input: undefined
552
554
  };
553
555
  case XSTATE_STOP:
554
- return {
555
- ...state,
556
- status: 'stopped',
557
- input: undefined
558
- };
556
+ {
557
+ controllerMap.get(scope.self)?.abort();
558
+ return {
559
+ ...state,
560
+ status: 'stopped',
561
+ input: undefined
562
+ };
563
+ }
559
564
  default:
560
565
  return state;
561
566
  }
@@ -569,15 +574,19 @@ function fromPromise(promiseCreator) {
569
574
  if (state.status !== 'active') {
570
575
  return;
571
576
  }
577
+ const controller = new AbortController();
578
+ controllerMap.set(self, controller);
572
579
  const resolvedPromise = Promise.resolve(promiseCreator({
573
580
  input: state.input,
574
581
  system,
575
- self
582
+ self,
583
+ signal: controller.signal
576
584
  }));
577
585
  resolvedPromise.then(response => {
578
586
  if (self.getSnapshot().status !== 'active') {
579
587
  return;
580
588
  }
589
+ controllerMap.delete(self);
581
590
  system._relay(self, self, {
582
591
  type: XSTATE_PROMISE_RESOLVE,
583
592
  data: response
@@ -586,6 +595,7 @@ function fromPromise(promiseCreator) {
586
595
  if (self.getSnapshot().status !== 'active') {
587
596
  return;
588
597
  }
598
+ controllerMap.delete(self);
589
599
  system._relay(self, self, {
590
600
  type: XSTATE_PROMISE_REJECT,
591
601
  data: errorData
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-040ba012.esm.js';
1
+ import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-29904799.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
3
3
 
4
4
  /**
@@ -525,10 +525,12 @@ const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
525
525
  * // }
526
526
  * ```
527
527
  */
528
+
529
+ const controllerMap = new WeakMap();
528
530
  function fromPromise(promiseCreator) {
529
531
  const logic = {
530
532
  config: promiseCreator,
531
- transition: (state, event) => {
533
+ transition: (state, event, scope) => {
532
534
  if (state.status !== 'active') {
533
535
  return state;
534
536
  }
@@ -551,11 +553,14 @@ function fromPromise(promiseCreator) {
551
553
  input: undefined
552
554
  };
553
555
  case XSTATE_STOP:
554
- return {
555
- ...state,
556
- status: 'stopped',
557
- input: undefined
558
- };
556
+ {
557
+ controllerMap.get(scope.self)?.abort();
558
+ return {
559
+ ...state,
560
+ status: 'stopped',
561
+ input: undefined
562
+ };
563
+ }
559
564
  default:
560
565
  return state;
561
566
  }
@@ -569,15 +574,19 @@ function fromPromise(promiseCreator) {
569
574
  if (state.status !== 'active') {
570
575
  return;
571
576
  }
577
+ const controller = new AbortController();
578
+ controllerMap.set(self, controller);
572
579
  const resolvedPromise = Promise.resolve(promiseCreator({
573
580
  input: state.input,
574
581
  system,
575
- self
582
+ self,
583
+ signal: controller.signal
576
584
  }));
577
585
  resolvedPromise.then(response => {
578
586
  if (self.getSnapshot().status !== 'active') {
579
587
  return;
580
588
  }
589
+ controllerMap.delete(self);
581
590
  system._relay(self, self, {
582
591
  type: XSTATE_PROMISE_RESOLVE,
583
592
  data: response
@@ -586,6 +595,7 @@ function fromPromise(promiseCreator) {
586
595
  if (self.getSnapshot().status !== 'active') {
587
596
  return;
588
597
  }
598
+ controllerMap.delete(self);
589
599
  system._relay(self, self, {
590
600
  type: XSTATE_PROMISE_REJECT,
591
601
  data: errorData
@@ -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 o=t=>{const s=i();s&&s.register(t)};function r(t){setTimeout((()=>{throw t}))}const n="function"==typeof Symbol&&Symbol.observable||"@@observable";function a(t,s){return`${t.sessionId}.${s}`}let h=0;function c(t,s,e){const i="object"==typeof t,o=i?t:void 0;return{next:(i?t.next:t)?.bind(o),error:(i?t.error:s)?.bind(o),complete:(i?t.complete:e)?.bind(o)}}let p=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const u={clock:{setTimeout:(t,s)=>setTimeout(t,s),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class d{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=p.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={...u,...e},{clock:o,logger:r,parent:n,syncSnapshot:d,id:l,systemId:_,inspect:v}=i;this.system=n?n.system:function(t,s){const e=new Map,i=new Map,o=new WeakMap,r=new Set,n={},{clock:c,logger:p}=s,u={schedule:(t,s,e,i,o=Math.random().toString(36).slice(2))=>{const r={source:t,target:s,event:e,delay:i,id:o,startedAt:Date.now()},h=a(t,o);d._snapshot._scheduledEvents[h]=r;const p=c.setTimeout((()=>{delete n[h],delete d._snapshot._scheduledEvents[h],d._relay(t,s,e)}),i);n[h]=p},cancel:(t,s)=>{const e=a(t,s),i=n[e];delete n[e],delete d._snapshot._scheduledEvents[e],c.clearTimeout(i)},cancelAll:t=>{for(const s in d._snapshot._scheduledEvents){const e=d._snapshot._scheduledEvents[s];e.source===t&&u.cancel(t,e.id)}}},d={_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=o.get(t);void 0!==s&&(i.delete(s),o.delete(t))},get:t=>i.get(t),_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),o.set(s,t)},inspect:t=>{r.add(t)},_sendInspectionEvent:s=>{if(!r.size)return;const e={...s,rootId:t.sessionId};r.forEach((t=>t.next?.(e)))},_relay:(t,s,e)=>{d._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:s,event:e}),s._send(e)},scheduler:u,getSnapshot:()=>({_scheduledEvents:{...d._snapshot._scheduledEvents}}),start:()=>{const t=d._snapshot._scheduledEvents;d._snapshot._scheduledEvents={};for(const s in t){const{source:e,target:i,event:o,delay:r,id:n}=t[s];u.schedule(e,i,o,r,n)}},_clock:c,_logger:p};return d}(this,{clock:o,logger:r}),v&&!n&&this.system.inspect(c(v)),this.sessionId=this.system._bookId(),this.id=l??this.sessionId,this.logger=e?.logger??this.system._logger,this.clock=e?.clock??this.system._clock,this._parent=n,this._syncSnapshot=d,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);if(s)for(const e of Array.from(s))e(t)}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),_&&(this._systemId=_,this.system._set(_,this)),this._initState(e?.snapshot??e?.state),_&&"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){r(t)}break;case"done":for(const s of this.observers)try{s.next?.(t)}catch(t){r(t)}this._stopProcedure(),this._complete(),this._doneEvent=(i=this.id,o=this._snapshot.output,{type:`xstate.done.actor.${i}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var i,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:s,snapshot:t})}subscribe(t,s,e){const i=c(t,s,e);if(this._processingStatus!==p.Stopped)this.observers.add(i);else switch(this._snapshot.status){case"done":try{i.complete?.()}catch(t){r(t)}break;case"error":{const t=this._snapshot.error;if(i.error)try{i.error(t)}catch(t){r(t)}else r(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===p.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=p.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===p.Stopped?this:(this.mailbox.clear(),this._processingStatus===p.NotStarted?(this._processingStatus=p.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){r(t)}this.observers.clear()}_reportError(t){if(!this.observers.size)return void(this._parent||r(t));let s=!1;for(const e of this.observers){const i=e.error;s||=!i;try{i?.(t)}catch(t){r(t)}}this.observers.clear(),s&&r(t)}_error(t){this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${this.id}`,error:t})}_stopProcedure(){return this._processingStatus!==p.Running||(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new s(this._process.bind(this)),this._processingStatus=p.Stopped,this.system._unregister(this)),this}_send(t){this._processingStatus!==p.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:o)(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 l(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 _=new WeakMap;const v="xstate.observable.next",f="xstate.observable.error",y="xstate.observable.complete";const g="xstate.promise.resolve",b="xstate.promise.reject";const m=l((t=>{}),void 0);t.createEmptyActor=function(){return function(t,...[s]){return new d(t,s)}(m)},t.fromCallback=function(t){return{config:t,start:(s,e)=>{const{self:i,system:o}=e,r={receivers:void 0,dispose:void 0};_.set(i,r),r.dispose=t({input:s.input,system:o,self:i,sendBack:t=>{"stopped"!==i.getSnapshot().status&&i._parent&&o._relay(i,i._parent,t)},receive:t=>{r.receivers??=new Set,r.receivers.add(t)}})},transition:(t,s,i)=>{const o=_.get(i.self);return s.type===e?(t={...t,status:"stopped",error:void 0},o.dispose?.(),t):(o.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 f:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case y: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})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e}).subscribe({next:t=>{e._parent&&i._relay(e,e._parent,t)},error:t=>{i._relay(e,e,{type:f,data:t})},complete:()=>{i._relay(e,e,{type:y})}}))},getPersistedSnapshot:({_subscription:t,...s})=>s,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){return{config:t,transition:(t,s,{self:i,id:o,defer:r,system:n})=>{if("active"!==t.status)return t;switch(s.type){case v:return{...t,context:s.data};case f:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case y: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})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e}).subscribe({next:t=>{i._relay(e,e,{type:v,data:t})},error:t=>{i._relay(e,e,{type:f,data:t})},complete:()=>{i._relay(e,e,{type:y})}}))},getPersistedSnapshot:({_subscription:t,...s})=>s,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case g:{const e=s.data;return{...t,status:"done",output:e,input:void 0}}case b:return{...t,status:"error",error:s.data,input:void 0};case e:return{...t,status:"stopped",input:void 0};default:return t}},start:(s,{self:e,system:i})=>{if("active"!==s.status)return;Promise.resolve(t({input:s.input,system:i,self:e})).then((t=>{"active"===e.getSnapshot().status&&i._relay(e,e,{type:g,data:t})}),(t=>{"active"===e.getSnapshot().status&&i._relay(e,e,{type:b,data:t})}))},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}},t.fromTransition=l,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 o=t=>{const s=i();s&&s.register(t)};function r(t){setTimeout((()=>{throw t}))}const n="function"==typeof Symbol&&Symbol.observable||"@@observable";function a(t,s){return`${t.sessionId}.${s}`}let h=0;function c(t,s,e){const i="object"==typeof t,o=i?t:void 0;return{next:(i?t.next:t)?.bind(o),error:(i?t.error:s)?.bind(o),complete:(i?t.complete:e)?.bind(o)}}let p=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const u={clock:{setTimeout:(t,s)=>setTimeout(t,s),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class d{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=p.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={...u,...e},{clock:o,logger:r,parent:n,syncSnapshot:d,id:l,systemId:_,inspect:v}=i;this.system=n?n.system:function(t,s){const e=new Map,i=new Map,o=new WeakMap,r=new Set,n={},{clock:c,logger:p}=s,u={schedule:(t,s,e,i,o=Math.random().toString(36).slice(2))=>{const r={source:t,target:s,event:e,delay:i,id:o,startedAt:Date.now()},h=a(t,o);d._snapshot._scheduledEvents[h]=r;const p=c.setTimeout((()=>{delete n[h],delete d._snapshot._scheduledEvents[h],d._relay(t,s,e)}),i);n[h]=p},cancel:(t,s)=>{const e=a(t,s),i=n[e];delete n[e],delete d._snapshot._scheduledEvents[e],c.clearTimeout(i)},cancelAll:t=>{for(const s in d._snapshot._scheduledEvents){const e=d._snapshot._scheduledEvents[s];e.source===t&&u.cancel(t,e.id)}}},d={_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=o.get(t);void 0!==s&&(i.delete(s),o.delete(t))},get:t=>i.get(t),_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),o.set(s,t)},inspect:t=>{r.add(t)},_sendInspectionEvent:s=>{if(!r.size)return;const e={...s,rootId:t.sessionId};r.forEach((t=>t.next?.(e)))},_relay:(t,s,e)=>{d._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:s,event:e}),s._send(e)},scheduler:u,getSnapshot:()=>({_scheduledEvents:{...d._snapshot._scheduledEvents}}),start:()=>{const t=d._snapshot._scheduledEvents;d._snapshot._scheduledEvents={};for(const s in t){const{source:e,target:i,event:o,delay:r,id:n}=t[s];u.schedule(e,i,o,r,n)}},_clock:c,_logger:p};return d}(this,{clock:o,logger:r}),v&&!n&&this.system.inspect(c(v)),this.sessionId=this.system._bookId(),this.id=l??this.sessionId,this.logger=e?.logger??this.system._logger,this.clock=e?.clock??this.system._clock,this._parent=n,this._syncSnapshot=d,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);if(s)for(const e of Array.from(s))e(t)}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),_&&(this._systemId=_,this.system._set(_,this)),this._initState(e?.snapshot??e?.state),_&&"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){r(t)}break;case"done":for(const s of this.observers)try{s.next?.(t)}catch(t){r(t)}this._stopProcedure(),this._complete(),this._doneEvent=(i=this.id,o=this._snapshot.output,{type:`xstate.done.actor.${i}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var i,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:s,snapshot:t})}subscribe(t,s,e){const i=c(t,s,e);if(this._processingStatus!==p.Stopped)this.observers.add(i);else switch(this._snapshot.status){case"done":try{i.complete?.()}catch(t){r(t)}break;case"error":{const t=this._snapshot.error;if(i.error)try{i.error(t)}catch(t){r(t)}else r(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===p.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=p.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===p.Stopped?this:(this.mailbox.clear(),this._processingStatus===p.NotStarted?(this._processingStatus=p.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){r(t)}this.observers.clear()}_reportError(t){if(!this.observers.size)return void(this._parent||r(t));let s=!1;for(const e of this.observers){const i=e.error;s||=!i;try{i?.(t)}catch(t){r(t)}}this.observers.clear(),s&&r(t)}_error(t){this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${this.id}`,error:t})}_stopProcedure(){return this._processingStatus!==p.Running||(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new s(this._process.bind(this)),this._processingStatus=p.Stopped,this.system._unregister(this)),this}_send(t){this._processingStatus!==p.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:o)(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 l(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 _=new WeakMap;const v="xstate.observable.next",f="xstate.observable.error",y="xstate.observable.complete";const g="xstate.promise.resolve",b="xstate.promise.reject",m=new WeakMap;const S=l((t=>{}),void 0);t.createEmptyActor=function(){return function(t,...[s]){return new d(t,s)}(S)},t.fromCallback=function(t){return{config:t,start:(s,e)=>{const{self:i,system:o}=e,r={receivers:void 0,dispose:void 0};_.set(i,r),r.dispose=t({input:s.input,system:o,self:i,sendBack:t=>{"stopped"!==i.getSnapshot().status&&i._parent&&o._relay(i,i._parent,t)},receive:t=>{r.receivers??=new Set,r.receivers.add(t)}})},transition:(t,s,i)=>{const o=_.get(i.self);return s.type===e?(t={...t,status:"stopped",error:void 0},o.dispose?.(),t):(o.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 f:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case y: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})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e}).subscribe({next:t=>{e._parent&&i._relay(e,e._parent,t)},error:t=>{i._relay(e,e,{type:f,data:t})},complete:()=>{i._relay(e,e,{type:y})}}))},getPersistedSnapshot:({_subscription:t,...s})=>s,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){return{config:t,transition:(t,s,{self:i,id:o,defer:r,system:n})=>{if("active"!==t.status)return t;switch(s.type){case v:return{...t,context:s.data};case f:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case y: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})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e}).subscribe({next:t=>{i._relay(e,e,{type:v,data:t})},error:t=>{i._relay(e,e,{type:f,data:t})},complete:()=>{i._relay(e,e,{type:y})}}))},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 g:{const e=s.data;return{...t,status:"done",output:e,input:void 0}}case b:return{...t,status:"error",error:s.data,input:void 0};case e:return m.get(i.self)?.abort(),{...t,status:"stopped",input:void 0};default:return t}},start:(s,{self:e,system:i})=>{if("active"!==s.status)return;const o=new AbortController;m.set(e,o);Promise.resolve(t({input:s.input,system:i,self:e,signal:o.signal})).then((t=>{"active"===e.getSnapshot().status&&(m.delete(e),i._relay(e,e,{type:g,data:t}))}),(t=>{"active"===e.getSnapshot().status&&(m.delete(e),i._relay(e,e,{type:b,data:t}))}))},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}},t.fromTransition=l,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actors.umd.min.js.map