xstate 5.0.0-beta.42 → 5.0.0-beta.44

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 (47) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -3
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -3
  3. package/actions/dist/xstate-actions.development.esm.js +2 -3
  4. package/actions/dist/xstate-actions.esm.js +2 -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 +50 -6
  8. package/actors/dist/xstate-actors.development.cjs.js +50 -6
  9. package/actors/dist/xstate-actors.development.esm.js +45 -1
  10. package/actors/dist/xstate-actors.esm.js +45 -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/declarations/src/State.d.ts +8 -12
  14. package/dist/declarations/src/actions/spawn.d.ts +11 -16
  15. package/dist/declarations/src/actors/promise.d.ts +53 -0
  16. package/dist/declarations/src/guards.d.ts +2 -2
  17. package/dist/declarations/src/index.d.ts +1 -1
  18. package/dist/declarations/src/spawn.d.ts +9 -13
  19. package/dist/declarations/src/stateUtils.d.ts +4 -4
  20. package/dist/declarations/src/types.d.ts +56 -22
  21. package/dist/declarations/src/utils.d.ts +2 -10
  22. package/dist/{raise-6b64c553.cjs.js → raise-348cc74e.development.esm.js} +1007 -116
  23. package/dist/{raise-51ae36e5.development.esm.js → raise-5854eaca.esm.js} +925 -101
  24. package/dist/{raise-8f482ce9.development.cjs.js → raise-ed700d14.development.cjs.js} +986 -87
  25. package/dist/{raise-d2084327.esm.js → raise-fb6f017b.cjs.js} +972 -54
  26. package/dist/{send-cc8f864e.development.cjs.js → send-00466e37.development.cjs.js} +14 -17
  27. package/dist/{send-85b562d8.cjs.js → send-53e5693c.cjs.js} +14 -17
  28. package/dist/{send-7a350091.development.esm.js → send-a0193bdb.development.esm.js} +7 -10
  29. package/dist/{send-4e732fa5.esm.js → send-b7b4befa.esm.js} +7 -10
  30. package/dist/xstate.cjs.js +25 -25
  31. package/dist/xstate.cjs.mjs +1 -0
  32. package/dist/xstate.development.cjs.js +25 -25
  33. package/dist/xstate.development.cjs.mjs +1 -0
  34. package/dist/xstate.development.esm.js +5 -7
  35. package/dist/xstate.esm.js +5 -7
  36. package/dist/xstate.umd.min.js +1 -1
  37. package/dist/xstate.umd.min.js.map +1 -1
  38. package/guards/dist/xstate-guards.cjs.js +1 -2
  39. package/guards/dist/xstate-guards.development.cjs.js +1 -2
  40. package/guards/dist/xstate-guards.development.esm.js +1 -2
  41. package/guards/dist/xstate-guards.esm.js +1 -2
  42. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  43. package/package.json +1 -1
  44. package/dist/interpreter-23e4041c.development.cjs.js +0 -905
  45. package/dist/interpreter-3d0c0ff2.esm.js +0 -864
  46. package/dist/interpreter-b6bdd134.cjs.js +0 -894
  47. package/dist/interpreter-f2620ea7.development.esm.js +0 -875
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var interpreter = require('../../dist/interpreter-b6bdd134.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-fb6f017b.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.cjs.js');
7
7
 
