xstate 6.0.0-alpha.46 → 6.0.0-alpha.48

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 (73) hide show
  1. package/README.md +7 -1
  2. package/dist/{StateMachine-9509c0bd.esm.js → StateMachine-8329769a.esm.js} +15 -8
  3. package/dist/{StateMachine-a5ede014.development.esm.js → StateMachine-88b3c26a.development.esm.js} +15 -8
  4. package/dist/{StateMachine-756e1335.cjs.js → StateMachine-b09d2b00.cjs.js} +20 -13
  5. package/dist/{StateMachine-fa133051.development.cjs.js → StateMachine-b3a890bf.development.cjs.js} +20 -13
  6. package/dist/{createActor-9523f716.esm.js → createActor-27287c65.esm.js} +84 -25
  7. package/dist/{createActor-f5277b31.development.esm.js → createActor-309d77eb.development.esm.js} +84 -25
  8. package/dist/{createActor-0816a84b.cjs.js → createActor-9b41ade9.cjs.js} +84 -25
  9. package/dist/{createActor-f6e6146f.development.cjs.js → createActor-cc3d6dde.development.cjs.js} +84 -25
  10. package/dist/declarations/src/StateMachine.d.ts +7 -3
  11. package/dist/declarations/src/createMachineFromConfig.d.ts +5 -77
  12. package/dist/declarations/src/durable/index.d.ts +9 -2
  13. package/dist/declarations/src/scxml/index.d.ts +1 -0
  14. package/dist/declarations/src/scxml/runtime.d.ts +251 -0
  15. package/dist/declarations/src/scxml/scxml.d.ts +19 -0
  16. package/dist/declarations/src/setup.d.ts +36 -7
  17. package/dist/declarations/src/types.d.ts +5 -4
  18. package/dist/declarations/src/types.v6.d.ts +27 -5
  19. package/dist/{effectDescriptor-9a14147f.development.esm.js → effectDescriptor-7707794b.development.esm.js} +4 -1
  20. package/dist/{effectDescriptor-8a5d62e0.development.cjs.js → effectDescriptor-a1678d06.development.cjs.js} +4 -1
  21. package/dist/{effectDescriptor-87826c41.esm.js → effectDescriptor-b1f5deb6.esm.js} +4 -1
  22. package/dist/{effectDescriptor-3a016bbe.cjs.js → effectDescriptor-bd36a33e.cjs.js} +4 -1
  23. package/dist/types-1793b8b6.development.esm.js +185 -0
  24. package/dist/types-7e468202.development.cjs.js +189 -0
  25. package/dist/types-81d03bef.esm.js +185 -0
  26. package/dist/types-9c1bc85f.cjs.js +189 -0
  27. package/dist/validation-05f1d517.esm.js +8 -0
  28. package/dist/validation-4561b43c.cjs.js +10 -0
  29. package/dist/validation-e69470ad.development.esm.js +8 -0
  30. package/dist/validation-f2714a35.development.cjs.js +10 -0
  31. package/dist/xstate-actors.cjs.js +934 -9
  32. package/dist/xstate-actors.development.cjs.js +934 -9
  33. package/dist/xstate-actors.development.esm.js +932 -2
  34. package/dist/xstate-actors.esm.js +932 -2
  35. package/dist/xstate-actors.umd.min.js.map +1 -1
  36. package/dist/xstate-durable.cjs.js +9 -6
  37. package/dist/xstate-durable.development.cjs.js +9 -6
  38. package/dist/xstate-durable.development.esm.js +9 -6
  39. package/dist/xstate-durable.esm.js +9 -6
  40. package/dist/xstate-durable.umd.min.js +1 -1
  41. package/dist/xstate-durable.umd.min.js.map +1 -1
  42. package/dist/xstate-graph.cjs.js +4 -3
  43. package/dist/xstate-graph.development.cjs.js +4 -3
  44. package/dist/xstate-graph.development.esm.js +4 -3
  45. package/dist/xstate-graph.esm.js +4 -3
  46. package/dist/xstate-graph.umd.min.js +1 -1
  47. package/dist/xstate-graph.umd.min.js.map +1 -1
  48. package/dist/xstate-scxml.cjs.d.mts +2 -0
  49. package/dist/xstate-scxml.cjs.d.ts +2 -0
  50. package/dist/xstate-scxml.cjs.js +2387 -0
  51. package/dist/xstate-scxml.cjs.mjs +3 -0
  52. package/dist/xstate-scxml.development.cjs.js +2387 -0
  53. package/dist/xstate-scxml.development.cjs.mjs +3 -0
  54. package/dist/xstate-scxml.development.esm.js +2383 -0
  55. package/dist/xstate-scxml.esm.js +2383 -0
  56. package/dist/xstate-scxml.umd.min.js +26 -0
  57. package/dist/xstate-scxml.umd.min.js.map +1 -0
  58. package/dist/xstate-validation.cjs.js +1 -1
  59. package/dist/xstate-validation.development.cjs.js +1 -1
  60. package/dist/xstate-validation.development.esm.js +1 -1
  61. package/dist/xstate-validation.esm.js +1 -1
  62. package/dist/xstate.cjs.js +160 -1036
  63. package/dist/xstate.development.cjs.js +160 -1036
  64. package/dist/xstate.development.esm.js +160 -1035
  65. package/dist/xstate.esm.js +160 -1035
  66. package/dist/xstate.umd.min.js +1 -1
  67. package/dist/xstate.umd.min.js.map +1 -1
  68. package/package.json +20 -2
  69. package/scxml/package.json +8 -0
  70. package/dist/index-1e1e1cba.development.cjs.js +0 -946
  71. package/dist/index-3907b7aa.development.esm.js +0 -937
  72. package/dist/index-7b9c4f77.cjs.js +0 -946
  73. package/dist/index-c7431a16.esm.js +0 -937
