xstate 5.18.1 → 5.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/actions/dist/xstate-actions.cjs.d.mts +1 -1
  2. package/actions/dist/xstate-actions.cjs.d.ts +1 -1
  3. package/actions/dist/xstate-actions.cjs.js +2 -2
  4. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  5. package/actions/dist/xstate-actions.development.esm.js +2 -2
  6. package/actions/dist/xstate-actions.esm.js +2 -2
  7. package/actions/dist/xstate-actions.umd.min.js +1 -1
  8. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  9. package/actors/dist/xstate-actors.cjs.d.mts +1 -1
  10. package/actors/dist/xstate-actors.cjs.d.ts +1 -1
  11. package/actors/dist/xstate-actors.cjs.js +1 -1
  12. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  13. package/actors/dist/xstate-actors.development.esm.js +1 -1
  14. package/actors/dist/xstate-actors.esm.js +1 -1
  15. package/actors/dist/xstate-actors.umd.min.js +1 -1
  16. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  17. package/dev/dist/xstate-dev.cjs.d.mts +1 -1
  18. package/dev/dist/xstate-dev.cjs.d.ts +1 -1
  19. package/dev/dist/xstate-dev.cjs.js +1 -1
  20. package/dev/dist/xstate-dev.development.cjs.js +1 -1
  21. package/dev/dist/xstate-dev.development.esm.js +1 -1
  22. package/dev/dist/xstate-dev.esm.js +1 -1
  23. package/dev/dist/xstate-dev.umd.min.js.map +1 -1
  24. package/dist/declarations/src/State.d.ts +1 -1
  25. package/dist/declarations/src/actions/raise.d.ts +9 -1
  26. package/dist/declarations/src/actions/send.d.ts +10 -1
  27. package/dist/declarations/src/createActor.d.ts +2 -1
  28. package/dist/declarations/src/getNextSnapshot.d.ts +2 -0
  29. package/dist/declarations/src/index.d.ts +1 -0
  30. package/dist/declarations/src/inspection.d.ts +3 -3
  31. package/dist/declarations/src/stateUtils.d.ts +17 -7
  32. package/dist/declarations/src/transition.d.ts +16 -0
  33. package/dist/declarations/src/types.d.ts +47 -14
  34. package/dist/declarations/src/utils.d.ts +1 -1
  35. package/dist/{log-d26be77d.development.cjs.js → log-17337367.development.cjs.js} +29 -19
  36. package/dist/{log-a2c94240.esm.js → log-2a773d37.esm.js} +28 -15
  37. package/dist/{log-098d2ed5.cjs.js → log-b0ee96de.cjs.js} +28 -15
  38. package/dist/{log-c92a07bc.development.esm.js → log-ef30c65f.development.esm.js} +29 -19
  39. package/dist/{raise-206d3d29.development.esm.js → raise-1db27a82.development.esm.js} +98 -73
  40. package/dist/{raise-830a98f7.development.cjs.js → raise-4acdb210.development.cjs.js} +98 -73
  41. package/dist/{raise-cde45f56.cjs.js → raise-60cebf03.cjs.js} +94 -71
  42. package/dist/{raise-c0e3c984.esm.js → raise-c17ec2bc.esm.js} +94 -71
  43. package/dist/xstate.cjs.d.mts +1 -1
  44. package/dist/xstate.cjs.d.ts +1 -1
  45. package/dist/xstate.cjs.js +60 -16
  46. package/dist/xstate.cjs.mjs +2 -0
  47. package/dist/xstate.development.cjs.js +60 -16
  48. package/dist/xstate.development.cjs.mjs +2 -0
  49. package/dist/xstate.development.esm.js +61 -19
  50. package/dist/xstate.esm.js +61 -19
  51. package/dist/xstate.umd.min.js +1 -1
  52. package/dist/xstate.umd.min.js.map +1 -1
  53. package/guards/dist/xstate-guards.cjs.d.mts +1 -1
  54. package/guards/dist/xstate-guards.cjs.d.ts +1 -1
  55. package/guards/dist/xstate-guards.cjs.js +1 -1
  56. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  57. package/guards/dist/xstate-guards.development.esm.js +1 -1
  58. package/guards/dist/xstate-guards.esm.js +1 -1
  59. package/guards/dist/xstate-guards.umd.min.js +1 -1
  60. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  61. package/package.json +7 -7
  62. package/actions/dist/xstate-actions.cjs.d.mts.map +0 -1
  63. package/actions/dist/xstate-actions.cjs.d.ts.map +0 -1
  64. package/actors/dist/xstate-actors.cjs.d.mts.map +0 -1
  65. package/actors/dist/xstate-actors.cjs.d.ts.map +0 -1
  66. package/dev/dist/xstate-dev.cjs.d.mts.map +0 -1
  67. package/dev/dist/xstate-dev.cjs.d.ts.map +0 -1
  68. package/dist/xstate.cjs.d.mts.map +0 -1
  69. package/dist/xstate.cjs.d.ts.map +0 -1
  70. package/guards/dist/xstate-guards.cjs.d.mts.map +0 -1
  71. package/guards/dist/xstate-guards.cjs.d.ts.map +0 -1
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var actors_dist_xstateActors = require('../actors/dist/xstate-actors.development.cjs.js');
6
- var guards_dist_xstateGuards = require('./raise-830a98f7.development.cjs.js');
7
- var log = require('./log-d26be77d.development.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-4acdb210.development.cjs.js');
7
+ var log = require('./log-17337367.development.cjs.js');
8
8
  require('../dev/dist/xstate-dev.development.cjs.js');
