xstate 5.0.0-beta.38 → 5.0.0-beta.39

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 (43) 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/types.d.ts +7 -7
  20. package/dist/{interpreter-ed3f81f7.development.cjs.js → interpreter-03a5c3f5.development.cjs.js} +90 -11
  21. package/dist/{interpreter-c80ce92e.esm.js → interpreter-1e8c1c0c.esm.js} +90 -11
  22. package/dist/{interpreter-b6f22ee2.cjs.js → interpreter-5dfcd203.cjs.js} +90 -11
  23. package/dist/{interpreter-4005eb36.development.esm.js → interpreter-70cd9217.development.esm.js} +90 -11
  24. package/dist/{raise-b69a3d16.development.cjs.js → raise-17cb3d9d.development.cjs.js} +84 -81
  25. package/dist/{raise-7faa9b3b.cjs.js → raise-291d2181.cjs.js} +83 -80
  26. package/dist/{raise-c989c7fb.esm.js → raise-62de3670.esm.js} +83 -80
  27. package/dist/{raise-42073973.development.esm.js → raise-e044f460.development.esm.js} +84 -81
  28. package/dist/{send-4b616da9.esm.js → send-1249d4ac.esm.js} +22 -22
  29. package/dist/{send-58725522.development.cjs.js → send-33433787.development.cjs.js} +22 -22
  30. package/dist/{send-34160163.cjs.js → send-af152aca.cjs.js} +22 -22
  31. package/dist/{send-bff8c910.development.esm.js → send-f1a2a827.development.esm.js} +22 -22
  32. package/dist/xstate.cjs.js +17 -17
  33. package/dist/xstate.development.cjs.js +17 -17
  34. package/dist/xstate.development.esm.js +20 -20
  35. package/dist/xstate.esm.js +20 -20
  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 +2 -2
  39. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  40. package/guards/dist/xstate-guards.development.esm.js +2 -2
  41. package/guards/dist/xstate-guards.esm.js +2 -2
  42. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  43. package/package.json +1 -1
@@ -1,10 +1,10 @@
1
1
  export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.development.esm.js';
2
- import { S as STATE_DELIMITER, m as mapValues, t as toArray, a as toTransitionConfigArray, N as NULL_EVENT, c as createInvokeId, i as isErrorActorEvent, b as createInitEvent, r as resolveReferencedActor, d as createActor, $ as $$ACTOR_TYPE, e as matchesState } from './interpreter-4005eb36.development.esm.js';
3
- export { A as Actor, f as ActorStatus, I as InterpreterStatus, d as createActor, g as interpret, e as matchesState, p as pathToStateValue, h as toObserver } from './interpreter-4005eb36.development.esm.js';
4
- import { f as formatTransitions, a as formatTransition, m as memo, e as evaluateGuard, g as getDelayedTransitions, b as formatInitialTransition, c as getCandidates, d as getConfiguration, h as getStateNodes, r as resolveStateValue, i as isInFinalState, S as State, j as cloneState, k as macrostep, t as transitionNode, l as getInitialConfiguration, n as resolveActionsAndContext, o as microstep, p as getInitialStateNodes, q as isStateId, s as getStateNodeByPath, u as getPersistedState } from './raise-42073973.development.esm.js';
5
- export { S as State, v as and, z as cancel, h as getStateNodes, w as not, x as or, A as raise, C as spawn, y as stateIn, B as stop } from './raise-42073973.development.esm.js';
6
- import { a as assign } from './send-bff8c910.development.esm.js';
7
- export { S as SpecialTargets, a as assign, c as choose, e as escalate, f as forwardTo, l as log, p as pure, s as sendParent, b as sendTo } from './send-bff8c910.development.esm.js';
2
+ import { S as STATE_DELIMITER, m as mapValues, t as toArray, a as toTransitionConfigArray, N as NULL_EVENT, c as createInvokeId, i as isErrorActorEvent, b as createInitEvent, r as resolveReferencedActor, d as createActor, $ as $$ACTOR_TYPE, e as matchesState } from './interpreter-70cd9217.development.esm.js';
3
+ export { A as Actor, f as ActorStatus, I as InterpreterStatus, d as createActor, g as interpret, e as matchesState, p as pathToStateValue, h as toObserver } from './interpreter-70cd9217.development.esm.js';
4
+ import { f as formatTransitions, a as formatTransition, m as memo, e as evaluateGuard, g as getDelayedTransitions, b as formatInitialTransition, c as getCandidates, d as getConfiguration, h as getStateNodes, r as resolveStateValue, i as isInFinalState, S as State, j as cloneState, k as macrostep, t as transitionNode, l as getInitialConfiguration, n as resolveActionsAndContext, o as microstep, p as getInitialStateNodes, q as isStateId, s as getStateNodeByPath, u as getPersistedState } from './raise-e044f460.development.esm.js';
5
+ export { S as State, v as and, z as cancel, h as getStateNodes, w as not, x as or, A as raise, C as spawn, y as stateIn, B as stop } from './raise-e044f460.development.esm.js';
6
+ import { a as assign } from './send-f1a2a827.development.esm.js';
7
+ export { S as SpecialTargets, a as assign, c as choose, e as escalate, f as forwardTo, l as log, p as pure, s as sendParent, b as sendTo } from './send-f1a2a827.development.esm.js';
8
8
  import '../dev/dist/xstate-dev.development.esm.js';
9
9
 