package/README.md CHANGED
@@ -11,7 +11,9 @@
11
11
  <br />
12
12
  </p>
13
13
 
14
- XState is a state management and orchestration solution for JavaScript and TypeScript apps. It has _zero_ dependencies, and is useful for frontend and backend application logic.
14
+ <!-- runtime dependencies and public entry points from package.json -->
15
+
16
+ XState is a state management and orchestration solution for JavaScript and TypeScript apps. The main `xstate` entry point has _zero_ runtime dependencies; the optional `xstate/scxml` entry point uses an XML parser. XState is useful for frontend and backend application logic.
15
17
 
16
18
  It uses event-driven programming, state machines, statecharts, and the actor model to handle complex logic in predictable, robust, and visual ways. XState provides a powerful and flexible way to manage application and workflow state by allowing developers to model logic as actors and state machines.
17
19
 
@@ -27,6 +29,10 @@ It uses event-driven programming, state machines, statecharts, and the actor mod
27
29
 
28
30
  📑 Inspired by the [SCXML specification](https://www.w3.org/TR/scxml/)
29
31
 
32
+ <!-- public SCXML entry point from src/scxml/index.ts -->
33
+
34
+ Create machines from SCXML documents with the opt-in `xstate/scxml` entry point. See the [SCXML guide](../../docs/scxml.md).
35
+
30
36
  💬 Chat on the [Stately Discord Community](https://discord.gg/xstate)
31
37
 
32
38
  ✍️ Browse through the many [XState examples](https://github.com/statelyai/xstate/tree/main/examples)
@@ -1,5 +1,5 @@
1
- import { Q as resolveRegisteredActorSource, R as allocateChildId, S as assertChildIdFree, T as reserveChildId, U as STATE_DELIMITER, V as mapValues, t as toArray, W as createInvokeId, Y as getDelayedTransitions, Z as getEventDescriptorKey, _ as getCandidates, $ as evaluateCandidate, a0 as formatTransition, a1 as NULL_EVENT, a2 as toTransitionConfigArray, a3 as createInvokeTimeoutEventId, a4 as matchesActorSession, a5 as formatRouteTransitions, a6 as resolveStateValue, a7 as getAllStateNodes, x as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, aa as createInertActorScope, ab as setInertActorScopeSnapshot, a as beginSpawnAllocation, ac as attachSnapshotActorRef, ad as macrostep, ae as isInertActorScope, af as setSnapshotActorRef, ag as cloneMachineSnapshot, ah as transitionNode, g as XSTATE_TIMER, m as matchesEventDescriptor, ai as getTransitionResult, aj as hasEffect, ak as createErrorPlatformEvent, al as withActorSelf, r as resolveActionsWithContext, am as takeSpawnAllocationCounters, an as mergeActorIdCounters, K as createInitEvent, ao as createSpawnEffect, ap as initialMicrostep, aq as toStatePath, ar as isStateId, as as getStateNodeByPath, at as getPersistedSnapshot, au as createRemoteActorRef, av as resolveReferencedActor, aw as createInvokeTimeoutEvent, ax as parseGeneratedActorId, ay as ACTOR_REF_TYPE, k as createActor } from './createActor-9523f716.esm.js';
2
- import { g as assertValid } from './index-c7431a16.esm.js';
1
+ import { Q as resolveRegisteredActorSource, R as allocateChildId, S as assertChildIdFree, T as reserveChildId, U as STATE_DELIMITER, V as mapValues, t as toArray, W as createInvokeId, Y as getDelayedTransitions, Z as getEventDescriptorKey, _ as getCandidates, $ as evaluateCandidate, a0 as formatTransition, a1 as NULL_EVENT, a2 as toTransitionConfigArray, a3 as createInvokeTimeoutEventId, a4 as matchesActorSession, a5 as formatRouteTransitions, a6 as resolveStateValue, a7 as getAllStateNodes, x as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, aa as createInertActorScope, ab as setInertActorScopeSnapshot, a as beginSpawnAllocation, ac as attachSnapshotActorRef, ad as macrostep, ae as isInertActorScope, af as setSnapshotActorRef, ag as cloneMachineSnapshot, ah as transitionNode, g as XSTATE_TIMER, m as matchesEventDescriptor, ai as getTransitionResult, aj as hasEffect, ak as createErrorPlatformEvent, al as withActorSelf, r as resolveActionsWithContext, am as takeSpawnAllocationCounters, an as mergeActorIdCounters, K as createInitEvent, ao as createSpawnEffect, ap as initialMicrostep, aq as toStatePath, ar as isStateId, as as getStateNodeByPath, at as getPersistedSnapshot, au as createRemoteActorRef, av as resolveReferencedActor, aw as createInvokeTimeoutEvent, ax as parseGeneratedActorId, ay as ACTOR_REF_TYPE, k as createActor } from './createActor-27287c65.esm.js';
2
+ import { a as assertValid } from './validation-05f1d517.esm.js';
3
3
 
4
4
  function createSpawner(actorScope, actors, spawnedChildren) {
5
5
  return (src, options) => {
@@ -385,14 +385,17 @@ function formatTransitions(stateNode) {
385
385
  function formatInitialTransition(stateNode, _target) {
386
386
  const targetString = typeof _target === 'object' && _target !== null ? _target.target : _target;
387
387
  const input = typeof _target === 'object' && _target !== null ? _target.input : undefined;
388
- const resolvedTarget = typeof targetString === 'string' ? stateNode.states[targetString] : undefined;
389
- if (!resolvedTarget && targetString) {
390
- throw new Error(`Initial state "${targetString}" not found on "#${stateNode.id}"`);
388
+ const to = typeof _target === 'object' && _target !== null ? _target.to : undefined;
389
+ const targetStrings = Array.isArray(targetString) ? targetString : targetString ? [targetString] : [];
390
+ const resolvedTargets = targetStrings.map(target => target.startsWith('#') ? stateNode.machine.getStateNodeById(target.slice(1)) : stateNode.states[target]);
391
+ if (resolvedTargets.some(target => !target)) {
392
+ throw new Error(`Initial state not found on "#${stateNode.id}"`);
391
393
  }
392
394
  const transition = {
393
395
  source: stateNode,
394
- target: resolvedTarget ? [resolvedTarget] : undefined,
395
- input
396
+ target: resolvedTargets.length ? resolvedTargets : undefined,
397
+ input,
398
+ to
396
399
  };
397
400
  return transition;
398
401
  }
@@ -1018,7 +1021,11 @@ class StateMachine {
1018
1021
  restoreSnapshot(snapshot, actorScope) {
1019
1022
  const usesInertScope = !actorScope;
1020
1023
  const resolvedActorScope = actorScope ?? createInertActorScope(this);
1021
- const persistedMachine = snapshot.machine;
1024
+ const snapshotMachine = snapshot.machine;
1025
+ // A live machine snapshot carries its producing machine so helpers such as
1026
+ // `matches()` can resolve state nodes. That runtime association is not the
1027
+ // persisted `{ id, version }` identity written by getPersistedSnapshot().
1028
+ const persistedMachine = snapshotMachine && typeof snapshotMachine === 'object' && typeof snapshotMachine.transition === 'function' && 'root' in snapshotMachine ? undefined : snapshotMachine;
1022
1029
  const legacyPersistedVersion = snapshot.version;
1023
1030
  const persistedVersion = typeof persistedMachine?.version === 'string' ? persistedMachine.version : legacyPersistedVersion;
1024
1031
  if (legacyPersistedVersion !== undefined && persistedMachine?.version !== undefined && persistedMachine.version !== legacyPersistedVersion) {
@@ -1,5 +1,5 @@
1
- import { Q as resolveRegisteredActorSource, R as allocateChildId, S as assertChildIdFree, T as reserveChildId, U as STATE_DELIMITER, V as mapValues, t as toArray, W as createInvokeId, Y as getDelayedTransitions, Z as getEventDescriptorKey, _ as getCandidates, $ as evaluateCandidate, a0 as formatTransition, a1 as NULL_EVENT, a2 as toTransitionConfigArray, a3 as createInvokeTimeoutEventId, a4 as matchesActorSession, a5 as formatRouteTransitions, a6 as resolveStateValue, a7 as getAllStateNodes, x as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, aa as createInertActorScope, ab as setInertActorScopeSnapshot, a as beginSpawnAllocation, ac as attachSnapshotActorRef, ad as macrostep, ae as isInertActorScope, af as setSnapshotActorRef, ag as cloneMachineSnapshot, ah as transitionNode, g as XSTATE_TIMER, m as matchesEventDescriptor, ai as getTransitionResult, aj as hasEffect, ak as createErrorPlatformEvent, al as withActorSelf, r as resolveActionsWithContext, am as takeSpawnAllocationCounters, an as mergeActorIdCounters, K as createInitEvent, ao as createSpawnEffect, ap as initialMicrostep, aq as toStatePath, ar as isStateId, as as getStateNodeByPath, at as getPersistedSnapshot, au as createRemoteActorRef, av as resolveReferencedActor, aw as createInvokeTimeoutEvent, ax as parseGeneratedActorId, ay as ACTOR_REF_TYPE, k as createActor } from './createActor-f5277b31.development.esm.js';
2
- import { g as assertValid } from './index-3907b7aa.development.esm.js';
1
+ import { Q as resolveRegisteredActorSource, R as allocateChildId, S as assertChildIdFree, T as reserveChildId, U as STATE_DELIMITER, V as mapValues, t as toArray, W as createInvokeId, Y as getDelayedTransitions, Z as getEventDescriptorKey, _ as getCandidates, $ as evaluateCandidate, a0 as formatTransition, a1 as NULL_EVENT, a2 as toTransitionConfigArray, a3 as createInvokeTimeoutEventId, a4 as matchesActorSession, a5 as formatRouteTransitions, a6 as resolveStateValue, a7 as getAllStateNodes, x as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, aa as createInertActorScope, ab as setInertActorScopeSnapshot, a as beginSpawnAllocation, ac as attachSnapshotActorRef, ad as macrostep, ae as isInertActorScope, af as setSnapshotActorRef, ag as cloneMachineSnapshot, ah as transitionNode, g as XSTATE_TIMER, m as matchesEventDescriptor, ai as getTransitionResult, aj as hasEffect, ak as createErrorPlatformEvent, al as withActorSelf, r as resolveActionsWithContext, am as takeSpawnAllocationCounters, an as mergeActorIdCounters, K as createInitEvent, ao as createSpawnEffect, ap as initialMicrostep, aq as toStatePath, ar as isStateId, as as getStateNodeByPath, at as getPersistedSnapshot, au as createRemoteActorRef, av as resolveReferencedActor, aw as createInvokeTimeoutEvent, ax as parseGeneratedActorId, ay as ACTOR_REF_TYPE, k as createActor } from './createActor-309d77eb.development.esm.js';
2
+ import { a as assertValid } from './validation-e69470ad.development.esm.js';
3
3
 
4
4
  function createSpawner(actorScope, actors, spawnedChildren) {
5
5
  return (src, options) => {
@@ -403,14 +403,17 @@ function formatTransitions(stateNode) {
403
403
  function formatInitialTransition(stateNode, _target) {
404
404
  const targetString = typeof _target === 'object' && _target !== null ? _target.target : _target;
405
405
  const input = typeof _target === 'object' && _target !== null ? _target.input : undefined;
406
- const resolvedTarget = typeof targetString === 'string' ? stateNode.states[targetString] : undefined;
407
- if (!resolvedTarget && targetString) {
408
- throw new Error(`Initial state node "${targetString}" not found on parent state node #${stateNode.id}` );
406
+ const to = typeof _target === 'object' && _target !== null ? _target.to : undefined;
407
+ const targetStrings = Array.isArray(targetString) ? targetString : targetString ? [targetString] : [];
408
+ const resolvedTargets = targetStrings.map(target => target.startsWith('#') ? stateNode.machine.getStateNodeById(target.slice(1)) : stateNode.states[target]);
409
+ if (resolvedTargets.some(target => !target)) {
410
+ throw new Error(`Initial state node "${targetStrings.find((_, index) => !resolvedTargets[index])}" not found on parent state node #${stateNode.id}` );
409
411
  }
410
412
  const transition = {
411
413
  source: stateNode,
412
- target: resolvedTarget ? [resolvedTarget] : undefined,
413
- input
414
+ target: resolvedTargets.length ? resolvedTargets : undefined,
415
+ input,
416
+ to
414
417
  };
415
418
  return transition;
416
419
  }
@@ -1047,7 +1050,11 @@ class StateMachine {
1047
1050
  restoreSnapshot(snapshot, actorScope) {
1048
1051
  const usesInertScope = !actorScope;
1049
1052
  const resolvedActorScope = actorScope ?? createInertActorScope(this);
1050
- const persistedMachine = snapshot.machine;
1053
+ const snapshotMachine = snapshot.machine;
1054
+ // A live machine snapshot carries its producing machine so helpers such as
1055
+ // `matches()` can resolve state nodes. That runtime association is not the
1056
+ // persisted `{ id, version }` identity written by getPersistedSnapshot().
1057
+ const persistedMachine = snapshotMachine && typeof snapshotMachine === 'object' && typeof snapshotMachine.transition === 'function' && 'root' in snapshotMachine ? undefined : snapshotMachine;
1051
1058
  const legacyPersistedVersion = snapshot.version;
1052
1059
  const persistedVersion = typeof persistedMachine?.version === 'string' ? persistedMachine.version : legacyPersistedVersion;
1053
1060
  if (legacyPersistedVersion !== undefined && persistedMachine?.version !== undefined && persistedMachine.version !== legacyPersistedVersion) {
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var createActor = require('./createActor-0816a84b.cjs.js');
4
- var dist_xstateActors = require('./index-7b9c4f77.cjs.js');
3
+ var createActor = require('./createActor-9b41ade9.cjs.js');
4
+ var validation = require('./validation-4561b43c.cjs.js');
5
5
 
6
6
  function createSpawner(actorScope, actors, spawnedChildren) {
7
7
  return (src, options) => {
@@ -387,14 +387,17 @@ function formatTransitions(stateNode) {
387
387
  function formatInitialTransition(stateNode, _target) {
388
388
  const targetString = typeof _target === 'object' && _target !== null ? _target.target : _target;
389
389
  const input = typeof _target === 'object' && _target !== null ? _target.input : undefined;
390
- const resolvedTarget = typeof targetString === 'string' ? stateNode.states[targetString] : undefined;
391
- if (!resolvedTarget && targetString) {
392
- throw new Error(`Initial state "${targetString}" not found on "#${stateNode.id}"`);
390
+ const to = typeof _target === 'object' && _target !== null ? _target.to : undefined;
391
+ const targetStrings = Array.isArray(targetString) ? targetString : targetString ? [targetString] : [];
392
+ const resolvedTargets = targetStrings.map(target => target.startsWith('#') ? stateNode.machine.getStateNodeById(target.slice(1)) : stateNode.states[target]);
393
+ if (resolvedTargets.some(target => !target)) {
394
+ throw new Error(`Initial state not found on "#${stateNode.id}"`);
393
395
  }
394
396
  const transition = {
395
397
  source: stateNode,
396
- target: resolvedTarget ? [resolvedTarget] : undefined,
397
- input
398
+ target: resolvedTargets.length ? resolvedTargets : undefined,
399
+ input,
400
+ to
398
401
  };
399
402
  return transition;
400
403
  }
@@ -712,7 +715,7 @@ class StateMachine {
712
715
  const usesInertScope = !actorScope;
713
716
  const resolvedActorScope = actorScope ?? createActor.createInertActorScope(this, snapshot);
714
717
  if (this.validator) {
715
- dist_xstateActors.assertValid(this.validator, {
718
+ validation.assertValid(this.validator, {
716
719
  kind: 'event',
717
720
  logic: this,
718
721
  event,
@@ -730,7 +733,7 @@ class StateMachine {
730
733
  }
731
734
  const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? createActor.attachSnapshotActorRef(resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
732
735
  if (this.validator) {
733
- dist_xstateActors.assertValid(this.validator, {
736
+ validation.assertValid(this.validator, {
734
737
  kind: 'result',
735
738
  logic: this,
736
739
  snapshot: returnedSnapshot,
@@ -749,7 +752,7 @@ class StateMachine {
749
752
  const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : createActor.attachSnapshotActorRef(resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
750
753
  const effects = this._collectEffects(microsteps);
751
754
  if (this.validator) {
752
- dist_xstateActors.assertValid(this.validator, {
755
+ validation.assertValid(this.validator, {
753
756
  kind: 'result',
754
757
  logic: this,
755
758
  snapshot: returnedSnapshot,
@@ -941,7 +944,7 @@ class StateMachine {
941
944
  }
942
945
  initialTransition(input, actorScope) {
943
946
  if (this.validator) {
944
- dist_xstateActors.assertValid(this.validator, {
947
+ validation.assertValid(this.validator, {
945
948
  kind: 'input',
946
949
  logic: this,
947
950
  input
@@ -961,7 +964,7 @@ class StateMachine {
961
964
  const returnedSnapshot = usesInertScope ? createActor.attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope, true);
962
965
  const effects = this._collectEffects(microsteps);
963
966
  if (this.validator) {
964
- dist_xstateActors.assertValid(this.validator, {
967
+ validation.assertValid(this.validator, {
965
968
  kind: 'result',
966
969
  logic: this,
967
970
  snapshot: returnedSnapshot,
@@ -1020,7 +1023,11 @@ class StateMachine {
1020
1023
  restoreSnapshot(snapshot, actorScope) {
1021
1024
  const usesInertScope = !actorScope;
1022
1025
  const resolvedActorScope = actorScope ?? createActor.createInertActorScope(this);
1023
- const persistedMachine = snapshot.machine;
1026
+ const snapshotMachine = snapshot.machine;
1027
+ // A live machine snapshot carries its producing machine so helpers such as
1028
+ // `matches()` can resolve state nodes. That runtime association is not the
1029
+ // persisted `{ id, version }` identity written by getPersistedSnapshot().
1030
+ const persistedMachine = snapshotMachine && typeof snapshotMachine === 'object' && typeof snapshotMachine.transition === 'function' && 'root' in snapshotMachine ? undefined : snapshotMachine;
1024
1031
  const legacyPersistedVersion = snapshot.version;
1025
1032
  const persistedVersion = typeof persistedMachine?.version === 'string' ? persistedMachine.version : legacyPersistedVersion;
1026
1033
  if (legacyPersistedVersion !== undefined && persistedMachine?.version !== undefined && persistedMachine.version !== legacyPersistedVersion) {
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var createActor = require('./createActor-f6e6146f.development.cjs.js');
4
- var dist_xstateActors = require('./index-1e1e1cba.development.cjs.js');
3
+ var createActor = require('./createActor-cc3d6dde.development.cjs.js');
4
+ var validation = require('./validation-f2714a35.development.cjs.js');
5
5
 
6
6
  function createSpawner(actorScope, actors, spawnedChildren) {
7
7
  return (src, options) => {
@@ -405,14 +405,17 @@ function formatTransitions(stateNode) {
405
405
  function formatInitialTransition(stateNode, _target) {
406
406
  const targetString = typeof _target === 'object' && _target !== null ? _target.target : _target;
407
407
  const input = typeof _target === 'object' && _target !== null ? _target.input : undefined;
408
- const resolvedTarget = typeof targetString === 'string' ? stateNode.states[targetString] : undefined;
409
- if (!resolvedTarget && targetString) {
410
- throw new Error(`Initial state node "${targetString}" not found on parent state node #${stateNode.id}` );
408
+ const to = typeof _target === 'object' && _target !== null ? _target.to : undefined;
409
+ const targetStrings = Array.isArray(targetString) ? targetString : targetString ? [targetString] : [];
410
+ const resolvedTargets = targetStrings.map(target => target.startsWith('#') ? stateNode.machine.getStateNodeById(target.slice(1)) : stateNode.states[target]);
411
+ if (resolvedTargets.some(target => !target)) {
412
+ throw new Error(`Initial state node "${targetStrings.find((_, index) => !resolvedTargets[index])}" not found on parent state node #${stateNode.id}` );
411
413
  }
412
414
  const transition = {
413
415
  source: stateNode,
414
- target: resolvedTarget ? [resolvedTarget] : undefined,
415
- input
416
+ target: resolvedTargets.length ? resolvedTargets : undefined,
417
+ input,
418
+ to
416
419
  };
417
420
  return transition;
418
421
  }
@@ -741,7 +744,7 @@ class StateMachine {
741
744
  const usesInertScope = !actorScope;
742
745
  const resolvedActorScope = actorScope ?? createActor.createInertActorScope(this, snapshot);
743
746
  if (this.validator) {
744
- dist_xstateActors.assertValid(this.validator, {
747
+ validation.assertValid(this.validator, {
745
748
  kind: 'event',
746
749
  logic: this,
747
750
  event,
@@ -759,7 +762,7 @@ class StateMachine {
759
762
  }
760
763
  const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? createActor.attachSnapshotActorRef(resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
761
764
  if (this.validator) {
762
- dist_xstateActors.assertValid(this.validator, {
765
+ validation.assertValid(this.validator, {
763
766
  kind: 'result',
764
767
  logic: this,
765
768
  snapshot: returnedSnapshot,
@@ -778,7 +781,7 @@ class StateMachine {
778
781
  const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : createActor.attachSnapshotActorRef(resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
779
782
  const effects = this._collectEffects(microsteps);
780
783
  if (this.validator) {
781
- dist_xstateActors.assertValid(this.validator, {
784
+ validation.assertValid(this.validator, {
782
785
  kind: 'result',
783
786
  logic: this,
784
787
  snapshot: returnedSnapshot,
@@ -970,7 +973,7 @@ class StateMachine {
970
973
  }
971
974
  initialTransition(input, actorScope) {
972
975
  if (this.validator) {
973
- dist_xstateActors.assertValid(this.validator, {
976
+ validation.assertValid(this.validator, {
974
977
  kind: 'input',
975
978
  logic: this,
976
979
  input
@@ -990,7 +993,7 @@ class StateMachine {
990
993
  const returnedSnapshot = usesInertScope ? createActor.attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope, true);
991
994
  const effects = this._collectEffects(microsteps);
992
995
  if (this.validator) {
993
- dist_xstateActors.assertValid(this.validator, {
996
+ validation.assertValid(this.validator, {
994
997
  kind: 'result',
995
998
  logic: this,
996
999
  snapshot: returnedSnapshot,
@@ -1049,7 +1052,11 @@ class StateMachine {
1049
1052
  restoreSnapshot(snapshot, actorScope) {
1050
1053
  const usesInertScope = !actorScope;
1051
1054
  const resolvedActorScope = actorScope ?? createActor.createInertActorScope(this);
1052
- const persistedMachine = snapshot.machine;
1055
+ const snapshotMachine = snapshot.machine;
1056
+ // A live machine snapshot carries its producing machine so helpers such as
1057
+ // `matches()` can resolve state nodes. That runtime association is not the
1058
+ // persisted `{ id, version }` identity written by getPersistedSnapshot().
1059
+ const persistedMachine = snapshotMachine && typeof snapshotMachine === 'object' && typeof snapshotMachine.transition === 'function' && 'root' in snapshotMachine ? undefined : snapshotMachine;
1053
1060
  const legacyPersistedVersion = snapshot.version;
1054
1061
  const persistedVersion = typeof persistedMachine?.version === 'string' ? persistedMachine.version : legacyPersistedVersion;
1055
1062
  if (legacyPersistedVersion !== undefined && persistedMachine?.version !== undefined && persistedMachine.version !== legacyPersistedVersion) {
@@ -898,12 +898,15 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
898
898
  },
899
899
  stop: actor => {
900
900
  if (actor) {
901
- const action = pushBuiltInAction(actions, builtInActions['@xstate.stop'], actorScope, actor);
901
+ // enq.stop accepts the consumer ActorRef contract; refs handed to
902
+ // machine code are always full actor instances at runtime.
903
+ const actorInstance = actor;
904
+ const action = pushBuiltInAction(actions, builtInActions['@xstate.stop'], actorScope, actorInstance);
902
905
  action.childUpdate = {
903
906
  type: 'remove',
904
- actor
907
+ actor: actorInstance
905
908
  };
906
- recordStoppedChild(actorScope, actor);
909
+ recordStoppedChild(actorScope, actorInstance);
907
910
  }
908
911
  }
909
912
  };
@@ -2277,7 +2280,8 @@ function resolveHistoryDefaultTransition(stateNode) {
2277
2280
  target,
2278
2281
  source: stateNode,
2279
2282
  reenter: false,
2280
- eventType: ''
2283
+ eventType: '',
2284
+ to: stateNode.config._historyDefaultTransition
2281
2285
  };
2282
2286
  }
2283
2287
  function isHistoryNode(stateNode) {
@@ -2291,7 +2295,9 @@ function getInitialStateNodes(stateNode) {
2291
2295
  }
2292
2296
  set.add(descStateNode);
2293
2297
  if (descStateNode.type === 'compound') {
2294
- iter(descStateNode.initial.target[0]);
2298
+ for (const target of descStateNode.initial.target ?? []) {
2299
+ iter(target);
2300
+ }
2295
2301
  } else if (descStateNode.type === 'parallel') {
2296
2302
  for (const child of getChildren(descStateNode)) {
2297
2303
  iter(child);
@@ -2544,6 +2550,18 @@ function getTransitionDomain(transition, snapshot, resolveTransition) {
2544
2550
  targets,
2545
2551
  reenter
2546
2552
  } = resolveTransition(transition);
2553
+ if (transition._transitionDomain) {
2554
+ if (transition._transitionDomain === 'internal' && transition.source.type === 'compound' && targetStates.every(target => isDescendant(target, transition.source))) {
2555
+ return transition.source;
2556
+ }
2557
+ const [head, ...tail] = targetStates.concat(transition.source);
2558
+ for (const ancestor of getProperAncestors(head, undefined)) {
2559
+ if (ancestor.type === 'compound' && tail.every(stateNode => isDescendant(stateNode, ancestor))) {
2560
+ return ancestor;
2561
+ }
2562
+ }
2563
+ return;
2564
+ }
2547
2565
 
2548
2566
  // A history target that restores the source itself must exit and reenter
2549
2567
  // the source (SCXML domain = the source's ancestor), unlike a plain
@@ -2596,12 +2614,28 @@ function computeExitSet(transitions, stateNodeSet, snapshot, resolveTransition)
2596
2614
  return [...statesToExit];
2597
2615
  }
2598
2616
  function initialMicrostep(root, preInitialState, actorScope, initEvent, internalQueue) {
2617
+ const initialStateNodes = [...getInitialStateNodes(root)];
2618
+ const statesForDefaultEntry = new Set();
2619
+ const collectDefaultEntryStates = stateNode => {
2620
+ if (stateNode.type === 'compound') {
2621
+ statesForDefaultEntry.add(stateNode);
2622
+ for (const target of stateNode.initial.target ?? []) {
2623
+ collectDefaultEntryStates(target);
2624
+ }
2625
+ } else if (stateNode.type === 'parallel') {
2626
+ for (const child of getChildren(stateNode)) {
2627
+ collectDefaultEntryStates(child);
2628
+ }
2629
+ }
2630
+ };
2631
+ collectDefaultEntryStates(root);
2599
2632
  return microstep([{
2600
- target: [...getInitialStateNodes(root)],
2633
+ target: initialStateNodes.filter(stateNode => !initialStateNodes.some(other => other !== stateNode && isDescendant(other, stateNode))),
2601
2634
  source: root,
2602
2635
  reenter: true,
2603
2636
  eventType: null,
2604
- toJSON: null
2637
+ toJSON: null,
2638
+ _statesForDefaultEntry: [...statesForDefaultEntry]
2605
2639
  }], preInitialState, actorScope, initEvent, true, internalQueue);
2606
2640
  }
2607
2641
 
@@ -2780,6 +2814,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2780
2814
  // in other words, those are states for which initial actions should be executed
2781
2815
  // when we target `#deep_child` initial actions of its ancestors shouldn't be executed
2782
2816
  const statesForDefaultEntry = new Set();
2817
+ const historyDefaultsByParent = new Map();
2783
2818
  const addAncestorStatesToEnter = (ancestors, reentrancyDomain) => {
2784
2819
  for (const anc of ancestors) {
2785
2820
  if (!reentrancyDomain || isDescendant(anc, reentrancyDomain)) {
@@ -2808,6 +2843,11 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2808
2843
  }
2809
2844
  } else {
2810
2845
  const historyDefaultTransition = resolveHistoryDefaultTransition(stateNode);
2846
+ const historyParent = stateNode.parent;
2847
+ statesForDefaultEntry.add(historyParent);
2848
+ const defaults = historyDefaultsByParent.get(historyParent) ?? [];
2849
+ defaults.push(historyDefaultTransition);
2850
+ historyDefaultsByParent.set(historyParent, defaults);
2811
2851
  const {
2812
2852
  targets
2813
2853
  } = getCurrentTransitionResult(historyDefaultTransition);
@@ -2825,13 +2865,19 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2825
2865
  return;
2826
2866
  }
2827
2867
  if (stateNode.type === 'compound') {
2828
- const [initialState] = getCurrentTransitionResult(stateNode.initial).targets;
2829
- if (!isHistoryNode(initialState)) {
2830
- statesToEnter.add(initialState);
2831
- statesForDefaultEntry.add(initialState);
2868
+ const initialStates = getCurrentTransitionResult(stateNode.initial).targets ?? [];
2869
+ for (const initialState of initialStates) {
2870
+ if (!isHistoryNode(initialState)) {
2871
+ statesToEnter.add(initialState);
2872
+ }
2873
+ }
2874
+ statesForDefaultEntry.add(stateNode);
2875
+ for (const initialState of initialStates) {
2876
+ addDescendantStatesToEnter(initialState);
2877
+ }
2878
+ for (const initialState of initialStates) {
2879
+ addAncestorStatesToEnter(getProperAncestors(initialState, stateNode), undefined);
2832
2880
  }
2833
- addDescendantStatesToEnter(initialState);
2834
- addAncestorStatesToEnter(getProperAncestors(initialState, stateNode), undefined);
2835
2881
  return;
2836
2882
  }
2837
2883
  if (stateNode.type === 'parallel') {
@@ -2873,7 +2919,9 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2873
2919
  }
2874
2920
  }
2875
2921
  if (isInitial) {
2876
- statesForDefaultEntry.add(currentSnapshot.machine.root);
2922
+ for (const stateNode of filteredTransitions[0]._statesForDefaultEntry ?? [currentSnapshot.machine.root]) {
2923
+ statesForDefaultEntry.add(stateNode);
2924
+ }
2877
2925
  }
2878
2926
  const stateInputMap = {
2879
2927
  ...currentSnapshot._stateInputs
@@ -2958,17 +3006,28 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2958
3006
  nextState.context = context;
2959
3007
  }
2960
3008
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
2961
- const {
2962
- actions: initialActions,
2963
- input: initialInput
2964
- } = getTransitionResult(stateNodeToEnter.initial, nextState, event, actorScope);
2965
- if (initialActions) {
2966
- actions.push(...initialActions);
2967
- }
2968
- if (initialInput && stateNodeToEnter.initial?.target) {
2969
- for (const targetNode of stateNodeToEnter.initial.target) {
2970
- stateInputMap[targetNode.id] = initialInput;
2971
- stateInputsChanged = true;
3009
+ const defaultTransitions = [stateNodeToEnter.initial, ...(historyDefaultsByParent.get(stateNodeToEnter) ?? [])].filter(Boolean);
3010
+ for (const defaultTransition of defaultTransitions) {
3011
+ const {
3012
+ actions: initialActions,
3013
+ context: initialContext,
3014
+ input: initialInput,
3015
+ internalEvents: initialInternalEvents
3016
+ } = getTransitionResult(defaultTransition, nextState, event, actorScope);
3017
+ if (initialActions) {
3018
+ actions.push(...initialActions);
3019
+ }
3020
+ if (initialInternalEvents?.length) {
3021
+ internalQueue.push(...initialInternalEvents);
3022
+ }
3023
+ if (initialContext !== undefined) {
3024
+ nextState.context = mergeContextPatch(nextState.context, initialContext);
3025
+ }
3026
+ if (initialInput && defaultTransition.target) {
3027
+ for (const targetNode of defaultTransition.target) {
3028
+ stateInputMap[targetNode.id] = initialInput;
3029
+ stateInputsChanged = true;
3030
+ }
2972
3031
  }
2973
3032
  }
2974
3033
  }