xstate 6.0.0-alpha.27 → 6.0.0-alpha.31

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 (54) hide show
  1. package/dist/{StateMachine-bcb27183.cjs.js → StateMachine-4e86e818.cjs.js} +70 -22
  2. package/dist/{StateMachine-24c5e3f9.development.esm.js → StateMachine-54d3cf7a.development.esm.js} +70 -22
  3. package/dist/{StateMachine-673e9a91.development.cjs.js → StateMachine-73b20de0.development.cjs.js} +70 -22
  4. package/dist/{StateMachine-7daabf24.esm.js → StateMachine-cc0c34dd.esm.js} +70 -22
  5. package/dist/declarations/src/StateMachine.d.ts +3 -1
  6. package/dist/declarations/src/actors/callback.d.ts +2 -0
  7. package/dist/declarations/src/actors/logic.d.ts +2 -0
  8. package/dist/declarations/src/actors/observable.d.ts +3 -0
  9. package/dist/declarations/src/actors/promise.d.ts +2 -0
  10. package/dist/declarations/src/createActor.d.ts +10 -5
  11. package/dist/declarations/src/createMachine.d.ts +10 -2
  12. package/dist/declarations/src/index.d.ts +2 -0
  13. package/dist/declarations/src/machineVersions.d.ts +51 -0
  14. package/dist/declarations/src/setup.d.ts +88 -10
  15. package/dist/declarations/src/system.d.ts +3 -1
  16. package/dist/declarations/src/types.d.ts +24 -4
  17. package/dist/declarations/src/validation/index.d.ts +37 -0
  18. package/dist/declarations/src/validation.types.d.ts +22 -0
  19. package/dist/{index-c55fff73.development.cjs.js → index-0317dbd5.development.cjs.js} +623 -323
  20. package/dist/{index-d33c7b85.esm.js → index-2b16671f.esm.js} +623 -324
  21. package/dist/{index-b228b8b6.cjs.js → index-85c48a0a.cjs.js} +623 -323
  22. package/dist/{index-41508d62.development.esm.js → index-fbe9e986.development.esm.js} +623 -324
  23. package/dist/xstate-actors.cjs.js +1 -1
  24. package/dist/xstate-actors.development.cjs.js +1 -1
  25. package/dist/xstate-actors.development.esm.js +1 -1
  26. package/dist/xstate-actors.esm.js +1 -1
  27. package/dist/xstate-actors.umd.min.js +1 -1
  28. package/dist/xstate-actors.umd.min.js.map +1 -1
  29. package/dist/xstate-graph.cjs.js +3 -3
  30. package/dist/xstate-graph.development.cjs.js +3 -3
  31. package/dist/xstate-graph.development.esm.js +3 -3
  32. package/dist/xstate-graph.esm.js +3 -3
  33. package/dist/xstate-graph.umd.min.js +1 -1
  34. package/dist/xstate-graph.umd.min.js.map +1 -1
  35. package/dist/xstate-validation.cjs.d.mts +2 -0
  36. package/dist/xstate-validation.cjs.d.ts +2 -0
  37. package/dist/xstate-validation.cjs.js +277 -0
  38. package/dist/xstate-validation.cjs.mjs +5 -0
  39. package/dist/xstate-validation.development.cjs.js +277 -0
  40. package/dist/xstate-validation.development.cjs.mjs +5 -0
  41. package/dist/xstate-validation.development.esm.js +271 -0
  42. package/dist/xstate-validation.esm.js +271 -0
  43. package/dist/xstate-validation.umd.min.js +2 -0
  44. package/dist/xstate-validation.umd.min.js.map +1 -0
  45. package/dist/xstate.cjs.js +128 -9
  46. package/dist/xstate.cjs.mjs +2 -0
  47. package/dist/xstate.development.cjs.js +128 -9
  48. package/dist/xstate.development.cjs.mjs +2 -0
  49. package/dist/xstate.development.esm.js +129 -12
  50. package/dist/xstate.esm.js +129 -12
  51. package/dist/xstate.umd.min.js +1 -1
  52. package/dist/xstate.umd.min.js.map +1 -1
  53. package/package.json +18 -2
  54. package/validation/package.json +8 -0
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var dist_xstateActors = require('./index-b228b8b6.cjs.js');
3
+ var dist_xstateActors = require('./index-85c48a0a.cjs.js');
4
4
 
