xstate 6.0.0-alpha.47 → 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 (66) hide show
  1. package/README.md +7 -1
  2. package/dist/{StateMachine-74929087.esm.js → StateMachine-8329769a.esm.js} +10 -7
  3. package/dist/{StateMachine-ee10f67e.development.esm.js → StateMachine-88b3c26a.development.esm.js} +10 -7
  4. package/dist/{StateMachine-7046f677.cjs.js → StateMachine-b09d2b00.cjs.js} +15 -12
  5. package/dist/{StateMachine-70c06bdd.development.cjs.js → StateMachine-b3a890bf.development.cjs.js} +15 -12
  6. package/dist/{createActor-915ea40c.esm.js → createActor-27287c65.esm.js} +78 -22
  7. package/dist/{createActor-ff132afa.development.esm.js → createActor-309d77eb.development.esm.js} +78 -22
  8. package/dist/{createActor-bfe8b6bd.cjs.js → createActor-9b41ade9.cjs.js} +78 -22
  9. package/dist/{createActor-6736cbbe.development.cjs.js → createActor-cc3d6dde.development.cjs.js} +78 -22
  10. package/dist/declarations/src/createMachineFromConfig.d.ts +5 -77
  11. package/dist/declarations/src/scxml/index.d.ts +1 -0
  12. package/dist/declarations/src/scxml/runtime.d.ts +251 -0
  13. package/dist/declarations/src/scxml/scxml.d.ts +19 -0
  14. package/dist/declarations/src/types.d.ts +1 -0
  15. package/dist/{effectDescriptor-fbe67d5a.development.esm.js → effectDescriptor-7707794b.development.esm.js} +1 -1
  16. package/dist/{effectDescriptor-3c699328.development.cjs.js → effectDescriptor-a1678d06.development.cjs.js} +1 -1
  17. package/dist/{effectDescriptor-8be60443.esm.js → effectDescriptor-b1f5deb6.esm.js} +1 -1
  18. package/dist/{effectDescriptor-a7e2d4d1.cjs.js → effectDescriptor-bd36a33e.cjs.js} +1 -1
  19. package/dist/types-1793b8b6.development.esm.js +185 -0
  20. package/dist/types-7e468202.development.cjs.js +189 -0
  21. package/dist/types-81d03bef.esm.js +185 -0
  22. package/dist/types-9c1bc85f.cjs.js +189 -0
  23. package/dist/validation-05f1d517.esm.js +8 -0
  24. package/dist/validation-4561b43c.cjs.js +10 -0
  25. package/dist/validation-e69470ad.development.esm.js +8 -0
  26. package/dist/validation-f2714a35.development.cjs.js +10 -0
  27. package/dist/xstate-actors.cjs.js +934 -9
  28. package/dist/xstate-actors.development.cjs.js +934 -9
  29. package/dist/xstate-actors.development.esm.js +932 -2
  30. package/dist/xstate-actors.esm.js +932 -2
  31. package/dist/xstate-durable.cjs.js +2 -2
  32. package/dist/xstate-durable.development.cjs.js +2 -2
  33. package/dist/xstate-durable.development.esm.js +2 -2
  34. package/dist/xstate-durable.esm.js +2 -2
  35. package/dist/xstate-graph.cjs.js +4 -3
  36. package/dist/xstate-graph.development.cjs.js +4 -3
  37. package/dist/xstate-graph.development.esm.js +4 -3
  38. package/dist/xstate-graph.esm.js +4 -3
  39. package/dist/xstate-graph.umd.min.js +1 -1
  40. package/dist/xstate-graph.umd.min.js.map +1 -1
  41. package/dist/xstate-scxml.cjs.d.mts +2 -0
  42. package/dist/xstate-scxml.cjs.d.ts +2 -0
  43. package/dist/xstate-scxml.cjs.js +2387 -0
  44. package/dist/xstate-scxml.cjs.mjs +3 -0
  45. package/dist/xstate-scxml.development.cjs.js +2387 -0
  46. package/dist/xstate-scxml.development.cjs.mjs +3 -0
  47. package/dist/xstate-scxml.development.esm.js +2383 -0
  48. package/dist/xstate-scxml.esm.js +2383 -0
  49. package/dist/xstate-scxml.umd.min.js +26 -0
  50. package/dist/xstate-scxml.umd.min.js.map +1 -0
  51. package/dist/xstate-validation.cjs.js +1 -1
  52. package/dist/xstate-validation.development.cjs.js +1 -1
  53. package/dist/xstate-validation.development.esm.js +1 -1
  54. package/dist/xstate-validation.esm.js +1 -1
  55. package/dist/xstate.cjs.js +141 -1036
  56. package/dist/xstate.development.cjs.js +141 -1036
  57. package/dist/xstate.development.esm.js +141 -1035
  58. package/dist/xstate.esm.js +141 -1035
  59. package/dist/xstate.umd.min.js +1 -1
  60. package/dist/xstate.umd.min.js.map +1 -1
  61. package/package.json +20 -2
  62. package/scxml/package.json +8 -0
  63. package/dist/index-0f498ef2.development.cjs.js +0 -946
  64. package/dist/index-70d56425.development.esm.js +0 -937
  65. package/dist/index-b051c924.esm.js +0 -937
  66. package/dist/index-b3fe59bc.cjs.js +0 -946
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-915ea40c.esm.js';
2
- import { g as assertValid } from './index-b051c924.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
  }