9
9
 
10
10
  /**
@@ -72,7 +72,7 @@ const toSerializableAction = action => {
72
72
  return action;
73
73
  };
74
74
  class StateNode {
75
- constructor( /** The raw config used to create the machine. */
75
+ constructor(/** The raw config used to create the machine. */
76
76
  config, options) {
77
77
  this.config = config;
78
78
  /**
@@ -224,10 +224,10 @@ class StateNode {
224
224
  systemId
225
225
  } = invokeConfig;
226
226
  const resolvedId = invokeConfig.id ?? guards_dist_xstateGuards.createInvokeId(this.id, i);
227
- const resolvedSrc = typeof src === 'string' ? src : `xstate.invoke.${guards_dist_xstateGuards.createInvokeId(this.id, i)}`;
227
+ const sourceName = typeof src === 'string' ? src : `xstate.invoke.${guards_dist_xstateGuards.createInvokeId(this.id, i)}`;
228
228
  return {
229
229
  ...invokeConfig,
230
- src: resolvedSrc,
230
+ src: sourceName,
231
231
  id: resolvedId,
232
232
  systemId: systemId,
233
233
  toJSON() {
@@ -239,7 +239,7 @@ class StateNode {
239
239
  return {
240
240
  ...invokeDefValues,
241
241
  type: 'xstate.invoke',
242
- src: resolvedSrc,
242
+ src: sourceName,
243
243
  id: resolvedId
244
244
  };
245
245
  }
@@ -328,7 +328,7 @@ class StateNode {
328
328
 
329
329
  const STATE_IDENTIFIER = '#';
330
330
  class StateMachine {
331
- constructor( /** The raw config used to create the machine. */
331
+ constructor(/** The raw config used to create the machine. */
332
332
  config, implementations) {
333
333
  this.config = config;
334
334
  /** The machine's own version. */
@@ -425,7 +425,7 @@ class StateMachine {
425
425
  * @param event The received event
426
426
  */
427
427
  transition(snapshot, event, actorScope) {
428
- return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope).snapshot;
428
+ return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope, []).snapshot;
429
429
  }
430
430
 
431
431
  /**
@@ -436,7 +436,7 @@ class StateMachine {
436
436
  * @param event The received event
437
437
  */
438
438
  microstep(snapshot, event, actorScope) {
439
- return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope).microstates;
439
+ return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope, []).microstates;
440
440
  }
441
441
  getTransitionData(snapshot, event) {
442
442
  return guards_dist_xstateGuards.transitionNode(this.root, snapshot.value, snapshot, event) || [];
@@ -466,7 +466,7 @@ class StateMachine {
466
466
  input: event.input,
467
467
  self
468
468
  });
469
- return guards_dist_xstateGuards.resolveActionsAndContext(preInitial, initEvent, actorScope, [log.assign(assignment)], internalQueue);
469
+ return guards_dist_xstateGuards.resolveActionsAndContext(preInitial, initEvent, actorScope, [log.assign(assignment)], internalQueue, undefined);
470
470
  }
471
471
  return preInitial;
472
472
  }
@@ -544,13 +544,13 @@ class StateMachine {
544
544
  children,
545
545
  _nodes: Array.from(guards_dist_xstateGuards.getAllStateNodes(guards_dist_xstateGuards.getStateNodes(this.root, snapshot.value)))
546
546
  }, this);
