xstate 5.13.1 → 5.14.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 +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +18 -15
  8. package/actors/dist/xstate-actors.development.cjs.js +18 -15
  9. package/actors/dist/xstate-actors.development.esm.js +18 -15
  10. package/actors/dist/xstate-actors.esm.js +18 -15
  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/declarations/src/actors/callback.d.ts +4 -3
  14. package/dist/declarations/src/actors/index.d.ts +1 -1
  15. package/dist/declarations/src/actors/observable.d.ts +9 -7
  16. package/dist/declarations/src/actors/promise.d.ts +5 -4
  17. package/dist/declarations/src/actors/transition.d.ts +1 -1
  18. package/dist/declarations/src/createActor.d.ts +2 -2
  19. package/dist/declarations/src/system.d.ts +2 -2
  20. package/dist/declarations/src/types.d.ts +16 -13
  21. package/dist/{log-d06dd00b.development.cjs.js → log-505687fd.development.cjs.js} +1 -1
  22. package/dist/{log-5b14d850.cjs.js → log-7ae0ddf8.cjs.js} +1 -1
  23. package/dist/{log-c447a931.esm.js → log-b87cb6bd.esm.js} +1 -1
  24. package/dist/{log-b3b03961.development.esm.js → log-c943e6aa.development.esm.js} +1 -1
  25. package/dist/{raise-bf7a8462.development.cjs.js → raise-0cd7e521.development.cjs.js} +138 -130
  26. package/dist/{raise-150d5679.development.esm.js → raise-0f400094.development.esm.js} +138 -130
  27. package/dist/{raise-f406edbd.esm.js → raise-4e39e875.esm.js} +138 -130
  28. package/dist/{raise-ff8990f7.cjs.js → raise-f79d2832.cjs.js} +138 -130
  29. package/dist/xstate.cjs.js +2 -2
  30. package/dist/xstate.development.cjs.js +2 -2
  31. package/dist/xstate.development.esm.js +4 -4
  32. package/dist/xstate.esm.js +4 -4
  33. package/dist/xstate.umd.min.js +1 -1
  34. package/dist/xstate.umd.min.js.map +1 -1
  35. package/guards/dist/xstate-guards.cjs.js +1 -1
  36. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  37. package/guards/dist/xstate-guards.development.esm.js +1 -1
  38. package/guards/dist/xstate-guards.esm.js +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-ff8990f7.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-f79d2832.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.cjs.js');
7
7
 