10
10
  class SimulatedClock {
@@ -480,7 +480,7 @@ class StateMachine {
480
480
  * @param state The current State instance or state value
481
481
  * @param event The received event
482
482
  */
483
- transition(state, event, actorCtx) {
483
+ transition(state, event, actorScope) {
484
484
  // TODO: handle error events in a better way
485
485
  if (isErrorActorEvent(event) && !state.nextEvents.some(nextEvent => nextEvent === event.type)) {
486
486
  return cloneState(state, {
@@ -490,7 +490,7 @@ class StateMachine {
490
490
  }
491
491
  const {
492
492
  state: nextState
493
- } = macrostep(state, event, actorCtx);
493
+ } = macrostep(state, event, actorScope);
494
494
  return nextState;
495
495
  }
496
496
 
@@ -501,8 +501,8 @@ class StateMachine {
501
501
  * @param state The current state
502
502
  * @param event The received event
503
503
  */
504
- microstep(state, event, actorCtx) {
505
- return macrostep(state, event, actorCtx).microstates;
504
+ microstep(state, event, actorScope) {
505
+ return macrostep(state, event, actorScope).microstates;
506
506
  }
507
507
  getTransitionData(state, event) {
508
508
  return transitionNode(this.root, state.value, state, event) || [];
@@ -512,7 +512,7 @@ class StateMachine {
512
512
  * The initial state _before_ evaluating any microsteps.
513
513
  * This "pre-initial" state is provided to initial actions executed in the initial state.
514
514
  */
515
- getPreInitialState(actorCtx, initEvent, internalQueue) {
515
+ getPreInitialState(actorScope, initEvent, internalQueue) {
516
516
  const {
517
517
  context
518
518
  } = this.config;
@@ -533,7 +533,7 @@ class StateMachine {
533
533
  spawn,
534
534
  input: event.input
535
535
  });
536
- return resolveActionsAndContext(preInitial, initEvent, actorCtx, [assign(assignment)], internalQueue);
536
+ return resolveActionsAndContext(preInitial, initEvent, actorScope, [assign(assignment)], internalQueue);
537
537
  }
538
538
  return preInitial;
539
539
  }
@@ -541,10 +541,10 @@ class StateMachine {
541
541
  /**
542
542
  * Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
543
543
  */
544
- getInitialState(actorCtx, input) {
544
+ getInitialState(actorScope, input) {
545
545
  const initEvent = createInitEvent(input); // TODO: fix;
546
546
  const internalQueue = [];
547
- const preInitialState = this.getPreInitialState(actorCtx, initEvent, internalQueue);
547
+ const preInitialState = this.getPreInitialState(actorScope, initEvent, internalQueue);
548
548
  const nextState = microstep([{
549
549
  target: [...getInitialStateNodes(this.root)],
550
550
  source: this.root,
@@ -552,15 +552,15 @@ class StateMachine {
552
552
  actions: [],
553
553
  eventType: null,
554
554
  toJSON: null // TODO: fix
555
- }], preInitialState, actorCtx, initEvent, true, internalQueue);
555
+ }], preInitialState, actorScope, initEvent, true, internalQueue);
556
556
  const {
557
557
  state: macroState
558
- } = macrostep(nextState, initEvent, actorCtx, internalQueue);
558
+ } = macrostep(nextState, initEvent, actorScope, internalQueue);
559
559
  return macroState;
560
560
  }
561
561
  start(state) {
562
562
  Object.values(state.children).forEach(child => {
563
- if (child.status === 0) {
563
+ if (child.getSnapshot().status === 'active') {
564
564
  child.start();
565
565
  }
566
566
  });
@@ -587,7 +587,7 @@ class StateMachine {
587
587
  createState(stateConfig) {
588
588
  return stateConfig instanceof State ? stateConfig : new State(stateConfig, this);
589
589
  }
590
- restoreState(snapshot, _actorCtx) {
590
+ restoreState(snapshot, _actorScope) {
591
591
  const children = {};
592
592
  const snapshotChildren = snapshot.children;
593
593
  Object.keys(snapshotChildren).forEach(actorId => {
@@ -598,10 +598,10 @@ class StateMachine {
598
598
  if (!logic) {
599
599
  return;
600
600
  }
601
- const actorState = logic.restoreState?.(childState, _actorCtx);
601
+ const actorState = logic.restoreState?.(childState, _actorScope);
602
602
  const actorRef = createActor(logic, {
603
603
  id: actorId,
604
- parent: _actorCtx?.self,
604
+ parent: _actorScope?.self,
605
605
  state: actorState,
606
606
  systemId: actorData.systemId
607
607
  });
@@ -1,10 +1,10 @@
1
1
  export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.esm.js';
2
- import { S as STATE_DELIMITER, m as mapValues, t as toArray, a as toTransitionConfigArray, N as NULL_EVENT, c as createInvokeId, i as isErrorActorEvent, b as createInitEvent, r as resolveReferencedActor, d as createActor, $ as $$ACTOR_TYPE, e as matchesState } from './interpreter-c80ce92e.esm.js';
3
- export { A as Actor, f as ActorStatus, I as InterpreterStatus, d as createActor, g as interpret, e as matchesState, p as pathToStateValue, h as toObserver } from './interpreter-c80ce92e.esm.js';
4
- import { f as formatTransitions, a as formatTransition, m as memo, e as evaluateGuard, g as getDelayedTransitions, b as formatInitialTransition, c as getCandidates, d as getConfiguration, h as getStateNodes, r as resolveStateValue, i as isInFinalState, S as State, j as cloneState, k as macrostep, t as transitionNode, l as getInitialConfiguration, n as resolveActionsAndContext, o as microstep, p as getInitialStateNodes, q as isStateId, s as getStateNodeByPath, u as getPersistedState } from './raise-c989c7fb.esm.js';
5
- export { S as State, v as and, z as cancel, h as getStateNodes, w as not, x as or, A as raise, C as spawn, y as stateIn, B as stop } from './raise-c989c7fb.esm.js';
6
- import { a as assign } from './send-4b616da9.esm.js';
7
- export { S as SpecialTargets, a as assign, c as choose, e as escalate, f as forwardTo, l as log, p as pure, s as sendParent, b as sendTo } from './send-4b616da9.esm.js';
2
+ import { S as STATE_DELIMITER, m as mapValues, t as toArray, a as toTransitionConfigArray, N as NULL_EVENT, c as createInvokeId, i as isErrorActorEvent, b as createInitEvent, r as resolveReferencedActor, d as createActor, $ as $$ACTOR_TYPE, e as matchesState } from './interpreter-1e8c1c0c.esm.js';
3
+ export { A as Actor, f as ActorStatus, I as InterpreterStatus, d as createActor, g as interpret, e as matchesState, p as pathToStateValue, h as toObserver } from './interpreter-1e8c1c0c.esm.js';
4
+ import { f as formatTransitions, a as formatTransition, m as memo, e as evaluateGuard, g as getDelayedTransitions, b as formatInitialTransition, c as getCandidates, d as getConfiguration, h as getStateNodes, r as resolveStateValue, i as isInFinalState, S as State, j as cloneState, k as macrostep, t as transitionNode, l as getInitialConfiguration, n as resolveActionsAndContext, o as microstep, p as getInitialStateNodes, q as isStateId, s as getStateNodeByPath, u as getPersistedState } from './raise-62de3670.esm.js';
5
+ export { S as State, v as and, z as cancel, h as getStateNodes, w as not, x as or, A as raise, C as spawn, y as stateIn, B as stop } from './raise-62de3670.esm.js';
6
+ import { a as assign } from './send-1249d4ac.esm.js';
7
+ export { S as SpecialTargets, a as assign, c as choose, e as escalate, f as forwardTo, l as log, p as pure, s as sendParent, b as sendTo } from './send-1249d4ac.esm.js';
8
8
  import '../dev/dist/xstate-dev.esm.js';
9
9
 
10
10
  class SimulatedClock {
@@ -477,7 +477,7 @@ class StateMachine {
477
477
  * @param state The current State instance or state value
478
478
  * @param event The received event
479
479
  */
480
- transition(state, event, actorCtx) {
480
+ transition(state, event, actorScope) {
481
481
  // TODO: handle error events in a better way
482
482
  if (isErrorActorEvent(event) && !state.nextEvents.some(nextEvent => nextEvent === event.type)) {
483
483
  return cloneState(state, {
@@ -487,7 +487,7 @@ class StateMachine {
487
487
  }
488
488
  const {
489
489
  state: nextState
490
- } = macrostep(state, event, actorCtx);
490
+ } = macrostep(state, event, actorScope);
491
491
  return nextState;
492
492
  }
493
493
 
@@ -498,8 +498,8 @@ class StateMachine {
498
498
  * @param state The current state
499
499
  * @param event The received event
500
500
  */
501
- microstep(state, event, actorCtx) {
502
- return macrostep(state, event, actorCtx).microstates;
501
+ microstep(state, event, actorScope) {
502
+ return macrostep(state, event, actorScope).microstates;
503
503
  }
504
504
  getTransitionData(state, event) {
505
505
  return transitionNode(this.root, state.value, state, event) || [];
@@ -509,7 +509,7 @@ class StateMachine {
509
509
  * The initial state _before_ evaluating any microsteps.
510
510
  * This "pre-initial" state is provided to initial actions executed in the initial state.
511
511
  */
512
- getPreInitialState(actorCtx, initEvent, internalQueue) {
512
+ getPreInitialState(actorScope, initEvent, internalQueue) {
513
513
  const {
514
514
  context
515
515
  } = this.config;
@@ -530,7 +530,7 @@ class StateMachine {
530
530
  spawn,
531
531
  input: event.input
532
532
  });
533
- return resolveActionsAndContext(preInitial, initEvent, actorCtx, [assign(assignment)], internalQueue);
533
+ return resolveActionsAndContext(preInitial, initEvent, actorScope, [assign(assignment)], internalQueue);
534
534
  }
535
535
  return preInitial;
536
536
  }
@@ -538,10 +538,10 @@ class StateMachine {
538
538
  /**
539
539
  * Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
540
540
  */
541
- getInitialState(actorCtx, input) {
541
+ getInitialState(actorScope, input) {
542
542
  const initEvent = createInitEvent(input); // TODO: fix;
543
543
  const internalQueue = [];
544
- const preInitialState = this.getPreInitialState(actorCtx, initEvent, internalQueue);
544
+ const preInitialState = this.getPreInitialState(actorScope, initEvent, internalQueue);
545
545
  const nextState = microstep([{
546
546
  target: [...getInitialStateNodes(this.root)],
547
547
  source: this.root,
@@ -549,15 +549,15 @@ class StateMachine {
549
549
  actions: [],
550
550
  eventType: null,
551
551
  toJSON: null // TODO: fix
552
- }], preInitialState, actorCtx, initEvent, true, internalQueue);
552
+ }], preInitialState, actorScope, initEvent, true, internalQueue);
553
553
  const {
554
554
  state: macroState
555
- } = macrostep(nextState, initEvent, actorCtx, internalQueue);
555
+ } = macrostep(nextState, initEvent, actorScope, internalQueue);
556
556
  return macroState;
557
557
  }
558
558
  start(state) {
559
559
  Object.values(state.children).forEach(child => {
560
- if (child.status === 0) {
560
+ if (child.getSnapshot().status === 'active') {
561
561
  child.start();
562
562
  }
563
563
  });
@@ -584,7 +584,7 @@ class StateMachine {
584
584
  createState(stateConfig) {
585
585
  return stateConfig instanceof State ? stateConfig : new State(stateConfig, this);
586
586
  }
587
- restoreState(snapshot, _actorCtx) {
587
+ restoreState(snapshot, _actorScope) {
588
588
  const children = {};
589
589
  const snapshotChildren = snapshot.children;
590
590
  Object.keys(snapshotChildren).forEach(actorId => {
@@ -595,10 +595,10 @@ class StateMachine {
595
595
  if (!logic) {
596
596
  return;
597
597
  }
598
- const actorState = logic.restoreState?.(childState, _actorCtx);
598
+ const actorState = logic.restoreState?.(childState, _actorScope);
599
599
  const actorRef = createActor(logic, {
600
600
  id: actorId,
601
- parent: _actorCtx?.self,
601
+ parent: _actorScope?.self,
602
602
  state: actorState,
603
603
  systemId: actorData.systemId
604
604
  });
@@ -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).XState={})}(this,(function(t){"use strict";const e=".",s="",n="",i="xstate.init",o="xstate.error",r="xstate.stop";class a{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}}function c(t,e){return{type:`xstate.done.state.${t}`,output:e}}function u(t,e){return{type:`xstate.error.actor.${t}`,data:e}}function f(t){return{type:i,input:t}}function h(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const d=t=>{const e=h();e&&e.register(t)};function p(t){setTimeout((()=>{throw t}))}const l="function"==typeof Symbol&&Symbol.observable||"@@observable";let y=0;function v(t,e){const s=m(t),n=m(e);return"string"==typeof n?"string"==typeof s&&n===s:"string"==typeof s?s in n:Object.keys(s).every((t=>t in n&&v(s[t],n[t])))}function g(t){try{return k(t)?t:t.toString().split(e)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}function m(t){if("object"==typeof(e=t)&&"value"in e&&"context"in e&&"event"in e)return t.value;var e;if(k(t))return _(t);if("string"!=typeof t)return t;return _(g(t))}function _(t){if(1===t.length)return t[0];const e={};let s=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)s[t[e]]=t[e+1];else{const n=s;s={},n[t[e]]=s}return e}function x(t,e){const s={},n=Object.keys(t);for(let i=0;i<n.length;i++){const o=n[i];s[o]=e(t[o],o,t,i)}return s}function b(t){return[].concat(...t)}function S(t){return k(t)?t:[t]}function w(t){return void 0===t?[]:S(t)}function $(t,e,s,n){return"function"==typeof t?t({context:e,event:s,self:n}):t}function k(t){return Array.isArray(t)}function I(t){return S(t).map((t=>void 0===t||"string"==typeof t?{target:t}:t))}function T(t){if(void 0!==t&&t!==s)return w(t)}function E(t,e,s){const n="object"==typeof t,i=n?t:void 0;return{next:(n?t.next:t)?.bind(i),error:(n?t.error:e)?.bind(i),complete:(n?t.complete:s)?.bind(i)}}function O(t,e){return`${t}[${e}]`}function j(t,e){if(e.startsWith("xstate#")){const[,s]=e.match(/\[(\d+)\]$/),n=t.getStateNodeById(e.slice(7,-(s.length+2))).config.invoke;return{src:(Array.isArray(n)?n[s]:n).src,input:void 0}}const s=t.implementations.actors[e];return s?"transition"in s?{src:s,input:void 0}:s:void 0}const A=1;let M=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const P=M,N={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class R{constructor(t,e){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new a(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=M.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 s={...N,...e},{clock:n,logger:i,parent:o,id:r,systemId:c,inspect:u}=s;this.system=o?.system??function(t){const e=new Map,s=new Map,n=new WeakMap,i=new Set,o={_bookId:()=>"x:"+y++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const i=n.get(t);void 0!==i&&(s.delete(i),n.delete(t))},get:t=>s.get(t),_set:(t,e)=>{const i=s.get(t);if(i&&i!==e)throw new Error(`Actor with system ID '${t}' already exists.`);s.set(t,e),n.set(e,t)},inspect:t=>{i.add(t)},_sendInspectionEvent:e=>{const s={...e,rootId:t.sessionId};i.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),u&&!o&&this.system.inspect(E(u)),c&&(this._systemId=c,this.system._set(c,this)),this.sessionId=this.system._bookId(),this.id=r??this.sessionId,this.logger=i,this.clock=n,this._parent=o,this.options=s,this.src=s.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){p(t)}switch(this._state.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(n=this.id,i=this._state.output,{type:`xstate.done.actor.${n}`,output:i}),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,u(this.id,this._state.error))}var n,i;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=E(t,e,s);if(this.status!==M.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){p(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this.status===M.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=M.Running;const t=f(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(u(this.id,t)),this}return this.update(this._state,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._state,t,this._actorContext)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._stopProcedure(),this._error(t),void this._parent?.send(u(this.id,t))}this.update(e,t),t.type===r&&(this._stopProcedure(),this._complete())}_stop(){return this.status===M.Stopped?this:(this.mailbox.clear(),this.status===M.NotStarted?(this.status=M.Stopped,this):(this.mailbox.enqueue({type:r}),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){p(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||p(t));let e=!1;for(const s of this.observers){const n=s.error;e||=!n;try{n?.(t)}catch(t){p(t)}}this.observers.clear(),e&&p(t)}_stopProcedure(){if(this.status!==M.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new a(this._process.bind(this)),this.status=M.Stopped,this.system._unregister(this),this}_send(t){this.status!==M.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}delaySend(t){const{event:e,id:s,delay:n}=t,i=this.clock.setTimeout((()=>{this.system._relay(this,t.to??this,e)}),n);s&&(this.delayedEventsMap[s]=i)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:d)(this)}}toJSON(){return{xstate$$type:A,id:this.id}}getPersistedState(){return this.logic.getPersistedState(this._state)}[l](){return this}getSnapshot(){return this._state}}function C(t,e){return new R(t,e)}const D=C,V=new WeakMap;function J(t,e,s){let n=V.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},V.set(t,n)),n[e]}function B(t,e,s,n,{sendId:i}){return[e,"function"==typeof i?i(s,n):i]}function z(t,e){t.self.cancel(e)}function W(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=B,e.execute=z,e}function U(t,e,s,n,{id:i,systemId:o,src:r,input:a,syncSnapshot:c}){const u="string"==typeof r?j(e.machine,r):{src:r,input:void 0},f="function"==typeof i?i(s):i;let h;if(u){const n=a||u.input;h=C(u.src,{id:f,src:"string"==typeof r?r:void 0,parent:t?.self,systemId:o,input:"function"==typeof n?n({context:e.context,event:s.event,self:t?.self}):n}),c&&h.subscribe({next:e=>{"active"===e.status&&t.self.send({type:`xstate.snapshot.${i}`,snapshot:e})},error:()=>{}})}return[zt(e,{children:{...e.children,[f]:h}}),{id:i,actorRef:h}]}function q(t,{id:e,actorRef:s}){s&&t.defer((()=>{if(s.status!==M.Stopped)try{s.start?.()}catch(s){return void t.self.send(u(e,s))}}))}function G(...[t,{id:e,systemId:s,input:n,syncSnapshot:i=!1}={}]){function o(t,e){}return o.type="xstate.spawn",o.id=e,o.systemId=s,o.src=t,o.input=n,o.syncSnapshot=i,o.resolve=U,o.execute=q,o}function Q(t,e,s,n,{actorRef:i}){const o="function"==typeof i?i(s,n):i,r="string"==typeof o?e.children[o]:o;let a=e.children;return r&&(a={...a},delete a[r.id]),[zt(e,{children:a}),r]}function F(t,e){e&&(t.system._unregister(e),e.status===M.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function X(t){function e(t,e){}return e.type="xstate.stop",e.actorRef=t,e.resolve=Q,e.execute=F,e}function H(t,e,{stateValue:s}){if("string"==typeof s&&ct(s)){const e=t.machine.getStateNodeById(s);return t.configuration.some((t=>t===e))}return t.matches(s)}function K(t,{context:e,event:s},{guards:n}){return!Z(n[0],e,s,t)}function L(t,{context:e,event:s},{guards:n}){return n.every((n=>Z(n,e,s,t)))}function Y(t,{context:e,event:s},{guards:n}){return n.some((n=>Z(n,e,s,t)))}function Z(t,e,s,n){const{machine:i}=n,o="function"==typeof t,r=o?t:i.implementations.guards["string"==typeof t?t:t.type];if(!o&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return Z(r,e,s,n);const a={context:e,event:s},c=o||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:e,event:s}):t.params:void 0;if(!("check"in r))return r(a,c);return r.check(n,a,r)}const tt=t=>"atomic"===t.type||"final"===t.type;function et(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function st(t,e){const s=[];if(e===t)return s;let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function nt(t){const e=new Set(t),s=new Set(t),n=ot(s);for(const t of e)if("compound"!==t.type||n.get(t)&&n.get(t).length){if("parallel"===t.type)for(const e of et(t))if("history"!==e.type&&!s.has(e)){const t=pt(e);for(const e of t)s.add(e)}}else pt(t).forEach((t=>s.add(t)));for(const t of s){let e=t.parent;for(;e;)s.add(e),e=e.parent}return s}function it(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(tt(t))return t.key}const n={};for(const t of s)n[t.key]=it(t,e);return n}function ot(t){const e=new Map;for(const s of t)e.has(s)||e.set(s,[]),s.parent&&(e.has(s.parent)||e.set(s.parent,[]),e.get(s.parent).push(s));return e}function rt(t,e){return it(t,ot(nt(e)))}function at(t,e){return"compound"===e.type?et(e).some((e=>"final"===e.type&&t.has(e))):"parallel"===e.type?et(e).every((e=>at(t,e))):"final"===e.type}const ct=t=>"#"===t[0];function ut(t){const e=t.config.after;if(!e)return[];return Object.keys(e).flatMap(((s,n)=>{const i=e[s],o="string"==typeof i?{target:i}:i,r=isNaN(+s)?s:+s,a=((e,s)=>{const n=(o="function"==typeof e?`${t.id}:delay[${s}]`:e,r=t.id,{type:`xstate.after(${o})${r?`#${r}`:""}`}),i=n.type;var o,r;return t.entry.push(Yt(n,{id:i,delay:e})),t.exit.push(W(i)),i})(r,n);return w(o).map((t=>({...t,event:a,delay:r})))})).map((e=>{const{delay:s}=e;return{...ft(t,e.event,e),delay:s}}))}function ft(t,s,n){const i=T(n.target),o=n.reenter??!1,r=function(t,s){if(void 0===s)return;return s.map((s=>{if("string"!=typeof s)return s;if(ct(s))return t.machine.getStateNodeById(s);const n=s[0]===e;if(n&&!t.parent)return vt(t,s.slice(1));const i=n?t.key+s:s;if(!t.parent)throw new Error(`Invalid target: "${s}" is not a valid target from the root node. Did you mean ".${s}"?`);try{return vt(t.parent,i)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}}))}(t,i),a={...n,actions:w(n.actions),guard:n.guard,target:r,source:t,reenter:o,eventType:s,toJSON:()=>({...a,source:`#${t.id}`,target:r?r.map((t=>`#${t.id}`)):void 0})};return a}function ht(t){const e=T(t.config.target);return e?{target:e.map((e=>"string"==typeof e?vt(t.parent,e):e))}:t.parent.initial}function dt(t){return"history"===t.type}function pt(t){const e=lt(t);for(const s of e)for(const n of st(s,t))e.add(n);return e}function lt(t){const e=new Set;return function t(s){if(!e.has(s))if(e.add(s),"compound"===s.type)t(s.initial.target[0]);else if("parallel"===s.type)for(const e of et(s))t(e)}(t),e}function yt(t,e){if(ct(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);const s=t.states[e];if(!s)throw new Error(`Child state '${e}' does not exist on '${t.id}'`);return s}function vt(t,e){if("string"==typeof e&&ct(e))try{return t.machine.getStateNodeById(e)}catch(t){}const s=g(e).slice();let n=t;for(;s.length;){const t=s.shift();if(!t.length)break;n=yt(n,t)}return n}function gt(t,e){const s=e instanceof Bt?e.value:m(e);if("string"==typeof s)return[t,t.states[s]];const n=Object.keys(s),i=n.map((e=>yt(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,n.reduce(((e,n)=>{const i=yt(t,n);if(!i)return e;const o=gt(i,s[n]);return e.concat(o)}),[]))}function mt(t,e,s,n){return"string"==typeof e?function(t,e,s,n){const i=yt(t,e).next(s,n);return i&&i.length?i:t.next(s,n)}(t,e,s,n):1===Object.keys(e).length?function(t,e,s,n){const i=Object.keys(e),o=mt(yt(t,i[0]),e[i[0]],s,n);return o&&o.length?o:t.next(s,n)}(t,e,s,n):function(t,e,s,n){const i=[];for(const o of Object.keys(e)){const r=e[o];if(!r)continue;const a=mt(yt(t,o),r,s,n);a&&i.push(...a)}return i.length?i:t.next(s,n)}(t,e,s,n)}function _t(t){return Object.keys(t.states).map((e=>t.states[e])).filter((t=>"history"===t.type))}function xt(t,e){let s=t;for(;s.parent&&s.parent!==e;)s=s.parent;return s.parent===e}function bt(t){const e=[];let s=t.parent;for(;s;)e.unshift(s),s=s.parent;return e}function St(t,e){const s=new Set(t),n=new Set(e);for(const t of s)if(n.has(t))return!0;for(const t of n)if(s.has(t))return!0;return!1}function wt(t,e,s){const n=new Set;for(const i of t){let t=!1;const o=new Set;for(const r of n)if(St(It([i],e,s),It([r],e,s))){if(!xt(i.source,r.source)){t=!0;break}o.add(r)}if(!t){for(const t of o)n.delete(t);n.add(i)}}return Array.from(n)}function $t(t,e){if(!t.target)return[];const s=new Set;for(const n of t.target)if(dt(n))if(e[n.id])for(const t of e[n.id])s.add(t);else for(const t of $t(ht(n),e))s.add(t);else s.add(n);return[...s]}function kt(t,e){const s=$t(t,e);if(!s)return;if(!t.reenter&&s.every((e=>e===t.source||xt(e,t.source))))return t.source;return function(t){const[e]=t;let s=bt(e),n=[];for(const e of t){const t=bt(e);n=s.filter((e=>t.includes(e))),s=n,n=[]}return s[s.length-1]}(s.concat(t.source))}function It(t,e,s){const n=new Set;for(const i of t)if(i.target?.length){const t=kt(i,s);i.reenter&&i.source===t&&n.add(t);for(const s of e)xt(s,t)&&n.add(s)}return[...n]}function Tt(t,e,s,n,i,o){if(!t.length)return e;const r=new Set(e.configuration);let a=e.historyValue;const u=wt(t,r,a);let f=e;i||([f,a]=function(t,e,s,n,i,o,r){let a=t;const c=It(n,i,o);let u;c.sort(((t,e)=>e.order-t.order));for(const t of c)for(const e of _t(t)){let s;s="deep"===e.history?e=>tt(e)&&xt(e,t):e=>e.parent===t,u??={...o},u[e.id]=Array.from(i).filter(s)}for(const t of c)a=Pt(a,e,s,[...t.exit,...t.invoke.map((t=>X(t.id)))],r),i.delete(t);return[a,u||o]}(f,n,s,u,r,a,o)),f=Pt(f,n,s,u.flatMap((t=>t.actions)),o),f=function(t,e,s,n,i,o,r,a){let u=t;const f=new Set,h=new Set;Ot(n,r,h,f),a&&h.add(t.machine.root);const d=new Set;for(const n of[...f].sort(((t,e)=>t.order-e.order))){i.add(n);const r=[];r.push(...n.entry);for(const t of n.invoke)r.push(G(t.src,{...t,syncSnapshot:!!t.onSnapshot}));if(h.has(n)){const t=n.initial.actions;r.push(...t)}if(u=Pt(u,e,s,r,o,n.invoke.map((t=>t.id))),"final"===n.type){const r=n.parent;let a="parallel"===r?.type?r:r?.parent,f=a||n;for("compound"===r?.type&&o.push(c(r.id,n.output?$(n.output,u.context,e,s.self):void 0));"parallel"===a?.type&&!d.has(a)&&at(i,a);)d.add(a),o.push(c(a.id)),f=a,a=a.parent;if(a)continue;u=zt(u,{status:"done",output:Et(u,e,s,t.configuration[0].machine.root,f)})}}return u}(f,n,s,u,r,o,a,i);const h=[...r];"done"===f.status&&(f=Pt(f,n,s,h.sort(((t,e)=>e.order-t.order)).flatMap((t=>t.exit)),o));try{return a===e.historyValue&&function(t,e){if(t.length!==e.size)return!1;for(const s of t)if(!e.has(s))return!1;return!0}(e.configuration,r)?f:zt(f,{configuration:h,historyValue:a})}catch(t){throw t}}function Et(t,e,s,n,i){if(!n.output)return;const o=c(i.id,i.output&&i.parent?$(i.output,t.context,e,s.self):void 0);return $(n.output,t.context,o,s.self)}function Ot(t,e,s,n){for(const i of t){const t=kt(i,e);for(const o of i.target||[])dt(o)||i.source===o&&i.source===t&&!i.reenter||(n.add(o),s.add(o)),jt(o,e,s,n);const o=$t(i,e);for(const i of o)At(i,t,n,e,s)}}function jt(t,e,s,n){if(dt(t))if(e[t.id]){const i=e[t.id];for(const t of i)n.add(t),jt(t,e,s,n);for(const o of i)At(o,t.parent,n,e,s)}else{const i=ht(t);for(const o of i.target)n.add(o),i===t.parent?.initial&&s.add(t.parent),jt(o,e,s,n);for(const o of i.target)At(o,t,n,e,s)}else if("compound"===t.type){const[i]=t.initial.target;dt(i)||(n.add(i),s.add(i)),jt(i,e,s,n),At(i,t,n,e,s)}else if("parallel"===t.type)for(const i of et(t).filter((t=>!dt(t))))[...n].some((t=>xt(t,i)))||(dt(i)||(n.add(i),s.add(i)),jt(i,e,s,n))}function At(t,e,s,n,i){const o=st(t,e);for(const t of o)if(s.add(t),"parallel"===t.type)for(const e of et(t).filter((t=>!dt(t))))[...s].some((t=>xt(t,e)))||(s.add(e),jt(e,n,i,s))}function Mt(t,e,s,n,i,o){const{machine:r}=t;let a=t;for(const t of n){const n="function"==typeof t,c=n?t:r.implementations.actions["string"==typeof t?t:t.type];if(!c)continue;const u={context:a.context,event:e,self:s?.self,system:s?.system},f=n||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:a.context,event:e}):t.params:void 0;if(!("resolve"in c)){s?.self.status===M.Running?c(u,f):s?.defer((()=>{c(u,f)}));continue}const h=c,[d,p,l]=h.resolve(s,a,u,f,c,i);a=d,"retryResolve"in h&&o?.push([h,p]),"execute"in h&&(s?.self.status===M.Running?h.execute(s,p):s?.defer(h.execute.bind(null,s,p))),l&&(a=Mt(a,e,s,l,i,o))}return a}function Pt(t,e,s,n,i,o){const r=o?[]:void 0,a=Mt(t,e,s,n,{internalQueue:i,deferredActorIds:o},r);return r?.forEach((([t,e])=>{t.retryResolve(s,a,e)})),a}function Nt(t,e,s,n=[]){let o=t;const a=[];if(e.type===r)return o=zt(Rt(o,e,s),{status:"stopped"}),a.push(o),{state:o,microstates:a};let c=e;if(c.type!==i){o=Tt(Ct(c,o),t,s,c,!1,n),a.push(o)}let u=!0;for(;"active"===o.status;){let t=u?Dt(o,c):[];const e=t.length?o:void 0;if(!t.length){if(!n.length)break;c=n.shift(),t=Ct(c,o)}o=Tt(t,o,s,c,!1,n),u=o!==e,a.push(o)}return"active"!==o.status&&Rt(o,c,s),{state:o,microstates:a}}function Rt(t,e,s){return Pt(t,e,s,Object.values(t.children).map((t=>X(t))),[])}function Ct(t,e){return e.machine.getTransitionData(e,t)}function Dt(t,e){const s=new Set,n=t.configuration.filter(tt);for(const i of n)t:for(const n of[i].concat(st(i,void 0)))if(n.always)for(const i of n.always)if(void 0===i.guard||Z(i.guard,t.context,e,t)){s.add(i);break t}return wt(Array.from(s),new Set(t.configuration),t.historyValue)}function Vt(t,e){return rt(t,[...nt(gt(t,e))])}function Jt(t){const e=[],s=t.initial,n=new Set;Ot([s],{},new Set([t]),n);for(const t of[...n].sort(((t,e)=>t.order-e.order)))e.push(t);return e}class Bt{static from(t,e={},s){if(t instanceof Bt)return t.context!==e?new Bt({value:t.value,context:e,meta:{},configuration:[],children:{},status:"active"},s):t;const n=nt(gt(s.root,t));return new Bt({value:t,context:e,meta:void 0,configuration:Array.from(n),children:{},status:"active"},s)}constructor(t,e){this.machine=e,this.tags=void 0,this.value=void 0,this.status=void 0,this.error=void 0,this.context=void 0,this.historyValue={},this.configuration=void 0,this.children=void 0,this.context=t.context,this.historyValue=t.historyValue||{},this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(nt(gt(e.root,t.value))),this.children=t.children,this.value=rt(e.root,this.configuration),this.tags=new Set(b(this.configuration.map((t=>t.tags)))),this.status=t.status,this.output=t.output,this.error=t.error}toStrings(t=this.value){if("string"==typeof t)return[t];const s=Object.keys(t);return s.concat(...s.map((s=>this.toStrings(t[s]).map((t=>s+e+t)))))}toJSON(){const{configuration:t,tags:e,machine:s,...n}=this;return{...n,tags:Array.from(e),meta:this.meta}}matches(t){return v(t,this.value)}hasTag(t){return this.tags.has(t)}can(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))}get nextEvents(){return J(this,"nextEvents",(()=>[...new Set(b([...this.configuration.map((t=>t.ownEvents))]))]))}get meta(){return this.configuration.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})}}function zt(t,e={}){return new Bt({...t,...e},t.machine)}function Wt(t){let e;for(const s in t){const n=t[s];if(n&&"object"==typeof n)if("sessionId"in n&&"send"in n&&"ref"in n)e??=Array.isArray(t)?t.slice():{...t},e[s]={xstate$$type:A,id:n.id};else{const i=Wt(n);i!==n&&(e??=Array.isArray(t)?t.slice():{...t},e[s]=i)}}return e??t}function Ut(t,{machine:e,context:s},n,i){return(o,r)=>{const a=((o,r={})=>{const{systemId:a}=r;if("string"==typeof o){const c=j(e,o);if(!c)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const u="input"in r?r.input:c.input,f=C(c.src,{id:r.id,parent:t.self,input:"function"==typeof u?u({context:s,event:n,self:t.self}):u,src:o,systemId:a});return i[f.id]=f,r.syncSnapshot&&f.subscribe({next:e=>{"active"===e.status&&t.self.send({type:`xstate.snapshot.${f.id}`,snapshot:e})},error:()=>{}}),f}{const e=C(o,{id:r.id,parent:t.self,input:r.input,src:void 0,systemId:a});return r.syncSnapshot&&e.subscribe({next:s=>{"active"===s.status&&t.self.send({type:`xstate.snapshot.${e.id}`,snapshot:s,id:e.id})},error:()=>{}}),e}})(o,r);return i[a.id]=a,t.defer((()=>{if(a.status!==M.Stopped)try{a.start?.()}catch(e){return void t.self.send(u(a.id,e))}})),a}}function qt(t,e,s,n,{assignment:i}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const o={},r={context:e.context,event:s.event,spawn:Ut(t,e,s.event,o),self:t?.self,system:t?.system};let a={};if("function"==typeof i)a=i(r,n);else for(const t of Object.keys(i)){const e=i[t];a[t]="function"==typeof e?e(r,n):e}return[zt(e,{context:Object.assign({},e.context,a),children:Object.keys(o).length?{...e.children,...o}:e.children})]}function Gt(t){function e(t,e){}return e.type="xstate.assign",e.assignment=t,e.resolve=qt,e}function Qt(t,e,s,n,{branches:i}){const o=i.find((t=>!t.guard||Z(t.guard,e.context,s.event,e)))?.actions;return[e,void 0,w(o)]}function Ft(t,e,s,n,{value:i,label:o}){return[e,{value:"function"==typeof i?i(s,n):i,label:o}]}function Xt({logger:t},{value:e,label:s}){s?t(s,e):t(e)}function Ht(t,e,s,n,{get:i}){return[e,void 0,w(i({context:s.context,event:s.event}))]}function Kt(t,e,s,n,{event:i,id:o,delay:r},{internalQueue:a}){const c=e.machine.implementations.delays;if("string"==typeof i)throw new Error(`Only event objects may be used with raise; use raise({ type: "${i}" }) instead`);const u="function"==typeof i?i(s,n):i;let f;if("string"==typeof r){const t=c&&c[r];f="function"==typeof t?t(s,n):t}else f="function"==typeof r?r(s,n):r;return"number"!=typeof f&&a.push(u),[e,{event:u,id:o,delay:f}]}function Lt(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}function Yt(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=Kt,s.execute=Lt,s}let Zt=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function te(t,e,s,n,{to:i,event:o,id:r,delay:a},c){const u=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${o}" }) instead`);const f="function"==typeof o?o(s,n):o;let h;if("string"==typeof a){const t=u&&u[a];h="function"==typeof t?t(s,n):t}else h="function"==typeof a?a(s,n):a;const d="function"==typeof i?i(s,n):i;let p;if("string"==typeof d){if(p=d===Zt.Parent?t?.self._parent:d===Zt.Internal?t?.self:d.startsWith("#_")?e.children[d.slice(2)]:c.deferredActorIds?.includes(d)?d:e.children[d],!p)throw new Error(`Unable to send event to actor '${d}' from machine '${e.machine.id}'.`)}else p=d||t?.self;return[e,{to:p,event:f,id:r,delay:h}]}function ee(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function se(t,e){"number"!=typeof e.delay?t.defer((()=>{const{to:s,event:n}=e;t?.system._relay(t.self,s,n.type===o?u(t.self.id,n.data):n)})):t.self.delaySend(e)}function ne(t,e,s){function n(t,e){}return n.type="xstate.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=te,n.retryResolve=ee,n.execute=se,n}function ie(t,e){return ne(Zt.Parent,t,e)}function oe(t,e){return{config:t,transition:(e,s,n)=>({...e,context:t(e.context,s,n)}),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 re="$$xstate.resolve",ae="$$xstate.reject";const ce=oe((t=>{}),void 0);const ue={},fe=t=>"string"==typeof t?{type:t}:"function"==typeof t?"resolve"in t?{type:t.type}:{type:t.name}:t;class he{constructor(t,s){if(this.config=t,this.key=void 0,this.id=void 0,this.type=void 0,this.path=void 0,this.states=void 0,this.history=void 0,this.entry=void 0,this.exit=void 0,this.parent=void 0,this.machine=void 0,this.meta=void 0,this.output=void 0,this.order=-1,this.description=void 0,this.tags=[],this.transitions=void 0,this.always=void 0,this.parent=s._parent,this.key=s._key,this.machine=s._machine,this.path=this.parent?this.parent.path.concat(this.key):[],this.id=this.config.id||[this.machine.id,...this.path].join(e),this.type=this.config.type||(this.config.states&&Object.keys(this.config.states).length?"compound":this.config.history?"history":"atomic"),this.description=this.config.description,this.order=this.machine.idMap.size,this.machine.idMap.set(this.id,this),this.states=this.config.states?x(this.config.states,((t,e)=>new he(t,{_parent:this,_key:e,_machine:this.machine}))):ue,"compound"===this.type&&!this.config.initial)throw new Error(`No initial state specified for compound state node "#${this.id}". Try adding { initial: "${Object.keys(this.states)[0]}" } to the state config.`);this.history=!0===this.config.history?"shallow":this.config.history||!1,this.entry=w(this.config.entry).slice(),this.exit=w(this.config.exit).slice(),this.meta=this.config.meta,this.output="final"!==this.type&&this.parent?void 0:this.config.output,this.tags=w(t.tags).slice()}_initialize(){this.transitions=function(t){const e=new Map;if(t.config.on)for(const s of Object.keys(t.config.on)){if(s===n)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const i=t.config.on[s];e.set(s,I(i).map((e=>ft(t,s,e))))}if(t.config.onDone){const s=`xstate.done.state.${t.id}`;e.set(s,I(t.config.onDone).map((e=>ft(t,s,e))))}for(const s of t.invoke){if(s.onDone){const n=`xstate.done.actor.${s.id}`;e.set(n,I(s.onDone).map((e=>ft(t,n,e))))}if(s.onError){const n=`xstate.error.actor.${s.id}`;e.set(n,I(s.onError).map((e=>ft(t,n,e))))}if(s.onSnapshot){const n=`xstate.snapshot.${s.id}`;e.set(n,I(s.onSnapshot).map((e=>ft(t,n,e))))}}for(const s of t.after){let t=e.get(s.eventType);t||(t=[],e.set(s.eventType,t)),t.push(s)}return e}(this),this.config.always&&(this.always=I(this.config.always).map((t=>ft(this,n,t)))),Object.keys(this.states).forEach((t=>{this.states[t]._initialize()}))}get definition(){return{id:this.id,key:this.key,version:this.machine.version,type:this.type,initial:this.initial?{target:this.initial.target,source:this,actions:this.initial.actions.map(fe),eventType:null,reenter:!1,toJSON:()=>({target:this.initial.target.map((t=>`#${t.id}`)),source:`#${this.id}`,actions:this.initial.actions.map(fe),eventType:null})}:void 0,history:this.history,states:x(this.states,(t=>t.definition)),on:this.on,transitions:[...this.transitions.values()].flat().map((t=>({...t,actions:t.actions.map(fe)}))),entry:this.entry.map(fe),exit:this.exit.map(fe),meta:this.meta,order:this.order||-1,output:this.output,invoke:this.invoke,description:this.description,tags:this.tags}}toJSON(){return this.definition}get invoke(){return J(this,"invoke",(()=>w(this.config.invoke).map(((t,e)=>{const{src:s,systemId:n}=t,i=t.id||O(this.id,e),o="string"==typeof s?s:`xstate#${O(this.id,e)}`;return{...t,src:o,id:i,systemId:n,toJSON(){const{onDone:e,onError:s,...n}=t;return{...n,type:"xstate.invoke",src:o,id:i}}}}))))}get on(){return J(this,"on",(()=>[...this.transitions].flatMap((([t,e])=>e.map((e=>[t,e])))).reduce(((t,[e,s])=>(t[e]=t[e]||[],t[e].push(s),t)),{})))}get after(){return J(this,"delayedTransitions",(()=>ut(this)))}get initial(){return J(this,"initial",(()=>function(t,e){const s="string"==typeof e?t.states[e]:e?t.states[e.target]:void 0;if(!s&&e)throw new Error(`Initial state node "${e}" not found on parent state node #${t.id}`);const n={source:t,actions:e&&"string"!=typeof e?w(e.actions):[],eventType:null,reenter:!1,target:s?[s]:[],toJSON:()=>({...n,source:`#${t.id}`,target:s?[`#${s.id}`]:[]})};return n}(this,this.config.initial)))}next(t,e){const s=e.type,n=[];let i;const o=J(this,`candidates-${s}`,(()=>{return e=s,(t=this).transitions.get(e)||[...t.transitions.keys()].filter((t=>{if("*"===t)return!0;if(!t.endsWith(".*"))return!1;const s=t.split("."),n=e.split(".");for(let t=0;t<s.length;t++){const e=s[t],i=n[t];if("*"===e)return t===s.length-1;if(e!==i)return!1}return!0})).sort(((t,e)=>e.length-t.length)).flatMap((e=>t.transitions.get(e)));var t,e}));for(const r of o){const{guard:o}=r,a=t.context;let c=!1;try{c=!o||Z(o,a,e,t)}catch(t){const e="string"==typeof o?o:"object"==typeof o?o.type:void 0;throw new Error(`Unable to evaluate guard ${e?`'${e}' `:""}in transition for event '${s}' in state node '${this.id}':\n${t.message}`)}if(c){n.push(...r.actions),i=r;break}}return i?[i]:void 0}get events(){return J(this,"events",(()=>{const{states:t}=this,e=new Set(this.ownEvents);if(t)for(const s of Object.keys(t)){const n=t[s];if(n.states)for(const t of n.events)e.add(`${t}`)}return Array.from(e)}))}get ownEvents(){const t=new Set([...this.transitions.keys()].filter((t=>this.transitions.get(t).some((t=>!(!t.target&&!t.actions.length&&!t.reenter))))));return Array.from(t)}}class de{constructor(t,e){this.config=t,this.version=void 0,this.implementations=void 0,this.types=void 0,this.__xstatenode=!0,this.idMap=new Map,this.root=void 0,this.id=void 0,this.states=void 0,this.events=void 0,this.__TContext=void 0,this.__TEvent=void 0,this.__TActor=void 0,this.__TAction=void 0,this.__TGuard=void 0,this.__TDelay=void 0,this.__TTag=void 0,this.__TInput=void 0,this.__TOutput=void 0,this.__TResolvedTypesMeta=void 0,this.id=t.id||"(machine)",this.implementations={actors:e?.actors??{},actions:e?.actions??{},delays:e?.delays??{},guards:e?.guards??{}},this.version=this.config.version,this.types=this.config.types??{},this.transition=this.transition.bind(this),this.getInitialState=this.getInitialState.bind(this),this.restoreState=this.restoreState.bind(this),this.start=this.start.bind(this),this.getPersistedState=this.getPersistedState.bind(this),this.root=new he(t,{_key:this.id,_machine:this}),this.root._initialize(),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:s,actors:n,delays:i}=this.implementations;return new de(this.config,{actions:{...e,...t.actions},guards:{...s,...t.guards},actors:{...n,...t.actors},delays:{...i,...t.delays}})}resolveState(t){const e=nt(gt(this.root,t.value)),s=Array.from(e);return this.createState({...t,value:Vt(this.root,t.value),configuration:s,status:at(e,this.root)?"done":t.status})}resolveStateValue(t,...[e]){const s=Vt(this.root,t);return this.resolveState(Bt.from(s,e,this))}transition(t,e,s){if(function(t){return t.type.startsWith("xstate.error.actor")}(e)&&!t.nextEvents.some((t=>t===e.type)))return zt(t,{status:"error",error:e.data});const{state:n}=Nt(t,e,s);return n}microstep(t,e,s){return Nt(t,e,s).microstates}getTransitionData(t,e){return mt(this.root,t.value,t,e)||[]}getPreInitialState(t,e,s){const{context:n}=this.config,i=this.resolveState(this.createState({value:{},context:"function"!=typeof n&&n?n:{},meta:void 0,configuration:Jt(this.root),children:{},status:"active"}));if("function"==typeof n){const o=({spawn:t,event:e})=>n({spawn:t,input:e.input});return Pt(i,e,t,[Gt(o)],s)}return i}getInitialState(t,e){const s=f(e),n=[],i=this.getPreInitialState(t,s,n),o=Tt([{target:[...lt(this.root)],source:this.root,reenter:!0,actions:[],eventType:null,toJSON:null}],i,t,s,!0,n),{state:r}=Nt(o,s,t,n);return r}start(t){Object.values(t.children).forEach((t=>{0===t.status&&t.start()}))}getStateNodeById(t){const s=t.split(e),n=s.slice(1),i=ct(s[0])?s[0].slice(1):s[0],o=this.idMap.get(i);if(!o)throw new Error(`Child state node '#${i}' does not exist on machine '${this.id}'`);return vt(o,n)}get definition(){return this.root.definition}toJSON(){return this.definition}getPersistedState(t){return function(t){const{configuration:e,tags:s,machine:n,children:i,context:o,...r}=t,a={};for(const t in i){const e=i[t];a[t]={state:e.getPersistedState(),src:e.src,systemId:e._systemId}}return{...r,context:Wt(o),children:a}}(t)}createState(t){return t instanceof Bt?t:new Bt(t,this)}restoreState(t,e){const s={},n=t.children;Object.keys(n).forEach((t=>{const i=n[t],o=i.state,r=i.src,a=r?j(this,r)?.src:void 0;if(!a)return;const c=a.restoreState?.(o,e),u=C(a,{id:t,parent:e?.self,state:c,systemId:i.systemId});s[t]=u}));const i=this.createState(new Bt({...t,children:s},this));let o=new Set;return function t(e,s){if(!o.has(e)){o.add(e);for(let n in e){const i=e[n];if(i&&"object"==typeof i){if("xstate$$type"in i&&i.xstate$$type===A){e[n]=s[i.id];continue}t(i,s)}}}}(i.context,s),i}}const pe={timeout:1e4};t.Actor=R,t.ActorStatus=M,t.InterpreterStatus=P,t.SimulatedClock=class{constructor(){this.timeouts=new Map,this._now=0,this._id=0}now(){return this._now}getId(){return this._id++}setTimeout(t,e){const s=this.getId();return this.timeouts.set(s,{start:this.now(),timeout:e,fn:t}),s}clearTimeout(t){this.timeouts.delete(t)}set(t){if(this._now>t)throw new Error("Unable to travel back in time");this._now=t,this.flushTimeouts()}flushTimeouts(){[...this.timeouts].sort((([t,e],[s,n])=>{const i=e.start+e.timeout;return n.start+n.timeout>i?-1:1})).forEach((([t,e])=>{this.now()-e.start>=e.timeout&&(this.timeouts.delete(t),e.fn.call(null))}))}increment(t){this._now+=t,this.flushTimeouts()}},t.SpecialTargets=Zt,t.State=Bt,t.StateMachine=de,t.StateNode=he,t.and=function(t){function e(t,e){return!1}return e.check=L,e.guards=t,e},t.assign=Gt,t.cancel=W,t.choose=function(t){function e(t,e){}return e.type="xstate.choose",e.branches=t,e.resolve=Qt,e},t.createActor=C,t.createEmptyActor=function(){return C(ce)},t.createMachine=function(t,e){return new de(t,e)},t.escalate=function(t,e){return ie((e=>({type:o,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return ne(t,(({event:t})=>t),e)},t.fromCallback=function(t){return{config:t,start:(t,{self:e,system:s})=>{s._relay(e,e,{type:"xstate.create"})},transition:(e,s,{self:n,system:i})=>{if("xstate.create"===s.type){const s=t=>{"stopped"!==e.status&&n._parent&&i._relay(n,n._parent,t)},o=t=>{e._receivers.add(t)};return e._dispose=t({input:e.input,system:i,self:n,sendBack:s,receive:o}),e}return s.type===r?("function"==typeof(e={...e,status:"stopped",error:void 0})._dispose&&e._dispose(),e):(e._receivers.forEach((t=>t(s))),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",s="$$xstate.complete";return{config:t,transition:(t,n)=>{if("active"!==t.status)return t;switch(n.type){case e:return{...t,status:"error",error:n.data,input:void 0,_subscription:void 0};case s:return{...t,status:"done",input:void 0,_subscription:void 0};case r: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:(n,{self:i,system:o})=>{"done"!==n.status&&(n._subscription=t({input:n.input,system:o,self:i}).subscribe({next:t=>{i._parent&&o._relay(i,i._parent,t)},error:t=>{o._relay(i,i,{type:e,data:t})},complete:()=>{o._relay(i,i,{type:s})}}))},getPersistedState:({_subscription:t,...e})=>e,restoreState:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){const e="$$xstate.next",s="$$xstate.error",n="$$xstate.complete";return{config:t,transition:(t,i,{self:o,id:a,defer:c,system:u})=>{if("active"!==t.status)return t;switch(i.type){case e:return{...t,context:i.data};case s:return{...t,status:"error",error:i.data,input:void 0,_subscription:void 0};case n:return{...t,status:"done",input:void 0,_subscription:void 0};case r: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:(i,{self:o,system:r})=>{"done"!==i.status&&(i._subscription=t({input:i.input,system:r,self:o}).subscribe({next:t=>{r._relay(o,o,{type:e,data:t})},error:t=>{r._relay(o,o,{type:s,data:t})},complete:()=>{r._relay(o,o,{type:n})}}))},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 re:{const s=e.data;return{...t,status:"done",output:s,input:void 0}}case ae:return{...t,status:"error",error:e.data,input:void 0};case r:return{...t,status:"stopped",input:void 0};default:return t}},start:(e,{self:s,system:n})=>{if("active"!==e.status)return;Promise.resolve(t({input:e.input,system:n,self:s})).then((t=>{"active"===s.getSnapshot().status&&n._relay(s,s,{type:re,data:t})}),(t=>{"active"===s.getSnapshot().status&&n._relay(s,s,{type:ae,data:t})}))},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,input:e}),getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=oe,t.getStateNodes=gt,t.interpret=D,t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function s(t,e){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=Ft,s.execute=Xt,s},t.mapState=function(t,e){let s;for(const n of Object.keys(t))v(n,e)&&(!s||e.length>s.length)&&(s=n);return t[s]},t.matchesState=v,t.not=function(t){function e(t,e){return!1}return e.check=K,e.guards=[t],e},t.or=function(t){function e(t,e){return!1}return e.check=Y,e.guards=t,e},t.pathToStateValue=_,t.pure=function(t){function e(t,e){}return e.type="xstate.pure",e.get=t,e.resolve=Ht,e},t.raise=Yt,t.sendParent=ie,t.sendTo=ne,t.spawn=G,t.stateIn=function(t){function e(t,e){return!1}return e.check=H,e.stateValue=t,e},t.stop=X,t.toObserver=E,t.waitFor=function(t,e,s){const n={...pe,...s};return new Promise(((s,i)=>{let o=!1;const r=n.timeout===1/0?void 0:setTimeout((()=>{u.unsubscribe(),i(new Error(`Timeout of ${n.timeout} ms exceeded`))}),n.timeout),a=()=>{clearTimeout(r),o=!0,u?.unsubscribe()};function c(t){e(t)&&(a(),s(t))}let u;c(t.getSnapshot()),o||(u=t.subscribe({next:c,error:t=>{a(),i(t)},complete:()=>{a(),i(new Error("Actor terminated without satisfying predicate"))}}),o&&u.unsubscribe())}))},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).XState={})}(this,(function(t){"use strict";const e=".",s="",n="",i="xstate.init",o="xstate.error",r="xstate.stop";class a{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}}function c(t,e){return{type:`xstate.done.state.${t}`,output:e}}function u(t,e){return{type:`xstate.error.actor.${t}`,data:e}}function f(t){return{type:i,input:t}}function h(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const d=t=>{const e=h();e&&e.register(t)};function p(t){setTimeout((()=>{throw t}))}const l="function"==typeof Symbol&&Symbol.observable||"@@observable";let y=0;function v(t,e){const s=m(t),n=m(e);return"string"==typeof n?"string"==typeof s&&n===s:"string"==typeof s?s in n:Object.keys(s).every((t=>t in n&&v(s[t],n[t])))}function g(t){try{return k(t)?t:t.toString().split(e)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}function m(t){if("object"==typeof(e=t)&&"value"in e&&"context"in e&&"event"in e)return t.value;var e;if(k(t))return _(t);if("string"!=typeof t)return t;return _(g(t))}function _(t){if(1===t.length)return t[0];const e={};let s=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)s[t[e]]=t[e+1];else{const n=s;s={},n[t[e]]=s}return e}function x(t,e){const s={},n=Object.keys(t);for(let i=0;i<n.length;i++){const o=n[i];s[o]=e(t[o],o,t,i)}return s}function b(t){return[].concat(...t)}function S(t){return k(t)?t:[t]}function w(t){return void 0===t?[]:S(t)}function $(t,e,s,n){return"function"==typeof t?t({context:e,event:s,self:n}):t}function k(t){return Array.isArray(t)}function I(t){return S(t).map((t=>void 0===t||"string"==typeof t?{target:t}:t))}function T(t){if(void 0!==t&&t!==s)return w(t)}function E(t,e,s){const n="object"==typeof t,i=n?t:void 0;return{next:(n?t.next:t)?.bind(i),error:(n?t.error:e)?.bind(i),complete:(n?t.complete:s)?.bind(i)}}function O(t,e){return`${t}[${e}]`}function j(t,e){if(e.startsWith("xstate#")){const[,s]=e.match(/\[(\d+)\]$/),n=t.getStateNodeById(e.slice(7,-(s.length+2))).config.invoke;return{src:(Array.isArray(n)?n[s]:n).src,input:void 0}}const s=t.implementations.actors[e];return s?"transition"in s?{src:s,input:void 0}:s:void 0}const A=1;let M=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const P=M,N={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class R{constructor(t,e){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new a(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=M.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 s={...N,...e},{clock:n,logger:i,parent:o,id:r,systemId:c,inspect:u}=s;this.system=o?.system??function(t){const e=new Map,s=new Map,n=new WeakMap,i=new Set,o={_bookId:()=>"x:"+y++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const i=n.get(t);void 0!==i&&(s.delete(i),n.delete(t))},get:t=>s.get(t),_set:(t,e)=>{const i=s.get(t);if(i&&i!==e)throw new Error(`Actor with system ID '${t}' already exists.`);s.set(t,e),n.set(e,t)},inspect:t=>{i.add(t)},_sendInspectionEvent:e=>{const s={...e,rootId:t.sessionId};i.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),u&&!o&&this.system.inspect(E(u)),this.sessionId=this.system._bookId(),this.id=r??this.sessionId,this.logger=i,this.clock=n,this._parent=o,this.options=s,this.src=s.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(),c&&"active"===this._state.status&&(this._systemId=c,this.system._set(c,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){p(t)}switch(this._state.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=(n=this.id,i=this._state.output,{type:`xstate.done.actor.${n}`,output:i}),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,u(this.id,this._state.error))}var n,i;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=E(t,e,s);if(this.status!==M.Stopped)this.observers.add(n);else try{n.complete?.()}catch(t){p(t)}return{unsubscribe:()=>{this.observers.delete(n)}}}start(){if(this.status===M.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=M.Running;const t=f(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._actorScope)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(u(this.id,t)),this}return this.update(this._state,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._state,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._stopProcedure(),this._error(t),void this._parent?.send(u(this.id,t))}this.update(e,t),t.type===r&&(this._stopProcedure(),this._complete())}_stop(){return this.status===M.Stopped?this:(this.mailbox.clear(),this.status===M.NotStarted?(this.status=M.Stopped,this):(this.mailbox.enqueue({type:r}),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){p(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||p(t));let e=!1;for(const s of this.observers){const n=s.error;e||=!n;try{n?.(t)}catch(t){p(t)}}this.observers.clear(),e&&p(t)}_stopProcedure(){if(this.status!==M.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new a(this._process.bind(this)),this.status=M.Stopped,this.system._unregister(this),this}_send(t){this.status!==M.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}delaySend(t){const{event:e,id:s,delay:n}=t,i=this.clock.setTimeout((()=>{this.system._relay(this,t.to??this,e)}),n);s&&(this.delayedEventsMap[s]=i)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:d)(this)}}toJSON(){return{xstate$$type:A,id:this.id}}getPersistedState(){return this.logic.getPersistedState(this._state)}[l](){return this}getSnapshot(){return this._state}}function D(t,e){return new R(t,e)}const V=D,C=new WeakMap;function J(t,e,s){let n=C.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},C.set(t,n)),n[e]}function B(t,e,s,n,{sendId:i}){return[e,"function"==typeof i?i(s,n):i]}function z(t,e){t.self.cancel(e)}function W(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=B,e.execute=z,e}function U(t,e,s,n,{id:i,systemId:o,src:r,input:a,syncSnapshot:c}){const u="string"==typeof r?j(e.machine,r):{src:r,input:void 0},f="function"==typeof i?i(s):i;let h;if(u){const n=a||u.input;h=D(u.src,{id:f,src:"string"==typeof r?r:void 0,parent:t?.self,systemId:o,input:"function"==typeof n?n({context:e.context,event:s.event,self:t?.self}):n}),c&&h.subscribe({next:e=>{"active"===e.status&&t.self.send({type:`xstate.snapshot.${i}`,snapshot:e})},error:()=>{}})}return[zt(e,{children:{...e.children,[f]:h}}),{id:i,actorRef:h}]}function q(t,{id:e,actorRef:s}){s&&t.defer((()=>{if(s.status!==M.Stopped)try{s.start?.()}catch(s){return void t.self.send(u(e,s))}}))}function G(...[t,{id:e,systemId:s,input:n,syncSnapshot:i=!1}={}]){function o(t,e){}return o.type="xstate.spawn",o.id=e,o.systemId=s,o.src=t,o.input=n,o.syncSnapshot=i,o.resolve=U,o.execute=q,o}function Q(t,e,s,n,{actorRef:i}){const o="function"==typeof i?i(s,n):i,r="string"==typeof o?e.children[o]:o;let a=e.children;return r&&(a={...a},delete a[r.id]),[zt(e,{children:a}),r]}function F(t,e){e&&(t.system._unregister(e),e.status===M.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function X(t){function e(t,e){}return e.type="xstate.stop",e.actorRef=t,e.resolve=Q,e.execute=F,e}function H(t,e,{stateValue:s}){if("string"==typeof s&&ct(s)){const e=t.machine.getStateNodeById(s);return t.configuration.some((t=>t===e))}return t.matches(s)}function K(t,{context:e,event:s},{guards:n}){return!Z(n[0],e,s,t)}function L(t,{context:e,event:s},{guards:n}){return n.every((n=>Z(n,e,s,t)))}function Y(t,{context:e,event:s},{guards:n}){return n.some((n=>Z(n,e,s,t)))}function Z(t,e,s,n){const{machine:i}=n,o="function"==typeof t,r=o?t:i.implementations.guards["string"==typeof t?t:t.type];if(!o&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return Z(r,e,s,n);const a={context:e,event:s},c=o||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:e,event:s}):t.params:void 0;if(!("check"in r))return r(a,c);return r.check(n,a,r)}const tt=t=>"atomic"===t.type||"final"===t.type;function et(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function st(t,e){const s=[];if(e===t)return s;let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function nt(t){const e=new Set(t),s=new Set(t),n=ot(s);for(const t of e)if("compound"!==t.type||n.get(t)&&n.get(t).length){if("parallel"===t.type)for(const e of et(t))if("history"!==e.type&&!s.has(e)){const t=pt(e);for(const e of t)s.add(e)}}else pt(t).forEach((t=>s.add(t)));for(const t of s){let e=t.parent;for(;e;)s.add(e),e=e.parent}return s}function it(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(tt(t))return t.key}const n={};for(const t of s)n[t.key]=it(t,e);return n}function ot(t){const e=new Map;for(const s of t)e.has(s)||e.set(s,[]),s.parent&&(e.has(s.parent)||e.set(s.parent,[]),e.get(s.parent).push(s));return e}function rt(t,e){return it(t,ot(nt(e)))}function at(t,e){return"compound"===e.type?et(e).some((e=>"final"===e.type&&t.has(e))):"parallel"===e.type?et(e).every((e=>at(t,e))):"final"===e.type}const ct=t=>"#"===t[0];function ut(t){const e=t.config.after;if(!e)return[];return Object.keys(e).flatMap(((s,n)=>{const i=e[s],o="string"==typeof i?{target:i}:i,r=isNaN(+s)?s:+s,a=((e,s)=>{const n=(o="function"==typeof e?`${t.id}:delay[${s}]`:e,r=t.id,{type:`xstate.after(${o})${r?`#${r}`:""}`}),i=n.type;var o,r;return t.entry.push(Yt(n,{id:i,delay:e})),t.exit.push(W(i)),i})(r,n);return w(o).map((t=>({...t,event:a,delay:r})))})).map((e=>{const{delay:s}=e;return{...ft(t,e.event,e),delay:s}}))}function ft(t,s,n){const i=T(n.target),o=n.reenter??!1,r=function(t,s){if(void 0===s)return;return s.map((s=>{if("string"!=typeof s)return s;if(ct(s))return t.machine.getStateNodeById(s);const n=s[0]===e;if(n&&!t.parent)return vt(t,s.slice(1));const i=n?t.key+s:s;if(!t.parent)throw new Error(`Invalid target: "${s}" is not a valid target from the root node. Did you mean ".${s}"?`);try{return vt(t.parent,i)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}}))}(t,i),a={...n,actions:w(n.actions),guard:n.guard,target:r,source:t,reenter:o,eventType:s,toJSON:()=>({...a,source:`#${t.id}`,target:r?r.map((t=>`#${t.id}`)):void 0})};return a}function ht(t){const e=T(t.config.target);return e?{target:e.map((e=>"string"==typeof e?vt(t.parent,e):e))}:t.parent.initial}function dt(t){return"history"===t.type}function pt(t){const e=lt(t);for(const s of e)for(const n of st(s,t))e.add(n);return e}function lt(t){const e=new Set;return function t(s){if(!e.has(s))if(e.add(s),"compound"===s.type)t(s.initial.target[0]);else if("parallel"===s.type)for(const e of et(s))t(e)}(t),e}function yt(t,e){if(ct(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);const s=t.states[e];if(!s)throw new Error(`Child state '${e}' does not exist on '${t.id}'`);return s}function vt(t,e){if("string"==typeof e&&ct(e))try{return t.machine.getStateNodeById(e)}catch(t){}const s=g(e).slice();let n=t;for(;s.length;){const t=s.shift();if(!t.length)break;n=yt(n,t)}return n}function gt(t,e){const s=e instanceof Bt?e.value:m(e);if("string"==typeof s)return[t,t.states[s]];const n=Object.keys(s),i=n.map((e=>yt(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,n.reduce(((e,n)=>{const i=yt(t,n);if(!i)return e;const o=gt(i,s[n]);return e.concat(o)}),[]))}function mt(t,e,s,n){return"string"==typeof e?function(t,e,s,n){const i=yt(t,e).next(s,n);return i&&i.length?i:t.next(s,n)}(t,e,s,n):1===Object.keys(e).length?function(t,e,s,n){const i=Object.keys(e),o=mt(yt(t,i[0]),e[i[0]],s,n);return o&&o.length?o:t.next(s,n)}(t,e,s,n):function(t,e,s,n){const i=[];for(const o of Object.keys(e)){const r=e[o];if(!r)continue;const a=mt(yt(t,o),r,s,n);a&&i.push(...a)}return i.length?i:t.next(s,n)}(t,e,s,n)}function _t(t){return Object.keys(t.states).map((e=>t.states[e])).filter((t=>"history"===t.type))}function xt(t,e){let s=t;for(;s.parent&&s.parent!==e;)s=s.parent;return s.parent===e}function bt(t,e){const s=new Set(t),n=new Set(e);for(const t of s)if(n.has(t))return!0;for(const t of n)if(s.has(t))return!0;return!1}function St(t,e,s){const n=new Set;for(const i of t){let t=!1;const o=new Set;for(const r of n)if(bt(kt([i],e,s),kt([r],e,s))){if(!xt(i.source,r.source)){t=!0;break}o.add(r)}if(!t){for(const t of o)n.delete(t);n.add(i)}}return Array.from(n)}function wt(t,e){if(!t.target)return[];const s=new Set;for(const n of t.target)if(dt(n))if(e[n.id])for(const t of e[n.id])s.add(t);else for(const t of wt(ht(n),e))s.add(t);else s.add(n);return[...s]}function $t(t,e){const s=wt(t,e);if(!s)return;if(!t.reenter&&s.every((e=>e===t.source||xt(e,t.source))))return t.source;const n=function(t){const[e,...s]=t;for(const t of st(e,void 0))if(s.every((e=>xt(e,t))))return t}(s.concat(t.source));return n||(t.reenter?void 0:t.source.machine.root)}function kt(t,e,s){const n=new Set;for(const i of t)if(i.target?.length){const t=$t(i,s);i.reenter&&i.source===t&&n.add(t);for(const s of e)xt(s,t)&&n.add(s)}return[...n]}function It(t,e,s,n,i,o){if(!t.length)return e;const r=new Set(e.configuration);let a=e.historyValue;const u=St(t,r,a);let f=e;i||([f,a]=function(t,e,s,n,i,o,r){let a=t;const c=kt(n,i,o);let u;c.sort(((t,e)=>e.order-t.order));for(const t of c)for(const e of _t(t)){let s;s="deep"===e.history?e=>tt(e)&&xt(e,t):e=>e.parent===t,u??={...o},u[e.id]=Array.from(i).filter(s)}for(const t of c)a=Pt(a,e,s,[...t.exit,...t.invoke.map((t=>X(t.id)))],r),i.delete(t);return[a,u||o]}(f,n,s,u,r,a,o)),f=Pt(f,n,s,u.flatMap((t=>t.actions)),o),f=function(t,e,s,n,i,o,r,a){let u=t;const f=new Set,h=new Set;Et(n,r,h,f),a&&h.add(t.machine.root);const d=new Set;for(const n of[...f].sort(((t,e)=>t.order-e.order))){i.add(n);const r=[];r.push(...n.entry);for(const t of n.invoke)r.push(G(t.src,{...t,syncSnapshot:!!t.onSnapshot}));if(h.has(n)){const t=n.initial.actions;r.push(...t)}if(u=Pt(u,e,s,r,o,n.invoke.map((t=>t.id))),"final"===n.type){const r=n.parent;let a="parallel"===r?.type?r:r?.parent,f=a||n;for("compound"===r?.type&&o.push(c(r.id,n.output?$(n.output,u.context,e,s.self):void 0));"parallel"===a?.type&&!d.has(a)&&at(i,a);)d.add(a),o.push(c(a.id)),f=a,a=a.parent;if(a)continue;u=zt(u,{status:"done",output:Tt(u,e,s,t.configuration[0].machine.root,f)})}}return u}(f,n,s,u,r,o,a,i);const h=[...r];"done"===f.status&&(f=Pt(f,n,s,h.sort(((t,e)=>e.order-t.order)).flatMap((t=>t.exit)),o));try{return a===e.historyValue&&function(t,e){if(t.length!==e.size)return!1;for(const s of t)if(!e.has(s))return!1;return!0}(e.configuration,r)?f:zt(f,{configuration:h,historyValue:a})}catch(t){throw t}}function Tt(t,e,s,n,i){if(!n.output)return;const o=c(i.id,i.output&&i.parent?$(i.output,t.context,e,s.self):void 0);return $(n.output,t.context,o,s.self)}function Et(t,e,s,n){for(const i of t){const t=$t(i,e);for(const o of i.target||[])dt(o)||i.source===o&&i.source===t&&!i.reenter||(n.add(o),s.add(o)),Ot(o,e,s,n);const o=wt(i,e);for(const r of o){const o=st(r,t);"parallel"===t?.type&&o.push(t),jt(n,e,s,o,!i.source.parent&&i.reenter?void 0:t)}}}function Ot(t,e,s,n){if(dt(t))if(e[t.id]){const i=e[t.id];for(const t of i)n.add(t),Ot(t,e,s,n);for(const o of i)At(o,t.parent,n,e,s)}else{const i=ht(t);for(const o of i.target)n.add(o),i===t.parent?.initial&&s.add(t.parent),Ot(o,e,s,n);for(const o of i.target)At(o,t,n,e,s)}else if("compound"===t.type){const[i]=t.initial.target;dt(i)||(n.add(i),s.add(i)),Ot(i,e,s,n),At(i,t,n,e,s)}else if("parallel"===t.type)for(const i of et(t).filter((t=>!dt(t))))[...n].some((t=>xt(t,i)))||(dt(i)||(n.add(i),s.add(i)),Ot(i,e,s,n))}function jt(t,e,s,n,i){for(const o of n)if(i&&!xt(o,i)||t.add(o),"parallel"===o.type)for(const n of et(o).filter((t=>!dt(t))))[...t].some((t=>xt(t,n)))||(t.add(n),Ot(n,e,s,t))}function At(t,e,s,n,i){jt(s,n,i,st(t,e))}function Mt(t,e,s,n,i,o){const{machine:r}=t;let a=t;for(const t of n){const n="function"==typeof t,c=n?t:r.implementations.actions["string"==typeof t?t:t.type];if(!c)continue;const u={context:a.context,event:e,self:s?.self,system:s?.system},f=n||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:a.context,event:e}):t.params:void 0;if(!("resolve"in c)){s?.self.status===M.Running?c(u,f):s?.defer((()=>{c(u,f)}));continue}const h=c,[d,p,l]=h.resolve(s,a,u,f,c,i);a=d,"retryResolve"in h&&o?.push([h,p]),"execute"in h&&(s?.self.status===M.Running?h.execute(s,p):s?.defer(h.execute.bind(null,s,p))),l&&(a=Mt(a,e,s,l,i,o))}return a}function Pt(t,e,s,n,i,o){const r=o?[]:void 0,a=Mt(t,e,s,n,{internalQueue:i,deferredActorIds:o},r);return r?.forEach((([t,e])=>{t.retryResolve(s,a,e)})),a}function Nt(t,e,s,n=[]){let o=t;const a=[];if(e.type===r)return o=zt(Rt(o,e,s),{status:"stopped"}),a.push(o),{state:o,microstates:a};let c=e;if(c.type!==i){o=It(Dt(c,o),t,s,c,!1,n),a.push(o)}let u=!0;for(;"active"===o.status;){let t=u?Vt(o,c):[];const e=t.length?o:void 0;if(!t.length){if(!n.length)break;c=n.shift(),t=Dt(c,o)}o=It(t,o,s,c,!1,n),u=o!==e,a.push(o)}return"active"!==o.status&&Rt(o,c,s),{state:o,microstates:a}}function Rt(t,e,s){return Pt(t,e,s,Object.values(t.children).map((t=>X(t))),[])}function Dt(t,e){return e.machine.getTransitionData(e,t)}function Vt(t,e){const s=new Set,n=t.configuration.filter(tt);for(const i of n)t:for(const n of[i].concat(st(i,void 0)))if(n.always)for(const i of n.always)if(void 0===i.guard||Z(i.guard,t.context,e,t)){s.add(i);break t}return St(Array.from(s),new Set(t.configuration),t.historyValue)}function Ct(t,e){return rt(t,[...nt(gt(t,e))])}function Jt(t){const e=[],s=t.initial,n=new Set;Et([s],{},new Set([t]),n);for(const t of[...n].sort(((t,e)=>t.order-e.order)))e.push(t);return e}class Bt{static from(t,e={},s){if(t instanceof Bt)return t.context!==e?new Bt({value:t.value,context:e,meta:{},configuration:[],children:{},status:"active"},s):t;const n=nt(gt(s.root,t));return new Bt({value:t,context:e,meta:void 0,configuration:Array.from(n),children:{},status:"active"},s)}constructor(t,e){this.machine=e,this.tags=void 0,this.value=void 0,this.status=void 0,this.error=void 0,this.context=void 0,this.historyValue={},this.configuration=void 0,this.children=void 0,this.context=t.context,this.historyValue=t.historyValue||{},this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(nt(gt(e.root,t.value))),this.children=t.children,this.value=rt(e.root,this.configuration),this.tags=new Set(b(this.configuration.map((t=>t.tags)))),this.status=t.status,this.output=t.output,this.error=t.error}toStrings(t=this.value){if("string"==typeof t)return[t];const s=Object.keys(t);return s.concat(...s.map((s=>this.toStrings(t[s]).map((t=>s+e+t)))))}toJSON(){const{configuration:t,tags:e,machine:s,...n}=this;return{...n,tags:Array.from(e),meta:this.meta}}matches(t){return v(t,this.value)}hasTag(t){return this.tags.has(t)}can(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))}get nextEvents(){return J(this,"nextEvents",(()=>[...new Set(b([...this.configuration.map((t=>t.ownEvents))]))]))}get meta(){return this.configuration.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})}}function zt(t,e={}){return new Bt({...t,...e},t.machine)}function Wt(t){let e;for(const s in t){const n=t[s];if(n&&"object"==typeof n)if("sessionId"in n&&"send"in n&&"ref"in n)e??=Array.isArray(t)?t.slice():{...t},e[s]={xstate$$type:A,id:n.id};else{const i=Wt(n);i!==n&&(e??=Array.isArray(t)?t.slice():{...t},e[s]=i)}}return e??t}function Ut(t,{machine:e,context:s},n,i){return(o,r)=>{const a=((o,r={})=>{const{systemId:a}=r;if("string"==typeof o){const c=j(e,o);if(!c)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const u="input"in r?r.input:c.input,f=D(c.src,{id:r.id,parent:t.self,input:"function"==typeof u?u({context:s,event:n,self:t.self}):u,src:o,systemId:a});return i[f.id]=f,r.syncSnapshot&&f.subscribe({next:e=>{"active"===e.status&&t.self.send({type:`xstate.snapshot.${f.id}`,snapshot:e})},error:()=>{}}),f}{const e=D(o,{id:r.id,parent:t.self,input:r.input,src:void 0,systemId:a});return r.syncSnapshot&&e.subscribe({next:s=>{"active"===s.status&&t.self.send({type:`xstate.snapshot.${e.id}`,snapshot:s,id:e.id})},error:()=>{}}),e}})(o,r);return i[a.id]=a,t.defer((()=>{if(a.status!==M.Stopped)try{a.start?.()}catch(e){return void t.self.send(u(a.id,e))}})),a}}function qt(t,e,s,n,{assignment:i}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const o={},r={context:e.context,event:s.event,spawn:Ut(t,e,s.event,o),self:t?.self,system:t?.system};let a={};if("function"==typeof i)a=i(r,n);else for(const t of Object.keys(i)){const e=i[t];a[t]="function"==typeof e?e(r,n):e}return[zt(e,{context:Object.assign({},e.context,a),children:Object.keys(o).length?{...e.children,...o}:e.children})]}function Gt(t){function e(t,e){}return e.type="xstate.assign",e.assignment=t,e.resolve=qt,e}function Qt(t,e,s,n,{branches:i}){const o=i.find((t=>!t.guard||Z(t.guard,e.context,s.event,e)))?.actions;return[e,void 0,w(o)]}function Ft(t,e,s,n,{value:i,label:o}){return[e,{value:"function"==typeof i?i(s,n):i,label:o}]}function Xt({logger:t},{value:e,label:s}){s?t(s,e):t(e)}function Ht(t,e,s,n,{get:i}){return[e,void 0,w(i({context:s.context,event:s.event}))]}function Kt(t,e,s,n,{event:i,id:o,delay:r},{internalQueue:a}){const c=e.machine.implementations.delays;if("string"==typeof i)throw new Error(`Only event objects may be used with raise; use raise({ type: "${i}" }) instead`);const u="function"==typeof i?i(s,n):i;let f;if("string"==typeof r){const t=c&&c[r];f="function"==typeof t?t(s,n):t}else f="function"==typeof r?r(s,n):r;return"number"!=typeof f&&a.push(u),[e,{event:u,id:o,delay:f}]}function Lt(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}function Yt(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=Kt,s.execute=Lt,s}let Zt=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function te(t,e,s,n,{to:i,event:o,id:r,delay:a},c){const u=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${o}" }) instead`);const f="function"==typeof o?o(s,n):o;let h;if("string"==typeof a){const t=u&&u[a];h="function"==typeof t?t(s,n):t}else h="function"==typeof a?a(s,n):a;const d="function"==typeof i?i(s,n):i;let p;if("string"==typeof d){if(p=d===Zt.Parent?t?.self._parent:d===Zt.Internal?t?.self:d.startsWith("#_")?e.children[d.slice(2)]:c.deferredActorIds?.includes(d)?d:e.children[d],!p)throw new Error(`Unable to send event to actor '${d}' from machine '${e.machine.id}'.`)}else p=d||t?.self;return[e,{to:p,event:f,id:r,delay:h}]}function ee(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function se(t,e){"number"!=typeof e.delay?t.defer((()=>{const{to:s,event:n}=e;t?.system._relay(t.self,s,n.type===o?u(t.self.id,n.data):n)})):t.self.delaySend(e)}function ne(t,e,s){function n(t,e){}return n.type="xstate.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=te,n.retryResolve=ee,n.execute=se,n}function ie(t,e){return ne(Zt.Parent,t,e)}function oe(t,e){return{config:t,transition:(e,s,n)=>({...e,context:t(e.context,s,n)}),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 re="$$xstate.resolve",ae="$$xstate.reject";const ce=oe((t=>{}),void 0);const ue={},fe=t=>"string"==typeof t?{type:t}:"function"==typeof t?"resolve"in t?{type:t.type}:{type:t.name}:t;class he{constructor(t,s){if(this.config=t,this.key=void 0,this.id=void 0,this.type=void 0,this.path=void 0,this.states=void 0,this.history=void 0,this.entry=void 0,this.exit=void 0,this.parent=void 0,this.machine=void 0,this.meta=void 0,this.output=void 0,this.order=-1,this.description=void 0,this.tags=[],this.transitions=void 0,this.always=void 0,this.parent=s._parent,this.key=s._key,this.machine=s._machine,this.path=this.parent?this.parent.path.concat(this.key):[],this.id=this.config.id||[this.machine.id,...this.path].join(e),this.type=this.config.type||(this.config.states&&Object.keys(this.config.states).length?"compound":this.config.history?"history":"atomic"),this.description=this.config.description,this.order=this.machine.idMap.size,this.machine.idMap.set(this.id,this),this.states=this.config.states?x(this.config.states,((t,e)=>new he(t,{_parent:this,_key:e,_machine:this.machine}))):ue,"compound"===this.type&&!this.config.initial)throw new Error(`No initial state specified for compound state node "#${this.id}". Try adding { initial: "${Object.keys(this.states)[0]}" } to the state config.`);this.history=!0===this.config.history?"shallow":this.config.history||!1,this.entry=w(this.config.entry).slice(),this.exit=w(this.config.exit).slice(),this.meta=this.config.meta,this.output="final"!==this.type&&this.parent?void 0:this.config.output,this.tags=w(t.tags).slice()}_initialize(){this.transitions=function(t){const e=new Map;if(t.config.on)for(const s of Object.keys(t.config.on)){if(s===n)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const i=t.config.on[s];e.set(s,I(i).map((e=>ft(t,s,e))))}if(t.config.onDone){const s=`xstate.done.state.${t.id}`;e.set(s,I(t.config.onDone).map((e=>ft(t,s,e))))}for(const s of t.invoke){if(s.onDone){const n=`xstate.done.actor.${s.id}`;e.set(n,I(s.onDone).map((e=>ft(t,n,e))))}if(s.onError){const n=`xstate.error.actor.${s.id}`;e.set(n,I(s.onError).map((e=>ft(t,n,e))))}if(s.onSnapshot){const n=`xstate.snapshot.${s.id}`;e.set(n,I(s.onSnapshot).map((e=>ft(t,n,e))))}}for(const s of t.after){let t=e.get(s.eventType);t||(t=[],e.set(s.eventType,t)),t.push(s)}return e}(this),this.config.always&&(this.always=I(this.config.always).map((t=>ft(this,n,t)))),Object.keys(this.states).forEach((t=>{this.states[t]._initialize()}))}get definition(){return{id:this.id,key:this.key,version:this.machine.version,type:this.type,initial:this.initial?{target:this.initial.target,source:this,actions:this.initial.actions.map(fe),eventType:null,reenter:!1,toJSON:()=>({target:this.initial.target.map((t=>`#${t.id}`)),source:`#${this.id}`,actions:this.initial.actions.map(fe),eventType:null})}:void 0,history:this.history,states:x(this.states,(t=>t.definition)),on:this.on,transitions:[...this.transitions.values()].flat().map((t=>({...t,actions:t.actions.map(fe)}))),entry:this.entry.map(fe),exit:this.exit.map(fe),meta:this.meta,order:this.order||-1,output:this.output,invoke:this.invoke,description:this.description,tags:this.tags}}toJSON(){return this.definition}get invoke(){return J(this,"invoke",(()=>w(this.config.invoke).map(((t,e)=>{const{src:s,systemId:n}=t,i=t.id||O(this.id,e),o="string"==typeof s?s:`xstate#${O(this.id,e)}`;return{...t,src:o,id:i,systemId:n,toJSON(){const{onDone:e,onError:s,...n}=t;return{...n,type:"xstate.invoke",src:o,id:i}}}}))))}get on(){return J(this,"on",(()=>[...this.transitions].flatMap((([t,e])=>e.map((e=>[t,e])))).reduce(((t,[e,s])=>(t[e]=t[e]||[],t[e].push(s),t)),{})))}get after(){return J(this,"delayedTransitions",(()=>ut(this)))}get initial(){return J(this,"initial",(()=>function(t,e){const s="string"==typeof e?t.states[e]:e?t.states[e.target]:void 0;if(!s&&e)throw new Error(`Initial state node "${e}" not found on parent state node #${t.id}`);const n={source:t,actions:e&&"string"!=typeof e?w(e.actions):[],eventType:null,reenter:!1,target:s?[s]:[],toJSON:()=>({...n,source:`#${t.id}`,target:s?[`#${s.id}`]:[]})};return n}(this,this.config.initial)))}next(t,e){const s=e.type,n=[];let i;const o=J(this,`candidates-${s}`,(()=>{return e=s,(t=this).transitions.get(e)||[...t.transitions.keys()].filter((t=>{if("*"===t)return!0;if(!t.endsWith(".*"))return!1;const s=t.split("."),n=e.split(".");for(let t=0;t<s.length;t++){const e=s[t],i=n[t];if("*"===e)return t===s.length-1;if(e!==i)return!1}return!0})).sort(((t,e)=>e.length-t.length)).flatMap((e=>t.transitions.get(e)));var t,e}));for(const r of o){const{guard:o}=r,a=t.context;let c=!1;try{c=!o||Z(o,a,e,t)}catch(t){const e="string"==typeof o?o:"object"==typeof o?o.type:void 0;throw new Error(`Unable to evaluate guard ${e?`'${e}' `:""}in transition for event '${s}' in state node '${this.id}':\n${t.message}`)}if(c){n.push(...r.actions),i=r;break}}return i?[i]:void 0}get events(){return J(this,"events",(()=>{const{states:t}=this,e=new Set(this.ownEvents);if(t)for(const s of Object.keys(t)){const n=t[s];if(n.states)for(const t of n.events)e.add(`${t}`)}return Array.from(e)}))}get ownEvents(){const t=new Set([...this.transitions.keys()].filter((t=>this.transitions.get(t).some((t=>!(!t.target&&!t.actions.length&&!t.reenter))))));return Array.from(t)}}class de{constructor(t,e){this.config=t,this.version=void 0,this.implementations=void 0,this.types=void 0,this.__xstatenode=!0,this.idMap=new Map,this.root=void 0,this.id=void 0,this.states=void 0,this.events=void 0,this.__TContext=void 0,this.__TEvent=void 0,this.__TActor=void 0,this.__TAction=void 0,this.__TGuard=void 0,this.__TDelay=void 0,this.__TTag=void 0,this.__TInput=void 0,this.__TOutput=void 0,this.__TResolvedTypesMeta=void 0,this.id=t.id||"(machine)",this.implementations={actors:e?.actors??{},actions:e?.actions??{},delays:e?.delays??{},guards:e?.guards??{}},this.version=this.config.version,this.types=this.config.types??{},this.transition=this.transition.bind(this),this.getInitialState=this.getInitialState.bind(this),this.restoreState=this.restoreState.bind(this),this.start=this.start.bind(this),this.getPersistedState=this.getPersistedState.bind(this),this.root=new he(t,{_key:this.id,_machine:this}),this.root._initialize(),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:s,actors:n,delays:i}=this.implementations;return new de(this.config,{actions:{...e,...t.actions},guards:{...s,...t.guards},actors:{...n,...t.actors},delays:{...i,...t.delays}})}resolveState(t){const e=nt(gt(this.root,t.value)),s=Array.from(e);return this.createState({...t,value:Ct(this.root,t.value),configuration:s,status:at(e,this.root)?"done":t.status})}resolveStateValue(t,...[e]){const s=Ct(this.root,t);return this.resolveState(Bt.from(s,e,this))}transition(t,e,s){if(function(t){return t.type.startsWith("xstate.error.actor")}(e)&&!t.nextEvents.some((t=>t===e.type)))return zt(t,{status:"error",error:e.data});const{state:n}=Nt(t,e,s);return n}microstep(t,e,s){return Nt(t,e,s).microstates}getTransitionData(t,e){return mt(this.root,t.value,t,e)||[]}getPreInitialState(t,e,s){const{context:n}=this.config,i=this.resolveState(this.createState({value:{},context:"function"!=typeof n&&n?n:{},meta:void 0,configuration:Jt(this.root),children:{},status:"active"}));if("function"==typeof n){const o=({spawn:t,event:e})=>n({spawn:t,input:e.input});return Pt(i,e,t,[Gt(o)],s)}return i}getInitialState(t,e){const s=f(e),n=[],i=this.getPreInitialState(t,s,n),o=It([{target:[...lt(this.root)],source:this.root,reenter:!0,actions:[],eventType:null,toJSON:null}],i,t,s,!0,n),{state:r}=Nt(o,s,t,n);return r}start(t){Object.values(t.children).forEach((t=>{"active"===t.getSnapshot().status&&t.start()}))}getStateNodeById(t){const s=t.split(e),n=s.slice(1),i=ct(s[0])?s[0].slice(1):s[0],o=this.idMap.get(i);if(!o)throw new Error(`Child state node '#${i}' does not exist on machine '${this.id}'`);return vt(o,n)}get definition(){return this.root.definition}toJSON(){return this.definition}getPersistedState(t){return function(t){const{configuration:e,tags:s,machine:n,children:i,context:o,...r}=t,a={};for(const t in i){const e=i[t];a[t]={state:e.getPersistedState(),src:e.src,systemId:e._systemId}}return{...r,context:Wt(o),children:a}}(t)}createState(t){return t instanceof Bt?t:new Bt(t,this)}restoreState(t,e){const s={},n=t.children;Object.keys(n).forEach((t=>{const i=n[t],o=i.state,r=i.src,a=r?j(this,r)?.src:void 0;if(!a)return;const c=a.restoreState?.(o,e),u=D(a,{id:t,parent:e?.self,state:c,systemId:i.systemId});s[t]=u}));const i=this.createState(new Bt({...t,children:s},this));let o=new Set;return function t(e,s){if(!o.has(e)){o.add(e);for(let n in e){const i=e[n];if(i&&"object"==typeof i){if("xstate$$type"in i&&i.xstate$$type===A){e[n]=s[i.id];continue}t(i,s)}}}}(i.context,s),i}}const pe={timeout:1e4};t.Actor=R,t.ActorStatus=M,t.InterpreterStatus=P,t.SimulatedClock=class{constructor(){this.timeouts=new Map,this._now=0,this._id=0}now(){return this._now}getId(){return this._id++}setTimeout(t,e){const s=this.getId();return this.timeouts.set(s,{start:this.now(),timeout:e,fn:t}),s}clearTimeout(t){this.timeouts.delete(t)}set(t){if(this._now>t)throw new Error("Unable to travel back in time");this._now=t,this.flushTimeouts()}flushTimeouts(){[...this.timeouts].sort((([t,e],[s,n])=>{const i=e.start+e.timeout;return n.start+n.timeout>i?-1:1})).forEach((([t,e])=>{this.now()-e.start>=e.timeout&&(this.timeouts.delete(t),e.fn.call(null))}))}increment(t){this._now+=t,this.flushTimeouts()}},t.SpecialTargets=Zt,t.State=Bt,t.StateMachine=de,t.StateNode=he,t.and=function(t){function e(t,e){return!1}return e.check=L,e.guards=t,e},t.assign=Gt,t.cancel=W,t.choose=function(t){function e(t,e){}return e.type="xstate.choose",e.branches=t,e.resolve=Qt,e},t.createActor=D,t.createEmptyActor=function(){return D(ce)},t.createMachine=function(t,e){return new de(t,e)},t.escalate=function(t,e){return ie((e=>({type:o,data:"function"==typeof t?t(e):t})),e)},t.forwardTo=function(t,e){return ne(t,(({event:t})=>t),e)},t.fromCallback=function(t){return{config:t,start:(t,{self:e,system:s})=>{s._relay(e,e,{type:"xstate.create"})},transition:(e,s,{self:n,system:i})=>{if("xstate.create"===s.type){const s=t=>{"stopped"!==e.status&&n._parent&&i._relay(n,n._parent,t)},o=t=>{e._receivers.add(t)};return e._dispose=t({input:e.input,system:i,self:n,sendBack:s,receive:o}),e}return s.type===r?("function"==typeof(e={...e,status:"stopped",error:void 0})._dispose&&e._dispose(),e):(e._receivers.forEach((t=>t(s))),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",s="$$xstate.complete";return{config:t,transition:(t,n)=>{if("active"!==t.status)return t;switch(n.type){case e:return{...t,status:"error",error:n.data,input:void 0,_subscription:void 0};case s:return{...t,status:"done",input:void 0,_subscription:void 0};case r: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:(n,{self:i,system:o})=>{"done"!==n.status&&(n._subscription=t({input:n.input,system:o,self:i}).subscribe({next:t=>{i._parent&&o._relay(i,i._parent,t)},error:t=>{o._relay(i,i,{type:e,data:t})},complete:()=>{o._relay(i,i,{type:s})}}))},getPersistedState:({_subscription:t,...e})=>e,restoreState:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){const e="$$xstate.next",s="$$xstate.error",n="$$xstate.complete";return{config:t,transition:(t,i,{self:o,id:a,defer:c,system:u})=>{if("active"!==t.status)return t;switch(i.type){case e:return{...t,context:i.data};case s:return{...t,status:"error",error:i.data,input:void 0,_subscription:void 0};case n:return{...t,status:"done",input:void 0,_subscription:void 0};case r: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:(i,{self:o,system:r})=>{"done"!==i.status&&(i._subscription=t({input:i.input,system:r,self:o}).subscribe({next:t=>{r._relay(o,o,{type:e,data:t})},error:t=>{r._relay(o,o,{type:s,data:t})},complete:()=>{r._relay(o,o,{type:n})}}))},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 re:{const s=e.data;return{...t,status:"done",output:s,input:void 0}}case ae:return{...t,status:"error",error:e.data,input:void 0};case r:return{...t,status:"stopped",input:void 0};default:return t}},start:(e,{self:s,system:n})=>{if("active"!==e.status)return;Promise.resolve(t({input:e.input,system:n,self:s})).then((t=>{"active"===s.getSnapshot().status&&n._relay(s,s,{type:re,data:t})}),(t=>{"active"===s.getSnapshot().status&&n._relay(s,s,{type:ae,data:t})}))},getInitialState:(t,e)=>({status:"active",output:void 0,error:void 0,input:e}),getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=oe,t.getStateNodes=gt,t.interpret=V,t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function s(t,e){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=Ft,s.execute=Xt,s},t.mapState=function(t,e){let s;for(const n of Object.keys(t))v(n,e)&&(!s||e.length>s.length)&&(s=n);return t[s]},t.matchesState=v,t.not=function(t){function e(t,e){return!1}return e.check=K,e.guards=[t],e},t.or=function(t){function e(t,e){return!1}return e.check=Y,e.guards=t,e},t.pathToStateValue=_,t.pure=function(t){function e(t,e){}return e.type="xstate.pure",e.get=t,e.resolve=Ht,e},t.raise=Yt,t.sendParent=ie,t.sendTo=ne,t.spawn=G,t.stateIn=function(t){function e(t,e){return!1}return e.check=H,e.stateValue=t,e},t.stop=X,t.toObserver=E,t.waitFor=function(t,e,s){const n={...pe,...s};return new Promise(((s,i)=>{let o=!1;const r=n.timeout===1/0?void 0:setTimeout((()=>{u.unsubscribe(),i(new Error(`Timeout of ${n.timeout} ms exceeded`))}),n.timeout),a=()=>{clearTimeout(r),o=!0,u?.unsubscribe()};function c(t){e(t)&&(a(),s(t))}let u;c(t.getSnapshot()),o||(u=t.subscribe({next:c,error:t=>{a(),i(t)},complete:()=>{a(),i(new Error("Actor terminated without satisfying predicate"))}}),o&&u.unsubscribe())}))},Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate.umd.min.js.map