547
- let seen = new Set();
547
+ const seen = new Set();
548
548
  function reviveContext(contextPart, children) {
549
549
  if (seen.has(contextPart)) {
550
550
  return;
551
551
  }
552
552
  seen.add(contextPart);
553
- for (let key in contextPart) {
553
+ for (const key in contextPart) {
554
554
  const value = contextPart[key];
555
555
  if (value && typeof value === 'object') {
556
556
  if ('xstate$$type' in value && value.xstate$$type === guards_dist_xstateGuards.$$ACTOR_TYPE) {
@@ -566,9 +566,6 @@ class StateMachine {
566
566
  }
567
567
  }
568
568
 
569
- // this is not 100% accurate since we can't make parallel regions required in the result
570
- // `TTestValue` doesn't encode this information anyhow for us to be able to do that
571
- // this is fine for most practical use cases anyway though
572
569
  /**
573
570
  * Creates a state machine (statechart) with the given configuration.
574
571
  *
@@ -626,10 +623,13 @@ function createInertActorScope(actorLogic) {
626
623
  sessionId: '',
627
624
  stopChild: () => {},
628
625
  system: self.system,
629
- emit: () => {}
626
+ emit: () => {},
627
+ actionExecutor: () => {}
630
628
  };
631
629
  return inertActorScope;
632
630
  }
631
+
632
+ /** @deprecated Use `initialTransition(…)` instead. */
633
633
  function getInitialSnapshot(actorLogic, ...[input]) {
634
634
  const actorScope = createInertActorScope(actorLogic);
635
635
  return actorLogic.getInitialSnapshot(actorScope, input);
@@ -642,6 +642,7 @@ function getInitialSnapshot(actorLogic, ...[input]) {
642
642
  * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
643
643
  * used.
644
644
  *
645
+ * @deprecated Use `transition(…)` instead.
645
646
  * @example
646
647
  *
647
648
  * ```ts
@@ -675,6 +676,7 @@ function getNextSnapshot(actorLogic, snapshot, event) {
675
676
 
676
677
  // at the moment we allow extra actors - ones that are not specified by `children`
677
678
  // this could be reconsidered in the future
679
+
678
680
  function setup({
679
681
  schemas,
680
682
  actors,
@@ -695,6 +697,9 @@ function setup({
695
697
  };
696
698
  }
697
699
 
700
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
701
+
702
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
698
703
  class SimulatedClock {
699
704
  constructor() {
700
705
  this.timeouts = new Map();
@@ -795,6 +800,39 @@ function toPromise(actor) {
795
800
  });
796
801
  }
797
802
 
803
+ /**
804
+ * Given actor `logic`, a `snapshot`, and an `event`, returns a tuple of the
805
+ * `nextSnapshot` and `actions` to execute.
806
+ *
807
+ * This is a pure function that does not execute `actions`.
808
+ */
809
+ function transition(logic, snapshot, event) {
810
+ const executableActions = [];
811
+ const actorScope = createInertActorScope(logic);
812
+ actorScope.actionExecutor = action => {
813
+ executableActions.push(action);
814
+ };
815
+ const nextSnapshot = logic.transition(snapshot, event, actorScope);
816
+ return [nextSnapshot, executableActions];
817
+ }
818
+
819
+ /**
820
+ * Given actor `logic` and optional `input`, returns a tuple of the
821
+ * `nextSnapshot` and `actions` to execute from the initial transition (no
822
+ * previous state).
823
+ *
824
+ * This is a pure function that does not execute `actions`.
825
+ */
826
+ function initialTransition(logic, ...[input]) {
827
+ const executableActions = [];
828
+ const actorScope = createInertActorScope(logic);
829
+ actorScope.actionExecutor = action => {
830
+ executableActions.push(action);
831
+ };
832
+ const nextSnapshot = logic.getInitialSnapshot(actorScope, input);
833
+ return [nextSnapshot, executableActions];
834
+ }
835
+
798
836
  const defaultWaitForOptions = {
799
837
  timeout: Infinity // much more than 10 seconds
800
838
  };
@@ -830,6 +868,7 @@ function waitFor(actorRef, predicate, options) {
830
868
  signal
831
869
  } = resolvedOptions;
832
870
  if (signal?.aborted) {
871
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
833
872
  rej(signal.reason);
834
873
  return;
835
874
  }
@@ -861,6 +900,7 @@ function waitFor(actorRef, predicate, options) {
861
900
  * `abort` event
862
901
  */
863
902
  let abortListener;
903
+ // eslint-disable-next-line prefer-const
864
904
  let sub; // avoid TDZ when disposing synchronously
865
905
 
866
906
  // See if the current snapshot already matches the predicate
@@ -874,6 +914,7 @@ function waitFor(actorRef, predicate, options) {
874
914
  abortListener = () => {
875
915
  dispose();
876
916
  // XState does not "own" the signal, so we should reject with its reason (if any)
917
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
877
918
  rej(signal.reason);
878
919
  };
879
920
  signal.addEventListener('abort', abortListener);
@@ -882,6 +923,7 @@ function waitFor(actorRef, predicate, options) {
882
923
  next: checkEmitted,
883
924
  error: err => {
884
925
  dispose();
926
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
885
927
  rej(err);
886
928
  },
887
929
  complete: () => {
@@ -934,6 +976,8 @@ exports.assertEvent = assertEvent;
934
976
  exports.createMachine = createMachine;
935
977
  exports.getInitialSnapshot = getInitialSnapshot;
936
978
  exports.getNextSnapshot = getNextSnapshot;
979
+ exports.initialTransition = initialTransition;
937
980
  exports.setup = setup;
938
981
  exports.toPromise = toPromise;
982
+ exports.transition = transition;
939
983
  exports.waitFor = waitFor;
@@ -23,6 +23,7 @@ export {
23
23
  getInitialSnapshot,
24
24
  getNextSnapshot,
25
25
  getStateNodes,
26
+ initialTransition,
26
27
  interpret,
27
28
  isMachineSnapshot,
28
29
  log,
@@ -40,5 +41,6 @@ export {
40
41
  stopChild,
41
42
  toObserver,
42
43
  toPromise,
44
+ transition,
43
45
  waitFor
44
46
  } from "./xstate.development.cjs.js";
@@ -1,8 +1,8 @@
1
1
  export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.development.esm.js';
2
- import { t as toArray, S as STATE_DELIMITER, m as mapValues, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './raise-206d3d29.development.esm.js';
3
- export { B as Actor, I as __unsafe_getAllOwnEventDescriptors, D as and, M as cancel, A as createActor, j as getStateNodes, C as interpret, H as isMachineSnapshot, J as matchesState, E as not, F as or, K as pathToStateValue, O as raise, P as spawnChild, G as stateIn, Q as stop, R as stopChild, L as toObserver } from './raise-206d3d29.development.esm.js';
4
- import { a as assign } from './log-c92a07bc.development.esm.js';
5
- export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-c92a07bc.development.esm.js';
2
+ import { t as toArray, S as STATE_DELIMITER, m as mapValues, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './raise-1db27a82.development.esm.js';
3
+ export { B as Actor, I as __unsafe_getAllOwnEventDescriptors, D as and, M as cancel, A as createActor, j as getStateNodes, C as interpret, H as isMachineSnapshot, J as matchesState, E as not, F as or, K as pathToStateValue, O as raise, P as spawnChild, G as stateIn, Q as stop, R as stopChild, L as toObserver } from './raise-1db27a82.development.esm.js';
4
+ import { a as assign } from './log-ef30c65f.development.esm.js';
5
+ export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-ef30c65f.development.esm.js';
6
6
  import '../dev/dist/xstate-dev.development.esm.js';
7
7
 
8
8
  /**
@@ -70,7 +70,7 @@ const toSerializableAction = action => {
70
70
  return action;
71
71
  };
72
72
  class StateNode {
73
- constructor( /** The raw config used to create the machine. */
73
+ constructor(/** The raw config used to create the machine. */
74
74
  config, options) {
75
75
  this.config = config;
76
76
  /**
@@ -222,10 +222,10 @@ class StateNode {
222
222
  systemId
223
223
  } = invokeConfig;
224
224
  const resolvedId = invokeConfig.id ?? createInvokeId(this.id, i);
225
- const resolvedSrc = typeof src === 'string' ? src : `xstate.invoke.${createInvokeId(this.id, i)}`;
225
+ const sourceName = typeof src === 'string' ? src : `xstate.invoke.${createInvokeId(this.id, i)}`;
226
226
  return {
227
227
  ...invokeConfig,
228
- src: resolvedSrc,
228
+ src: sourceName,
229
229
  id: resolvedId,
230
230
  systemId: systemId,
231
231
  toJSON() {
@@ -237,7 +237,7 @@ class StateNode {
237
237
  return {
238
238
  ...invokeDefValues,
239
239
  type: 'xstate.invoke',
240
- src: resolvedSrc,
240
+ src: sourceName,
241
241
  id: resolvedId
242
242
  };
243
243
  }
@@ -326,7 +326,7 @@ class StateNode {
326
326
 
327
327
  const STATE_IDENTIFIER = '#';
328
328
  class StateMachine {
329
- constructor( /** The raw config used to create the machine. */
329
+ constructor(/** The raw config used to create the machine. */
330
330
  config, implementations) {
331
331
  this.config = config;
332
332
  /** The machine's own version. */
@@ -423,7 +423,7 @@ class StateMachine {
423
423
  * @param event The received event
424
424
  */
425
425
  transition(snapshot, event, actorScope) {
426
- return macrostep(snapshot, event, actorScope).snapshot;
426
+ return macrostep(snapshot, event, actorScope, []).snapshot;
427
427
  }
428
428
 
429
429
  /**
@@ -434,7 +434,7 @@ class StateMachine {
434
434
  * @param event The received event
435
435
  */
436
436
  microstep(snapshot, event, actorScope) {
437
- return macrostep(snapshot, event, actorScope).microstates;
437
+ return macrostep(snapshot, event, actorScope, []).microstates;
438
438
  }
439
439
  getTransitionData(snapshot, event) {
440
440
  return transitionNode(this.root, snapshot.value, snapshot, event) || [];
@@ -464,7 +464,7 @@ class StateMachine {
464
464
  input: event.input,
465
465
  self
466
466
  });
467
- return resolveActionsAndContext(preInitial, initEvent, actorScope, [assign(assignment)], internalQueue);
467
+ return resolveActionsAndContext(preInitial, initEvent, actorScope, [assign(assignment)], internalQueue, undefined);
468
468
  }
469
469
  return preInitial;
470
470
  }
@@ -542,13 +542,13 @@ class StateMachine {
542
542
  children,
543
543
  _nodes: Array.from(getAllStateNodes(getStateNodes(this.root, snapshot.value)))
544
544
  }, this);
545
- let seen = new Set();
545
+ const seen = new Set();
546
546
  function reviveContext(contextPart, children) {
547
547
  if (seen.has(contextPart)) {
548
548
  return;
549
549
  }
550
550
  seen.add(contextPart);
551
- for (let key in contextPart) {
551
+ for (const key in contextPart) {
552
552
  const value = contextPart[key];
553
553
  if (value && typeof value === 'object') {
554
554
  if ('xstate$$type' in value && value.xstate$$type === $$ACTOR_TYPE) {
@@ -564,9 +564,6 @@ class StateMachine {
564
564
  }
565
565
  }
566
566
 
567
- // this is not 100% accurate since we can't make parallel regions required in the result
568
- // `TTestValue` doesn't encode this information anyhow for us to be able to do that
569
- // this is fine for most practical use cases anyway though
570
567
  /**
571
568
  * Creates a state machine (statechart) with the given configuration.
572
569
  *
@@ -624,10 +621,13 @@ function createInertActorScope(actorLogic) {
624
621
  sessionId: '',
625
622
  stopChild: () => {},
626
623
  system: self.system,
627
- emit: () => {}
624
+ emit: () => {},
625
+ actionExecutor: () => {}
628
626
  };
629
627
  return inertActorScope;
630
628
  }
629
+
630
+ /** @deprecated Use `initialTransition(…)` instead. */
631
631
  function getInitialSnapshot(actorLogic, ...[input]) {
632
632
  const actorScope = createInertActorScope(actorLogic);
633
633
  return actorLogic.getInitialSnapshot(actorScope, input);
@@ -640,6 +640,7 @@ function getInitialSnapshot(actorLogic, ...[input]) {
640
640
  * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
641
641
  * used.
642
642
  *
643
+ * @deprecated Use `transition(…)` instead.
643
644
  * @example
644
645
  *
645
646
  * ```ts
@@ -673,6 +674,7 @@ function getNextSnapshot(actorLogic, snapshot, event) {
673
674
 
674
675
  // at the moment we allow extra actors - ones that are not specified by `children`
675
676
  // this could be reconsidered in the future
677
+
676
678
  function setup({
677
679
  schemas,
678
680
  actors,
@@ -693,6 +695,9 @@ function setup({
693
695
  };
694
696
  }
695
697
 
698
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
699
+
700
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
696
701
  class SimulatedClock {
697
702
  constructor() {
698
703
  this.timeouts = new Map();
@@ -793,6 +798,39 @@ function toPromise(actor) {
793
798
  });
794
799
  }
795
800
 
801
+ /**
802
+ * Given actor `logic`, a `snapshot`, and an `event`, returns a tuple of the
803
+ * `nextSnapshot` and `actions` to execute.
804
+ *
805
+ * This is a pure function that does not execute `actions`.
806
+ */
807
+ function transition(logic, snapshot, event) {
808
+ const executableActions = [];
809
+ const actorScope = createInertActorScope(logic);
810
+ actorScope.actionExecutor = action => {
811
+ executableActions.push(action);
812
+ };
813
+ const nextSnapshot = logic.transition(snapshot, event, actorScope);
814
+ return [nextSnapshot, executableActions];
815
+ }
816
+
817
+ /**
818
+ * Given actor `logic` and optional `input`, returns a tuple of the
819
+ * `nextSnapshot` and `actions` to execute from the initial transition (no
820
+ * previous state).
821
+ *
822
+ * This is a pure function that does not execute `actions`.
823
+ */
824
+ function initialTransition(logic, ...[input]) {
825
+ const executableActions = [];
826
+ const actorScope = createInertActorScope(logic);
827
+ actorScope.actionExecutor = action => {
828
+ executableActions.push(action);
829
+ };
830
+ const nextSnapshot = logic.getInitialSnapshot(actorScope, input);
831
+ return [nextSnapshot, executableActions];
832
+ }
833
+
796
834
  const defaultWaitForOptions = {
797
835
  timeout: Infinity // much more than 10 seconds
798
836
  };
@@ -828,6 +866,7 @@ function waitFor(actorRef, predicate, options) {
828
866
  signal
829
867
  } = resolvedOptions;
830
868
  if (signal?.aborted) {
869
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
831
870
  rej(signal.reason);
832
871
  return;
833
872
  }
@@ -859,6 +898,7 @@ function waitFor(actorRef, predicate, options) {
859
898
  * `abort` event
860
899
  */
861
900
  let abortListener;
901
+ // eslint-disable-next-line prefer-const
862
902
  let sub; // avoid TDZ when disposing synchronously
863
903
 
864
904
  // See if the current snapshot already matches the predicate
@@ -872,6 +912,7 @@ function waitFor(actorRef, predicate, options) {
872
912
  abortListener = () => {
873
913
  dispose();
874
914
  // XState does not "own" the signal, so we should reject with its reason (if any)
915
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
875
916
  rej(signal.reason);
876
917
  };
877
918
  signal.addEventListener('abort', abortListener);
@@ -880,6 +921,7 @@ function waitFor(actorRef, predicate, options) {
880
921
  next: checkEmitted,
881
922
  error: err => {
882
923
  dispose();
924
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
883
925
  rej(err);
884
926
  },
885
927
  complete: () => {
@@ -893,4 +935,4 @@ function waitFor(actorRef, predicate, options) {
893
935
  });
894
936
  }
895
937
 
896
- export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, getInitialSnapshot, getNextSnapshot, setup, toPromise, waitFor };
938
+ export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, getInitialSnapshot, getNextSnapshot, initialTransition, setup, toPromise, transition, waitFor };
@@ -1,8 +1,8 @@
1
1
  export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.esm.js';
2
- import { t as toArray, S as STATE_DELIMITER, m as mapValues, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './raise-c0e3c984.esm.js';
3
- export { B as Actor, I as __unsafe_getAllOwnEventDescriptors, D as and, M as cancel, A as createActor, j as getStateNodes, C as interpret, H as isMachineSnapshot, J as matchesState, E as not, F as or, K as pathToStateValue, O as raise, P as spawnChild, G as stateIn, Q as stop, R as stopChild, L as toObserver } from './raise-c0e3c984.esm.js';
4
- import { a as assign } from './log-a2c94240.esm.js';
5
- export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-a2c94240.esm.js';
2
+ import { t as toArray, S as STATE_DELIMITER, m as mapValues, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './raise-c17ec2bc.esm.js';
3
+ export { B as Actor, I as __unsafe_getAllOwnEventDescriptors, D as and, M as cancel, A as createActor, j as getStateNodes, C as interpret, H as isMachineSnapshot, J as matchesState, E as not, F as or, K as pathToStateValue, O as raise, P as spawnChild, G as stateIn, Q as stop, R as stopChild, L as toObserver } from './raise-c17ec2bc.esm.js';
4
+ import { a as assign } from './log-2a773d37.esm.js';
5
+ export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-2a773d37.esm.js';
6
6
  import '../dev/dist/xstate-dev.esm.js';
7
7
 
8
8
  /**
@@ -70,7 +70,7 @@ const toSerializableAction = action => {
70
70
  return action;
71
71
  };
72
72
  class StateNode {
73
- constructor( /** The raw config used to create the machine. */
73
+ constructor(/** The raw config used to create the machine. */
74
74
  config, options) {
75
75
  this.config = config;
76
76
  /**
@@ -222,10 +222,10 @@ class StateNode {
222
222
  systemId
223
223
  } = invokeConfig;
224
224
  const resolvedId = invokeConfig.id ?? createInvokeId(this.id, i);
225
- const resolvedSrc = typeof src === 'string' ? src : `xstate.invoke.${createInvokeId(this.id, i)}`;
225
+ const sourceName = typeof src === 'string' ? src : `xstate.invoke.${createInvokeId(this.id, i)}`;
226
226
  return {
227
227
  ...invokeConfig,
228
- src: resolvedSrc,
228
+ src: sourceName,
229
229
  id: resolvedId,
230
230
  systemId: systemId,
231
231
  toJSON() {
@@ -237,7 +237,7 @@ class StateNode {
237
237
  return {
238
238
  ...invokeDefValues,
239
239
  type: 'xstate.invoke',
240
- src: resolvedSrc,
240
+ src: sourceName,
241
241
  id: resolvedId
242
242
  };
243
243
  }
@@ -326,7 +326,7 @@ class StateNode {
326
326
 
327
327
  const STATE_IDENTIFIER = '#';
328
328
  class StateMachine {
329
- constructor( /** The raw config used to create the machine. */
329
+ constructor(/** The raw config used to create the machine. */
330
330
  config, implementations) {
331
331
  this.config = config;
332
332
  /** The machine's own version. */
@@ -420,7 +420,7 @@ class StateMachine {
420
420
  * @param event The received event
421
421
  */
422
422
  transition(snapshot, event, actorScope) {
423
- return macrostep(snapshot, event, actorScope).snapshot;
423
+ return macrostep(snapshot, event, actorScope, []).snapshot;
424
424
  }
425
425
 
426
426
  /**
@@ -431,7 +431,7 @@ class StateMachine {
431
431
  * @param event The received event
432
432
  */
433
433
  microstep(snapshot, event, actorScope) {
434
- return macrostep(snapshot, event, actorScope).microstates;
434
+ return macrostep(snapshot, event, actorScope, []).microstates;
435
435
  }
436
436
  getTransitionData(snapshot, event) {
437
437
  return transitionNode(this.root, snapshot.value, snapshot, event) || [];
@@ -461,7 +461,7 @@ class StateMachine {
461
461
  input: event.input,
462
462
  self
463
463
  });
464
- return resolveActionsAndContext(preInitial, initEvent, actorScope, [assign(assignment)], internalQueue);
464
+ return resolveActionsAndContext(preInitial, initEvent, actorScope, [assign(assignment)], internalQueue, undefined);
465
465
  }
466
466
  return preInitial;
467
467
  }
@@ -539,13 +539,13 @@ class StateMachine {
539
539
  children,
540
540
  _nodes: Array.from(getAllStateNodes(getStateNodes(this.root, snapshot.value)))
541
541
  }, this);
542
- let seen = new Set();
542
+ const seen = new Set();
543
543
  function reviveContext(contextPart, children) {
544
544
  if (seen.has(contextPart)) {
545
545
  return;
546
546
  }
547
547
  seen.add(contextPart);
548
- for (let key in contextPart) {
548
+ for (const key in contextPart) {
549
549
  const value = contextPart[key];
550
550
  if (value && typeof value === 'object') {
551
551
  if ('xstate$$type' in value && value.xstate$$type === $$ACTOR_TYPE) {
@@ -561,9 +561,6 @@ class StateMachine {
561
561
  }
562
562
  }
563
563
 
564
- // this is not 100% accurate since we can't make parallel regions required in the result
565
- // `TTestValue` doesn't encode this information anyhow for us to be able to do that
566
- // this is fine for most practical use cases anyway though
567
564
  /**
568
565
  * Creates a state machine (statechart) with the given configuration.
569
566
  *
@@ -621,10 +618,13 @@ function createInertActorScope(actorLogic) {
621
618
  sessionId: '',
622
619
  stopChild: () => {},
623
620
  system: self.system,
624
- emit: () => {}
621
+ emit: () => {},
622
+ actionExecutor: () => {}
625
623
  };
626
624
  return inertActorScope;
627
625
  }
626
+
627
+ /** @deprecated Use `initialTransition(…)` instead. */
628
628
  function getInitialSnapshot(actorLogic, ...[input]) {
629
629
  const actorScope = createInertActorScope(actorLogic);
630
630
  return actorLogic.getInitialSnapshot(actorScope, input);
@@ -637,6 +637,7 @@ function getInitialSnapshot(actorLogic, ...[input]) {
637
637
  * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
638
638
  * used.
639
639
  *
640
+ * @deprecated Use `transition(…)` instead.
640
641
  * @example
641
642
  *
642
643
  * ```ts
@@ -670,6 +671,7 @@ function getNextSnapshot(actorLogic, snapshot, event) {
670
671
 
671
672
  // at the moment we allow extra actors - ones that are not specified by `children`
672
673
  // this could be reconsidered in the future
674
+
673
675
  function setup({
674
676
  schemas,
675
677
  actors,
@@ -690,6 +692,9 @@ function setup({
690
692
  };
691
693
  }
692
694
 
695
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
696
+
697
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
693
698
  class SimulatedClock {
694
699
  constructor() {
695
700
  this.timeouts = new Map();
@@ -790,6 +795,39 @@ function toPromise(actor) {
790
795
  });
791
796
  }
792
797
 
798
+ /**
799
+ * Given actor `logic`, a `snapshot`, and an `event`, returns a tuple of the
800
+ * `nextSnapshot` and `actions` to execute.
801
+ *
802
+ * This is a pure function that does not execute `actions`.
803
+ */
804
+ function transition(logic, snapshot, event) {
805
+ const executableActions = [];
806
+ const actorScope = createInertActorScope(logic);
807
+ actorScope.actionExecutor = action => {
808
+ executableActions.push(action);
809
+ };
810
+ const nextSnapshot = logic.transition(snapshot, event, actorScope);
811
+ return [nextSnapshot, executableActions];
812
+ }
813
+
814
+ /**
815
+ * Given actor `logic` and optional `input`, returns a tuple of the
816
+ * `nextSnapshot` and `actions` to execute from the initial transition (no
817
+ * previous state).
818
+ *
819
+ * This is a pure function that does not execute `actions`.
820
+ */
821
+ function initialTransition(logic, ...[input]) {
822
+ const executableActions = [];
823
+ const actorScope = createInertActorScope(logic);
824
+ actorScope.actionExecutor = action => {
825
+ executableActions.push(action);
826
+ };
827
+ const nextSnapshot = logic.getInitialSnapshot(actorScope, input);
828
+ return [nextSnapshot, executableActions];
829
+ }
830
+
793
831
  const defaultWaitForOptions = {
794
832
  timeout: Infinity // much more than 10 seconds
795
833
  };
@@ -825,6 +863,7 @@ function waitFor(actorRef, predicate, options) {
825
863
  signal
826
864
  } = resolvedOptions;
827
865
  if (signal?.aborted) {
866
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
828
867
  rej(signal.reason);
829
868
  return;
830
869
  }
@@ -853,6 +892,7 @@ function waitFor(actorRef, predicate, options) {
853
892
  * `abort` event
854
893
  */
855
894
  let abortListener;
895
+ // eslint-disable-next-line prefer-const
856
896
  let sub; // avoid TDZ when disposing synchronously
857
897
 
858
898
  // See if the current snapshot already matches the predicate
@@ -866,6 +906,7 @@ function waitFor(actorRef, predicate, options) {
866
906
  abortListener = () => {
867
907
  dispose();
868
908
  // XState does not "own" the signal, so we should reject with its reason (if any)
909
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
869
910
  rej(signal.reason);
870
911
  };
871
912
  signal.addEventListener('abort', abortListener);
@@ -874,6 +915,7 @@ function waitFor(actorRef, predicate, options) {
874
915
  next: checkEmitted,
875
916
  error: err => {
876
917
  dispose();
918
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
877
919
  rej(err);
878
920
  },
879
921
  complete: () => {
@@ -887,4 +929,4 @@ function waitFor(actorRef, predicate, options) {
887
929
  });
888
930
  }
889
931
 
890
- export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, getInitialSnapshot, getNextSnapshot, setup, toPromise, waitFor };
932
+ export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, getInitialSnapshot, getNextSnapshot, initialTransition, setup, toPromise, transition, waitFor };