8
8
  /**
@@ -151,7 +151,8 @@ function fromCallback(invokeCallback) {
151
151
  start: (state, actorScope) => {
152
152
  const {
153
153
  self,
154
- system
154
+ system,
155
+ emit
155
156
  } = actorScope;
156
157
  const callbackState = {
157
158
  receivers: undefined,
@@ -173,7 +174,8 @@ function fromCallback(invokeCallback) {
173
174
  receive: listener => {
174
175
  callbackState.receivers ??= new Set();
175
176
  callbackState.receivers.add(listener);
176
- }
177
+ },
178
+ emit
177
179
  });
178
180
  },
179
181
  transition: (state, event, actorScope) => {
@@ -251,12 +253,7 @@ function fromObservable(observableCreator) {
251
253
  // TODO: add event types
252
254
  const logic = {
253
255
  config: observableCreator,
254
- transition: (snapshot, event, {
255
- self,
256
- id,
257
- defer,
258
- system
259
- }) => {
256
+ transition: (snapshot, event) => {
260
257
  if (snapshot.status !== 'active') {
261
258
  return snapshot;
262
259
  }
@@ -308,7 +305,8 @@ function fromObservable(observableCreator) {
308
305
  },
309
306
  start: (state, {
310
307
  self,
311
- system
308
+ system,
309
+ emit
312
310
  }) => {
313
311
  if (state.status === 'done') {
314
312
  // Do not restart a completed observable
@@ -317,7 +315,8 @@ function fromObservable(observableCreator) {
317
315
  state._subscription = observableCreator({
318
316
  input: state.input,
319
317
  system,
320
- self
318
+ self,
319
+ emit
321
320
  }).subscribe({
322
321
  next: value => {
323
322
  system._relay(self, self, {
@@ -442,7 +441,8 @@ function fromEventObservable(lazyObservable) {
442
441
  },
443
442
  start: (state, {
444
443
  self,
445
- system
444
+ system,
445
+ emit
446
446
  }) => {
447
447
  if (state.status === 'done') {
448
448
  // Do not restart a completed observable
@@ -451,7 +451,8 @@ function fromEventObservable(lazyObservable) {
451
451
  state._subscription = lazyObservable({
452
452
  input: state.input,
453
453
  system,
454
- self
454
+ self,
455
+ emit
455
456
  }).subscribe({
456
457
  next: value => {
457
458
  if (self._parent) {
@@ -571,7 +572,8 @@ function fromPromise(promiseCreator) {
571
572
  },
572
573
  start: (state, {
573
574
  self,
574
- system
575
+ system,
576
+ emit
575
577
  }) => {
576
578
  // TODO: determine how to allow customizing this so that promises
577
579
  // can be restarted if necessary
@@ -584,7 +586,8 @@ function fromPromise(promiseCreator) {
584
586
  input: state.input,
585
587
  system,
586
588
  self,
587
- signal: controller.signal
589
+ signal: controller.signal,
590
+ emit
588
591
  }));
589
592
  resolvedPromise.then(response => {
590
593
  if (self.getSnapshot().status !== 'active') {
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var guards_dist_xstateGuards = require('../../dist/raise-bf7a8462.development.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-0cd7e521.development.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.development.cjs.js');
7
7
 
8
8
  /**
@@ -151,7 +151,8 @@ function fromCallback(invokeCallback) {
151
151
  start: (state, actorScope) => {
152
152
  const {
153
153
  self,
154
- system
154
+ system,
155
+ emit
155
156
  } = actorScope;
156
157
  const callbackState = {
157
158
  receivers: undefined,
@@ -173,7 +174,8 @@ function fromCallback(invokeCallback) {
173
174
  receive: listener => {
174
175
  callbackState.receivers ??= new Set();
175
176
  callbackState.receivers.add(listener);
176
- }
177
+ },
178
+ emit
177
179
  });
178
180
  },
179
181
  transition: (state, event, actorScope) => {
@@ -251,12 +253,7 @@ function fromObservable(observableCreator) {
251
253
  // TODO: add event types
252
254
  const logic = {
253
255
  config: observableCreator,
254
- transition: (snapshot, event, {
255
- self,
256
- id,
257
- defer,
258
- system
259
- }) => {
256
+ transition: (snapshot, event) => {
260
257
  if (snapshot.status !== 'active') {
261
258
  return snapshot;
262
259
  }
@@ -308,7 +305,8 @@ function fromObservable(observableCreator) {
308
305
  },
309
306
  start: (state, {
310
307
  self,
311
- system
308
+ system,
309
+ emit
312
310
  }) => {
313
311
  if (state.status === 'done') {
314
312
  // Do not restart a completed observable
@@ -317,7 +315,8 @@ function fromObservable(observableCreator) {
317
315
  state._subscription = observableCreator({
318
316
  input: state.input,
319
317
  system,
320
- self
318
+ self,
319
+ emit
321
320
  }).subscribe({
322
321
  next: value => {
323
322
  system._relay(self, self, {
@@ -442,7 +441,8 @@ function fromEventObservable(lazyObservable) {
442
441
  },
443
442
  start: (state, {
444
443
  self,
445
- system
444
+ system,
445
+ emit
446
446
  }) => {
447
447
  if (state.status === 'done') {
448
448
  // Do not restart a completed observable
@@ -451,7 +451,8 @@ function fromEventObservable(lazyObservable) {
451
451
  state._subscription = lazyObservable({
452
452
  input: state.input,
453
453
  system,
454
- self
454
+ self,
455
+ emit
455
456
  }).subscribe({
456
457
  next: value => {
457
458
  if (self._parent) {
@@ -571,7 +572,8 @@ function fromPromise(promiseCreator) {
571
572
  },
572
573
  start: (state, {
573
574
  self,
574
- system
575
+ system,
576
+ emit
575
577
  }) => {
576
578
  // TODO: determine how to allow customizing this so that promises
577
579
  // can be restarted if necessary
@@ -584,7 +586,8 @@ function fromPromise(promiseCreator) {
584
586
  input: state.input,
585
587
  system,
586
588
  self,
587
- signal: controller.signal
589
+ signal: controller.signal,
590
+ emit
588
591
  }));
589
592
  resolvedPromise.then(response => {
590
593
  if (self.getSnapshot().status !== 'active') {
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-150d5679.development.esm.js';
1
+ import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-0f400094.development.esm.js';
2
2
  import '../../dev/dist/xstate-dev.development.esm.js';
3
3
 
4
4
  /**
@@ -147,7 +147,8 @@ function fromCallback(invokeCallback) {
147
147
  start: (state, actorScope) => {
148
148
  const {
149
149
  self,
150
- system
150
+ system,
151
+ emit
151
152
  } = actorScope;
152
153
  const callbackState = {
153
154
  receivers: undefined,
@@ -169,7 +170,8 @@ function fromCallback(invokeCallback) {
169
170
  receive: listener => {
170
171
  callbackState.receivers ??= new Set();
171
172
  callbackState.receivers.add(listener);
172
- }
173
+ },
174
+ emit
173
175
  });
174
176
  },
175
177
  transition: (state, event, actorScope) => {
@@ -247,12 +249,7 @@ function fromObservable(observableCreator) {
247
249
  // TODO: add event types
248
250
  const logic = {
249
251
  config: observableCreator,
250
- transition: (snapshot, event, {
251
- self,
252
- id,
253
- defer,
254
- system
255
- }) => {
252
+ transition: (snapshot, event) => {
256
253
  if (snapshot.status !== 'active') {
257
254
  return snapshot;
258
255
  }
@@ -304,7 +301,8 @@ function fromObservable(observableCreator) {
304
301
  },
305
302
  start: (state, {
306
303
  self,
307
- system
304
+ system,
305
+ emit
308
306
  }) => {
309
307
  if (state.status === 'done') {
310
308
  // Do not restart a completed observable
@@ -313,7 +311,8 @@ function fromObservable(observableCreator) {
313
311
  state._subscription = observableCreator({
314
312
  input: state.input,
315
313
  system,
316
- self
314
+ self,
315
+ emit
317
316
  }).subscribe({
318
317
  next: value => {
319
318
  system._relay(self, self, {
@@ -438,7 +437,8 @@ function fromEventObservable(lazyObservable) {
438
437
  },
439
438
  start: (state, {
440
439
  self,
441
- system
440
+ system,
441
+ emit
442
442
  }) => {
443
443
  if (state.status === 'done') {
444
444
  // Do not restart a completed observable
@@ -447,7 +447,8 @@ function fromEventObservable(lazyObservable) {
447
447
  state._subscription = lazyObservable({
448
448
  input: state.input,
449
449
  system,
450
- self
450
+ self,
451
+ emit
451
452
  }).subscribe({
452
453
  next: value => {
453
454
  if (self._parent) {
@@ -567,7 +568,8 @@ function fromPromise(promiseCreator) {
567
568
  },
568
569
  start: (state, {
569
570
  self,
570
- system
571
+ system,
572
+ emit
571
573
  }) => {
572
574
  // TODO: determine how to allow customizing this so that promises
573
575
  // can be restarted if necessary
@@ -580,7 +582,8 @@ function fromPromise(promiseCreator) {
580
582
  input: state.input,
581
583
  system,
582
584
  self,
583
- signal: controller.signal
585
+ signal: controller.signal,
586
+ emit
584
587
  }));
585
588
  resolvedPromise.then(response => {
586
589
  if (self.getSnapshot().status !== 'active') {
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-f406edbd.esm.js';
1
+ import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-4e39e875.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
3
3
 
4
4
  /**
@@ -147,7 +147,8 @@ function fromCallback(invokeCallback) {
147
147
  start: (state, actorScope) => {
148
148
  const {
149
149
  self,
150
- system
150
+ system,
151
+ emit
151
152
  } = actorScope;
152
153
  const callbackState = {
153
154
  receivers: undefined,
@@ -169,7 +170,8 @@ function fromCallback(invokeCallback) {
169
170
  receive: listener => {
170
171
  callbackState.receivers ??= new Set();
171
172
  callbackState.receivers.add(listener);
172
- }
173
+ },
174
+ emit
173
175
  });
174
176
  },
175
177
  transition: (state, event, actorScope) => {
@@ -247,12 +249,7 @@ function fromObservable(observableCreator) {
247
249
  // TODO: add event types
248
250
  const logic = {
249
251
  config: observableCreator,
250
- transition: (snapshot, event, {
251
- self,
252
- id,
253
- defer,
254
- system
255
- }) => {
252
+ transition: (snapshot, event) => {
256
253
  if (snapshot.status !== 'active') {
257
254
  return snapshot;
258
255
  }
@@ -304,7 +301,8 @@ function fromObservable(observableCreator) {
304
301
  },
305
302
  start: (state, {
306
303
  self,
307
- system
304
+ system,
305
+ emit
308
306
  }) => {
309
307
  if (state.status === 'done') {
310
308
  // Do not restart a completed observable
@@ -313,7 +311,8 @@ function fromObservable(observableCreator) {
313
311
  state._subscription = observableCreator({
314
312
  input: state.input,
315
313
  system,
316
- self
314
+ self,
315
+ emit
317
316
  }).subscribe({
318
317
  next: value => {
319
318
  system._relay(self, self, {
@@ -438,7 +437,8 @@ function fromEventObservable(lazyObservable) {
438
437
  },
439
438
  start: (state, {
440
439
  self,
441
- system
440
+ system,
441
+ emit
442
442
  }) => {
443
443
  if (state.status === 'done') {
444
444
  // Do not restart a completed observable
@@ -447,7 +447,8 @@ function fromEventObservable(lazyObservable) {
447
447
  state._subscription = lazyObservable({
448
448
  input: state.input,
449
449
  system,
450
- self
450
+ self,
451
+ emit
451
452
  }).subscribe({
452
453
  next: value => {
453
454
  if (self._parent) {
@@ -567,7 +568,8 @@ function fromPromise(promiseCreator) {
567
568
  },
568
569
  start: (state, {
569
570
  self,
570
- system
571
+ system,
572
+ emit
571
573
  }) => {
572
574
  // TODO: determine how to allow customizing this so that promises
573
575
  // can be restarted if necessary
@@ -580,7 +582,8 @@ function fromPromise(promiseCreator) {
580
582
  input: state.input,
581
583
  system,
582
584
  self,
583
- signal: controller.signal
585
+ signal: controller.signal,
586
+ emit
584
587
  }));
585
588
  resolvedPromise.then(response => {
586
589
  if (self.getSnapshot().status !== 'active') {
@@ -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),e=this.eventListeners.get("*");if(!s&&!e)return;const i=new Set([...s?s.values():[],...e?e.values():[]]);for(const s of Array.from(i))s(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})}));
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 h(t,s){return`${t.sessionId}.${s}`}let c=0;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:r,logger:o,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,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=h(t,r);l._snapshot._scheduledEvents[a]=o;const c=p.setTimeout((()=>{delete n[a],delete l._snapshot._scheduledEvents[a],l._relay(t,s,e)}),i);n[a]=c},cancel:(t,s)=>{const e=h(t,s),i=n[e];delete n[e],delete l._snapshot._scheduledEvents[e],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:"+c++,_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),_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:o}),v&&!n&&this.system.inspect(a(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),e=this.eventListeners.get("*");if(!s&&!e)return;const i=new Set([...s?s.values():[],...e?e.values():[]]);for(const s of Array.from(i))s(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){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!==p.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===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){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!==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: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 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:r,emit:o}=e,n={receivers:void 0,dispose:void 0};_.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=_.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 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,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: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)=>{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,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: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,emit:r})=>{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,emit:r})).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