@@ -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-ff132afa.development.esm.js';
2
- import { g as assertValid } from './index-70d56425.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
  }
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var createActor = require('./createActor-bfe8b6bd.cjs.js');
4
- var dist_xstateActors = require('./index-b3fe59bc.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,
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var createActor = require('./createActor-6736cbbe.development.cjs.js');
4
- var dist_xstateActors = require('./index-0f498ef2.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,
@@ -2280,7 +2280,8 @@ function resolveHistoryDefaultTransition(stateNode) {
2280
2280
  target,
2281
2281
  source: stateNode,
2282
2282
  reenter: false,
2283
- eventType: ''
2283
+ eventType: '',
2284
+ to: stateNode.config._historyDefaultTransition
2284
2285
  };
2285
2286
  }
2286
2287
  function isHistoryNode(stateNode) {
@@ -2294,7 +2295,9 @@ function getInitialStateNodes(stateNode) {
2294
2295
  }
2295
2296
  set.add(descStateNode);
2296
2297
  if (descStateNode.type === 'compound') {
2297
- iter(descStateNode.initial.target[0]);
2298
+ for (const target of descStateNode.initial.target ?? []) {
2299
+ iter(target);
2300
+ }
2298
2301
  } else if (descStateNode.type === 'parallel') {
2299
2302
  for (const child of getChildren(descStateNode)) {
2300
2303
  iter(child);
@@ -2547,6 +2550,18 @@ function getTransitionDomain(transition, snapshot, resolveTransition) {
2547
2550
  targets,
2548
2551
  reenter
2549
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
+ }
2550
2565
 
2551
2566
  // A history target that restores the source itself must exit and reenter
2552
2567
  // the source (SCXML domain = the source's ancestor), unlike a plain
@@ -2599,12 +2614,28 @@ function computeExitSet(transitions, stateNodeSet, snapshot, resolveTransition)
2599
2614
  return [...statesToExit];
2600
2615
  }
2601
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);
2602
2632
  return microstep([{
2603
- target: [...getInitialStateNodes(root)],
2633
+ target: initialStateNodes.filter(stateNode => !initialStateNodes.some(other => other !== stateNode && isDescendant(other, stateNode))),
2604
2634
  source: root,
2605
2635
  reenter: true,
2606
2636
  eventType: null,
2607
- toJSON: null
2637
+ toJSON: null,
2638
+ _statesForDefaultEntry: [...statesForDefaultEntry]
2608
2639
  }], preInitialState, actorScope, initEvent, true, internalQueue);
2609
2640
  }
2610
2641
 
@@ -2783,6 +2814,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2783
2814
  // in other words, those are states for which initial actions should be executed
2784
2815
  // when we target `#deep_child` initial actions of its ancestors shouldn't be executed
2785
2816
  const statesForDefaultEntry = new Set();
2817
+ const historyDefaultsByParent = new Map();
2786
2818
  const addAncestorStatesToEnter = (ancestors, reentrancyDomain) => {
2787
2819
  for (const anc of ancestors) {
2788
2820
  if (!reentrancyDomain || isDescendant(anc, reentrancyDomain)) {
@@ -2811,6 +2843,11 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2811
2843
  }
2812
2844
  } else {
2813
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);
2814
2851
  const {
2815
2852
  targets
2816
2853
  } = getCurrentTransitionResult(historyDefaultTransition);
@@ -2828,13 +2865,19 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2828
2865
  return;
2829
2866
  }
2830
2867
  if (stateNode.type === 'compound') {
2831
- const [initialState] = getCurrentTransitionResult(stateNode.initial).targets;
2832
- if (!isHistoryNode(initialState)) {
2833
- statesToEnter.add(initialState);
2834
- 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);
2835
2880
  }
2836
- addDescendantStatesToEnter(initialState);
2837
- addAncestorStatesToEnter(getProperAncestors(initialState, stateNode), undefined);
2838
2881
  return;
2839
2882
  }
2840
2883
  if (stateNode.type === 'parallel') {
@@ -2876,7 +2919,9 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2876
2919
  }
2877
2920
  }
