xstate 6.0.0-alpha.32 → 6.0.0-alpha.34

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 (34) hide show
  1. package/dist/{StateMachine-ff580dd5.cjs.js → StateMachine-204661ea.cjs.js} +27 -137
  2. package/dist/{StateMachine-d073209c.esm.js → StateMachine-269b22a3.esm.js} +19 -124
  3. package/dist/{StateMachine-33eec203.development.esm.js → StateMachine-b72b23cc.development.esm.js} +19 -124
  4. package/dist/{StateMachine-d3243bd1.development.cjs.js → StateMachine-b827f6a8.development.cjs.js} +27 -137
  5. package/dist/declarations/src/StateMachine.d.ts +2 -2
  6. package/dist/declarations/src/index.d.ts +1 -1
  7. package/dist/declarations/src/machineVersions.d.ts +19 -15
  8. package/dist/declarations/src/stateUtils.d.ts +4 -4
  9. package/dist/declarations/src/types.d.ts +1 -1
  10. package/dist/{index-ab93831d.esm.js → index-16832816.esm.js} +593 -235
  11. package/dist/{index-691f27b9.development.cjs.js → index-692b05ac.development.cjs.js} +601 -236
  12. package/dist/{index-9939f02a.cjs.js → index-b440472d.cjs.js} +601 -236
  13. package/dist/{index-92227865.development.esm.js → index-ea9227d3.development.esm.js} +593 -235
  14. package/dist/xstate-actors.cjs.js +1 -1
  15. package/dist/xstate-actors.development.cjs.js +1 -1
  16. package/dist/xstate-actors.development.esm.js +1 -1
  17. package/dist/xstate-actors.esm.js +1 -1
  18. package/dist/xstate-actors.umd.min.js +1 -1
  19. package/dist/xstate-actors.umd.min.js.map +1 -1
  20. package/dist/xstate-graph.cjs.js +2 -2
  21. package/dist/xstate-graph.development.cjs.js +2 -2
  22. package/dist/xstate-graph.development.esm.js +2 -2
  23. package/dist/xstate-graph.esm.js +2 -2
  24. package/dist/xstate-graph.umd.min.js +1 -1
  25. package/dist/xstate-graph.umd.min.js.map +1 -1
  26. package/dist/xstate.cjs.js +134 -102
  27. package/dist/xstate.cjs.mjs +0 -1
  28. package/dist/xstate.development.cjs.js +134 -102
  29. package/dist/xstate.development.cjs.mjs +0 -1
  30. package/dist/xstate.development.esm.js +127 -94
  31. package/dist/xstate.esm.js +127 -94
  32. package/dist/xstate.umd.min.js +1 -1
  33. package/dist/xstate.umd.min.js.map +1 -1
  34. package/package.json +1 -1
@@ -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 getCandidates, W as evaluateCandidate, Y as formatTransition, Z as NULL_EVENT, _ as toTransitionConfigArray, $ as createInvokeTimeoutEventId, 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-92227865.development.esm.js';
1
+ import { 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, B as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, k as createInertActorScope, aa as assertValid, s as setInertActorScopeSnapshot, l as attachSnapshotActorRef, n as macrostep, ab as isInertActorScope, ac as setSnapshotActorRef, ad as cloneMachineSnapshot, ae as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, af as getTransitionResult, ag as hasEffect, ah as createErrorPlatformEvent, ai as withActorSelf, r as resolveActionsWithContext, o as createInitEvent, q as createSpawnEffect, u as initialMicrostep, aj as toStatePath, ak as isStateId, al as getStateNodeByPath, am as getPersistedSnapshot, an as resolveReferencedActor, ao as createInvokeTimeoutEvent, ap as $$ACTOR_TYPE, j as createActor } from './index-ea9227d3.development.esm.js';
2
2
 
