xstate 5.0.0-beta.38 → 5.0.0-beta.40

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 (44) hide show
  1. package/actions/dist/xstate-actions.cjs.js +3 -3
  2. package/actions/dist/xstate-actions.development.cjs.js +3 -3
  3. package/actions/dist/xstate-actions.development.esm.js +3 -3
  4. package/actions/dist/xstate-actions.esm.js +3 -3
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +60 -3
  8. package/actors/dist/xstate-actors.development.cjs.js +60 -3
  9. package/actors/dist/xstate-actors.development.esm.js +60 -3
  10. package/actors/dist/xstate-actors.esm.js +60 -3
  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/StateMachine.d.ts +5 -5
  14. package/dist/declarations/src/actors/callback.d.ts +74 -0
  15. package/dist/declarations/src/actors/transition.d.ts +2 -2
  16. package/dist/declarations/src/interpreter.d.ts +70 -9
  17. package/dist/declarations/src/spawn.d.ts +2 -2
  18. package/dist/declarations/src/stateUtils.d.ts +4 -4
  19. package/dist/declarations/src/system.d.ts +7 -3
  20. package/dist/declarations/src/types.d.ts +7 -7
  21. package/dist/{interpreter-4005eb36.development.esm.js → interpreter-410d7ca9.development.esm.js} +92 -13
  22. package/dist/{interpreter-b6f22ee2.cjs.js → interpreter-586abde4.cjs.js} +92 -13
  23. package/dist/{interpreter-ed3f81f7.development.cjs.js → interpreter-bae5c279.development.cjs.js} +92 -13
  24. package/dist/{interpreter-c80ce92e.esm.js → interpreter-ed0fac7e.esm.js} +92 -13
  25. package/dist/{raise-7faa9b3b.cjs.js → raise-27909189.cjs.js} +83 -80
  26. package/dist/{raise-c989c7fb.esm.js → raise-2b2fdec3.esm.js} +83 -80
  27. package/dist/{raise-42073973.development.esm.js → raise-37f9f3b8.development.esm.js} +84 -81
  28. package/dist/{raise-b69a3d16.development.cjs.js → raise-8325e2df.development.cjs.js} +84 -81
  29. package/dist/{send-34160163.cjs.js → send-4fdf275e.cjs.js} +22 -22
  30. package/dist/{send-4b616da9.esm.js → send-59f66c58.esm.js} +22 -22
  31. package/dist/{send-58725522.development.cjs.js → send-c45d0d2c.development.cjs.js} +22 -22
  32. package/dist/{send-bff8c910.development.esm.js → send-f6b49072.development.esm.js} +22 -22
  33. package/dist/xstate.cjs.js +18 -17
  34. package/dist/xstate.development.cjs.js +18 -17
  35. package/dist/xstate.development.esm.js +21 -20
  36. package/dist/xstate.esm.js +21 -20
  37. package/dist/xstate.umd.min.js +1 -1
  38. package/dist/xstate.umd.min.js.map +1 -1
  39. package/guards/dist/xstate-guards.cjs.js +2 -2
  40. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  41. package/guards/dist/xstate-guards.development.esm.js +2 -2
  42. package/guards/dist/xstate-guards.esm.js +2 -2
  43. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  44. package/package.json +1 -1
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var interpreter = require('../../dist/interpreter-b6f22ee2.cjs.js');
5
+ var interpreter = require('../../dist/interpreter-586abde4.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.cjs.js');
7
7
 