2878
2921
  if (isInitial) {
2879
- statesForDefaultEntry.add(currentSnapshot.machine.root);
2922
+ for (const stateNode of filteredTransitions[0]._statesForDefaultEntry ?? [currentSnapshot.machine.root]) {
2923
+ statesForDefaultEntry.add(stateNode);
2924
+ }
2880
2925
  }
2881
2926
  const stateInputMap = {
2882
2927
  ...currentSnapshot._stateInputs
@@ -2961,17 +3006,28 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2961
3006
  nextState.context = context;
2962
3007
  }
2963
3008
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
2964
- const {
2965
- actions: initialActions,
2966
- input: initialInput
2967
- } = getTransitionResult(stateNodeToEnter.initial, nextState, event, actorScope);
2968
- if (initialActions) {
2969
- actions.push(...initialActions);
2970
- }
2971
- if (initialInput && stateNodeToEnter.initial?.target) {
2972
- for (const targetNode of stateNodeToEnter.initial.target) {
2973
- stateInputMap[targetNode.id] = initialInput;
2974
- 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
+ }
2975
3031
  }
2976
3032
  }
2977
3033
  }
@@ -2298,7 +2298,8 @@ function resolveHistoryDefaultTransition(stateNode) {
2298
2298
  target,
2299
2299
  source: stateNode,
2300
2300
  reenter: false,
2301
- eventType: ''
2301
+ eventType: '',
2302
+ to: stateNode.config._historyDefaultTransition
2302
2303
  };
2303
2304
  }