3
3
  function createSpawner(actorScope, actors, spawnedChildren) {
4
4
  return (src, options) => {
@@ -16,111 +16,6 @@ function createSpawner(actorScope, actors, spawnedChildren) {
16
16
  };
17
17
  }
18
18
 
19
- /** @internal */
20
- function setInertActorScopeSnapshot(actorScope, snapshot, attachActorRef = true) {
21
- actorScope.self._snapshot = snapshot;
22
- if (attachActorRef && snapshot && typeof snapshot === 'object') {
23
- setSnapshotActorRef(snapshot, actorScope.self);
24
- }
25
- return snapshot;
26
- }
27
-
28
- /** @internal */
29
- function isInertActorScope(actorScope) {
30
- return !!actorScope.self.options?._inert;
31
- }
32
-
33
- /** @internal */
34
- function attachSnapshotActorRef(actorLogic, actorScope, snapshot) {
35
- const snapshotScope = createInertActorScope(actorLogic, snapshot, actorScope.self);
36
- return setInertActorScopeSnapshot(snapshotScope, snapshot);
37
- }
38
-
39
- /** @internal */
40
- function createInertActorScope(actorLogic, snapshot, sourceSelf) {
41
- const snapshotRef = snapshot ? getSnapshotActorRef(snapshot) : undefined;
42
- const previousSelf = sourceSelf ?? snapshotRef?.actor;
43
- const baseSystem = previousSelf?.system;
44
- const system = previousSelf && baseSystem ? createSnapshotSystem(baseSystem, isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
45
- const self = createActor(actorLogic, {
46
- _inert: true,
47
- ...(previousSelf ? {
48
- id: previousSelf.id,
49
- _sessionId: previousSelf.sessionId
50
- } : {}),
51
- ...(system ? {
52
- _systemRef: {
53
- current: system
54
- }
55
- } : {})
56
- });
57
- if (previousSelf?._parent) {
58
- self._parent = previousSelf._parent;
59
- }
60
- if (snapshot) {
61
- self._snapshot = snapshot;
62
- }
63
- const actorScope = {
64
- self: self,
65
- defer: () => {},
66
- id: self.id,
67
- logger: self.system._logger,
68
- sessionId: self.sessionId,
69
- stopChild: child => child._stop(),
70
- system: self.system,
71
- emit: event => self.system.emitEvent(self, event),
72
- actionExecutor: () => {}
73
- };
74
- return actorScope;
75
- }
76
-
77
- /** @deprecated Use `initialTransition(…)` instead. */
78
- function getInitialSnapshot(actorLogic, ...[input]) {
79
- const actorScope = createInertActorScope(actorLogic);
80
- return actorLogic.initialTransition(input, actorScope)[0];
81
- }
82
-
83
- /**
84
- * Determines the next snapshot for the given `actorLogic` based on the given
85
- * `snapshot` and `event`.
86
- *
87
- * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
88
- * used.
89
- *
90
- * @deprecated Use `transition(…)` instead.
91
- * @example
92
- *
93
- * ```ts
94
- * import { getNextSnapshot } from 'xstate';
95
- * import { trafficLightMachine } from './trafficLightMachine.ts';
96
- *
97
- * const nextSnapshot = getNextSnapshot(
98
- * trafficLightMachine, // actor logic
99
- * undefined, // snapshot (or initial state if undefined)
100
- * { type: 'TIMER' }
101
- * ); // event object
102
- *
103
- * console.log(nextSnapshot.value);
104
- * // => 'yellow'
105
- *
106
- * const nextSnapshot2 = getNextSnapshot(
107
- * trafficLightMachine, // actor logic
108
- * nextSnapshot, // snapshot
109
- * { type: 'TIMER' }
110
- * ); // event object
111
- *
112
- * console.log(nextSnapshot2.value);
113
- * // =>'red'
114
- * ```
115
- */
116
- function getNextSnapshot(actorLogic, snapshot, event) {
117
- const actorScope = createInertActorScope(actorLogic, snapshot);
118
- const transitionResult = actorLogic.transition(snapshot, event, actorScope);
119
- const nextSnapshot = Array.isArray(transitionResult) ? transitionResult[0] : transitionResult;
120
- setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
121
- return nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(actorLogic, actorScope, nextSnapshot);
122
- }
123
-
124
19
  const cache = new WeakMap();
125
20
  function memo(object, key, fn) {
126
21
  let memoizedData = cache.get(object);
@@ -304,7 +199,7 @@ class StateNode {
304
199
  }
305
200
 
306
201
  /** @internal */
307
- next(snapshot, event, self, selectionResults) {
202
+ next(snapshot, event, actorScope, selectionResults) {
308
203
  const descriptorKey = getEventDescriptorKey(event);
309
204
  let candidates = this._candidateCache?.get(descriptorKey);
310
205
  if (!candidates) {
@@ -312,7 +207,7 @@ class StateNode {
312
207
  (this._candidateCache ??= new Map()).set(descriptorKey, candidates);
313
208
  }
314
209
  for (const candidate of candidates) {
315
- const guardPassed = evaluateCandidate(candidate, event, snapshot, this, self, selectionResults);
210
+ const guardPassed = evaluateCandidate(candidate, event, snapshot, this, actorScope, selectionResults);
316
211
  if (guardPassed) {
317
212
  return [candidate];
318
213
  }
@@ -701,7 +596,7 @@ class StateMachine {
701
596
  if (usesInertScope) {
702
597
  setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
703
598
  }
704
- const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(this, resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
599
+ const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
705
600
  if (this.validator) {
706
601
  assertValid(this.validator, {
707
602
  kind: 'result',
@@ -719,7 +614,7 @@ class StateMachine {
719
614
  if (usesInertScope) {
720
615
  setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
721
616
  }
722
- const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(this, resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
617
+ const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
723
618
  const effects = this._collectEffects(microsteps);
724
619
  if (this.validator) {
725
620
  assertValid(this.validator, {
@@ -766,9 +661,11 @@ class StateMachine {
766
661
  ...snapshot.context,
767
662
  ...selected.context
768
663
  } : snapshot.context;
769
- const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
770
- collectedMicrosteps.push(selected);
771
- actorScope.self._collectedMicrosteps = collectedMicrosteps;
664
+ if (!isInertActorScope(actorScope) && (actorScope.system._hasInspectionObservers?.() ?? true)) {
665
+ const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
666
+ collectedMicrosteps.push(selected);
667
+ actorScope.self._collectedMicrosteps = collectedMicrosteps;
668
+ }
772
669
  return cloneMachineSnapshot(snapshot, {
773
670
  ...(context !== snapshot.context ? {
774
671
  context
@@ -796,8 +693,8 @@ class StateMachine {
796
693
  }
797
694
  return snapshots;
798
695
  }
799
- getTransitionData(snapshot, event, self, selectionResults) {
800
- return transitionNode(this.root, snapshot.value, snapshot, event, self, selectionResults) || [];
696
+ getTransitionData(snapshot, event, actorScope, selectionResults) {
697
+ return transitionNode(this.root, snapshot.value, snapshot, event, actorScope, selectionResults) || [];
801
698
  }
802
699
  isInternalEventType(eventType) {
803
700
  if (eventType === XSTATE_TIMER) {
@@ -819,9 +716,8 @@ class StateMachine {
819
716
  * @internal
820
717
  */
821
718
  _canTransition(snapshot, event) {
822
- const emptyActor = getEmptyCanActor();
823
719
  const emptyActorScope = getEmptyCanActorScope();
824
- const transitionData = this.getTransitionData(snapshot, event, emptyActor);
720
+ const transitionData = this.getTransitionData(snapshot, event, emptyActorScope);
825
721
  if (!transitionData?.length) {
826
722
  return false;
827
723
  }
@@ -834,7 +730,7 @@ class StateMachine {
834
730
  const res = getTransitionResult(transition, snapshot, event, emptyActorScope, {
835
731
  resolveActions: false
836
732
  });
837
- if (res.targets?.length || res.context || hasEffect(transition, snapshot.context, event, snapshot, emptyActor)) {
733
+ if (res.targets?.length || res.context || hasEffect(transition, snapshot.context, event, snapshot, emptyActorScope)) {
838
734
  return true;
839
735
  }
840
736
  }
@@ -873,12 +769,11 @@ class StateMachine {
873
769
  if (typeof context === 'function') {
874
770
  const children = {};
875
771
  const spawn = createSpawner(actorScope, this.sources.actors, children);
876
- const resolvedContext = context({
772
+ const resolvedContext = context(withActorSelf({
877
773
  spawn,
878
774
  input: initEvent.input,
879
- self: actorScope.self,
880
775
  actors: this.sources.actors
881
- });
776
+ }, actorScope));
882
777
  const [nextState] = resolveActionsWithContext(preInitial, initEvent, actorScope, []);
883
778
  if (resolvedContext) {
884
779
  nextState.context = resolvedContext;
@@ -919,7 +814,7 @@ class StateMachine {
919
814
  if (usesInertScope) {
920
815
  setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
921
816
  }
922
- const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
817
+ const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
923
818
  const effects = this._collectEffects(microsteps);
924
819
  if (this.validator) {
925
820
  assertValid(this.validator, {
@@ -1103,10 +998,10 @@ class StateMachine {
1103
998
  reviveContext(restoredSnapshot.context);
1104
999
  if (usesInertScope) {
1105
1000
  setInertActorScopeSnapshot(resolvedActorScope, restoredSnapshot, false);
1106
- return attachSnapshotActorRef(this, resolvedActorScope, restoredSnapshot);
1001
+ return attachSnapshotActorRef(resolvedActorScope, restoredSnapshot);
1107
1002
  }
1108
1003
  return this._attachPureActorRef(restoredSnapshot, resolvedActorScope);
1109
1004
  }
1110
1005
  }
1111
1006
 
1112
- export { StateMachine as S, attachSnapshotActorRef as a, getNextSnapshot as b, createInertActorScope as c, StateNode as d, getInitialSnapshot as g, setInertActorScopeSnapshot as s };
1007
+ export { StateMachine as S, StateNode as a };
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var dist_xstateActors = require('./index-691f27b9.development.cjs.js');
3
+ var dist_xstateActors = require('./index-692b05ac.development.cjs.js');
4
4
 
5
5
  function createSpawner(actorScope, actors, spawnedChildren) {
6
6
  return (src, options) => {
@@ -18,111 +18,6 @@ function createSpawner(actorScope, actors, spawnedChildren) {
18
18
  };
19
19
  }
20
20
 
21
- /** @internal */
22
- function setInertActorScopeSnapshot(actorScope, snapshot, attachActorRef = true) {
23
- actorScope.self._snapshot = snapshot;
24
- if (attachActorRef && snapshot && typeof snapshot === 'object') {
25
- dist_xstateActors.setSnapshotActorRef(snapshot, actorScope.self);
26
- }
27
- return snapshot;
28
- }
29
-
30
- /** @internal */
31
- function isInertActorScope(actorScope) {
32
- return !!actorScope.self.options?._inert;
33
- }
34
-
35
- /** @internal */
36
- function attachSnapshotActorRef(actorLogic, actorScope, snapshot) {
37
- const snapshotScope = createInertActorScope(actorLogic, snapshot, actorScope.self);
38
- return setInertActorScopeSnapshot(snapshotScope, snapshot);
39
- }
40
-
41
- /** @internal */
42
- function createInertActorScope(actorLogic, snapshot, sourceSelf) {
43
- const snapshotRef = snapshot ? dist_xstateActors.getSnapshotActorRef(snapshot) : undefined;
44
- const previousSelf = sourceSelf ?? snapshotRef?.actor;
45
- const baseSystem = previousSelf?.system;
46
- const system = previousSelf && baseSystem ? dist_xstateActors.createSnapshotSystem(baseSystem, dist_xstateActors.isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
47
- const self = dist_xstateActors.createActor(actorLogic, {
48
- _inert: true,
49
- ...(previousSelf ? {
50
- id: previousSelf.id,
51
- _sessionId: previousSelf.sessionId
52
- } : {}),
53
- ...(system ? {
54
- _systemRef: {
55
- current: system
56
- }
57
- } : {})
58
- });
59
- if (previousSelf?._parent) {
60
- self._parent = previousSelf._parent;
61
- }
62
- if (snapshot) {
63
- self._snapshot = snapshot;
64
- }
65
- const actorScope = {
66
- self: self,
67
- defer: () => {},
68
- id: self.id,
69
- logger: self.system._logger,
70
- sessionId: self.sessionId,
71
- stopChild: child => child._stop(),
72
- system: self.system,
73
- emit: event => self.system.emitEvent(self, event),
74
- actionExecutor: () => {}
75
- };
76
- return actorScope;
77
- }
78
-
79
- /** @deprecated Use `initialTransition(…)` instead. */
80
- function getInitialSnapshot(actorLogic, ...[input]) {
81
- const actorScope = createInertActorScope(actorLogic);
82
- return actorLogic.initialTransition(input, actorScope)[0];
83
- }
84
-
85
- /**
86
- * Determines the next snapshot for the given `actorLogic` based on the given
87
- * `snapshot` and `event`.
88
- *
89
- * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
90
- * used.
91
- *
92
- * @deprecated Use `transition(…)` instead.
93
- * @example
94
- *
95
- * ```ts
96
- * import { getNextSnapshot } from 'xstate';
97
- * import { trafficLightMachine } from './trafficLightMachine.ts';
98
- *
99
- * const nextSnapshot = getNextSnapshot(
100
- * trafficLightMachine, // actor logic
101
- * undefined, // snapshot (or initial state if undefined)
102
- * { type: 'TIMER' }
103
- * ); // event object
104
- *
105
- * console.log(nextSnapshot.value);
106
- * // => 'yellow'
107
- *
108
- * const nextSnapshot2 = getNextSnapshot(
109
- * trafficLightMachine, // actor logic
110
- * nextSnapshot, // snapshot
111
- * { type: 'TIMER' }
112
- * ); // event object
113
- *
114
- * console.log(nextSnapshot2.value);
115
- * // =>'red'
116
- * ```
117
- */
118
- function getNextSnapshot(actorLogic, snapshot, event) {
119
- const actorScope = createInertActorScope(actorLogic, snapshot);
120
- const transitionResult = actorLogic.transition(snapshot, event, actorScope);
121
- const nextSnapshot = Array.isArray(transitionResult) ? transitionResult[0] : transitionResult;
122
- setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
123
- return nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(actorLogic, actorScope, nextSnapshot);
124
- }
125
-
126
21
  const cache = new WeakMap();
127
22
  function memo(object, key, fn) {
128
23
  let memoizedData = cache.get(object);
@@ -306,7 +201,7 @@ class StateNode {
306
201
  }
307
202
 
308
203
  /** @internal */
309
- next(snapshot, event, self, selectionResults) {
204
+ next(snapshot, event, actorScope, selectionResults) {
310
205
  const descriptorKey = dist_xstateActors.getEventDescriptorKey(event);
311
206
  let candidates = this._candidateCache?.get(descriptorKey);
312
207
  if (!candidates) {
@@ -314,7 +209,7 @@ class StateNode {
314
209
  (this._candidateCache ??= new Map()).set(descriptorKey, candidates);
315
210
  }
316
211
  for (const candidate of candidates) {
317
- const guardPassed = dist_xstateActors.evaluateCandidate(candidate, event, snapshot, this, self, selectionResults);
212
+ const guardPassed = dist_xstateActors.evaluateCandidate(candidate, event, snapshot, this, actorScope, selectionResults);
318
213
  if (guardPassed) {
319
214
  return [candidate];
320
215
  }
@@ -686,7 +581,7 @@ class StateMachine {
686
581
  */
687
582
  transition(snapshot, event, actorScope) {
688
583
  const usesInertScope = !actorScope;
689
- const resolvedActorScope = actorScope ?? createInertActorScope(this, snapshot);
584
+ const resolvedActorScope = actorScope ?? dist_xstateActors.createInertActorScope(this, snapshot);
690
585
  if (this.validator) {
691
586
  dist_xstateActors.assertValid(this.validator, {
692
587
  kind: 'event',
@@ -696,14 +591,14 @@ class StateMachine {
696
591
  });
697
592
  }
698
593
  if (usesInertScope) {
699
- setInertActorScopeSnapshot(resolvedActorScope, snapshot, false);
594
+ dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, snapshot, false);
700
595
  }
701
596
  const fastSnapshot = this._transitionFast(snapshot, event, resolvedActorScope);
702
597
  if (fastSnapshot) {
703
598
  if (usesInertScope) {
704
- setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
599
+ dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
705
600
  }
706
- const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(this, resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
601
+ const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
707
602
  if (this.validator) {
708
603
  dist_xstateActors.assertValid(this.validator, {
709
604
  kind: 'result',
@@ -719,9 +614,9 @@ class StateMachine {
719
614
  microsteps
720
615
  } = dist_xstateActors.macrostep(snapshot, event, resolvedActorScope, []);
721
616
  if (usesInertScope) {
722
- setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
617
+ dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
723
618
  }
724
- const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(this, resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
619
+ const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
725
620
  const effects = this._collectEffects(microsteps);
726
621
  if (this.validator) {
727
622
  dist_xstateActors.assertValid(this.validator, {
@@ -737,7 +632,7 @@ class StateMachine {
737
632
  return microsteps.flatMap(([, actions]) => actions);
738
633
  }
739
634
  _attachPureActorRef(snapshot, actorScope) {
740
- if (isInertActorScope(actorScope)) {
635
+ if (dist_xstateActors.isInertActorScope(actorScope)) {
741
636
  return snapshot;
742
637
  }
743
638
  dist_xstateActors.setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
@@ -768,9 +663,11 @@ class StateMachine {
768
663
  ...snapshot.context,
769
664
  ...selected.context
770
665
  } : snapshot.context;
771
- const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
772
- collectedMicrosteps.push(selected);
773
- actorScope.self._collectedMicrosteps = collectedMicrosteps;
666
+ if (!dist_xstateActors.isInertActorScope(actorScope) && (actorScope.system._hasInspectionObservers?.() ?? true)) {
667
+ const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
668
+ collectedMicrosteps.push(selected);
669
+ actorScope.self._collectedMicrosteps = collectedMicrosteps;
670
+ }
774
671
  return dist_xstateActors.cloneMachineSnapshot(snapshot, {
775
672
  ...(context !== snapshot.context ? {
776
673
  context
@@ -798,8 +695,8 @@ class StateMachine {
798
695
  }
799
696
  return snapshots;
800
697
  }
801
- getTransitionData(snapshot, event, self, selectionResults) {
802
- return dist_xstateActors.transitionNode(this.root, snapshot.value, snapshot, event, self, selectionResults) || [];
698
+ getTransitionData(snapshot, event, actorScope, selectionResults) {
699
+ return dist_xstateActors.transitionNode(this.root, snapshot.value, snapshot, event, actorScope, selectionResults) || [];
803
700
  }
804
701
  isInternalEventType(eventType) {
805
702
  if (eventType === dist_xstateActors.XSTATE_TIMER) {
@@ -821,9 +718,8 @@ class StateMachine {
821
718
  * @internal
822
719
  */
823
720
  _canTransition(snapshot, event) {
824
- const emptyActor = getEmptyCanActor();
825
721
  const emptyActorScope = getEmptyCanActorScope();
826
- const transitionData = this.getTransitionData(snapshot, event, emptyActor);
722
+ const transitionData = this.getTransitionData(snapshot, event, emptyActorScope);
827
723
  if (!transitionData?.length) {
828
724
  return false;
829
725
  }
@@ -836,7 +732,7 @@ class StateMachine {
836
732
  const res = dist_xstateActors.getTransitionResult(transition, snapshot, event, emptyActorScope, {
837
733
  resolveActions: false
838
734
  });
839
- if (res.targets?.length || res.context || dist_xstateActors.hasEffect(transition, snapshot.context, event, snapshot, emptyActor)) {
735
+ if (res.targets?.length || res.context || dist_xstateActors.hasEffect(transition, snapshot.context, event, snapshot, emptyActorScope)) {
840
736
  return true;
841
737
  }
842
738
  }
@@ -875,12 +771,11 @@ class StateMachine {
875
771
  if (typeof context === 'function') {
876
772
  const children = {};
877
773
  const spawn = createSpawner(actorScope, this.sources.actors, children);
878
- const resolvedContext = context({
774
+ const resolvedContext = context(dist_xstateActors.withActorSelf({
879
775
  spawn,
880
776
  input: initEvent.input,
881
- self: actorScope.self,
882
777
  actors: this.sources.actors
883
- });
778
+ }, actorScope));
884
779
  const [nextState] = dist_xstateActors.resolveActionsWithContext(preInitial, initEvent, actorScope, []);
885
780
  if (resolvedContext) {
886
781
  nextState.context = resolvedContext;
@@ -912,16 +807,16 @@ class StateMachine {
912
807
  });
913
808
  }
914
809
  const usesInertScope = !actorScope;
915
- const resolvedActorScope = actorScope ?? createInertActorScope(this);
810
+ const resolvedActorScope = actorScope ?? dist_xstateActors.createInertActorScope(this);
916
811
  const initEvent = dist_xstateActors.createInitEvent(input); // TODO: fix;
917
812
  const internalQueue = [];
918
813
  const preInitialState = this._getPreInitialState(resolvedActorScope, initEvent);
919
814
  const contextSpawnEffects = Object.values(preInitialState.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
920
815
  const finalizeInitialResult = (macroState, microsteps) => {
921
816
  if (usesInertScope) {
922
- setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
817
+ dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
923
818
  }
924
- const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
819
+ const returnedSnapshot = usesInertScope ? dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
925
820
  const effects = this._collectEffects(microsteps);
926
821
  if (this.validator) {
927
822
  dist_xstateActors.assertValid(this.validator, {
@@ -977,7 +872,7 @@ class StateMachine {
977
872
  }
978
873
  restoreSnapshot(snapshot, actorScope) {
979
874
  const usesInertScope = !actorScope;
980
- const resolvedActorScope = actorScope ?? createInertActorScope(this);
875
+ const resolvedActorScope = actorScope ?? dist_xstateActors.createInertActorScope(this);
981
876
  const persistedMachine = snapshot.machine;
982
877
  const legacyPersistedVersion = snapshot.version;
983
878
  const persistedVersion = typeof persistedMachine?.version === 'string' ? persistedMachine.version : legacyPersistedVersion;
@@ -1104,8 +999,8 @@ class StateMachine {
1104
999
  }
1105
1000
  reviveContext(restoredSnapshot.context);
1106
1001
  if (usesInertScope) {
1107
- setInertActorScopeSnapshot(resolvedActorScope, restoredSnapshot, false);
1108
- return attachSnapshotActorRef(this, resolvedActorScope, restoredSnapshot);
1002
+ dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, restoredSnapshot, false);
1003
+ return dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, restoredSnapshot);
1109
1004
  }
1110
1005
  return this._attachPureActorRef(restoredSnapshot, resolvedActorScope);
1111
1006
  }
@@ -1113,8 +1008,3 @@ class StateMachine {
1113
1008
 
1114
1009
  exports.StateMachine = StateMachine;
1115
1010
  exports.StateNode = StateNode;
1116
- exports.attachSnapshotActorRef = attachSnapshotActorRef;
1117
- exports.createInertActorScope = createInertActorScope;
1118
- exports.getInitialSnapshot = getInitialSnapshot;
1119
- exports.getNextSnapshot = getNextSnapshot;
1120
- exports.setInertActorScopeSnapshot = setInertActorScopeSnapshot;
@@ -2,7 +2,7 @@ import { MachineSnapshot } from "./State.js";
2
2
  import { StateNode } from "./StateNode.js";
3
3
  import { type TransitionSelectionResults } from "./stateUtils.js";
4
4
  import { AnyActorSystem } from "./system.js";
5
- import type { ActorLogic, ActorLogicTransitionResult, ActorScope, AnyActor, AnyActorLogic, AnyActorRef, AnyActorScope, AnyTransitionDefinition, Equals, EventDescriptor, EmittedFrom, EventObject, EventFromLogic, ExecutableActionObjectFromLogic, HistoryValue, InputFrom, IsAny, MachineContext, MetaObject, OutputFrom, Snapshot, SnapshotFrom, StateValue, StateSchema, SnapshotStatus } from "./types.js";
5
+ import type { ActorLogic, ActorLogicTransitionResult, ActorScope, AnyActorLogic, AnyActorRef, AnyActorScope, AnyTransitionDefinition, Equals, EventDescriptor, EmittedFrom, EventObject, EventFromLogic, ExecutableActionObjectFromLogic, HistoryValue, InputFrom, IsAny, MachineContext, MetaObject, OutputFrom, Snapshot, SnapshotFrom, StateValue, StateSchema, SnapshotStatus } from "./types.js";
6
6
  import { AnyMachineSchemas, Sources, Next_MachineConfig, MachineOptions } from "./types.v6.js";
7
7
  import type { ActorLogicValidator } from "./validation.types.js";
8
8
  type CompatibleProvidedActorSource<TExpected extends AnyActorLogic, TActual extends AnyActorLogic> = IsAny<TActual> extends true ? TActual : [OutputFrom<TActual>] extends [OutputFrom<TExpected>] ? [Omit<SnapshotFrom<TActual>, 'input'>] extends [
@@ -77,7 +77,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
77
77
  * @param event The received event
78
78
  */
79
79
  microstep(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, actorScope: AnyActorScope): Array<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>>;
80
- getTransitionData(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, self: AnyActor, selectionResults?: TransitionSelectionResults): Array<AnyTransitionDefinition>;
80
+ getTransitionData(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, actorScope: AnyActorScope, selectionResults?: TransitionSelectionResults): Array<AnyTransitionDefinition>;
81
81
  isInternalEventType(eventType: string): boolean;
82
82
  /**
83
83
  * Returns the initial `State` instance, with reference to `self` as an
@@ -7,7 +7,7 @@ export { createMachineFromConfig } from "./createMachineFromConfig.js";
7
7
  export type { ActionJSON, GuardJSON, InvokeJSON, MachineJSON, StateNodeJSON, TransitionJSON } from "./createMachineFromConfig.js";
8
8
  export { machineConfigToJSON, serializeMachine, type CodeExpression } from "./serialize.js";
9
9
  export { mapState } from "./mapState.js";
10
- export { machineVersions, migrateSnapshot, type MachineVersionsOptions, type ParsedPersistedSnapshot, type PersistedMachineIdentity, type PersistedSnapshotDataFrom, type SnapshotMigrationHandlers, type PersistedSnapshotFrom } from "./machineVersions.js";
10
+ export { machineVersions, type MigrateSnapshotOptions, type MachineVersionsOptions, type ParsedPersistedSnapshot, type PersistedMachineIdentity, type PersistedSnapshotDataFrom, type PersistedSnapshotSource, type SnapshotMigrationHandlers, type PersistedSnapshotFrom } from "./machineVersions.js";
11
11
  export { types, isTypeSchema, type StandardSchemaV1, type TypeSchema } from "./schema.types.js";
12
12
  export type { ActorValidationBoundary, ActorValidationEventOrigin, ActorValidationRequest, ActorLogicValidator } from "./validation.types.js";
13
13
  export { createSystem, setup } from "./setup.js";
@@ -26,26 +26,30 @@ export type ParsedPersistedSnapshot<TMachines extends readonly AnyStateMachine[]
26
26
  export type MachineVersionsOptions<TMachines extends readonly AnyStateMachine[]> = {
27
27
  unversioned?: NonNullable<TMachines[number]['version']>;
28
28
  };
29
- type ParsedSnapshotSource = {
30
- machine: VersionedStateMachine;
31
- snapshot: Snapshot<unknown> & {
32
- context: unknown;
33
- machine: PersistedMachineIdentity;
34
- [key: string]: unknown;
35
- };
36
- };
37
- type SourceVersion<TSource extends ParsedSnapshotSource> = TSource extends unknown ? NonNullable<TSource['machine']['version']> : never;
38
- type SourceForVersion<TSource extends ParsedSnapshotSource, TVersion extends string> = TSource extends unknown ? TSource['machine']['version'] extends TVersion ? TSource : never : never;
39
- export type SnapshotMigrationHandlers<TSource extends ParsedSnapshotSource, TTarget extends AnyStateMachine> = {
40
- [TVersion in SourceVersion<TSource>]?: (snapshot: SourceForVersion<TSource, TVersion>['snapshot']) => PersistedSnapshotDataFrom<TTarget>;
29
+ type MaybePromise<T> = T | PromiseLike<T>;
30
+ type MachineVersion<TMachines extends readonly VersionedStateMachine[]> = TMachines[number]['version'];
31
+ type MachineForVersion<TMachines extends readonly VersionedStateMachine[], TVersion extends string> = {
32
+ [K in keyof TMachines]: TMachines[K] extends VersionedStateMachine ? TMachines[K]['version'] extends TVersion ? TMachines[K] : never : never;
33
+ }[number];
34
+ export type PersistedSnapshotSource = {
35
+ id?: string;
36
+ version?: string;
37
+ };
38
+ export type SnapshotMigrationHandlers<TMachines extends readonly VersionedStateMachine[], TTarget extends VersionedStateMachine> = {
39
+ [TVersion in Exclude<MachineVersion<TMachines>, TTarget['version']>]?: (snapshot: PersistedSnapshotFrom<MachineForVersion<TMachines, TVersion>>) => MaybePromise<PersistedSnapshotDataFrom<TTarget>>;
40
+ } & {
41
+ '*'?: (snapshot: unknown, source: PersistedSnapshotSource) => MaybePromise<PersistedSnapshotDataFrom<TTarget>>;
42
+ };
43
+ export type MigrateSnapshotOptions<TMachines extends readonly VersionedStateMachine[], TTargetVersion extends MachineVersion<TMachines>> = {
44
+ to: TTargetVersion;
45
+ migrations: SnapshotMigrationHandlers<TMachines, MachineForVersion<TMachines, TTargetVersion>>;
41
46
  };
42
47
  /** Creates parsers backed by retained versions of one machine. */
43
48
  export declare function machineVersions<const TMachines extends readonly [
44
49
  VersionedStateMachine,
45
50
  ...VersionedStateMachine[]
46
51
  ]>(machines: TMachines, options?: MachineVersionsOptions<TMachines>): {
47
- parseSnapshot(raw: unknown): Promise<ParsedPersistedSnapshot<TMachines>>;
52
+ parseSnapshot: (raw: unknown) => Promise<ParsedPersistedSnapshot<TMachines>>;
53
+ migrateSnapshot<TTargetVersion extends MachineVersion<TMachines>>(raw: unknown, migrationOptions: MigrateSnapshotOptions<TMachines, TTargetVersion>): Promise<PersistedSnapshotFrom<MachineForVersion<TMachines, TTargetVersion>>>;
48
54
  };
49
- /** Migrates a parsed persisted snapshot directly to a target machine. */
50
- export declare function migrateSnapshot<TSource extends ParsedSnapshotSource, TTarget extends VersionedStateMachine>(source: TSource, target: TTarget, migrations: SnapshotMigrationHandlers<TSource, TTarget>): Promise<PersistedSnapshotFrom<TTarget>>;
51
55
  export {};
@@ -1,6 +1,6 @@
1
1
  import { MachineSnapshot } from "./State.js";
2
2
  import type { StateNode } from "./StateNode.js";
3
- import { AnyEventObject, AnyMachineSnapshot, AnyStateNode, AnyTransitionDefinition, DelayedTransitionDefinition, EventObject, ExecutableActionObject, MachineContext, StateValue, TransitionDefinition, AnyAction, AnyTransitionConfig, AnyActor, AnyActorScope } from "./types.js";
3
+ import { AnyEventObject, AnyMachineSnapshot, AnyStateNode, AnyTransitionDefinition, DelayedTransitionDefinition, EventObject, ExecutableActionObject, MachineContext, StateValue, TransitionDefinition, AnyAction, AnyTransitionConfig, AnyActorScope } from "./types.js";
4
4
  type AnyStateNodeIterable = Iterable<AnyStateNode>;
5
5
  export declare function isAtomicStateNode(stateNode: AnyStateNode): boolean;
6
6
  export declare function getProperAncestors(stateNode: AnyStateNode, toStateNode: AnyStateNode | undefined): Array<typeof stateNode>;
@@ -37,7 +37,7 @@ export type TransitionSelectionResult = {
37
37
  reusable: boolean;
38
38
  };
39
39
  export type TransitionSelectionResults = Map<AnyTransitionDefinition, TransitionSelectionResult>;
40
- export declare function transitionNode<TContext extends MachineContext, TEvent extends EventObject>(stateNode: AnyStateNode, stateValue: StateValue, snapshot: MachineSnapshot<TContext, TEvent, any, any, any, any, any, any>, event: TEvent, self: AnyActor, selectionResults?: TransitionSelectionResults): Array<TransitionDefinition<TContext, TEvent>> | undefined;
40
+ export declare function transitionNode<TContext extends MachineContext, TEvent extends EventObject>(stateNode: AnyStateNode, stateValue: StateValue, snapshot: MachineSnapshot<TContext, TEvent, any, any, any, any, any, any>, event: TEvent, actorScope: AnyActorScope, selectionResults?: TransitionSelectionResults): Array<TransitionDefinition<TContext, TEvent>> | undefined;
41
41
  type Microstep = readonly [AnyMachineSnapshot, ExecutableActionObject[]];
42
42
  export declare function initialMicrostep(root: AnyStateNode, preInitialState: AnyMachineSnapshot, actorScope: AnyActorScope, initEvent: AnyEventObject, internalQueue: AnyEventObject[]): Microstep;
43
43
  /**
@@ -70,6 +70,6 @@ export declare function macrostep(snapshot: AnyMachineSnapshot, event: EventObje
70
70
  * @param stateValue The partial state value to resolve.
71
71
  */
72
72
  export declare function resolveStateValue(rootNode: AnyStateNode, stateValue: StateValue): StateValue;
73
- export declare function hasEffect(transition: AnyTransitionDefinition, context: MachineContext, event: EventObject, snapshot: AnyMachineSnapshot, self: AnyActor): boolean;
74
- export declare function evaluateCandidate(candidate: AnyTransitionDefinition, event: EventObject, snapshot: AnyMachineSnapshot, stateNode: AnyStateNode, self: AnyActor, selectionResults?: TransitionSelectionResults): boolean;
73
+ export declare function hasEffect(transition: AnyTransitionDefinition, context: MachineContext, event: EventObject, snapshot: AnyMachineSnapshot, actorScope: AnyActorScope): boolean;
74
+ export declare function evaluateCandidate(candidate: AnyTransitionDefinition, event: EventObject, snapshot: AnyMachineSnapshot, stateNode: AnyStateNode, actorScope: AnyActorScope, selectionResults?: TransitionSelectionResults): boolean;
75
75
  export {};
@@ -351,7 +351,7 @@ export interface AnyStateMachine extends AnyActorLogic {
351
351
  schemas?: import("./types.v6.js").AnyMachineSchemas;
352
352
  provide(sources: any): AnyStateMachine;
353
353
  resolveState(config: any): any;
354
- getTransitionData(snapshot: any, event: any, actor: AnyActorRef, selectionResults?: Map<AnyTransitionDefinition, unknown>): AnyTransitionDefinition[];
354
+ getTransitionData(snapshot: any, event: any, actorScope: AnyActorScope, selectionResults?: Map<AnyTransitionDefinition, unknown>): AnyTransitionDefinition[];
355
355
  getStateNodeById(stateId: string): AnyStateNode;
356
356
  getPersistedSnapshot(snapshot: any, options?: unknown): Snapshot<unknown>;
357
357
  }