8
8
  /**
@@ -131,7 +131,7 @@ function fromCallback(invokeCallback) {
131
131
  });
132
132
  return state;
133
133
  }
134
- if (event.type === interpreter.XSTATE_STOP) {
134
+ if (event.type === guards_dist_xstateGuards.XSTATE_STOP) {
135
135
  state = {
136
136
  ...state,
137
137
  status: 'stopped',
@@ -210,7 +210,7 @@ function fromObservable(observableCreator) {
210
210
  input: undefined,
211
211
  _subscription: undefined
212
212
  };
213
- case interpreter.XSTATE_STOP:
213
+ case guards_dist_xstateGuards.XSTATE_STOP:
214
214
  snapshot._subscription.unsubscribe();
215
215
  return {
216
216
  ...snapshot,
@@ -312,7 +312,7 @@ function fromEventObservable(lazyObservable) {
312
312
  input: undefined,
313
313
  _subscription: undefined
314
314
  };
315
- case interpreter.XSTATE_STOP:
315
+ case guards_dist_xstateGuards.XSTATE_STOP:
316
316
  state._subscription.unsubscribe();
317
317
  return {
318
318
  ...state,
@@ -379,6 +379,50 @@ function fromEventObservable(lazyObservable) {
379
379
 
380
380
  const resolveEventType = '$$xstate.resolve';
381
381
  const rejectEventType = '$$xstate.reject';
382
+ /**
383
+ * An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.
384
+ *
385
+ * Actors created from promise actor logic (“promise actors”) can:
386
+ * - Emit the resolved value of the promise
387
+ * - Output the resolved value of the promise
388
+ *
389
+ * Sending events to promise actors will have no effect.
390
+ *
391
+ * @param promiseCreator
392
+ * A function which returns a Promise, and accepts an object with the following properties:
393
+ * - `input` - Data that was provided to the promise actor
394
+ * - `self` - The parent actor of the promise actor
395
+ * - `system` - The actor system to which the promise actor belongs
396
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
397
+ *
398
+ * @example
399
+ * ```ts
400
+ * const promiseLogic = fromPromise(async () => {
401
+ * const result = await fetch('https://example.com/...')
402
+ * .then((data) => data.json());
403
+ *
404
+ * return result;
405
+ * });
406
+ *
407
+ * const promiseActor = createActor(promiseLogic);
408
+ * promiseActor.subscribe((snapshot) => {
409
+ * console.log(snapshot);
410
+ * });
411
+ * promiseActor.start();
412
+ * // => {
413
+ * // output: undefined,
414
+ * // status: 'active'
415
+ * // ...
416
+ * // }
417
+ *
418
+ * // After promise resolves
419
+ * // => {
420
+ * // output: { ... },
421
+ * // status: 'done',
422
+ * // ...
423
+ * // }
424
+ * ```
425
+ */
382
426
  function fromPromise(
383
427
  // TODO: add types
384
428
  promiseCreator) {
@@ -407,7 +451,7 @@ promiseCreator) {
407
451
  error: event.data,
408
452
  input: undefined
409
453
  };
