xstate 5.0.0-beta.36 → 5.0.0-beta.38

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 (65) 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 +1 -1
  10. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  11. package/actors/dist/xstate-actors.development.esm.js +1 -1
  12. package/actors/dist/xstate-actors.esm.js +1 -1
  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 +4 -4
  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 +2 -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 +1 -1
  31. package/dist/declarations/src/guards.d.ts +9 -10
  32. package/dist/declarations/src/interpreter.d.ts +2 -2
  33. package/dist/declarations/src/spawn.d.ts +1 -2
  34. package/dist/declarations/src/stateUtils.d.ts +0 -5
  35. package/dist/declarations/src/typegenTypes.d.ts +2 -0
  36. package/dist/declarations/src/types.d.ts +47 -58
  37. package/dist/declarations/src/utils.d.ts +6 -6
  38. package/dist/declarations/src/waitFor.d.ts +1 -1
  39. package/dist/{interpreter-5c4e6634.development.esm.js → interpreter-4005eb36.development.esm.js} +14 -24
  40. package/dist/{interpreter-69605bf0.cjs.js → interpreter-b6f22ee2.cjs.js} +14 -24
  41. package/dist/{interpreter-de5217bc.esm.js → interpreter-c80ce92e.esm.js} +14 -24
  42. package/dist/{interpreter-d3567419.development.cjs.js → interpreter-ed3f81f7.development.cjs.js} +14 -24
  43. package/dist/{raise-5b7ad3b7.development.esm.js → raise-42073973.development.esm.js} +138 -118
  44. package/dist/{raise-c51b81a3.cjs.js → raise-7faa9b3b.cjs.js} +139 -118
  45. package/dist/{raise-106ea558.development.cjs.js → raise-b69a3d16.development.cjs.js} +138 -117
  46. package/dist/{raise-ffe1014a.esm.js → raise-c989c7fb.esm.js} +139 -119
  47. package/dist/{send-778692de.cjs.js → send-34160163.cjs.js} +31 -27
  48. package/dist/{send-0a7aa74e.esm.js → send-4b616da9.esm.js} +31 -27
  49. package/dist/{send-25e70bd4.development.cjs.js → send-58725522.development.cjs.js} +31 -27
  50. package/dist/{send-e93554d6.development.esm.js → send-bff8c910.development.esm.js} +31 -27
  51. package/dist/xstate.cjs.js +11 -16
  52. package/dist/xstate.cjs.mjs +1 -0
  53. package/dist/xstate.development.cjs.js +11 -16
  54. package/dist/xstate.development.cjs.mjs +1 -0
  55. package/dist/xstate.development.esm.js +13 -19
  56. package/dist/xstate.esm.js +13 -19
  57. package/dist/xstate.umd.min.js +1 -1
  58. package/dist/xstate.umd.min.js.map +1 -1
  59. package/guards/dist/xstate-guards.cjs.js +2 -2
  60. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  61. package/guards/dist/xstate-guards.development.esm.js +2 -2
  62. package/guards/dist/xstate-guards.esm.js +2 -2
  63. package/guards/dist/xstate-guards.umd.min.js +1 -1
  64. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  65. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var interpreter = require('./interpreter-69605bf0.cjs.js');
3
+ var interpreter = require('./interpreter-b6f22ee2.cjs.js');
4
4
 
5
5
  const cache = new WeakMap();