5
5
  function createSpawner(actorScope, actors, spawnedChildren) {
6
6
  return (src, options) => {
@@ -43,7 +43,7 @@ function createInertActorScope(actorLogic, snapshot, sourceSelf) {
43
43
  const snapshotRef = snapshot ? dist_xstateActors.getSnapshotActorRef(snapshot) : undefined;
44
44
  const previousSelf = sourceSelf ?? snapshotRef?.actor;
45
45
  const baseSystem = previousSelf?.system;
46
- const system = previousSelf && baseSystem ? dist_xstateActors.createSnapshotSystem(baseSystem, previousSelf, dist_xstateActors.isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
46
+ const system = previousSelf && baseSystem ? dist_xstateActors.createSnapshotSystem(baseSystem, dist_xstateActors.isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
47
47
  const self = dist_xstateActors.createActor(actorLogic, {
48
48
  _inert: true,
49
49
  ...(previousSelf ? {
@@ -531,8 +531,9 @@ function getEmptyCanActorScope() {
531
531
  }
532
532
  class StateMachine {
533
533
  constructor(/** The raw config used to create the machine. */
534
- config, sources) {
534
+ config, sources, validator) {
535
535
  this.config = config;
536
+ this.validator = validator;
536
537
  /** The machine's own version. */
537
538
  this.version = void 0;
538
539
  this.schemas = void 0;
@@ -617,7 +618,7 @@ class StateMachine {
617
618
  ...delays,
618
619
  ...sources.delays
619
620
  }
620
- });
621
+ }, this.validator);
621
622
  // Providing sources does not change the serializable definition.
622
623
  provided._json = this._json;
623
624
  return provided;
@@ -648,6 +649,14 @@ class StateMachine {
648
649
  transition(snapshot, event, actorScope) {
649
650
  const usesInertScope = !actorScope;
650
651
  const resolvedActorScope = actorScope ?? createInertActorScope(this, snapshot);
652
+ if (this.validator) {
653
+ dist_xstateActors.assertValid(this.validator, {
654
+ kind: 'event',
655
+ logic: this,
656
+ event,
657
+ eventOrigin: actorScope && actorScope.self._lastSourceRef ? 'actor' : 'external'
658
+ });
659
+ }
651
660
  if (usesInertScope) {
652
661
  setInertActorScopeSnapshot(resolvedActorScope, snapshot, false);
653
662
  }
@@ -657,6 +666,14 @@ class StateMachine {
657
666
  setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
658
667
  }
659
668
  const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(this, resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
669
+ if (this.validator) {
670
+ dist_xstateActors.assertValid(this.validator, {
671
+ kind: 'result',
672
+ logic: this,
673
+ snapshot: returnedSnapshot,
674
+ effects: []
675
+ });
676
+ }
660
677
  return [returnedSnapshot, []];
661
678
  }
662
679
  const {
@@ -667,7 +684,16 @@ class StateMachine {
667
684
  setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
668
685
  }
669
686
  const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(this, resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
670
- return [returnedSnapshot, this._collectEffects(microsteps)];
687
+ const effects = this._collectEffects(microsteps);
688
+ if (this.validator) {
689
+ dist_xstateActors.assertValid(this.validator, {
690
+ kind: 'result',
691
+ logic: this,
692
+ snapshot: returnedSnapshot,
693
+ effects
694
+ });
695
+ }
696
+ return [returnedSnapshot, effects];
671
697
  }
672
698
  _collectEffects(microsteps) {
673
699
  return microsteps.flatMap(([, actions]) => actions);
@@ -676,7 +702,8 @@ class StateMachine {
676
702
  if (isInertActorScope(actorScope)) {
677
703
  return snapshot;
678
704
  }
679
- return attachSnapshotActorRef(this, actorScope, snapshot);
705
+ dist_xstateActors.setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
706
+ return snapshot;
680
707
  }
681
708
  _transitionFast(snapshot, event, actorScope) {
682
709
  if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
@@ -839,37 +866,50 @@ class StateMachine {
839
866
  return this.initialTransition(input, actorScope)[0];
840
867
  }
841
868
  initialTransition(input, actorScope) {
869
+ if (this.validator) {
870
+ dist_xstateActors.assertValid(this.validator, {
871
+ kind: 'input',
872
+ logic: this,
873
+ input
874
+ });
875
+ }
842
876
  const usesInertScope = !actorScope;
843
877
  const resolvedActorScope = actorScope ?? createInertActorScope(this);
844
878
  const initEvent = dist_xstateActors.createInitEvent(input); // TODO: fix;
845
879
  const internalQueue = [];
846
880
  const preInitialState = this._getPreInitialState(resolvedActorScope, initEvent);
847
881
  const contextSpawnEffects = Object.values(preInitialState.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
848
- let nextState;
849
- let initialActions = [];
850
- let microsteps = [];
851
- let macroState;
882
+ const finalizeInitialResult = (macroState, microsteps) => {
883
+ if (usesInertScope) {
884
+ setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
885
+ }
886
+ const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
887
+ const effects = this._collectEffects(microsteps);
888
+ if (this.validator) {
889
+ dist_xstateActors.assertValid(this.validator, {
890
+ kind: 'result',
891
+ logic: this,
892
+ snapshot: returnedSnapshot,
893
+ effects
894
+ });
895
+ }
896
+ return [returnedSnapshot, effects];
897
+ };
852
898
  try {
853
- [nextState, initialActions] = dist_xstateActors.initialMicrostep(this.root, preInitialState, resolvedActorScope, initEvent, internalQueue);
854
- ({
899
+ const [nextState, initialActions] = dist_xstateActors.initialMicrostep(this.root, preInitialState, resolvedActorScope, initEvent, internalQueue);
900
+ const {
855
901
  snapshot: macroState,
856
902
  microsteps
857
- } = dist_xstateActors.macrostep(nextState, initEvent, resolvedActorScope, internalQueue, [[nextState, [...contextSpawnEffects, ...initialActions]]]));
903
+ } = dist_xstateActors.macrostep(nextState, initEvent, resolvedActorScope, internalQueue, [[nextState, [...contextSpawnEffects, ...initialActions]]]);
904
+ return finalizeInitialResult(macroState, microsteps);
858
905
  } catch (err) {
859
906
  if (!this.root.config.onError) {
860
907
  throw err;
861
908
  }
862
909
  const errorEvent = dist_xstateActors.createErrorPlatformEvent('execution', err);
863
910
  const errorMacrostep = dist_xstateActors.macrostep(preInitialState, errorEvent, resolvedActorScope, [], [[preInitialState, contextSpawnEffects]]);
864
- macroState = errorMacrostep.snapshot;
865
- microsteps = errorMacrostep.microsteps;
866
- initialActions = [];
911
+ return finalizeInitialResult(errorMacrostep.snapshot, errorMacrostep.microsteps);
867
912
  }
868
- if (usesInertScope) {
869
- setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
870
- }
871
- const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
872
- return [returnedSnapshot, this._collectEffects(microsteps)];
873
913
  }
874
914
  start(snapshot) {
875
915
  // Start rehydrated children that were active when persisted. Freshly
@@ -900,7 +940,15 @@ class StateMachine {
900
940
  restoreSnapshot(snapshot, actorScope) {
901
941
  const usesInertScope = !actorScope;
902
942
  const resolvedActorScope = actorScope ?? createInertActorScope(this);
903
- const persistedVersion = snapshot.version;
943
+ const persistedMachine = snapshot.machine;
944
+ const legacyPersistedVersion = snapshot.version;
945
+ const persistedVersion = typeof persistedMachine?.version === 'string' ? persistedMachine.version : legacyPersistedVersion;
946
+ if (legacyPersistedVersion !== undefined && persistedMachine?.version !== undefined && persistedMachine.version !== legacyPersistedVersion) {
947
+ throw new Error(`Persisted snapshot version '${legacyPersistedVersion}' conflicts with machine version '${persistedMachine.version}'.`);
948
+ }
949
+ if (persistedMachine && typeof persistedMachine.id === 'string' && persistedMachine.id !== this.id) {
950
+ throw new Error(`Machine ID mismatch: persisted snapshot machine '${persistedMachine.id}' does not match '${this.id}'.`);
951
+ }
904
952
  if (persistedVersion !== this.version) {
905
953
  const migrate = this.config.migrate;
906
954
  if (typeof migrate !== 'function') {
@@ -1,4 +1,4 @@
1
- import { M as setSnapshotActorRef, N as getSnapshotActorRef, O as createSnapshotSystem, q as isMachineSnapshot, h as createActor, S as STATE_DELIMITER, P as mapValues, t as toArray, Q as createInvokeId, R as getDelayedTransitions, U as getEventDescriptorKey, V as evaluateCandidate, W as formatTransition, Y as NULL_EVENT, Z as toTransitionConfigArray, _ as createInvokeTimeoutEventId, $ as getCandidates, a0 as matchesActorSession, a1 as formatRouteTransitions, a2 as resolveStateValue, a3 as getAllStateNodes, s as getStateNodes, a4 as createMachineSnapshot, a5 as isInFinalState, j as macrostep, a6 as cloneMachineSnapshot, a7 as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, a8 as getTransitionResult, a9 as hasEffect, aa as createErrorPlatformEvent, r as resolveActionsWithContext, k as createInitEvent, l as createSpawnEffect, n as initialMicrostep, ab as toStatePath, ac as isStateId, ad as getStateNodeByPath, ae as getPersistedSnapshot, af as resolveReferencedActor, ag as createInvokeTimeoutEvent, ah as $$ACTOR_TYPE } from './index-41508d62.development.esm.js';
1
+ import { M as setSnapshotActorRef, N as getSnapshotActorRef, O as createSnapshotSystem, q as isMachineSnapshot, h as createActor, S as STATE_DELIMITER, P as mapValues, t as toArray, Q as createInvokeId, R as getDelayedTransitions, U as getEventDescriptorKey, V as evaluateCandidate, W as formatTransition, Y as NULL_EVENT, Z as toTransitionConfigArray, _ as createInvokeTimeoutEventId, $ as getCandidates, a0 as matchesActorSession, a1 as formatRouteTransitions, a2 as resolveStateValue, a3 as getAllStateNodes, s as getStateNodes, a4 as createMachineSnapshot, a5 as isInFinalState, a6 as assertValid, j as macrostep, a7 as cloneMachineSnapshot, a8 as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, a9 as getTransitionResult, aa as hasEffect, ab as createErrorPlatformEvent, r as resolveActionsWithContext, k as createInitEvent, l as createSpawnEffect, n as initialMicrostep, ac as toStatePath, ad as isStateId, ae as getStateNodeByPath, af as getPersistedSnapshot, ag as resolveReferencedActor, ah as createInvokeTimeoutEvent, ai as $$ACTOR_TYPE } from './index-fbe9e986.development.esm.js';
2
2
 
3
3
  function createSpawner(actorScope, actors, spawnedChildren) {
4
4
  return (src, options) => {
@@ -41,7 +41,7 @@ function createInertActorScope(actorLogic, snapshot, sourceSelf) {
41
41
  const snapshotRef = snapshot ? getSnapshotActorRef(snapshot) : undefined;
42
42
  const previousSelf = sourceSelf ?? snapshotRef?.actor;
43
43
  const baseSystem = previousSelf?.system;
44
- const system = previousSelf && baseSystem ? createSnapshotSystem(baseSystem, previousSelf, isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
44
+ const system = previousSelf && baseSystem ? createSnapshotSystem(baseSystem, isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
45
45
  const self = createActor(actorLogic, {
46
46
  _inert: true,
47
47
  ...(previousSelf ? {
@@ -547,8 +547,9 @@ function getEmptyCanActorScope() {
547
547
  }
548
548
  class StateMachine {
549
549
  constructor(/** The raw config used to create the machine. */
550
- config, sources) {
550
+ config, sources, validator) {
551
551
  this.config = config;
552
+ this.validator = validator;
552
553
  /** The machine's own version. */
553
554
  this.version = void 0;
554
555
  this.schemas = void 0;
@@ -644,7 +645,7 @@ class StateMachine {
644
645
  ...delays,
645
646
  ...sources.delays
646
647
  }
647
- });
648
+ }, this.validator);
648
649
  // Providing sources does not change the serializable definition.
649
650
  provided._json = this._json;
650
651
  return provided;
@@ -675,6 +676,14 @@ class StateMachine {
675
676
  transition(snapshot, event, actorScope) {
676
677
  const usesInertScope = !actorScope;
677
678
  const resolvedActorScope = actorScope ?? createInertActorScope(this, snapshot);
679
+ if (this.validator) {
680
+ assertValid(this.validator, {
681
+ kind: 'event',
682
+ logic: this,
683
+ event,
684
+ eventOrigin: actorScope && actorScope.self._lastSourceRef ? 'actor' : 'external'
685
+ });
686
+ }
678
687
  if (usesInertScope) {
679
688
  setInertActorScopeSnapshot(resolvedActorScope, snapshot, false);
680
689
  }
@@ -684,6 +693,14 @@ class StateMachine {
684
693
  setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
685
694
  }
686
695
  const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(this, resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
696
+ if (this.validator) {
697
+ assertValid(this.validator, {
698
+ kind: 'result',
699
+ logic: this,
700
+ snapshot: returnedSnapshot,
701
+ effects: []
702
+ });
703
+ }
687
704
  return [returnedSnapshot, []];
688
705
  }
689
706
  const {
@@ -694,7 +711,16 @@ class StateMachine {
694
711
  setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
695
712
  }
696
713
  const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(this, resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
697
- return [returnedSnapshot, this._collectEffects(microsteps)];
714
+ const effects = this._collectEffects(microsteps);
715
+ if (this.validator) {
716
+ assertValid(this.validator, {
717
+ kind: 'result',
718
+ logic: this,
719
+ snapshot: returnedSnapshot,
720
+ effects
721
+ });
722
+ }
723
+ return [returnedSnapshot, effects];
698
724
  }
699
725
  _collectEffects(microsteps) {
700
726
  return microsteps.flatMap(([, actions]) => actions);
@@ -703,7 +729,8 @@ class StateMachine {
703
729
  if (isInertActorScope(actorScope)) {
704
730
  return snapshot;
705
731
  }
706
- return attachSnapshotActorRef(this, actorScope, snapshot);
732
+ setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
733
+ return snapshot;
707
734
  }
708
735
  _transitionFast(snapshot, event, actorScope) {
709
736
  if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
@@ -866,37 +893,50 @@ class StateMachine {
866
893
  return this.initialTransition(input, actorScope)[0];
867
894
  }
868
895
  initialTransition(input, actorScope) {
896
+ if (this.validator) {
897
+ assertValid(this.validator, {
898
+ kind: 'input',
899
+ logic: this,
900
+ input
901
+ });
902
+ }
869
903
  const usesInertScope = !actorScope;
870
904
  const resolvedActorScope = actorScope ?? createInertActorScope(this);
871
905
  const initEvent = createInitEvent(input); // TODO: fix;
872
906
  const internalQueue = [];
873
907
  const preInitialState = this._getPreInitialState(resolvedActorScope, initEvent);
874
908
  const contextSpawnEffects = Object.values(preInitialState.children).filter(Boolean).map(actor => createSpawnEffect(actor));
875
- let nextState;
876
- let initialActions = [];
877
- let microsteps = [];
878
- let macroState;
909
+ const finalizeInitialResult = (macroState, microsteps) => {
910
+ if (usesInertScope) {
911
+ setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
912
+ }
913
+ const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
914
+ const effects = this._collectEffects(microsteps);
915
+ if (this.validator) {
916
+ assertValid(this.validator, {
917
+ kind: 'result',
918
+ logic: this,
919
+ snapshot: returnedSnapshot,
920
+ effects
921
+ });
922
+ }
923
+ return [returnedSnapshot, effects];
924
+ };
879
925
  try {
880
- [nextState, initialActions] = initialMicrostep(this.root, preInitialState, resolvedActorScope, initEvent, internalQueue);
881
- ({
926
+ const [nextState, initialActions] = initialMicrostep(this.root, preInitialState, resolvedActorScope, initEvent, internalQueue);
927
+ const {
882
928
  snapshot: macroState,
883
929
  microsteps
884
- } = macrostep(nextState, initEvent, resolvedActorScope, internalQueue, [[nextState, [...contextSpawnEffects, ...initialActions]]]));
930
+ } = macrostep(nextState, initEvent, resolvedActorScope, internalQueue, [[nextState, [...contextSpawnEffects, ...initialActions]]]);
931
+ return finalizeInitialResult(macroState, microsteps);
885
932
  } catch (err) {
886
933
  if (!this.root.config.onError) {
887
934
  throw err;
888
935
  }
889
936
  const errorEvent = createErrorPlatformEvent('execution', err);
890
937
  const errorMacrostep = macrostep(preInitialState, errorEvent, resolvedActorScope, [], [[preInitialState, contextSpawnEffects]]);
891
- macroState = errorMacrostep.snapshot;
892
- microsteps = errorMacrostep.microsteps;
893
- initialActions = [];
938
+ return finalizeInitialResult(errorMacrostep.snapshot, errorMacrostep.microsteps);
894
939
  }
895
- if (usesInertScope) {
896
- setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
897
- }
898
- const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
899
- return [returnedSnapshot, this._collectEffects(microsteps)];
900
940
  }
901
941
  start(snapshot) {
902
942
  // Start rehydrated children that were active when persisted. Freshly
@@ -927,7 +967,15 @@ class StateMachine {
927
967
  restoreSnapshot(snapshot, actorScope) {
928
968
  const usesInertScope = !actorScope;
929
969
  const resolvedActorScope = actorScope ?? createInertActorScope(this);
930
- const persistedVersion = snapshot.version;
970
+ const persistedMachine = snapshot.machine;
971
+ const legacyPersistedVersion = snapshot.version;
972
+ const persistedVersion = typeof persistedMachine?.version === 'string' ? persistedMachine.version : legacyPersistedVersion;
973
+ if (legacyPersistedVersion !== undefined && persistedMachine?.version !== undefined && persistedMachine.version !== legacyPersistedVersion) {
974
+ throw new Error(`Persisted snapshot version '${legacyPersistedVersion}' conflicts with machine version '${persistedMachine.version}'.`);
975
+ }
976
+ if (persistedMachine && typeof persistedMachine.id === 'string' && persistedMachine.id !== this.id) {
977
+ throw new Error(`Machine ID mismatch: persisted snapshot was created by machine '${persistedMachine.id}', but machine '${this.id}' was provided.` );
978
+ }
931
979
  if (persistedVersion !== this.version) {
932
980
  const migrate = this.config.migrate;
933
981
  if (typeof migrate !== 'function') {
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var dist_xstateActors = require('./index-c55fff73.development.cjs.js');
3
+ var dist_xstateActors = require('./index-0317dbd5.development.cjs.js');
4
4
 
5
5
  function createSpawner(actorScope, actors, spawnedChildren) {
6
6
  return (src, options) => {
@@ -43,7 +43,7 @@ function createInertActorScope(actorLogic, snapshot, sourceSelf) {
43
43
  const snapshotRef = snapshot ? dist_xstateActors.getSnapshotActorRef(snapshot) : undefined;
44
44
  const previousSelf = sourceSelf ?? snapshotRef?.actor;
45
45
  const baseSystem = previousSelf?.system;
46
- const system = previousSelf && baseSystem ? dist_xstateActors.createSnapshotSystem(baseSystem, previousSelf, dist_xstateActors.isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
46
+ const system = previousSelf && baseSystem ? dist_xstateActors.createSnapshotSystem(baseSystem, dist_xstateActors.isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
47
47
  const self = dist_xstateActors.createActor(actorLogic, {
48
48
  _inert: true,
49
49
  ...(previousSelf ? {
@@ -549,8 +549,9 @@ function getEmptyCanActorScope() {
549
549
  }
550
550
  class StateMachine {
551
551
  constructor(/** The raw config used to create the machine. */
552
- config, sources) {
552
+ config, sources, validator) {
553
553
  this.config = config;
554
+ this.validator = validator;
554
555
  /** The machine's own version. */
555
556
  this.version = void 0;
556
557
  this.schemas = void 0;
@@ -646,7 +647,7 @@ class StateMachine {
646
647
  ...delays,
647
648
  ...sources.delays
648
649
  }
649
- });
650
+ }, this.validator);
650
651
  // Providing sources does not change the serializable definition.
651
652
  provided._json = this._json;
652
653
  return provided;
@@ -677,6 +678,14 @@ class StateMachine {
677
678
  transition(snapshot, event, actorScope) {
678
679
  const usesInertScope = !actorScope;
679
680
  const resolvedActorScope = actorScope ?? createInertActorScope(this, snapshot);
681
+ if (this.validator) {
682
+ dist_xstateActors.assertValid(this.validator, {
683
+ kind: 'event',
684
+ logic: this,
685
+ event,
686
+ eventOrigin: actorScope && actorScope.self._lastSourceRef ? 'actor' : 'external'
687
+ });
688
+ }
680
689
  if (usesInertScope) {
681
690
  setInertActorScopeSnapshot(resolvedActorScope, snapshot, false);
682
691
  }
@@ -686,6 +695,14 @@ class StateMachine {
686
695
  setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
687
696
  }
688
697
  const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(this, resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
698
+ if (this.validator) {
699
+ dist_xstateActors.assertValid(this.validator, {
700
+ kind: 'result',
701
+ logic: this,
702
+ snapshot: returnedSnapshot,
703
+ effects: []
704
+ });
705
+ }
689
706
  return [returnedSnapshot, []];
690
707
  }
691
708
  const {
@@ -696,7 +713,16 @@ class StateMachine {
696
713
  setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
697
714
  }
698
715
  const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(this, resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
699
- return [returnedSnapshot, this._collectEffects(microsteps)];
716
+ const effects = this._collectEffects(microsteps);
717
+ if (this.validator) {
718
+ dist_xstateActors.assertValid(this.validator, {
719
+ kind: 'result',
720
+ logic: this,
721
+ snapshot: returnedSnapshot,
722
+ effects
723
+ });
724
+ }
725
+ return [returnedSnapshot, effects];
700
726
  }
701
727
  _collectEffects(microsteps) {
702
728
  return microsteps.flatMap(([, actions]) => actions);
@@ -705,7 +731,8 @@ class StateMachine {
705
731
  if (isInertActorScope(actorScope)) {
706
732
  return snapshot;
707
733
  }
708
- return attachSnapshotActorRef(this, actorScope, snapshot);
734
+ dist_xstateActors.setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
735
+ return snapshot;
709
736
  }
710
737
  _transitionFast(snapshot, event, actorScope) {
711
738
  if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
@@ -868,37 +895,50 @@ class StateMachine {
868
895
  return this.initialTransition(input, actorScope)[0];
869
896
  }
870
897
  initialTransition(input, actorScope) {
898
+ if (this.validator) {
899
+ dist_xstateActors.assertValid(this.validator, {
900
+ kind: 'input',
901
+ logic: this,
902
+ input
903
+ });
904
+ }
871
905
  const usesInertScope = !actorScope;
872
906
  const resolvedActorScope = actorScope ?? createInertActorScope(this);
873
907
  const initEvent = dist_xstateActors.createInitEvent(input); // TODO: fix;
874
908
  const internalQueue = [];
875
909
  const preInitialState = this._getPreInitialState(resolvedActorScope, initEvent);
876
910
  const contextSpawnEffects = Object.values(preInitialState.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
877
- let nextState;
878
- let initialActions = [];
879
- let microsteps = [];
880
- let macroState;
911
+ const finalizeInitialResult = (macroState, microsteps) => {
912
+ if (usesInertScope) {
913
+ setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
914
+ }
915
+ const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
916
+ const effects = this._collectEffects(microsteps);
917
+ if (this.validator) {
918
+ dist_xstateActors.assertValid(this.validator, {
919
+ kind: 'result',
920
+ logic: this,
921
+ snapshot: returnedSnapshot,
922
+ effects
923
+ });
924
+ }
925
+ return [returnedSnapshot, effects];
926
+ };
881
927
  try {
882
- [nextState, initialActions] = dist_xstateActors.initialMicrostep(this.root, preInitialState, resolvedActorScope, initEvent, internalQueue);
883
- ({
928
+ const [nextState, initialActions] = dist_xstateActors.initialMicrostep(this.root, preInitialState, resolvedActorScope, initEvent, internalQueue);
929
+ const {
884
930
  snapshot: macroState,
885
931
  microsteps
886
- } = dist_xstateActors.macrostep(nextState, initEvent, resolvedActorScope, internalQueue, [[nextState, [...contextSpawnEffects, ...initialActions]]]));
932
+ } = dist_xstateActors.macrostep(nextState, initEvent, resolvedActorScope, internalQueue, [[nextState, [...contextSpawnEffects, ...initialActions]]]);
933
+ return finalizeInitialResult(macroState, microsteps);
887
934
  } catch (err) {
888
935
  if (!this.root.config.onError) {
889
936
  throw err;
890
937
  }
891
938
  const errorEvent = dist_xstateActors.createErrorPlatformEvent('execution', err);
892
939
  const errorMacrostep = dist_xstateActors.macrostep(preInitialState, errorEvent, resolvedActorScope, [], [[preInitialState, contextSpawnEffects]]);
893
- macroState = errorMacrostep.snapshot;
894
- microsteps = errorMacrostep.microsteps;
895
- initialActions = [];
940
+ return finalizeInitialResult(errorMacrostep.snapshot, errorMacrostep.microsteps);
896
941
  }
897
- if (usesInertScope) {
898
- setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
899
- }
900
- const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
901
- return [returnedSnapshot, this._collectEffects(microsteps)];
902
942
  }
903
943
  start(snapshot) {
904
944
  // Start rehydrated children that were active when persisted. Freshly
@@ -929,7 +969,15 @@ class StateMachine {
929
969
  restoreSnapshot(snapshot, actorScope) {
930
970
  const usesInertScope = !actorScope;
931
971
  const resolvedActorScope = actorScope ?? createInertActorScope(this);
932
- const persistedVersion = snapshot.version;
972
+ const persistedMachine = snapshot.machine;
973
+ const legacyPersistedVersion = snapshot.version;
974
+ const persistedVersion = typeof persistedMachine?.version === 'string' ? persistedMachine.version : legacyPersistedVersion;
975
+ if (legacyPersistedVersion !== undefined && persistedMachine?.version !== undefined && persistedMachine.version !== legacyPersistedVersion) {
976
+ throw new Error(`Persisted snapshot version '${legacyPersistedVersion}' conflicts with machine version '${persistedMachine.version}'.`);
977
+ }
978
+ if (persistedMachine && typeof persistedMachine.id === 'string' && persistedMachine.id !== this.id) {
979
+ throw new Error(`Machine ID mismatch: persisted snapshot was created by machine '${persistedMachine.id}', but machine '${this.id}' was provided.` );
980
+ }
933
981
  if (persistedVersion !== this.version) {
934
982
  const migrate = this.config.migrate;
935
983
  if (typeof migrate !== 'function') {