xstate 5.0.0-beta.37 → 5.0.0-beta.39

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 (62) hide show
  1. package/actions/dist/xstate-actions.cjs.js +4 -3
  2. package/actions/dist/xstate-actions.cjs.mjs +1 -0
  3. package/actions/dist/xstate-actions.development.cjs.js +4 -3
  4. package/actions/dist/xstate-actions.development.cjs.mjs +1 -0
  5. package/actions/dist/xstate-actions.development.esm.js +3 -3
  6. package/actions/dist/xstate-actions.esm.js +3 -3
  7. package/actions/dist/xstate-actions.umd.min.js +1 -1
  8. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  9. package/actors/dist/xstate-actors.cjs.js +60 -3
  10. package/actors/dist/xstate-actors.development.cjs.js +60 -3
  11. package/actors/dist/xstate-actors.development.esm.js +60 -3
  12. package/actors/dist/xstate-actors.esm.js +60 -3
  13. package/actors/dist/xstate-actors.umd.min.js +1 -1
  14. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  15. package/dist/declarations/src/State.d.ts +2 -2
  16. package/dist/declarations/src/StateMachine.d.ts +7 -7
  17. package/dist/declarations/src/actions/assign.d.ts +4 -4
  18. package/dist/declarations/src/actions/cancel.d.ts +4 -4
  19. package/dist/declarations/src/actions/choose.d.ts +3 -3
  20. package/dist/declarations/src/actions/log.d.ts +4 -4
  21. package/dist/declarations/src/actions/pure.d.ts +4 -4
  22. package/dist/declarations/src/actions/raise.d.ts +3 -3
  23. package/dist/declarations/src/actions/send.d.ts +7 -7
  24. package/dist/declarations/src/actions/spawn.d.ts +34 -0
  25. package/dist/declarations/src/actions/stop.d.ts +4 -4
  26. package/dist/declarations/src/actions.d.ts +1 -0
  27. package/dist/declarations/src/actors/callback.d.ts +76 -2
  28. package/dist/declarations/src/actors/observable.d.ts +2 -3
  29. package/dist/declarations/src/actors/promise.d.ts +0 -1
  30. package/dist/declarations/src/actors/transition.d.ts +3 -3
  31. package/dist/declarations/src/guards.d.ts +9 -10
  32. package/dist/declarations/src/interpreter.d.ts +71 -10
  33. package/dist/declarations/src/spawn.d.ts +3 -4
  34. package/dist/declarations/src/stateUtils.d.ts +4 -9
  35. package/dist/declarations/src/types.d.ts +51 -62
  36. package/dist/{interpreter-e58ca48d.development.cjs.js → interpreter-03a5c3f5.development.cjs.js} +91 -12
  37. package/dist/{interpreter-8def682e.esm.js → interpreter-1e8c1c0c.esm.js} +91 -12
  38. package/dist/{interpreter-97aff8d2.cjs.js → interpreter-5dfcd203.cjs.js} +91 -12
  39. package/dist/{interpreter-1c52b23c.development.esm.js → interpreter-70cd9217.development.esm.js} +91 -12
  40. package/dist/{raise-1fd59c65.development.cjs.js → raise-17cb3d9d.development.cjs.js} +145 -152
  41. package/dist/{raise-800296d7.cjs.js → raise-291d2181.cjs.js} +145 -152
  42. package/dist/{raise-21c417c1.esm.js → raise-62de3670.esm.js} +145 -153
  43. package/dist/{raise-e342a840.development.esm.js → raise-e044f460.development.esm.js} +145 -153
  44. package/dist/{send-92854675.esm.js → send-1249d4ac.esm.js} +45 -44
  45. package/dist/{send-b309ef4e.development.cjs.js → send-33433787.development.cjs.js} +45 -44
  46. package/dist/{send-4cc29786.cjs.js → send-af152aca.cjs.js} +45 -44
  47. package/dist/{send-83ccc98b.development.esm.js → send-f1a2a827.development.esm.js} +45 -44
  48. package/dist/xstate.cjs.js +23 -20
  49. package/dist/xstate.cjs.mjs +1 -0
  50. package/dist/xstate.development.cjs.js +23 -20
  51. package/dist/xstate.development.cjs.mjs +1 -0
  52. package/dist/xstate.development.esm.js +25 -23
  53. package/dist/xstate.esm.js +25 -23
  54. package/dist/xstate.umd.min.js +1 -1
  55. package/dist/xstate.umd.min.js.map +1 -1
  56. package/guards/dist/xstate-guards.cjs.js +2 -2
  57. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  58. package/guards/dist/xstate-guards.development.esm.js +2 -2
  59. package/guards/dist/xstate-guards.esm.js +2 -2
  60. package/guards/dist/xstate-guards.umd.min.js +1 -1
  61. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  62. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import { r as resolveReferencedActor, d as createActor, f as ActorStatus, j as createErrorActorEvent, k as toStateValue, l as STATE_IDENTIFIER, n as normalizeTarget, t as toArray, N as NULL_EVENT, a as toTransitionConfigArray, S as STATE_DELIMITER, o as toStatePath, q as createDoneStateEvent, s as resolveOutput, W as WILDCARD, X as XSTATE_STOP, u as XSTATE_INIT, v as createAfterEvent, w as flatten, e as matchesState, $ as $$ACTOR_TYPE } from './interpreter-1c52b23c.development.esm.js';