6
6
  function memo(object, key, fn) {
@@ -16,10 +16,10 @@ function memo(object, key, fn) {
16
16
  return memoizedData[key];
17
17
  }
18
18
 
19
- function resolveCancel(_, state, actionArgs, {
19
+ function resolveCancel(_, state, actionArgs, actionParams, {
20
20
  sendId
21
21
  }) {
22
- const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs) : sendId;
22
+ const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs, actionParams) : sendId;
23
23
  return [state, resolvedSendId];
24
24
  }
25
25
  function executeCancel(actorContext, resolvedSendId) {
@@ -33,7 +33,7 @@ function executeCancel(actorContext, resolvedSendId) {
33
33
  * @param sendId The `id` of the `send(...)` action to cancel.
34
34
  */
35
35
  function cancel(sendId) {
36
- function cancel(_) {
36
+ function cancel(args, params) {
37
37
  }
38
38
  cancel.type = 'xstate.cancel';
39
39
  cancel.sendId = sendId;
@@ -42,21 +42,25 @@ function cancel(sendId) {
42
42
  return cancel;
43
43
  }
44
44
 
45
- function resolveInvoke(actorContext, state, actionArgs, {
45
+ function resolveSpawn(actorContext, state, actionArgs, _actionParams, {
46
46
  id,
47
47
  systemId,
48
48
  src,
49
49
  input,
50
50
  syncSnapshot
51
51
  }) {
52
- const referenced = interpreter.resolveReferencedActor(state.machine.implementations.actors[src]);
52
+ const referenced = typeof src === 'string' ? interpreter.resolveReferencedActor(state.machine, src) : {
53
+ src,
54
+ input: undefined
55
+ };
56
+ const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
53
57
  let actorRef;
54
58
  if (referenced) {
55
59
  // TODO: inline `input: undefined` should win over the referenced one
56
60
  const configuredInput = input || referenced.input;
57
61
  actorRef = interpreter.createActor(referenced.src, {
58
- id,
59
- src,
62
+ id: resolvedId,
63
+ src: typeof src === 'string' ? src : undefined,
60
64
  parent: actorContext?.self,
61
65
  systemId,
62
66
  input: typeof configuredInput === 'function' ? configuredInput({
@@ -75,23 +79,21 @@ function resolveInvoke(actorContext, state, actionArgs, {
75
79
  });
76
80
  }
77
81
  },
78
- error: () => {
79
- /* TODO */
80
- }
82
+ error: () => {}
81
83
  });
82
84
  }
83
85
  }
84
86
  return [cloneState(state, {
85
87
  children: {
86
88
  ...state.children,
87
- [id]: actorRef
89
+ [resolvedId]: actorRef
88
90
  }
89
91
  }), {
90
92
  id,
91
93
  actorRef
92
94
  }];
93
95
  }
94
- function executeInvoke(actorContext, {
96
+ function executeSpawn(actorContext, {
95
97
  id,
96
98
  actorRef
97
99
  }) {
@@ -110,33 +112,29 @@ function executeInvoke(actorContext, {
110
112
  }
111
113
  });
112
114
  }
113
-
114
- // we don't export this since it's an internal action that is not meant to be used in the user's code
115
-
116
- function invoke({
115
+ function spawn(...[src, {
117
116
  id,
118
117
  systemId,
119
- src,
120
118
  input,
121
- onSnapshot
122
- }) {
123
- function invoke(_) {
124
- }
125
- invoke.type = 'xstate.invoke';
126
- invoke.id = id;
127
- invoke.systemId = systemId;
128
- invoke.src = src;
129
- invoke.input = input;
130
- invoke.syncSnapshot = !!onSnapshot;
131
- invoke.resolve = resolveInvoke;
132
- invoke.execute = executeInvoke;
133
- return invoke;
119
+ syncSnapshot = false
120
+ } = {}]) {
121
+ function spawn(args, params) {
122
+ }
123
+ spawn.type = 'xstate.spawn';
124
+ spawn.id = id;
125
+ spawn.systemId = systemId;
126
+ spawn.src = src;
127
+ spawn.input = input;
128
+ spawn.syncSnapshot = syncSnapshot;
129
+ spawn.resolve = resolveSpawn;
130
+ spawn.execute = executeSpawn;
131
+ return spawn;
134
132
  }
135
133
 
136
- function resolveStop(_, state, args, {
134
+ function resolveStop(_, state, args, actionParams, {
137
135
  actorRef
138
136
  }) {
139
- const actorRefOrString = typeof actorRef === 'function' ? actorRef(args) : actorRef;
137
+ const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
140
138
  const resolvedActorRef = typeof actorRefOrString === 'string' ? state.children[actorRefOrString] : actorRefOrString;
141
139
  let children = state.children;
142
140
  if (resolvedActorRef) {
@@ -153,11 +151,22 @@ function executeStop(actorContext, actorRef) {
153
151
  if (!actorRef) {
154
152
  return;
155
153
  }
154
+
155
+ // we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
156
+ // since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
157
+ // this could throw on `systemId` collision, for example, when dealing with reentering transitions
158
+ actorContext.system._unregister(actorRef);
159
+
160
+ // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
161
+ // this can happen, for example, when the invoking state is being exited immediately by an always transition
156
162
  if (actorRef.status !== interpreter.ActorStatus.Running) {
157
163
  actorContext.stopChild(actorRef);
158
164
  return;
159
165
  }
160
- // TODO: recheck why this one has to be deferred
166
+ // stopping a child enqueues a stop event in the child actor's mailbox
167
+ // we need for all of the already enqueued events to be processed before we stop the child
168
+ // the parent itself might want to send some events to a child (for example from exit actions on the invoking state)
169
+ // and we don't want to ignore those events
161
170
  actorContext.defer(() => {
162
171
  actorContext.stopChild(actorRef);
163
172
  });
@@ -168,7 +177,7 @@ function executeStop(actorContext, actorRef) {
168
177
  * @param actorRef The actor to stop.
169
178
  */
170
179
  function stop(actorRef) {
171
- function stop(_) {
180
+ function stop(args, params) {
172
181
  }
173
182
  stop.type = 'xstate.stop';
174
183
  stop.actorRef = actorRef;
@@ -181,12 +190,13 @@ function checkStateIn(state, _, {
181
190
  stateValue
182
191
  }) {
183
192
  if (typeof stateValue === 'string' && isStateId(stateValue)) {
184
- return state.configuration.some(sn => sn.id === stateValue.slice(1));
193
+ const target = state.machine.getStateNodeById(stateValue);
194
+ return state.configuration.some(sn => sn === target);
185
195
  }
186
196
  return state.matches(stateValue);
187
197
  }
188
198
  function stateIn(stateValue) {
189
- function stateIn(_) {
199
+ function stateIn(args, params) {
190
200
  return false;
191
201
  }
192
202
  stateIn.check = checkStateIn;
@@ -202,7 +212,7 @@ function checkNot(state, {
202
212
  return !evaluateGuard(guards[0], context, event, state);
203
213
  }
204
214
  function not(guard) {
205
- function not(_) {
215
+ function not(args, params) {
206
216
  return false;
207
217
  }
208
218
  not.check = checkNot;
@@ -218,7 +228,7 @@ function checkAnd(state, {
218
228
  return guards.every(guard => evaluateGuard(guard, context, event, state));
219
229
  }
220
230
  function and(guards) {
221
- function and(_) {
231
+ function and(args, params) {
222
232
  return false;
223
233
  }
224
234
  and.check = checkAnd;
@@ -234,7 +244,7 @@ function checkOr(state, {
234
244
  return guards.some(guard => evaluateGuard(guard, context, event, state));
235
245
  }
236
246
  function or(guards) {
237
- function or(_) {
247
+ function or(args, params) {
238
248
  return false;
239
249
  }
240
250
  or.check = checkOr;
@@ -257,22 +267,17 @@ function evaluateGuard(guard, context, event, state) {
257
267
  }
258
268
  const guardArgs = {
259
269
  context,
260
- event,
261
- guard: isInline ? undefined : typeof guard === 'string' ? {
262
- type: guard
263
- } : typeof guard.params === 'function' ? {
264
- type: guard.type,
265
- params: guard.params({
266
- context,
267
- event
268
- })
269
- } : guard
270
+ event
270
271
  };
272
+ const guardParams = isInline || typeof guard === 'string' ? undefined : 'params' in guard ? typeof guard.params === 'function' ? guard.params({
273
+ context,
274
+ event
275
+ }) : guard.params : undefined;
271
276
  if (!('check' in resolved)) {
272
277
  // the existing type of `.guards` assumes non-nullable `TExpressionGuard`
273
278
  // inline guards expect `TExpressionGuard` to be set to `undefined`
274
279
  // it's fine to cast this here, our logic makes sure that we call those 2 "variants" correctly
275
- return resolved(guardArgs);
280
+ return resolved(guardArgs, guardParams);
276
281
  }
277
282
  const builtinGuard = resolved;
278
283
  return builtinGuard.check(state, guardArgs, resolved // this holds all params
@@ -285,6 +290,9 @@ function getChildren(stateNode) {
285
290
  }
286
291
  function getProperAncestors(stateNode, toStateNode) {
287
292
  const ancestors = [];
293
+ if (toStateNode === stateNode) {
294
+ return ancestors;
295
+ }
288
296
 
289
297
  // add all ancestors
290
298
  let m = stateNode.parent;
@@ -561,12 +569,14 @@ function resolveTarget(stateNode, targets) {
561
569
  }
562
570
  });
563
571
  }
564
- function resolveHistoryTarget(stateNode) {
572
+ function resolveHistoryDefaultTransition(stateNode) {
565
573
  const normalizedTarget = interpreter.normalizeTarget(stateNode.config.target);
566
574
  if (!normalizedTarget) {
567
- return stateNode.parent.initial.target;
575
+ return stateNode.parent.initial;
568
576
  }
569
- return normalizedTarget.map(t => typeof t === 'string' ? getStateNodeByPath(stateNode.parent, t) : t);
577
+ return {
578
+ target: normalizedTarget.map(t => typeof t === 'string' ? getStateNodeByPath(stateNode.parent, t) : t)
579
+ };
570
580
  }
571
581
  function isHistoryNode(stateNode) {
572
582
  return stateNode.type === 'history';
@@ -793,9 +803,7 @@ function getEffectiveTargetStates(transition, historyValue) {
793
803
  targets.add(node);
794
804
  }
795
805
  } else {
796
- for (const node of getEffectiveTargetStates({
797
- target: resolveHistoryTarget(targetNode)
798
- }, historyValue)) {
806
+ for (const node of getEffectiveTargetStates(resolveHistoryDefaultTransition(targetNode), historyValue)) {
799
807
  targets.add(node);
800
808
  }
801
809
  }
@@ -808,9 +816,9 @@ function getEffectiveTargetStates(transition, historyValue) {
808
816
  function getTransitionDomain(transition, historyValue) {
809
817
  const targetStates = getEffectiveTargetStates(transition, historyValue);
810
818
  if (!targetStates) {
811
- return null;
819
+ return;
812
820
  }
813
- if (!transition.reenter && transition.source.type !== 'parallel' && targetStates.every(targetStateNode => isDescendant(targetStateNode, transition.source))) {
821
+ if (!transition.reenter && targetStates.every(target => target === transition.source || isDescendant(target, transition.source))) {
814
822
  return transition.source;
815
823
  }
816
824
  const lcca = findLCCA(targetStates.concat(transition.source));
@@ -821,6 +829,9 @@ function computeExitSet(transitions, configuration, historyValue) {
821
829
  for (const t of transitions) {
822
830
  if (t.target?.length) {
823
831
  const domain = getTransitionDomain(t, historyValue);
832
+ if (t.reenter && t.source === domain) {
833
+ statesToExit.add(domain);
834
+ }
824
835
  for (const stateNode of configuration) {
825
836
  if (isDescendant(stateNode, domain)) {
826
837
  statesToExit.add(stateNode);
@@ -844,11 +855,6 @@ function areConfigurationsEqual(previousConfiguration, nextConfigurationSet) {
844
855
 
845
856
  /**
846
857
  * https://www.w3.org/TR/scxml/#microstepProcedure
847
- *
848
- * @private
849
- * @param transitions
850
- * @param currentState
851
- * @param mutConfiguration
852
858
  */
853
859
  function microstep(transitions, currentState, actorCtx, event, isInitial, internalQueue) {
854
860
  if (!transitions.length) {
@@ -897,6 +903,9 @@ function getMachineOutput(state, event, actorCtx, rootNode, rootCompletionNode)
897
903
  function enterStates(currentState, event, actorCtx, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial) {
898
904
  let nextState = currentState;
899
905
  const statesToEnter = new Set();
906
+ // those are states that were directly targeted or indirectly targeted by the explicit target
907
+ // in other words, those are states for which initial actions should be executed
908
+ // when we target `#deep_child` initial actions of its ancestors shouldn't be executed
900
909
  const statesForDefaultEntry = new Set();
901
910
  computeEntrySet(filteredTransitions, historyValue, statesForDefaultEntry, statesToEnter);
902
911
 
@@ -912,7 +921,10 @@ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConf
912
921
  // Add entry actions
913
922
  actions.push(...stateNodeToEnter.entry);
914
923
  for (const invokeDef of stateNodeToEnter.invoke) {
915
- actions.push(invoke(invokeDef));
924
+ actions.push(spawn(invokeDef.src, {
925
+ ...invokeDef,
926
+ syncSnapshot: !!invokeDef.onSnapshot
927
+ }));
916
928
  }
917
929
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
918
930
  const initialActions = stateNodeToEnter.initial.actions;
@@ -921,20 +933,16 @@ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConf
921
933
  nextState = resolveActionsAndContext(nextState, event, actorCtx, actions, internalQueue, stateNodeToEnter.invoke.map(invokeDef => invokeDef.id));
922
934
  if (stateNodeToEnter.type === 'final') {
923
935
  const parent = stateNodeToEnter.parent;
924
- if (completedNodes.has(parent)) {
925
- continue;
926
- }
927
- completedNodes.add(parent);
928
- let rootCompletionNode = parent?.type === 'parallel' ? parent : stateNodeToEnter;
929
- let ancestorMarker = parent?.parent;
930
- if (ancestorMarker) {
936
+ let ancestorMarker = parent?.type === 'parallel' ? parent : parent?.parent;
937
+ let rootCompletionNode = ancestorMarker || stateNodeToEnter;
938
+ if (parent?.type === 'compound') {
931
939
  internalQueue.push(interpreter.createDoneStateEvent(parent.id, stateNodeToEnter.output ? interpreter.resolveOutput(stateNodeToEnter.output, nextState.context, event, actorCtx.self) : undefined));
932
- while (ancestorMarker?.type === 'parallel' && !completedNodes.has(ancestorMarker) && isInFinalState(mutConfiguration, ancestorMarker)) {
933
- completedNodes.add(ancestorMarker);
934
- internalQueue.push(interpreter.createDoneStateEvent(ancestorMarker.id));
935
- rootCompletionNode = ancestorMarker;
936
- ancestorMarker = ancestorMarker.parent;
937
- }
940
+ }
941
+ while (ancestorMarker?.type === 'parallel' && !completedNodes.has(ancestorMarker) && isInFinalState(mutConfiguration, ancestorMarker)) {
942
+ completedNodes.add(ancestorMarker);
943
+ internalQueue.push(interpreter.createDoneStateEvent(ancestorMarker.id));
944
+ rootCompletionNode = ancestorMarker;
945
+ ancestorMarker = ancestorMarker.parent;
938
946
  }
939
947
  if (ancestorMarker) {
940
948
  continue;
@@ -949,13 +957,24 @@ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConf
949
957
  }
950
958
  function computeEntrySet(transitions, historyValue, statesForDefaultEntry, statesToEnter) {
951
959
  for (const t of transitions) {
960
+ const domain = getTransitionDomain(t, historyValue);
952
961
  for (const s of t.target || []) {
962
+ if (!isHistoryNode(s) && (
963
+ // if the target is different than the source then it will *definitely* be entered
964
+ t.source !== s ||
965
+ // we know that the domain can't lie within the source
966
+ // if it's different than the source then it's outside of it and it means that the target has to be entered as well
967
+ t.source !== domain ||
968
+ // reentering transitions always enter the target, even if it's the source itself
969
+ t.reenter)) {
970
+ statesToEnter.add(s);
971
+ statesForDefaultEntry.add(s);
972
+ }
953
973
  addDescendantStatesToEnter(s, historyValue, statesForDefaultEntry, statesToEnter);
954
974
  }
955
- const ancestor = getTransitionDomain(t, historyValue);
956
975
  const targetStates = getEffectiveTargetStates(t, historyValue);
957
976
  for (const s of targetStates) {
958
- addAncestorStatesToEnter(s, ancestor, statesToEnter, historyValue, statesForDefaultEntry);
977
+ addAncestorStatesToEnter(s, domain, statesToEnter, historyValue, statesForDefaultEntry);
959
978
  }
960
979
  }
961
980
  }
@@ -964,37 +983,42 @@ function addDescendantStatesToEnter(stateNode, historyValue, statesForDefaultEnt
964
983
  if (historyValue[stateNode.id]) {
965
984
  const historyStateNodes = historyValue[stateNode.id];
966
985
  for (const s of historyStateNodes) {
986
+ statesToEnter.add(s);
967
987
  addDescendantStatesToEnter(s, historyValue, statesForDefaultEntry, statesToEnter);
968
988
  }
969
989
  for (const s of historyStateNodes) {
970
990
  addAncestorStatesToEnter(s, stateNode.parent, statesToEnter, historyValue, statesForDefaultEntry);
971
- for (const stateForDefaultEntry of statesForDefaultEntry) {
972
- statesForDefaultEntry.add(stateForDefaultEntry);
973
- }
974
991
  }
975
992
  } else {
976
- const targets = resolveHistoryTarget(stateNode);
977
- for (const s of targets) {
993
+ const historyDefaultTransition = resolveHistoryDefaultTransition(stateNode);
994
+ for (const s of historyDefaultTransition.target) {
995
+ statesToEnter.add(s);
996
+ if (historyDefaultTransition === stateNode.parent?.initial) {
997
+ statesForDefaultEntry.add(stateNode.parent);
998
+ }
978
999
  addDescendantStatesToEnter(s, historyValue, statesForDefaultEntry, statesToEnter);
979
1000
  }
980
- for (const s of targets) {
1001
+ for (const s of historyDefaultTransition.target) {
981
1002
  addAncestorStatesToEnter(s, stateNode, statesToEnter, historyValue, statesForDefaultEntry);
982
- for (const stateForDefaultEntry of statesForDefaultEntry) {
983
- statesForDefaultEntry.add(stateForDefaultEntry);
984
- }
985
1003
  }
986
1004
  }
987
1005
  } else {
988
- statesToEnter.add(stateNode);
989
1006
  if (stateNode.type === 'compound') {
990
- statesForDefaultEntry.add(stateNode);
991
1007
  const [initialState] = stateNode.initial.target;
1008
+ if (!isHistoryNode(initialState)) {
1009
+ statesToEnter.add(initialState);
1010
+ statesForDefaultEntry.add(initialState);
1011
+ }
992
1012
  addDescendantStatesToEnter(initialState, historyValue, statesForDefaultEntry, statesToEnter);
993
1013
  addAncestorStatesToEnter(initialState, stateNode, statesToEnter, historyValue, statesForDefaultEntry);
994
1014
  } else {
995
1015
  if (stateNode.type === 'parallel') {
996
1016
  for (const child of getChildren(stateNode).filter(sn => !isHistoryNode(sn))) {
997
1017
  if (![...statesToEnter].some(s => isDescendant(s, child))) {
1018
+ if (!isHistoryNode(child)) {
1019
+ statesToEnter.add(child);
1020
+ statesForDefaultEntry.add(child);
1021
+ }
998
1022
  addDescendantStatesToEnter(child, historyValue, statesForDefaultEntry, statesToEnter);
999
1023
  }
1000
1024
  }
@@ -1009,6 +1033,7 @@ function addAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, history
1009
1033
  if (anc.type === 'parallel') {
1010
1034
  for (const child of getChildren(anc).filter(sn => !isHistoryNode(sn))) {
1011
1035
  if (![...statesToEnter].some(s => isDescendant(s, child))) {
1036
+ statesToEnter.add(child);
1012
1037
  addDescendantStatesToEnter(child, historyValue, statesForDefaultEntry, statesToEnter);
1013
1038
  }
1014
1039
  }
@@ -1063,31 +1088,24 @@ function resolveActionsAndContextWorker(currentState, event, actorCtx, actions,
1063
1088
  context: intermediateState.context,
1064
1089
  event,
1065
1090
  self: actorCtx?.self,
1066
- system: actorCtx?.system,
1067
- action: isInline ? undefined : typeof action === 'string' ? {
1068
- type: action
1069
- } : typeof action.params === 'function' ? {
1070
- type: action.type,
1071
- params: action.params({
1072
- context: intermediateState.context,
1073
- event
1074
- })
1075
- } :
1076
- // TS isn't able to narrow it down here
1077
- action
1091
+ system: actorCtx?.system
1078
1092
  };
1093
+ const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
1094
+ context: intermediateState.context,
1095
+ event
1096
+ }) : action.params : undefined;
1079
1097
  if (!('resolve' in resolvedAction)) {
1080
1098
  if (actorCtx?.self.status === interpreter.ActorStatus.Running) {
1081
- resolvedAction(actionArgs);
1099
+ resolvedAction(actionArgs, actionParams);
1082
1100
  } else {
1083
1101
  actorCtx?.defer(() => {
1084
- resolvedAction(actionArgs);
1102
+ resolvedAction(actionArgs, actionParams);
1085
1103
  });
1086
1104
  }
1087
1105
  continue;
1088
1106
  }
1089
1107
  const builtinAction = resolvedAction;
1090
- const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolvedAction,
1108
+ const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, actionParams, resolvedAction,
1091
1109
  // this holds all params
1092
1110
  extra);
1093
1111
  intermediateState = nextState;
@@ -1124,7 +1142,9 @@ function macrostep(state, event, actorCtx, internalQueue = []) {
1124
1142
 
1125
1143
  // Handle stop event
1126
1144
  if (event.type === interpreter.XSTATE_STOP) {
1127
- nextState = stopChildren(nextState, event, actorCtx);
1145
+ nextState = cloneState(stopChildren(nextState, event, actorCtx), {
1146
+ status: 'stopped'
1147
+ });
1128
1148
  states.push(nextState);
1129
1149
  return {
1130
1150
  state: nextState,
@@ -1176,7 +1196,7 @@ function selectEventlessTransitions(nextState, event) {
1176
1196
  const enabledTransitionSet = new Set();
1177
1197
  const atomicStates = nextState.configuration.filter(isAtomicStateNode);
1178
1198
  for (const stateNode of atomicStates) {
1179
- loop: for (const s of [stateNode].concat(getProperAncestors(stateNode, null))) {
1199
+ loop: for (const s of [stateNode].concat(getProperAncestors(stateNode, undefined))) {
1180
1200
  if (!s.always) {
1181
1201
  continue;
1182
1202
  }
@@ -1381,17 +1401,17 @@ function getPersistedState(state) {
1381
1401
  for (const id in children) {
1382
1402
  const child = children[id];
1383
1403
  childrenJson[id] = {
1384
- state: child.getPersistedState?.(),
1385
- src: child.src
1404
+ state: child.getPersistedState(),
1405
+ src: child.src,
1406
+ systemId: child._systemId
1386
1407
  };
1387
1408
  }
1388
- return {
1409
+ const persisted = {
1389
1410
  ...jsonValues,
1390
- // TODO: this makes `PersistedMachineState`'s type kind of a lie
1391
- // it doesn't truly use `TContext` but rather some kind of a derived form of it
1392
1411
  context: persistContext(context),
1393
1412
  children: childrenJson
1394
1413
  };
1414
+ return persisted;
1395
1415
  }
1396
1416
  function persistContext(contextPart) {
1397
1417
  let copy;
@@ -1420,7 +1440,7 @@ function persistContext(contextPart) {
1420
1440
  return copy ?? contextPart;
1421
1441
  }
1422
1442
 
1423
- function resolveRaise(_, state, args, {
1443
+ function resolveRaise(_, state, args, actionParams, {
1424
1444
  event: eventOrExpr,
1425
1445
  id,
1426
1446
  delay
@@ -1431,13 +1451,13 @@ function resolveRaise(_, state, args, {
1431
1451
  if (typeof eventOrExpr === 'string') {
1432
1452
  throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
1433
1453
  }
1434
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args) : eventOrExpr;
1454
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
1435
1455
  let resolvedDelay;
1436
1456
  if (typeof delay === 'string') {
1437
1457
  const configDelay = delaysMap && delaysMap[delay];
1438
- resolvedDelay = typeof configDelay === 'function' ? configDelay(args) : configDelay;
1458
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
1439
1459
  } else {
1440
- resolvedDelay = typeof delay === 'function' ? delay(args) : delay;
1460
+ resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
1441
1461
  }
1442
1462
  if (typeof resolvedDelay !== 'number') {
1443
1463
  internalQueue.push(resolvedEvent);
@@ -1461,7 +1481,7 @@ function executeRaise(actorContext, params) {
1461
1481
  * @param eventType The event to raise.
1462
1482
  */
1463
1483
  function raise(eventOrExpr, options) {
1464
- function raise(_) {
1484
+ function raise(args, params) {
1465
1485
  }
1466
1486
  raise.type = 'xstate.raise';
1467
1487
  raise.event = eventOrExpr;
@@ -1498,6 +1518,7 @@ exports.or = or;
1498
1518
  exports.raise = raise;
1499
1519
  exports.resolveActionsAndContext = resolveActionsAndContext;
1500
1520
  exports.resolveStateValue = resolveStateValue;
1521
+ exports.spawn = spawn;
1501
1522
  exports.stateIn = stateIn;
1502
1523
  exports.stop = stop;
1503
1524
  exports.transitionNode = transitionNode;