2304
2305
  function isHistoryNode(stateNode) {
@@ -2312,7 +2313,9 @@ function getInitialStateNodes(stateNode) {
2312
2313
  }
2313
2314
  set.add(descStateNode);
2314
2315
  if (descStateNode.type === 'compound') {
2315
- iter(descStateNode.initial.target[0]);
2316
+ for (const target of descStateNode.initial.target ?? []) {
2317
+ iter(target);
2318
+ }
2316
2319
  } else if (descStateNode.type === 'parallel') {
2317
2320
  for (const child of getChildren(descStateNode)) {
2318
2321
  iter(child);
@@ -2565,6 +2568,18 @@ function getTransitionDomain(transition, snapshot, resolveTransition) {
2565
2568
  targets,
2566
2569
  reenter
2567
2570
  } = resolveTransition(transition);
2571
+ if (transition._transitionDomain) {
2572
+ if (transition._transitionDomain === 'internal' && transition.source.type === 'compound' && targetStates.every(target => isDescendant(target, transition.source))) {
2573
+ return transition.source;
2574
+ }
2575
+ const [head, ...tail] = targetStates.concat(transition.source);
2576
+ for (const ancestor of getProperAncestors(head, undefined)) {
2577
+ if (ancestor.type === 'compound' && tail.every(stateNode => isDescendant(stateNode, ancestor))) {
2578
+ return ancestor;
2579
+ }
2580
+ }
2581
+ return;
2582
+ }
2568
2583
 
2569
2584
  // A history target that restores the source itself must exit and reenter
2570
2585
  // the source (SCXML domain = the source's ancestor), unlike a plain
@@ -2617,12 +2632,28 @@ function computeExitSet(transitions, stateNodeSet, snapshot, resolveTransition)
2617
2632
  return [...statesToExit];
2618
2633
  }
2619
2634
  function initialMicrostep(root, preInitialState, actorScope, initEvent, internalQueue) {
2635
+ const initialStateNodes = [...getInitialStateNodes(root)];
2636
+ const statesForDefaultEntry = new Set();
2637
+ const collectDefaultEntryStates = stateNode => {
2638
+ if (stateNode.type === 'compound') {
2639
+ statesForDefaultEntry.add(stateNode);
2640
+ for (const target of stateNode.initial.target ?? []) {
2641
+ collectDefaultEntryStates(target);
2642
+ }
2643
+ } else if (stateNode.type === 'parallel') {
2644
+ for (const child of getChildren(stateNode)) {
2645
+ collectDefaultEntryStates(child);
2646
+ }
2647
+ }
2648
+ };
2649
+ collectDefaultEntryStates(root);
2620
2650
  return microstep([{
2621
- target: [...getInitialStateNodes(root)],
2651
+ target: initialStateNodes.filter(stateNode => !initialStateNodes.some(other => other !== stateNode && isDescendant(other, stateNode))),
2622
2652
  source: root,
2623
2653
  reenter: true,
2624
2654
  eventType: null,
2625
- toJSON: null
2655
+ toJSON: null,
2656
+ _statesForDefaultEntry: [...statesForDefaultEntry]
2626
2657
  }], preInitialState, actorScope, initEvent, true, internalQueue);
2627
2658
  }
2628
2659
 
@@ -2801,6 +2832,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2801
2832
  // in other words, those are states for which initial actions should be executed
2802
2833
  // when we target `#deep_child` initial actions of its ancestors shouldn't be executed
2803
2834
  const statesForDefaultEntry = new Set();
2835
+ const historyDefaultsByParent = new Map();
2804
2836
  const addAncestorStatesToEnter = (ancestors, reentrancyDomain) => {
2805
2837
  for (const anc of ancestors) {
2806
2838
  if (!reentrancyDomain || isDescendant(anc, reentrancyDomain)) {
@@ -2829,6 +2861,11 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2829
2861
  }
2830
2862
  } else {
2831
2863
  const historyDefaultTransition = resolveHistoryDefaultTransition(stateNode);
2864
+ const historyParent = stateNode.parent;
2865
+ statesForDefaultEntry.add(historyParent);
2866
+ const defaults = historyDefaultsByParent.get(historyParent) ?? [];
2867
+ defaults.push(historyDefaultTransition);
2868
+ historyDefaultsByParent.set(historyParent, defaults);
2832
2869
  const {
2833
2870
  targets
2834
2871
  } = getCurrentTransitionResult(historyDefaultTransition);
@@ -2846,13 +2883,19 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2846
2883
  return;
2847
2884
  }
2848
2885
  if (stateNode.type === 'compound') {
2849
- const [initialState] = getCurrentTransitionResult(stateNode.initial).targets;
2850
- if (!isHistoryNode(initialState)) {
2851
- statesToEnter.add(initialState);
2852
- statesForDefaultEntry.add(initialState);
2886
+ const initialStates = getCurrentTransitionResult(stateNode.initial).targets ?? [];
2887
+ for (const initialState of initialStates) {
2888
+ if (!isHistoryNode(initialState)) {
2889
+ statesToEnter.add(initialState);
2890
+ }
2891
+ }
2892
+ statesForDefaultEntry.add(stateNode);
2893
+ for (const initialState of initialStates) {
2894
+ addDescendantStatesToEnter(initialState);
2895
+ }
2896
+ for (const initialState of initialStates) {
2897
+ addAncestorStatesToEnter(getProperAncestors(initialState, stateNode), undefined);
2853
2898
  }
2854
- addDescendantStatesToEnter(initialState);
2855
- addAncestorStatesToEnter(getProperAncestors(initialState, stateNode), undefined);
2856
2899
  return;
2857
2900
  }
2858
2901
  if (stateNode.type === 'parallel') {
@@ -2894,7 +2937,9 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2894
2937
  }
2895
2938
  }
2896
2939
  if (isInitial) {
2897
- statesForDefaultEntry.add(currentSnapshot.machine.root);
2940
+ for (const stateNode of filteredTransitions[0]._statesForDefaultEntry ?? [currentSnapshot.machine.root]) {
2941
+ statesForDefaultEntry.add(stateNode);
2942
+ }
2898
2943
  }
2899
2944
  const stateInputMap = {
2900
2945
  ...currentSnapshot._stateInputs
@@ -2979,17 +3024,28 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2979
3024
  nextState.context = context;
2980
3025
  }
2981
3026
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
2982
- const {
2983
- actions: initialActions,
2984
- input: initialInput
2985
- } = getTransitionResult(stateNodeToEnter.initial, nextState, event, actorScope);
2986
- if (initialActions) {
2987
- actions.push(...initialActions);
2988
- }
2989
- if (initialInput && stateNodeToEnter.initial?.target) {
2990
- for (const targetNode of stateNodeToEnter.initial.target) {
2991
- stateInputMap[targetNode.id] = initialInput;
2992
- stateInputsChanged = true;
3027
+ const defaultTransitions = [stateNodeToEnter.initial, ...(historyDefaultsByParent.get(stateNodeToEnter) ?? [])].filter(Boolean);
3028
+ for (const defaultTransition of defaultTransitions) {
3029
+ const {
3030
+ actions: initialActions,
3031
+ context: initialContext,
3032
+ input: initialInput,
3033
+ internalEvents: initialInternalEvents
3034
+ } = getTransitionResult(defaultTransition, nextState, event, actorScope);
3035
+ if (initialActions) {
3036
+ actions.push(...initialActions);
3037
+ }
3038
+ if (initialInternalEvents?.length) {
3039
+ internalQueue.push(...initialInternalEvents);
3040
+ }
3041
+ if (initialContext !== undefined) {
3042
+ nextState.context = mergeContextPatch(nextState.context, initialContext);
3043
+ }
3044
+ if (initialInput && defaultTransition.target) {
3045
+ for (const targetNode of defaultTransition.target) {
3046
+ stateInputMap[targetNode.id] = initialInput;
3047
+ stateInputsChanged = true;
3048
+ }
2993
3049
  }
2994
3050
  }
2995
3051
  }