410
- case interpreter.XSTATE_STOP:
454
+ case guards_dist_xstateGuards.XSTATE_STOP:
411
455
  return {
412
456
  ...state,
413
457
  status: 'stopped',
@@ -465,7 +509,7 @@ promiseCreator) {
465
509
 
466
510
  const emptyLogic = fromTransition(_ => undefined, undefined);
467
511
  function createEmptyActor() {
468
- return interpreter.createActor(emptyLogic);
512
+ return guards_dist_xstateGuards.createActor(emptyLogic);
469
513
  }
470
514
 
471
515
  exports.createEmptyActor = createEmptyActor;
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var interpreter = require('../../dist/interpreter-23e4041c.development.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-ed700d14.development.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.development.cjs.js');
7
7
 
8
8
  /**
@@ -131,7 +131,7 @@ function fromCallback(invokeCallback) {
131
131
  });
132
132
  return state;
133
133
  }
134
- if (event.type === interpreter.XSTATE_STOP) {
134
+ if (event.type === guards_dist_xstateGuards.XSTATE_STOP) {
135
135
  state = {
136
136
  ...state,
137
137
  status: 'stopped',
@@ -210,7 +210,7 @@ function fromObservable(observableCreator) {
210
210
  input: undefined,
211
211
  _subscription: undefined
212
212
  };
213
- case interpreter.XSTATE_STOP:
213
+ case guards_dist_xstateGuards.XSTATE_STOP:
214
214
  snapshot._subscription.unsubscribe();
215
215
  return {
216
216
  ...snapshot,
@@ -312,7 +312,7 @@ function fromEventObservable(lazyObservable) {
312
312
  input: undefined,
313
313
  _subscription: undefined
314
314
  };
315
- case interpreter.XSTATE_STOP:
315
+ case guards_dist_xstateGuards.XSTATE_STOP:
316
316
  state._subscription.unsubscribe();
317
317
  return {
318
318
  ...state,
@@ -379,6 +379,50 @@ function fromEventObservable(lazyObservable) {
379
379
 
380
380
  const resolveEventType = '$$xstate.resolve';
381
381
  const rejectEventType = '$$xstate.reject';
382
+ /**
383
+ * An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.
384
+ *
385
+ * Actors created from promise actor logic (“promise actors”) can:
386
+ * - Emit the resolved value of the promise
387
+ * - Output the resolved value of the promise
388
+ *
389
+ * Sending events to promise actors will have no effect.
390
+ *
391
+ * @param promiseCreator
392
+ * A function which returns a Promise, and accepts an object with the following properties:
393
+ * - `input` - Data that was provided to the promise actor
394
+ * - `self` - The parent actor of the promise actor
395
+ * - `system` - The actor system to which the promise actor belongs
396
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
397
+ *
398
+ * @example
399
+ * ```ts
400
+ * const promiseLogic = fromPromise(async () => {
401
+ * const result = await fetch('https://example.com/...')
402
+ * .then((data) => data.json());
403
+ *
404
+ * return result;
405
+ * });
406
+ *
407
+ * const promiseActor = createActor(promiseLogic);
408
+ * promiseActor.subscribe((snapshot) => {
409
+ * console.log(snapshot);
410
+ * });
411
+ * promiseActor.start();
412
+ * // => {
413
+ * // output: undefined,
414
+ * // status: 'active'
415
+ * // ...
416
+ * // }
417
+ *
418
+ * // After promise resolves
419
+ * // => {
420
+ * // output: { ... },
421
+ * // status: 'done',
422
+ * // ...
423
+ * // }
424
+ * ```
425
+ */
382
426
  function fromPromise(
383
427
  // TODO: add types
384
428
  promiseCreator) {
@@ -407,7 +451,7 @@ promiseCreator) {
407
451
  error: event.data,
408
452
  input: undefined
409
453
  };
410
- case interpreter.XSTATE_STOP:
454
+ case guards_dist_xstateGuards.XSTATE_STOP:
411
455
  return {
412
456
  ...state,
413
457
  status: 'stopped',
@@ -465,7 +509,7 @@ promiseCreator) {
465
509
 
466
510
  const emptyLogic = fromTransition(_ => undefined, undefined);
467
511
  function createEmptyActor() {
468
- return interpreter.createActor(emptyLogic);
512
+ return guards_dist_xstateGuards.createActor(emptyLogic);
469
513
  }
470
514
 
471
515
  exports.createEmptyActor = createEmptyActor;
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, d as createActor } from '../../dist/interpreter-f2620ea7.development.esm.js';
1
+ import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-348cc74e.development.esm.js';
2
2
  import '../../dev/dist/xstate-dev.development.esm.js';
3
3
 
4
4
  /**
@@ -375,6 +375,50 @@ function fromEventObservable(lazyObservable) {
375
375
 
376
376
  const resolveEventType = '$$xstate.resolve';
377
377
  const rejectEventType = '$$xstate.reject';
378
+ /**
379
+ * An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.
380
+ *
381
+ * Actors created from promise actor logic (“promise actors”) can:
382
+ * - Emit the resolved value of the promise
383
+ * - Output the resolved value of the promise
384
+ *
385
+ * Sending events to promise actors will have no effect.
386
+ *
387
+ * @param promiseCreator
388
+ * A function which returns a Promise, and accepts an object with the following properties:
389
+ * - `input` - Data that was provided to the promise actor
390
+ * - `self` - The parent actor of the promise actor
391
+ * - `system` - The actor system to which the promise actor belongs
392
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
393
+ *
394
+ * @example
395
+ * ```ts
396
+ * const promiseLogic = fromPromise(async () => {
397
+ * const result = await fetch('https://example.com/...')
398
+ * .then((data) => data.json());
399
+ *
400
+ * return result;
401
+ * });
402
+ *
403
+ * const promiseActor = createActor(promiseLogic);
404
+ * promiseActor.subscribe((snapshot) => {
405
+ * console.log(snapshot);
406
+ * });
407
+ * promiseActor.start();
408
+ * // => {
409
+ * // output: undefined,
410
+ * // status: 'active'
411
+ * // ...
412
+ * // }
413
+ *
414
+ * // After promise resolves
415
+ * // => {
416
+ * // output: { ... },
417
+ * // status: 'done',
418
+ * // ...
419
+ * // }
420
+ * ```
421
+ */
378
422
  function fromPromise(
379
423
  // TODO: add types
380
424
  promiseCreator) {
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, d as createActor } from '../../dist/interpreter-3d0c0ff2.esm.js';
1
+ import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-5854eaca.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
3
3
 
4
4
  /**
@@ -375,6 +375,50 @@ function fromEventObservable(lazyObservable) {
375
375
 
376
376
  const resolveEventType = '$$xstate.resolve';
377
377
  const rejectEventType = '$$xstate.reject';
378
+ /**
379
+ * An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.
380
+ *
381
+ * Actors created from promise actor logic (“promise actors”) can:
382
+ * - Emit the resolved value of the promise
383
+ * - Output the resolved value of the promise
384
+ *
385
+ * Sending events to promise actors will have no effect.
386
+ *
387
+ * @param promiseCreator
388
+ * A function which returns a Promise, and accepts an object with the following properties:
389
+ * - `input` - Data that was provided to the promise actor
390
+ * - `self` - The parent actor of the promise actor
391
+ * - `system` - The actor system to which the promise actor belongs
392
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
393
+ *
394
+ * @example
395
+ * ```ts
396
+ * const promiseLogic = fromPromise(async () => {
397
+ * const result = await fetch('https://example.com/...')
398
+ * .then((data) => data.json());
399
+ *
400
+ * return result;
401
+ * });
402
+ *
403
+ * const promiseActor = createActor(promiseLogic);
404
+ * promiseActor.subscribe((snapshot) => {
405
+ * console.log(snapshot);
406
+ * });
407
+ * promiseActor.start();
408
+ * // => {
409
+ * // output: undefined,
410
+ * // status: 'active'
411
+ * // ...
412
+ * // }
413
+ *
414
+ * // After promise resolves
415
+ * // => {
416
+ * // output: { ... },
417
+ * // status: 'done',
418
+ * // ...
419
+ * // }
420
+ * ```
421
+ */
378
422
  function fromPromise(
379
423
  // TODO: add types
380
424
  promiseCreator) {
@@ -1,2 +1,2 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActors={})}(this,(function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const s="xstate.stop";function i(t,e){return{type:`xstate.error.actor.${t}`,data:e}}function r(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const o=t=>{const e=r();e&&e.register(t)};function n(t){setTimeout((()=>{throw t}))}const a="function"==typeof Symbol&&Symbol.observable||"@@observable";let c=0;function h(t,e,s){const i="object"==typeof t,r=i?t:void 0;return{next:(i?t.next:t)?.bind(r),error:(i?t.error:e)?.bind(r),complete:(i?t.complete:s)?.bind(r)}}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,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class p{constructor(t,s){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this._processingStatus=u.NotStarted,this._parent=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,...s},{clock:r,logger:o,parent:n,id:a,systemId:p,inspect:_}=i;this.system=n?.system??function(t){const e=new Map,s=new Map,i=new WeakMap,r=new Set,o={_bookId:()=>"x:"+c++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const r=i.get(t);void 0!==r&&(s.delete(r),i.delete(t))},get:t=>s.get(t),_set:(t,e)=>{const r=s.get(t);if(r&&r!==e)throw new Error(`Actor with system ID '${t}' already exists.`);s.set(t,e),i.set(e,t)},inspect:t=>{r.add(t)},_sendInspectionEvent:e=>{const s={...e,rootId:t.sessionId};r.forEach((t=>t.next?.(s)))},_relay:(t,e,s)=>{o._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)}};return o}(this),_&&!n&&this.system.inspect(h(_)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=o,this.clock=r,this._parent=n,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()}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),this._initState(s?.state),p&&"active"===this._state.status&&(this._systemId=p,this.system._set(p,this))}_initState(t){this._state=t?this.logic.restoreState?this.logic.restoreState(t,this._actorScope):t:this.logic.getInitialState(this._actorScope,this.options?.input)}update(t,e){let s;for(this._state=t;s=this._deferred.shift();)s();for(const e of this.observers)try{e.next?.(t)}catch(t){n(t)}switch(this._state.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(r=this.id,o=this._state.output,{type:`xstate.done.actor.${r}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._stopProcedure(),this._error(this._state.error),this._parent&&this.system._relay(this,this._parent,i(this.id,this._state.error))}var r,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const i=h(t,e,s);if(this._processingStatus!==u.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){n(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this._processingStatus===u.Running)return this;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._state.status){case"done":this.update(this._state,t);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorScope)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(i(this.id,t)),this}return this.update(this._state,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,r;try{e=this.logic.transition(this._state,t,this._actorScope)}catch(t){r={err:t}}if(r){const{err:t}=r;return this._stopProcedure(),this._error(t),void this._parent?.send(i(this.id,t))}this.update(e,t),t.type===s&&(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:s}),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){n(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||n(t));let e=!1;for(const s of this.observers){const i=s.error;e||=!i;try{i?.(t)}catch(t){n(t)}}this.observers.clear(),e&&n(t)}_stopProcedure(){if(this._processingStatus!==u.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new e(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)}delaySend(t){const{event:e,id:s,delay:i}=t,r=this.clock.setTimeout((()=>{this.system._relay(this,t.to??this,e)}),i);s&&(this.delayedEventsMap[s]=r)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:o)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedState(t){return this.logic.getPersistedState(this._state,t)}[a](){return this}getSnapshot(){return this._state}}function _(t,e){return{config:t,transition:(e,s,i)=>({...e,context:t(e.context,s,i)}),getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,context:"function"==typeof e?e({input:s}):e}),getPersistedState:t=>t,restoreState:t=>t}}const l="$$xstate.resolve",v="$$xstate.reject";const f=_((t=>{}),void 0);t.createEmptyActor=function(){return new p(f,t);var t},t.fromCallback=function(t){return{config:t,start:(t,{self:e,system:s})=>{s._relay(e,e,{type:"xstate.create"})},transition:(e,i,{self:r,system:o})=>{if("xstate.create"===i.type){const s=t=>{"stopped"!==e.status&&r._parent&&o._relay(r,r._parent,t)},i=t=>{e._receivers.add(t)};return e._dispose=t({input:e.input,system:o,self:r,sendBack:s,receive:i}),e}return i.type===s?("function"==typeof(e={...e,status:"stopped",error:void 0})._dispose&&e._dispose(),e):(e._receivers.forEach((t=>t(i))),e)},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,input:e,_receivers:new Set,_dispose:void 0}),getPersistedState:({_dispose:t,_receivers:e,...s})=>s,restoreState:t=>({_receivers:new Set,_dispose:void 0,...t})}},t.fromEventObservable=function(t){const e="$$xstate.error",i="$$xstate.complete";return{config:t,transition:(t,r)=>{if("active"!==t.status)return t;switch(r.type){case e:return{...t,status:"error",error:r.data,input:void 0,_subscription:void 0};case i:return{...t,status:"done",input:void 0,_subscription:void 0};case s:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,context:void 0,input:e,_subscription:void 0}),start:(s,{self:r,system:o})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:o,self:r}).subscribe({next:t=>{r._parent&&o._relay(r,r._parent,t)},error:t=>{o._relay(r,r,{type:e,data:t})},complete:()=>{o._relay(r,r,{type:i})}}))},getPersistedState:({_subscription:t,...e})=>e,restoreState:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){const e="$$xstate.next",i="$$xstate.error",r="$$xstate.complete";return{config:t,transition:(t,o,{self:n,id:a,defer:c,system:h})=>{if("active"!==t.status)return t;switch(o.type){case e:return{...t,context:o.data};case i:return{...t,status:"error",error:o.data,input:void 0,_subscription:void 0};case r:return{...t,status:"done",input:void 0,_subscription:void 0};case s:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,context:void 0,input:e,_subscription:void 0}),start:(s,{self:o,system:n})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:n,self:o}).subscribe({next:t=>{n._relay(o,o,{type:e,data:t})},error:t=>{n._relay(o,o,{type:i,data:t})},complete:()=>{n._relay(o,o,{type:r})}}))},getPersistedState:({_subscription:t,...e})=>e,restoreState:t=>({...t,_subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,e)=>{if("active"!==t.status)return t;switch(e.type){case l:{const s=e.data;return{...t,status:"done",output:s,input:void 0}}case v:return{...t,status:"error",error:e.data,input:void 0};case s:return{...t,status:"stopped",input:void 0};default:return t}},start:(e,{self:s,system:i})=>{if("active"!==e.status)return;Promise.resolve(t({input:e.input,system:i,self:s})).then((t=>{"active"===s.getSnapshot().status&&i._relay(s,s,{type:l,data:t})}),(t=>{"active"===s.getSnapshot().status&&i._relay(s,s,{type:v,data:t})}))},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,input:e}),getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=_,Object.defineProperty(t,"__esModule",{value:!0})}));
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActors={})}(this,(function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const s="xstate.stop";function i(t,e){return{type:`xstate.error.actor.${t}`,data:e}}function r(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const o=t=>{const e=r();e&&e.register(t)};function n(t){setTimeout((()=>{throw t}))}const a="function"==typeof Symbol&&Symbol.observable||"@@observable";let c=0;function h(t,e,s){const i="object"==typeof t,r=i?t:void 0;return{next:(i?t.next:t)?.bind(r),error:(i?t.error:e)?.bind(r),complete:(i?t.complete:s)?.bind(r)}}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,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class p{constructor(t,s){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this._processingStatus=u.NotStarted,this._parent=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,...s},{clock:r,logger:o,parent:n,id:a,systemId:p,inspect:_}=i;this.system=n?.system??function(t){const e=new Map,s=new Map,i=new WeakMap,r=new Set,o={_bookId:()=>"x:"+c++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const r=i.get(t);void 0!==r&&(s.delete(r),i.delete(t))},get:t=>s.get(t),_set:(t,e)=>{const r=s.get(t);if(r&&r!==e)throw new Error(`Actor with system ID '${t}' already exists.`);s.set(t,e),i.set(e,t)},inspect:t=>{r.add(t)},_sendInspectionEvent:e=>{const s={...e,rootId:t.sessionId};r.forEach((t=>t.next?.(s)))},_relay:(t,e,s)=>{o._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)}};return o}(this),_&&!n&&this.system.inspect(h(_)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=o,this.clock=r,this._parent=n,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()}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),p&&(this._systemId=p,this.system._set(p,this)),this._initState(s?.state),p&&"active"!==this._state.status&&this.system._unregister(this)}_initState(t){this._state=t?this.logic.restoreState?this.logic.restoreState(t,this._actorScope):t:this.logic.getInitialState(this._actorScope,this.options?.input)}update(t,e){let s;for(this._state=t;s=this._deferred.shift();)s();for(const e of this.observers)try{e.next?.(t)}catch(t){n(t)}switch(this._state.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(r=this.id,o=this._state.output,{type:`xstate.done.actor.${r}`,output:o}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._stopProcedure(),this._error(this._state.error),this._parent&&this.system._relay(this,this._parent,i(this.id,this._state.error))}var r,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const i=h(t,e,s);if(this._processingStatus!==u.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){n(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this._processingStatus===u.Running)return this;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._state.status){case"done":this.update(this._state,t);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorScope)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(i(this.id,t)),this}return this.update(this._state,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,r;try{e=this.logic.transition(this._state,t,this._actorScope)}catch(t){r={err:t}}if(r){const{err:t}=r;return this._stopProcedure(),this._error(t),void this._parent?.send(i(this.id,t))}this.update(e,t),t.type===s&&(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:s}),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){n(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||n(t));let e=!1;for(const s of this.observers){const i=s.error;e||=!i;try{i?.(t)}catch(t){n(t)}}this.observers.clear(),e&&n(t)}_stopProcedure(){if(this._processingStatus!==u.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new e(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)}delaySend(t){const{event:e,id:s,delay:i}=t,r=this.clock.setTimeout((()=>{this.system._relay(this,t.to??this,e)}),i);s&&(this.delayedEventsMap[s]=r)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:o)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedState(t){return this.logic.getPersistedState(this._state,t)}[a](){return this}getSnapshot(){return this._state}}function _(t,e){return{config:t,transition:(e,s,i)=>({...e,context:t(e.context,s,i)}),getInitialState:(t,s)=>({status:"active",output:void 0,error:void 0,context:"function"==typeof e?e({input:s}):e}),getPersistedState:t=>t,restoreState:t=>t}}const l="$$xstate.resolve",v="$$xstate.reject";const f=_((t=>{}),void 0);t.createEmptyActor=function(){return new p(f,t);var t},t.fromCallback=function(t){return{config:t,start:(t,{self:e,system:s})=>{s._relay(e,e,{type:"xstate.create"})},transition:(e,i,{self:r,system:o})=>{if("xstate.create"===i.type){const s=t=>{"stopped"!==e.status&&r._parent&&o._relay(r,r._parent,t)},i=t=>{e._receivers.add(t)};return e._dispose=t({input:e.input,system:o,self:r,sendBack:s,receive:i}),e}return i.type===s?("function"==typeof(e={...e,status:"stopped",error:void 0})._dispose&&e._dispose(),e):(e._receivers.forEach((t=>t(i))),e)},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,input:e,_receivers:new Set,_dispose:void 0}),getPersistedState:({_dispose:t,_receivers:e,...s})=>s,restoreState:t=>({_receivers:new Set,_dispose:void 0,...t})}},t.fromEventObservable=function(t){const e="$$xstate.error",i="$$xstate.complete";return{config:t,transition:(t,r)=>{if("active"!==t.status)return t;switch(r.type){case e:return{...t,status:"error",error:r.data,input:void 0,_subscription:void 0};case i:return{...t,status:"done",input:void 0,_subscription:void 0};case s:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,context:void 0,input:e,_subscription:void 0}),start:(s,{self:r,system:o})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:o,self:r}).subscribe({next:t=>{r._parent&&o._relay(r,r._parent,t)},error:t=>{o._relay(r,r,{type:e,data:t})},complete:()=>{o._relay(r,r,{type:i})}}))},getPersistedState:({_subscription:t,...e})=>e,restoreState:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){const e="$$xstate.next",i="$$xstate.error",r="$$xstate.complete";return{config:t,transition:(t,o,{self:n,id:a,defer:c,system:h})=>{if("active"!==t.status)return t;switch(o.type){case e:return{...t,context:o.data};case i:return{...t,status:"error",error:o.data,input:void 0,_subscription:void 0};case r:return{...t,status:"done",input:void 0,_subscription:void 0};case s:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,context:void 0,input:e,_subscription:void 0}),start:(s,{self:o,system:n})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:n,self:o}).subscribe({next:t=>{n._relay(o,o,{type:e,data:t})},error:t=>{n._relay(o,o,{type:i,data:t})},complete:()=>{n._relay(o,o,{type:r})}}))},getPersistedState:({_subscription:t,...e})=>e,restoreState:t=>({...t,_subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,e)=>{if("active"!==t.status)return t;switch(e.type){case l:{const s=e.data;return{...t,status:"done",output:s,input:void 0}}case v:return{...t,status:"error",error:e.data,input:void 0};case s:return{...t,status:"stopped",input:void 0};default:return t}},start:(e,{self:s,system:i})=>{if("active"!==e.status)return;Promise.resolve(t({input:e.input,system:i,self:s})).then((t=>{"active"===s.getSnapshot().status&&i._relay(s,s,{type:l,data:t})}),(t=>{"active"===s.getSnapshot().status&&i._relay(s,s,{type:v,data:t})}))},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,input:e}),getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=_,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actors.umd.min.js.map