8
8
  /**
@@ -17,10 +17,10 @@ require('../../dev/dist/xstate-dev.cjs.js');
17
17
  function fromTransition(transition, initialContext) {
18
18
  return {
19
19
  config: transition,
20
- transition: (state, event, actorContext) => {
20
+ transition: (state, event, actorScope) => {
21
21
  return {
22
22
  ...state,
23
- context: transition(state.context, event, actorContext)
23
+ context: transition(state.context, event, actorScope)
24
24
  };
25
25
  },
26
26
  getInitialState: (_, input) => {
@@ -38,6 +38,63 @@ function fromTransition(transition, initialContext) {
38
38
  };
39
39
  }
40
40
 
41
+ /**
42
+ * An actor logic creator which returns callback logic as defined by a callback function.
43
+ *
44
+ * @remarks
45
+ * Useful for subscription-based or other free-form logic that can send events back to the parent actor.
46
+ *
47
+ * Actors created from callback logic (“callback actors”) can:
48
+ * - Receive events via the `receive` function
49
+ * - Send events to the parent actor via the `sendBack` function
50
+ *
51
+ * Callback actors are a bit different from other actors in that they:
52
+ * - Do not work with `onDone`
53
+ * - Do not produce a snapshot using `.getSnapshot()`
54
+ * - Do not emit values when used with `.subscribe()`
55
+ * - Can not be stopped with `.stop()`
56
+ *
57
+ * @param invokeCallback - The callback function used to describe the callback logic
58
+ * The callback function is passed an object with the following properties:
59
+ * - `receive` - A function that can send events back to the parent actor; the listener is then called whenever events are received by the callback actor
60
+ * - `sendBack` - A function that can send events back to the parent actor
61
+ * - `input` - Data that was provided to the callback actor
62
+ * - `self` - The parent actor of the callback actor
63
+ * - `system` - The actor system to which the callback actor belongs
64
+ * The callback function can (optionally) return a cleanup function, which is called when the actor is stopped.
65
+ * @see {@link InvokeCallback} for more information about the callback function and its object argument
66
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
67
+
68
+ * @returns Callback logic
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const callbackLogic = fromCallback(({ sendBack, receive }) => {
73
+ * let lockStatus = 'unlocked';
74
+ *
75
+ * const handler = (event) => {
76
+ * if (lockStatus === 'locked') {
77
+ * return;
78
+ * }
79
+ * sendBack(event);
80
+ * };
81
+ *
82
+ * receive((event) => {
83
+ * if (event.type === 'lock') {
84
+ * lockStatus = 'locked';
85
+ * } else if (event.type === 'unlock') {
86
+ * lockStatus = 'unlocked';
87
+ * }
88
+ * });
89
+ *
90
+ * document.body.addEventListener('click', handler);
91
+ *
92
+ * return () => {
93
+ * document.body.removeEventListener('click', handler);
94
+ * };
95
+ * });
96
+ * ```
97
+ */
41
98
  function fromCallback(invokeCallback) {
42
99
  const logic = {
43
100
  config: invokeCallback,
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var interpreter = require('../../dist/interpreter-ed3f81f7.development.cjs.js');
5
+ var interpreter = require('../../dist/interpreter-bae5c279.development.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.development.cjs.js');
7
7
 
8
8
  /**
@@ -17,10 +17,10 @@ require('../../dev/dist/xstate-dev.development.cjs.js');
17
17
  function fromTransition(transition, initialContext) {
18
18
  return {
19
19
  config: transition,
20
- transition: (state, event, actorContext) => {
20
+ transition: (state, event, actorScope) => {
21
21
  return {
22
22
  ...state,
23
- context: transition(state.context, event, actorContext)
23
+ context: transition(state.context, event, actorScope)
24
24
  };
25
25
  },
26
26
  getInitialState: (_, input) => {
@@ -38,6 +38,63 @@ function fromTransition(transition, initialContext) {
38
38
  };
39
39
  }
40
40
 
41
+ /**
42
+ * An actor logic creator which returns callback logic as defined by a callback function.
43
+ *
44
+ * @remarks
45
+ * Useful for subscription-based or other free-form logic that can send events back to the parent actor.
46
+ *
47
+ * Actors created from callback logic (“callback actors”) can:
48
+ * - Receive events via the `receive` function
49
+ * - Send events to the parent actor via the `sendBack` function
50
+ *
51
+ * Callback actors are a bit different from other actors in that they:
52
+ * - Do not work with `onDone`
53
+ * - Do not produce a snapshot using `.getSnapshot()`
54
+ * - Do not emit values when used with `.subscribe()`
55
+ * - Can not be stopped with `.stop()`
56
+ *
57
+ * @param invokeCallback - The callback function used to describe the callback logic
58
+ * The callback function is passed an object with the following properties:
59
+ * - `receive` - A function that can send events back to the parent actor; the listener is then called whenever events are received by the callback actor
60
+ * - `sendBack` - A function that can send events back to the parent actor
61
+ * - `input` - Data that was provided to the callback actor
62
+ * - `self` - The parent actor of the callback actor
63
+ * - `system` - The actor system to which the callback actor belongs
64
+ * The callback function can (optionally) return a cleanup function, which is called when the actor is stopped.
65
+ * @see {@link InvokeCallback} for more information about the callback function and its object argument
66
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
67
+
68
+ * @returns Callback logic
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const callbackLogic = fromCallback(({ sendBack, receive }) => {
73
+ * let lockStatus = 'unlocked';
74
+ *
75
+ * const handler = (event) => {
76
+ * if (lockStatus === 'locked') {
77
+ * return;
78
+ * }
79
+ * sendBack(event);
80
+ * };
81
+ *
82
+ * receive((event) => {
83
+ * if (event.type === 'lock') {
84
+ * lockStatus = 'locked';
85
+ * } else if (event.type === 'unlock') {
86
+ * lockStatus = 'unlocked';
87
+ * }
88
+ * });
89
+ *
90
+ * document.body.addEventListener('click', handler);
91
+ *
92
+ * return () => {
93
+ * document.body.removeEventListener('click', handler);
94
+ * };
95
+ * });
96
+ * ```
97
+ */
41
98
  function fromCallback(invokeCallback) {
42
99
  const logic = {
43
100
  config: invokeCallback,
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, d as createActor } from '../../dist/interpreter-4005eb36.development.esm.js';
1
+ import { X as XSTATE_STOP, d as createActor } from '../../dist/interpreter-410d7ca9.development.esm.js';
2
2
  import '../../dev/dist/xstate-dev.development.esm.js';
3
3
 
4
4
  /**
@@ -13,10 +13,10 @@ import '../../dev/dist/xstate-dev.development.esm.js';
13
13
  function fromTransition(transition, initialContext) {
14
14
  return {
15
15
  config: transition,
16
- transition: (state, event, actorContext) => {
16
+ transition: (state, event, actorScope) => {
17
17
  return {
18
18
  ...state,
19
- context: transition(state.context, event, actorContext)
19
+ context: transition(state.context, event, actorScope)
20
20
  };
21
21
  },
22
22
  getInitialState: (_, input) => {
@@ -34,6 +34,63 @@ function fromTransition(transition, initialContext) {
34
34
  };
35
35
  }
36
36
 
37
+ /**
38
+ * An actor logic creator which returns callback logic as defined by a callback function.
39
+ *
40
+ * @remarks
41
+ * Useful for subscription-based or other free-form logic that can send events back to the parent actor.
42
+ *
43
+ * Actors created from callback logic (“callback actors”) can:
44
+ * - Receive events via the `receive` function
45
+ * - Send events to the parent actor via the `sendBack` function
46
+ *
47
+ * Callback actors are a bit different from other actors in that they:
48
+ * - Do not work with `onDone`
49
+ * - Do not produce a snapshot using `.getSnapshot()`
50
+ * - Do not emit values when used with `.subscribe()`
51
+ * - Can not be stopped with `.stop()`
52
+ *
53
+ * @param invokeCallback - The callback function used to describe the callback logic
54
+ * The callback function is passed an object with the following properties:
55
+ * - `receive` - A function that can send events back to the parent actor; the listener is then called whenever events are received by the callback actor
56
+ * - `sendBack` - A function that can send events back to the parent actor
57
+ * - `input` - Data that was provided to the callback actor
58
+ * - `self` - The parent actor of the callback actor
59
+ * - `system` - The actor system to which the callback actor belongs
60
+ * The callback function can (optionally) return a cleanup function, which is called when the actor is stopped.
61
+ * @see {@link InvokeCallback} for more information about the callback function and its object argument
62
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
63
+
64
+ * @returns Callback logic
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const callbackLogic = fromCallback(({ sendBack, receive }) => {
69
+ * let lockStatus = 'unlocked';
70
+ *
71
+ * const handler = (event) => {
72
+ * if (lockStatus === 'locked') {
73
+ * return;
74
+ * }
75
+ * sendBack(event);
76
+ * };
77
+ *
78
+ * receive((event) => {
79
+ * if (event.type === 'lock') {
80
+ * lockStatus = 'locked';
81
+ * } else if (event.type === 'unlock') {
82
+ * lockStatus = 'unlocked';
83
+ * }
84
+ * });
85
+ *
86
+ * document.body.addEventListener('click', handler);
87
+ *
88
+ * return () => {
89
+ * document.body.removeEventListener('click', handler);
90
+ * };
91
+ * });
92
+ * ```
93
+ */
37
94
  function fromCallback(invokeCallback) {
38
95
  const logic = {
39
96
  config: invokeCallback,
@@ -1,4 +1,4 @@
1
- import { X as XSTATE_STOP, d as createActor } from '../../dist/interpreter-c80ce92e.esm.js';
1
+ import { X as XSTATE_STOP, d as createActor } from '../../dist/interpreter-ed0fac7e.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
3
3
 
4
4
  /**
@@ -13,10 +13,10 @@ import '../../dev/dist/xstate-dev.esm.js';
13
13
  function fromTransition(transition, initialContext) {
14
14
  return {
15
15
  config: transition,
16
- transition: (state, event, actorContext) => {
16
+ transition: (state, event, actorScope) => {
17
17
  return {
18
18
  ...state,
19
- context: transition(state.context, event, actorContext)
19
+ context: transition(state.context, event, actorScope)
20
20
  };
21
21
  },
22
22
  getInitialState: (_, input) => {
@@ -34,6 +34,63 @@ function fromTransition(transition, initialContext) {
34
34
  };
35
35
  }
36
36
 
37
+ /**
38
+ * An actor logic creator which returns callback logic as defined by a callback function.
39
+ *
40
+ * @remarks
41
+ * Useful for subscription-based or other free-form logic that can send events back to the parent actor.
42
+ *
43
+ * Actors created from callback logic (“callback actors”) can:
44
+ * - Receive events via the `receive` function
45
+ * - Send events to the parent actor via the `sendBack` function
46
+ *
47
+ * Callback actors are a bit different from other actors in that they:
48
+ * - Do not work with `onDone`
49
+ * - Do not produce a snapshot using `.getSnapshot()`
50
+ * - Do not emit values when used with `.subscribe()`
51
+ * - Can not be stopped with `.stop()`
52
+ *
53
+ * @param invokeCallback - The callback function used to describe the callback logic
54
+ * The callback function is passed an object with the following properties:
55
+ * - `receive` - A function that can send events back to the parent actor; the listener is then called whenever events are received by the callback actor
56
+ * - `sendBack` - A function that can send events back to the parent actor
57
+ * - `input` - Data that was provided to the callback actor
58
+ * - `self` - The parent actor of the callback actor
59
+ * - `system` - The actor system to which the callback actor belongs
60
+ * The callback function can (optionally) return a cleanup function, which is called when the actor is stopped.
61
+ * @see {@link InvokeCallback} for more information about the callback function and its object argument
62
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
63
+
64
+ * @returns Callback logic
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const callbackLogic = fromCallback(({ sendBack, receive }) => {
69
+ * let lockStatus = 'unlocked';
70
+ *
71
+ * const handler = (event) => {
72
+ * if (lockStatus === 'locked') {
73
+ * return;
74
+ * }
75
+ * sendBack(event);
76
+ * };
77
+ *
78
+ * receive((event) => {
79
+ * if (event.type === 'lock') {
80
+ * lockStatus = 'locked';
81
+ * } else if (event.type === 'unlock') {
82
+ * lockStatus = 'unlocked';
83
+ * }
84
+ * });
85
+ *
86
+ * document.body.addEventListener('click', handler);
87
+ *
88
+ * return () => {
89
+ * document.body.removeEventListener('click', handler);
90
+ * };
91
+ * });
92
+ * ```
93
+ */
37
94
  function fromCallback(invokeCallback) {
38
95
  const logic = {
39
96
  config: invokeCallback,
@@ -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.status=u.NotStarted,this._parent=void 0,this.ref=void 0,this._actorContext=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:l}=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,targetRef:e,event:s}),e._send(s)}};return o}(this),l&&!n&&this.system.inspect(h(l)),p&&(this._systemId=p,this.system._set(p,this)),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,this.ref=this,this._actorContext={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()}_initState(){this._state=this.options.state?this.logic.restoreState?this.logic.restoreState(this.options.state,this._actorContext):this.options.state:this.logic.getInitialState(this._actorContext,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.status!==u.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){n(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this.status===u.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=u.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,targetRef: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._actorContext)}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._actorContext)}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.status===u.Stopped?this:(this.mailbox.clear(),this.status===u.NotStarted?(this.status=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.status!==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.status=u.Stopped,this.system._unregister(this),this}_send(t){this.status!==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(){return this.logic.getPersistedState(this._state)}[a](){return this}getSnapshot(){return this._state}}function l(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 _="$$xstate.resolve",v="$$xstate.reject";const f=l((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 _:{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:_,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=l,Object.defineProperty(t,"__esModule",{value:!0})}));
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActors={})}(this,(function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const s="xstate.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.status=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:l}=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),l&&!n&&this.system.inspect(h(l)),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,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(),p&&"active"===this._state.status&&(this._systemId=p,this.system._set(p,this))}_initState(){this._state=this.options.state?this.logic.restoreState?this.logic.restoreState(this.options.state,this._actorScope):this.options.state: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.status!==u.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){n(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this.status===u.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=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.status===u.Stopped?this:(this.mailbox.clear(),this.status===u.NotStarted?(this.status=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.status!==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.status=u.Stopped,this.system._unregister(this),this}_send(t){this.status!==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(){return this.logic.getPersistedState(this._state)}[a](){return this}getSnapshot(){return this._state}}function l(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 _="$$xstate.resolve",v="$$xstate.reject";const f=l((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 _:{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:_,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=l,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate-actors.umd.min.js.map