1
+ import { r as resolveReferencedActor, d as createActor, f as ActorStatus, j as createErrorActorEvent, k as toStateValue, l as STATE_IDENTIFIER, n as normalizeTarget, t as toArray, N as NULL_EVENT, a as toTransitionConfigArray, S as STATE_DELIMITER, o as toStatePath, q as createDoneStateEvent, s as resolveOutput, W as WILDCARD, X as XSTATE_STOP, u as XSTATE_INIT, v as createAfterEvent, w as flatten, e as matchesState, $ as $$ACTOR_TYPE } from './interpreter-70cd9217.development.esm.js';
2
2
 
3
3
  const cache = new WeakMap();
4
4
  function memo(object, key, fn) {
@@ -14,14 +14,14 @@ function memo(object, key, fn) {
14
14
  return memoizedData[key];
15
15
  }
16
16
 
17
- function resolveCancel(_, state, actionArgs, {
17
+ function resolveCancel(_, state, actionArgs, actionParams, {
18
18
  sendId
19
19
  }) {
20
- const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs) : sendId;
20
+ const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs, actionParams) : sendId;
21
21
  return [state, resolvedSendId];
22
22
  }
23
- function executeCancel(actorContext, resolvedSendId) {
24
- actorContext.self.cancel(resolvedSendId);
23
+ function executeCancel(actorScope, resolvedSendId) {
24
+ actorScope.self.cancel(resolvedSendId);
25
25
  }
26
26
  /**
27
27
  * Cancels an in-flight `send(...)` action. A canceled sent action will not
@@ -31,7 +31,7 @@ function executeCancel(actorContext, resolvedSendId) {
31
31
  * @param sendId The `id` of the `send(...)` action to cancel.
32
32
  */
33
33
  function cancel(sendId) {
34
- function cancel(_) {
34
+ function cancel(args, params) {
35
35
  {
36
36
  throw new Error(`This isn't supposed to be called`);
37
37
  }
@@ -43,7 +43,7 @@ function cancel(sendId) {
43
43
  return cancel;
44
44
  }
45
45
 
46
- function resolveInvoke(actorContext, state, actionArgs, {
46
+ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
47
47
  id,
48
48
  systemId,
49
49
  src,
@@ -54,99 +54,94 @@ function resolveInvoke(actorContext, state, actionArgs, {
54
54
  src,
55
55
  input: undefined
56
56
  };
57
+ const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
57
58
  let actorRef;
58
59
  if (referenced) {
59
60
  // TODO: inline `input: undefined` should win over the referenced one
60
61
  const configuredInput = input || referenced.input;
61
62
  actorRef = createActor(referenced.src, {
62
- id,
63
+ id: resolvedId,
63
64
  src: typeof src === 'string' ? src : undefined,
64
- parent: actorContext?.self,
65
+ parent: actorScope?.self,
65
66
  systemId,
66
67
  input: typeof configuredInput === 'function' ? configuredInput({
67
68
  context: state.context,
68
69
  event: actionArgs.event,
69
- self: actorContext?.self
70
+ self: actorScope?.self
70
71
  }) : configuredInput
71
72
  });
72
73
  if (syncSnapshot) {
73
74
  actorRef.subscribe({
74
75
  next: snapshot => {
75
76
  if (snapshot.status === 'active') {
76
- actorContext.self.send({
77
+ actorScope.self.send({
77
78
  type: `xstate.snapshot.${id}`,
78
79
  snapshot
79
80
  });
80
81
  }
81
82
  },
82
- error: () => {
83
- /* TODO */
84
- }
83
+ error: () => {}
85
84
  });
86
85
  }
87
86
  }
88
87
  if (!actorRef) {
89
- console.warn(`Actor type '${src}' not found in machine '${actorContext.id}'.`);
88
+ console.warn(`Actor type '${src}' not found in machine '${actorScope.id}'.`);
90
89
  }
91
90
  return [cloneState(state, {
92
91
  children: {
93
92
  ...state.children,
94
- [id]: actorRef
93
+ [resolvedId]: actorRef
95
94
  }
96
95
  }), {
97
96
  id,
98
97
  actorRef
99
98
  }];
100
99
  }
101
- function executeInvoke(actorContext, {
100
+ function executeSpawn(actorScope, {
102
101
  id,
103
102
  actorRef
104
103
  }) {
105
104
  if (!actorRef) {
106
105
  return;
107
106
  }
108
- actorContext.defer(() => {
107
+ actorScope.defer(() => {
109
108
  if (actorRef.status === ActorStatus.Stopped) {
110
109
  return;
111
110
  }
112
111
  try {
113
112
  actorRef.start?.();
114
113
  } catch (err) {
115
- actorContext.self.send(createErrorActorEvent(id, err));
114
+ actorScope.self.send(createErrorActorEvent(id, err));
116
115
  return;
117
116
  }
118
117
  });
119
118
  }
120
-
121
- // we don't export this since it's an internal action that is not meant to be used in the user's code
122
-
123
- function invoke({
119
+ function spawn(...[src, {
124
120
  id,
125
121
  systemId,
126
- src,
127
122
  input,
128
- onSnapshot
129
- }) {
130
- function invoke(_) {
123
+ syncSnapshot = false
124
+ } = {}]) {
125
+ function spawn(args, params) {
131
126
  {
132
127
  throw new Error(`This isn't supposed to be called`);
133
128
  }
134
129
  }
135
- invoke.type = 'xstate.invoke';
136
- invoke.id = id;
137
- invoke.systemId = systemId;
138
- invoke.src = src;
139
- invoke.input = input;
140
- invoke.syncSnapshot = !!onSnapshot;
141
- invoke.resolve = resolveInvoke;
142
- invoke.execute = executeInvoke;
143
- return invoke;
130
+ spawn.type = 'xstate.spawn';
131
+ spawn.id = id;
132
+ spawn.systemId = systemId;
133
+ spawn.src = src;
134
+ spawn.input = input;
135
+ spawn.syncSnapshot = syncSnapshot;
136
+ spawn.resolve = resolveSpawn;
137
+ spawn.execute = executeSpawn;
138
+ return spawn;
144
139
  }
145
140
 
146
- function resolveStop(_, state, args, {
141
+ function resolveStop(_, state, args, actionParams, {
147
142
  actorRef
148
143
  }) {
149
- const actorRefOrString = typeof actorRef === 'function' ? actorRef(args) : actorRef;
144
+ const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
150
145
  const resolvedActorRef = typeof actorRefOrString === 'string' ? state.children[actorRefOrString] : actorRefOrString;
151
146
  let children = state.children;
152
147
  if (resolvedActorRef) {
@@ -159,22 +154,28 @@ function resolveStop(_, state, args, {
159
154
  children
160
155
  }), resolvedActorRef];
161
156
  }
162
- function executeStop(actorContext, actorRef) {
157
+ function executeStop(actorScope, actorRef) {
163
158
  if (!actorRef) {
164
159
  return;
165
160
  }
161
+
162
+ // we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
163
+ // since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
164
+ // this could throw on `systemId` collision, for example, when dealing with reentering transitions
165
+ actorScope.system._unregister(actorRef);
166
+
166
167
  // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
167
168
  // this can happen, for example, when the invoking state is being exited immediately by an always transition
168
169
  if (actorRef.status !== ActorStatus.Running) {
169
- actorContext.stopChild(actorRef);
170
+ actorScope.stopChild(actorRef);
170
171
  return;
171
172
  }
172
173
  // stopping a child enqueues a stop event in the child actor's mailbox
173
174
  // we need for all of the already enqueued events to be processed before we stop the child
174
175
  // the parent itself might want to send some events to a child (for example from exit actions on the invoking state)
175
176
  // and we don't want to ignore those events
176
- actorContext.defer(() => {
177
- actorContext.stopChild(actorRef);
177
+ actorScope.defer(() => {
178
+ actorScope.stopChild(actorRef);
178
179
  });
179
180
  }
180
181
  /**
@@ -183,7 +184,7 @@ function executeStop(actorContext, actorRef) {
183
184
  * @param actorRef The actor to stop.
184
185
  */
185
186
  function stop(actorRef) {
186
- function stop(_) {
187
+ function stop(args, params) {
187
188
  {
188
189
  throw new Error(`This isn't supposed to be called`);
189
190
  }
@@ -205,7 +206,7 @@ function checkStateIn(state, _, {
205
206
  return state.matches(stateValue);
206
207
  }
207
208
  function stateIn(stateValue) {
208
- function stateIn(_) {
209
+ function stateIn(args, params) {
209
210
  {
210
211
  throw new Error(`This isn't supposed to be called`);
211
212
  }
@@ -223,7 +224,7 @@ function checkNot(state, {
223
224
  return !evaluateGuard(guards[0], context, event, state);
224
225
  }
225
226
  function not(guard) {
226
- function not(_) {
227
+ function not(args, params) {
227
228
  {
228
229
  throw new Error(`This isn't supposed to be called`);
229
230
  }
@@ -241,7 +242,7 @@ function checkAnd(state, {
241
242
  return guards.every(guard => evaluateGuard(guard, context, event, state));
242
243
  }
243
244
  function and(guards) {
244
- function and(_) {
245
+ function and(args, params) {
245
246
  {
246
247
  throw new Error(`This isn't supposed to be called`);
247
248
  }
@@ -259,7 +260,7 @@ function checkOr(state, {
259
260
  return guards.some(guard => evaluateGuard(guard, context, event, state));
260
261
  }
261
262
  function or(guards) {
262
- function or(_) {
263
+ function or(args, params) {
263
264
  {
264
265
  throw new Error(`This isn't supposed to be called`);
265
266
  }
@@ -284,22 +285,17 @@ function evaluateGuard(guard, context, event, state) {
284
285
  }
285
286
  const guardArgs = {
286
287
  context,
287
- event,
288
- guard: isInline ? undefined : typeof guard === 'string' ? {
289
- type: guard
290
- } : typeof guard.params === 'function' ? {
291
- type: guard.type,
292
- params: guard.params({
293
- context,
294
- event
295
- })
296
- } : guard
288
+ event
297
289
  };
290
+ const guardParams = isInline || typeof guard === 'string' ? undefined : 'params' in guard ? typeof guard.params === 'function' ? guard.params({
291
+ context,
292
+ event
293
+ }) : guard.params : undefined;
298
294
  if (!('check' in resolved)) {
299
295
  // the existing type of `.guards` assumes non-nullable `TExpressionGuard`
300
296
  // inline guards expect `TExpressionGuard` to be set to `undefined`
301
297
  // it's fine to cast this here, our logic makes sure that we call those 2 "variants" correctly
302
- return resolved(guardArgs);
298
+ return resolved(guardArgs, guardParams);
303
299
  }
304
300
  const builtinGuard = resolved;
305
301
  return builtinGuard.check(state, guardArgs, resolved // this holds all params
@@ -764,15 +760,6 @@ function isDescendant(childStateNode, parentStateNode) {
764
760
  }
765
761
  return marker.parent === parentStateNode;
766
762
  }
767
- function getPathFromRootToNode(stateNode) {
768
- const path = [];
769
- let marker = stateNode.parent;
770
- while (marker) {
771
- path.unshift(marker);
772
- marker = marker.parent;
773
- }
774
- return path;
775
- }
776
763
  function hasIntersection(s1, s2) {
777
764
  const set1 = new Set(s1);
778
765
  const set2 = new Set(s2);
@@ -812,17 +799,13 @@ function removeConflictingTransitions(enabledTransitions, configuration, history
812
799
  }
813
800
  return Array.from(filteredTransitions);
814
801
  }
815
- function findLCCA(stateNodes) {
816
- const [head] = stateNodes;
817
- let current = getPathFromRootToNode(head);
818
- let candidates = [];
819
- for (const stateNode of stateNodes) {
820
- const path = getPathFromRootToNode(stateNode);
821
- candidates = current.filter(sn => path.includes(sn));
822
- current = candidates;
823
- candidates = [];
802
+ function findLeastCommonAncestor(stateNodes) {
803
+ const [head, ...tail] = stateNodes;
804
+ for (const ancestor of getProperAncestors(head, undefined)) {
805
+ if (tail.every(sn => isDescendant(sn, ancestor))) {
806
+ return ancestor;
807
+ }
824
808
  }
825
- return current[current.length - 1];
826
809
  }
827
810
  function getEffectiveTargetStates(transition, historyValue) {
828
811
  if (!transition.target) {
@@ -854,8 +837,16 @@ function getTransitionDomain(transition, historyValue) {
854
837
  if (!transition.reenter && targetStates.every(target => target === transition.source || isDescendant(target, transition.source))) {
855
838
  return transition.source;
856
839
  }
857
- const lcca = findLCCA(targetStates.concat(transition.source));
858
- return lcca;
840
+ const lca = findLeastCommonAncestor(targetStates.concat(transition.source));
841
+ if (lca) {
842
+ return lca;
843
+ }
844
+
845
+ // at this point we know that it's a root transition since LCA couldn't be found
846
+ if (transition.reenter) {
847
+ return;
848
+ }
849
+ return transition.source.machine.root;
859
850
  }
860
851
  function computeExitSet(transitions, configuration, historyValue) {
861
852
  const statesToExit = new Set();
@@ -888,13 +879,8 @@ function areConfigurationsEqual(previousConfiguration, nextConfigurationSet) {
888
879
 
889
880
  /**
890
881
  * https://www.w3.org/TR/scxml/#microstepProcedure
891
- *
892
- * @private
893
- * @param transitions
894
- * @param currentState
895
- * @param mutConfiguration
896
882
  */
897
- function microstep(transitions, currentState, actorCtx, event, isInitial, internalQueue) {
883
+ function microstep(transitions, currentState, actorScope, event, isInitial, internalQueue) {
898
884
  if (!transitions.length) {
899
885
  return currentState;
900
886
  }
@@ -905,17 +891,17 @@ function microstep(transitions, currentState, actorCtx, event, isInitial, intern
905
891
 
906
892
  // Exit states
907
893
  if (!isInitial) {
908
- [nextState, historyValue] = exitStates(nextState, event, actorCtx, filteredTransitions, mutConfiguration, historyValue, internalQueue);
894
+ [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutConfiguration, historyValue, internalQueue);
909
895
  }
910
896
 
911
897
  // Execute transition content
912
- nextState = resolveActionsAndContext(nextState, event, actorCtx, filteredTransitions.flatMap(t => t.actions), internalQueue);
898
+ nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue);
913
899
 
914
900
  // Enter states
915
- nextState = enterStates(nextState, event, actorCtx, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial);
901
+ nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial);
916
902
  const nextConfiguration = [...mutConfiguration];
917
903
  if (nextState.status === 'done') {
918
- nextState = resolveActionsAndContext(nextState, event, actorCtx, nextConfiguration.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue);
904
+ nextState = resolveActionsAndContext(nextState, event, actorScope, nextConfiguration.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue);
919
905
  }
920
906
  try {
921
907
  if (historyValue === currentState.historyValue && areConfigurationsEqual(currentState.configuration, mutConfiguration)) {
@@ -931,14 +917,14 @@ function microstep(transitions, currentState, actorCtx, event, isInitial, intern
931
917
  throw e;
932
918
  }
933
919
  }
934
- function getMachineOutput(state, event, actorCtx, rootNode, rootCompletionNode) {
920
+ function getMachineOutput(state, event, actorScope, rootNode, rootCompletionNode) {
935
921
  if (!rootNode.output) {
936
922
  return;
937
923
  }
938
- const doneStateEvent = createDoneStateEvent(rootCompletionNode.id, rootCompletionNode.output && rootCompletionNode.parent ? resolveOutput(rootCompletionNode.output, state.context, event, actorCtx.self) : undefined);
939
- return resolveOutput(rootNode.output, state.context, doneStateEvent, actorCtx.self);
924
+ const doneStateEvent = createDoneStateEvent(rootCompletionNode.id, rootCompletionNode.output && rootCompletionNode.parent ? resolveOutput(rootCompletionNode.output, state.context, event, actorScope.self) : undefined);
925
+ return resolveOutput(rootNode.output, state.context, doneStateEvent, actorScope.self);
940
926
  }
941
- function enterStates(currentState, event, actorCtx, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial) {
927
+ function enterStates(currentState, event, actorScope, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial) {
942
928
  let nextState = currentState;
943
929
  const statesToEnter = new Set();
944
930
  // those are states that were directly targeted or indirectly targeted by the explicit target
@@ -959,19 +945,22 @@ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConf
959
945
  // Add entry actions
960
946
  actions.push(...stateNodeToEnter.entry);
961
947
  for (const invokeDef of stateNodeToEnter.invoke) {
962
- actions.push(invoke(invokeDef));
948
+ actions.push(spawn(invokeDef.src, {
949
+ ...invokeDef,
950
+ syncSnapshot: !!invokeDef.onSnapshot
951
+ }));
963
952
  }
964
953
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
965
954
  const initialActions = stateNodeToEnter.initial.actions;
966
955
  actions.push(...initialActions);
967
956
  }
968
- nextState = resolveActionsAndContext(nextState, event, actorCtx, actions, internalQueue, stateNodeToEnter.invoke.map(invokeDef => invokeDef.id));
957
+ nextState = resolveActionsAndContext(nextState, event, actorScope, actions, internalQueue, stateNodeToEnter.invoke.map(invokeDef => invokeDef.id));
969
958
  if (stateNodeToEnter.type === 'final') {
970
959
  const parent = stateNodeToEnter.parent;
971
960
  let ancestorMarker = parent?.type === 'parallel' ? parent : parent?.parent;
972
961
  let rootCompletionNode = ancestorMarker || stateNodeToEnter;
973
962
  if (parent?.type === 'compound') {
974
- internalQueue.push(createDoneStateEvent(parent.id, stateNodeToEnter.output ? resolveOutput(stateNodeToEnter.output, nextState.context, event, actorCtx.self) : undefined));
963
+ internalQueue.push(createDoneStateEvent(parent.id, stateNodeToEnter.output ? resolveOutput(stateNodeToEnter.output, nextState.context, event, actorScope.self) : undefined));
975
964
  }
976
965
  while (ancestorMarker?.type === 'parallel' && !completedNodes.has(ancestorMarker) && isInFinalState(mutConfiguration, ancestorMarker)) {
977
966
  completedNodes.add(ancestorMarker);
@@ -984,7 +973,7 @@ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConf
984
973
  }
985
974
  nextState = cloneState(nextState, {
986
975
  status: 'done',
987
- output: getMachineOutput(nextState, event, actorCtx, currentState.configuration[0].machine.root, rootCompletionNode)
976
+ output: getMachineOutput(nextState, event, actorScope, currentState.configuration[0].machine.root, rootCompletionNode)
988
977
  });
989
978
  }
990
979
  }
@@ -1009,7 +998,11 @@ function computeEntrySet(transitions, historyValue, statesForDefaultEntry, state
1009
998
  }
1010
999
  const targetStates = getEffectiveTargetStates(t, historyValue);
1011
1000
  for (const s of targetStates) {
1012
- addAncestorStatesToEnter(s, domain, statesToEnter, historyValue, statesForDefaultEntry);
1001
+ const ancestors = getProperAncestors(s, domain);
1002
+ if (domain?.type === 'parallel') {
1003
+ ancestors.push(domain);
1004
+ }
1005
+ addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultEntry, ancestors, !t.source.parent && t.reenter ? undefined : domain);
1013
1006
  }
1014
1007
  }
1015
1008
  }
@@ -1022,7 +1015,7 @@ function addDescendantStatesToEnter(stateNode, historyValue, statesForDefaultEnt
1022
1015
  addDescendantStatesToEnter(s, historyValue, statesForDefaultEntry, statesToEnter);
1023
1016
  }
1024
1017
  for (const s of historyStateNodes) {
1025
- addAncestorStatesToEnter(s, stateNode.parent, statesToEnter, historyValue, statesForDefaultEntry);
1018
+ addProperAncestorStatesToEnter(s, stateNode.parent, statesToEnter, historyValue, statesForDefaultEntry);
1026
1019
  }
1027
1020
  } else {
1028
1021
  const historyDefaultTransition = resolveHistoryDefaultTransition(stateNode);
@@ -1034,7 +1027,7 @@ function addDescendantStatesToEnter(stateNode, historyValue, statesForDefaultEnt
1034
1027
  addDescendantStatesToEnter(s, historyValue, statesForDefaultEntry, statesToEnter);
1035
1028
  }
1036
1029
  for (const s of historyDefaultTransition.target) {
1037
- addAncestorStatesToEnter(s, stateNode, statesToEnter, historyValue, statesForDefaultEntry);
1030
+ addProperAncestorStatesToEnter(s, stateNode, statesToEnter, historyValue, statesForDefaultEntry);
1038
1031
  }
1039
1032
  }
1040
1033
  } else {
@@ -1045,7 +1038,7 @@ function addDescendantStatesToEnter(stateNode, historyValue, statesForDefaultEnt
1045
1038
  statesForDefaultEntry.add(initialState);
1046
1039
  }
1047
1040
  addDescendantStatesToEnter(initialState, historyValue, statesForDefaultEntry, statesToEnter);
1048
- addAncestorStatesToEnter(initialState, stateNode, statesToEnter, historyValue, statesForDefaultEntry);
1041
+ addProperAncestorStatesToEnter(initialState, stateNode, statesToEnter, historyValue, statesForDefaultEntry);
1049
1042
  } else {
1050
1043
  if (stateNode.type === 'parallel') {
1051
1044
  for (const child of getChildren(stateNode).filter(sn => !isHistoryNode(sn))) {
@@ -1061,10 +1054,11 @@ function addDescendantStatesToEnter(stateNode, historyValue, statesForDefaultEnt
1061
1054
  }
1062
1055
  }
1063
1056
  }
1064
- function addAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, historyValue, statesForDefaultEntry) {
1065
- const properAncestors = getProperAncestors(stateNode, toStateNode);
1066
- for (const anc of properAncestors) {
1067
- statesToEnter.add(anc);
1057
+ function addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultEntry, ancestors, reentrancyDomain) {
1058
+ for (const anc of ancestors) {
1059
+ if (!reentrancyDomain || isDescendant(anc, reentrancyDomain)) {
1060
+ statesToEnter.add(anc);
1061
+ }
1068
1062
  if (anc.type === 'parallel') {
1069
1063
  for (const child of getChildren(anc).filter(sn => !isHistoryNode(sn))) {
1070
1064
  if (![...statesToEnter].some(s => isDescendant(s, child))) {
@@ -1075,7 +1069,10 @@ function addAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, history
1075
1069
  }
1076
1070
  }
1077
1071
  }
1078
- function exitStates(currentState, event, actorCtx, transitions, mutConfiguration, historyValue, internalQueue) {
1072
+ function addProperAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, historyValue, statesForDefaultEntry) {
1073
+ addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultEntry, getProperAncestors(stateNode, toStateNode));
1074
+ }
1075
+ function exitStates(currentState, event, actorScope, transitions, mutConfiguration, historyValue, internalQueue) {
1079
1076
  let nextState = currentState;
1080
1077
  const statesToExit = computeExitSet(transitions, mutConfiguration, historyValue);
1081
1078
  statesToExit.sort((a, b) => b.order - a.order);
@@ -1099,12 +1096,12 @@ function exitStates(currentState, event, actorCtx, transitions, mutConfiguration
1099
1096
  }
1100
1097
  }
1101
1098
  for (const s of statesToExit) {
1102
- nextState = resolveActionsAndContext(nextState, event, actorCtx, [...s.exit, ...s.invoke.map(def => stop(def.id))], internalQueue);
1099
+ nextState = resolveActionsAndContext(nextState, event, actorScope, [...s.exit, ...s.invoke.map(def => stop(def.id))], internalQueue);
1103
1100
  mutConfiguration.delete(s);
1104
1101
  }
1105
1102
  return [nextState, changedHistory || historyValue];
1106
1103
  }
1107
- function resolveActionsAndContextWorker(currentState, event, actorCtx, actions, extra, retries) {
1104
+ function resolveActionsAndContextWorker(currentState, event, actorScope, actions, extra, retries) {
1108
1105
  const {
1109
1106
  machine
1110
1107
  } = currentState;
@@ -1122,32 +1119,25 @@ function resolveActionsAndContextWorker(currentState, event, actorCtx, actions,
1122
1119
  const actionArgs = {
1123
1120
  context: intermediateState.context,
1124
1121
  event,
1125
- self: actorCtx?.self,
1126
- system: actorCtx?.system,
1127
- action: isInline ? undefined : typeof action === 'string' ? {
1128
- type: action
1129
- } : typeof action.params === 'function' ? {
1130
- type: action.type,
1131
- params: action.params({
1132
- context: intermediateState.context,
1133
- event
1134
- })
1135
- } :
1136
- // TS isn't able to narrow it down here
1137
- action
1122
+ self: actorScope?.self,
1123
+ system: actorScope?.system
1138
1124
  };
1125
+ const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
1126
+ context: intermediateState.context,
1127
+ event
1128
+ }) : action.params : undefined;
1139
1129
  if (!('resolve' in resolvedAction)) {
1140
- if (actorCtx?.self.status === ActorStatus.Running) {
1141
- resolvedAction(actionArgs);
1130
+ if (actorScope?.self.status === ActorStatus.Running) {
1131
+ resolvedAction(actionArgs, actionParams);
1142
1132
  } else {
1143
- actorCtx?.defer(() => {
1144
- resolvedAction(actionArgs);
1133
+ actorScope?.defer(() => {
1134
+ resolvedAction(actionArgs, actionParams);
1145
1135
  });
1146
1136
  }
1147
1137
  continue;
1148
1138
  }
1149
1139
  const builtinAction = resolvedAction;
1150
- const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolvedAction,
1140
+ const [nextState, params, actions] = builtinAction.resolve(actorScope, intermediateState, actionArgs, actionParams, resolvedAction,
1151
1141
  // this holds all params
1152
1142
  extra);
1153
1143
  intermediateState = nextState;
@@ -1155,30 +1145,30 @@ function resolveActionsAndContextWorker(currentState, event, actorCtx, actions,
1155
1145
  retries?.push([builtinAction, params]);
1156
1146
  }
1157
1147
  if ('execute' in builtinAction) {
1158
- if (actorCtx?.self.status === ActorStatus.Running) {
1159
- builtinAction.execute(actorCtx, params);
1148
+ if (actorScope?.self.status === ActorStatus.Running) {
1149
+ builtinAction.execute(actorScope, params);
1160
1150
  } else {
1161
- actorCtx?.defer(builtinAction.execute.bind(null, actorCtx, params));
1151
+ actorScope?.defer(builtinAction.execute.bind(null, actorScope, params));
1162
1152
  }
1163
1153
  }
1164
1154
  if (actions) {
1165
- intermediateState = resolveActionsAndContextWorker(intermediateState, event, actorCtx, actions, extra, retries);
1155
+ intermediateState = resolveActionsAndContextWorker(intermediateState, event, actorScope, actions, extra, retries);
1166
1156
  }
1167
1157
  }
1168
1158
  return intermediateState;
1169
1159
  }
1170
- function resolveActionsAndContext(currentState, event, actorCtx, actions, internalQueue, deferredActorIds) {
1160
+ function resolveActionsAndContext(currentState, event, actorScope, actions, internalQueue, deferredActorIds) {
1171
1161
  const retries = deferredActorIds ? [] : undefined;
1172
- const nextState = resolveActionsAndContextWorker(currentState, event, actorCtx, actions, {
1162
+ const nextState = resolveActionsAndContextWorker(currentState, event, actorScope, actions, {
1173
1163
  internalQueue,
1174
1164
  deferredActorIds
1175
1165
  }, retries);
1176
1166
  retries?.forEach(([builtinAction, params]) => {
1177
- builtinAction.retryResolve(actorCtx, nextState, params);
1167
+ builtinAction.retryResolve(actorScope, nextState, params);
1178
1168
  });
1179
1169
  return nextState;
1180
1170
  }
1181
- function macrostep(state, event, actorCtx, internalQueue = []) {
1171
+ function macrostep(state, event, actorScope, internalQueue = []) {
1182
1172
  if (event.type === WILDCARD) {
1183
1173
  throw new Error(`An event cannot have the wildcard type ('${WILDCARD}')`);
1184
1174
  }
@@ -1187,7 +1177,9 @@ function macrostep(state, event, actorCtx, internalQueue = []) {
1187
1177
 
1188
1178
  // Handle stop event
1189
1179
  if (event.type === XSTATE_STOP) {
1190
- nextState = stopChildren(nextState, event, actorCtx);
1180
+ nextState = cloneState(stopChildren(nextState, event, actorScope), {
1181
+ status: 'stopped'
1182
+ });
1191
1183
  states.push(nextState);
1192
1184
  return {
1193
1185
  state: nextState,
@@ -1200,7 +1192,7 @@ function macrostep(state, event, actorCtx, internalQueue = []) {
1200
1192
  // Determine the next state based on the next microstep
1201
1193
  if (nextEvent.type !== XSTATE_INIT) {
1202
1194
  const transitions = selectTransitions(nextEvent, nextState);
1203
- nextState = microstep(transitions, state, actorCtx, nextEvent, false, internalQueue);
1195
+ nextState = microstep(transitions, state, actorScope, nextEvent, false, internalQueue);
1204
1196
  states.push(nextState);
1205
1197
  }
1206
1198
  let shouldSelectEventlessTransitions = true;
@@ -1217,20 +1209,20 @@ function macrostep(state, event, actorCtx, internalQueue = []) {
1217
1209
  nextEvent = internalQueue.shift();
1218
1210
  enabledTransitions = selectTransitions(nextEvent, nextState);
1219
1211
  }
1220
- nextState = microstep(enabledTransitions, nextState, actorCtx, nextEvent, false, internalQueue);
1212
+ nextState = microstep(enabledTransitions, nextState, actorScope, nextEvent, false, internalQueue);
1221
1213
  shouldSelectEventlessTransitions = nextState !== previousState;
1222
1214
  states.push(nextState);
1223
1215
  }
1224
1216
  if (nextState.status !== 'active') {
1225
- stopChildren(nextState, nextEvent, actorCtx);
1217
+ stopChildren(nextState, nextEvent, actorScope);
1226
1218
  }
1227
1219
  return {
1228
1220
  state: nextState,
1229
1221
  microstates: states
1230
1222
  };
1231
1223
  }
1232
- function stopChildren(nextState, event, actorCtx) {
1233
- return resolveActionsAndContext(nextState, event, actorCtx, Object.values(nextState.children).map(child => stop(child)), []);
1224
+ function stopChildren(nextState, event, actorScope) {
1225
+ return resolveActionsAndContext(nextState, event, actorScope, Object.values(nextState.children).map(child => stop(child)), []);
1234
1226
  }
1235
1227
  function selectTransitions(event, nextState) {
1236
1228
  return nextState.machine.getTransitionData(nextState, event);
@@ -1450,17 +1442,17 @@ function getPersistedState(state) {
1450
1442
  throw new Error('An inline child actor cannot be persisted.');
1451
1443
  }
1452
1444
  childrenJson[id] = {
1453
- state: child.getPersistedState?.(),
1454
- src: child.src
1445
+ state: child.getPersistedState(),
1446
+ src: child.src,
1447
+ systemId: child._systemId
1455
1448
  };
1456
1449
  }
1457
- return {
1450
+ const persisted = {
1458
1451
  ...jsonValues,
1459
- // TODO: this makes `PersistedMachineState`'s type kind of a lie
1460
- // it doesn't truly use `TContext` but rather some kind of a derived form of it
1461
1452
  context: persistContext(context),
1462
1453
  children: childrenJson
1463
1454
  };
1455
+ return persisted;
1464
1456
  }
1465
1457
  function persistContext(contextPart) {
1466
1458
  let copy;
@@ -1489,7 +1481,7 @@ function persistContext(contextPart) {
1489
1481
  return copy ?? contextPart;
1490
1482
  }
1491
1483
 
1492
- function resolveRaise(_, state, args, {
1484
+ function resolveRaise(_, state, args, actionParams, {
1493
1485
  event: eventOrExpr,
1494
1486
  id,
1495
1487
  delay
@@ -1500,13 +1492,13 @@ function resolveRaise(_, state, args, {
1500
1492
  if (typeof eventOrExpr === 'string') {
1501
1493
  throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
1502
1494
  }
1503
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args) : eventOrExpr;
1495
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
1504
1496
  let resolvedDelay;
1505
1497
  if (typeof delay === 'string') {
1506
1498
  const configDelay = delaysMap && delaysMap[delay];
1507
- resolvedDelay = typeof configDelay === 'function' ? configDelay(args) : configDelay;
1499
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
1508
1500
  } else {
1509
- resolvedDelay = typeof delay === 'function' ? delay(args) : delay;
1501
+ resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
1510
1502
  }
1511
1503
  if (typeof resolvedDelay !== 'number') {
1512
1504
  internalQueue.push(resolvedEvent);
@@ -1517,9 +1509,9 @@ function resolveRaise(_, state, args, {
1517
1509
  delay: resolvedDelay
1518
1510
  }];
1519
1511
  }
1520
- function executeRaise(actorContext, params) {
1512
+ function executeRaise(actorScope, params) {
1521
1513
  if (typeof params.delay === 'number') {
1522
- actorContext.self.delaySend(params);
1514
+ actorScope.self.delaySend(params);
1523
1515
  return;
1524
1516
  }
1525
1517
  }
@@ -1530,7 +1522,7 @@ function executeRaise(actorContext, params) {
1530
1522
  * @param eventType The event to raise.
1531
1523
  */
1532
1524
  function raise(eventOrExpr, options) {
1533
- function raise(_) {
1525
+ function raise(args, params) {
1534
1526
  {
1535
1527
  throw new Error(`This isn't supposed to be called`);
1536
1528
  }
@@ -1544,4 +1536,4 @@ function raise(eventOrExpr, options) {
1544
1536
  return raise;
1545
1537
  }
1546
1538
 
1547
- export { raise as A, stop as B, State as S, formatTransition as a, formatInitialTransition as b, getCandidates as c, getConfiguration as d, evaluateGuard as e, formatTransitions as f, getDelayedTransitions as g, getStateNodes as h, isInFinalState as i, cloneState as j, macrostep as k, getInitialConfiguration as l, memo as m, resolveActionsAndContext as n, microstep as o, getInitialStateNodes as p, isStateId as q, resolveStateValue as r, getStateNodeByPath as s, transitionNode as t, getPersistedState as u, and as v, not as w, or as x, stateIn as y, cancel as z };
1539
+ export { raise as A, stop as B, spawn as C, State as S, formatTransition as a, formatInitialTransition as b, getCandidates as c, getConfiguration as d, evaluateGuard as e, formatTransitions as f, getDelayedTransitions as g, getStateNodes as h, isInFinalState as i, cloneState as j, macrostep as k, getInitialConfiguration as l, memo as m, resolveActionsAndContext as n, microstep as o, getInitialStateNodes as p, isStateId as q, resolveStateValue as r, getStateNodeByPath as s, transitionNode as t, getPersistedState as u, and as v, not as w, or as x, stateIn as y, cancel as z };