xstate 6.0.0-alpha.64 → 6.0.0-alpha.66

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/README.md +27 -0
  2. package/dist/{createActor-8493caa9.esm.js → createActor-18a56099.esm.js} +255 -209
  3. package/dist/{createActor-29adb8a4.cjs.js → createActor-19ee5e3c.cjs.js} +255 -209
  4. package/dist/{createActor-bd2527dc.development.esm.js → createActor-57ac74ac.development.esm.js} +287 -208
  5. package/dist/{createActor-521c8805.development.cjs.js → createActor-ff6a685e.development.cjs.js} +287 -208
  6. package/dist/declarations/src/State.d.ts +25 -3
  7. package/dist/declarations/src/StateMachine.d.ts +19 -7
  8. package/dist/declarations/src/actors/promise.d.ts +5 -5
  9. package/dist/declarations/src/createMachine.d.ts +5 -3
  10. package/dist/declarations/src/createMachineFromConfig.d.ts +13 -2
  11. package/dist/declarations/src/eventTypes.d.ts +8 -0
  12. package/dist/declarations/src/graph/index.d.ts +1 -0
  13. package/dist/declarations/src/graph/types.d.ts +20 -2
  14. package/dist/declarations/src/index.d.ts +1 -1
  15. package/dist/declarations/src/machineVersions.d.ts +2 -2
  16. package/dist/declarations/src/serialize.d.ts +7 -2
  17. package/dist/declarations/src/setup.d.ts +60 -19
  18. package/dist/declarations/src/transitionActions.d.ts +1 -1
  19. package/dist/declarations/src/types.d.ts +48 -27
  20. package/dist/declarations/src/types.v6.d.ts +10 -7
  21. package/dist/declarations/src/utils.d.ts +9 -0
  22. package/dist/{effectDescriptor-6dbc400a.cjs.js → effectDescriptor-515bb8bd.cjs.js} +1 -1
  23. package/dist/{effectDescriptor-85ea06c9.esm.js → effectDescriptor-c96275e6.esm.js} +1 -1
  24. package/dist/{effectDescriptor-62b53c6d.development.cjs.js → effectDescriptor-d941b462.development.cjs.js} +1 -1
  25. package/dist/{effectDescriptor-f76948fc.development.esm.js → effectDescriptor-f87ea294.development.esm.js} +1 -1
  26. package/dist/{index-5587e13b.cjs.js → index-66c07691.development.cjs.js} +29 -14
  27. package/dist/{index-2afc57c3.development.cjs.js → index-72e3c72c.cjs.js} +29 -14
  28. package/dist/{index-b0dc2b39.esm.js → index-b0b89ca7.development.esm.js} +29 -14
  29. package/dist/{index-90ebf5ae.development.esm.js → index-e540ddba.esm.js} +29 -14
  30. package/dist/xstate-actors.cjs.js +2 -2
  31. package/dist/xstate-actors.development.cjs.js +2 -2
  32. package/dist/xstate-actors.development.esm.js +2 -2
  33. package/dist/xstate-actors.esm.js +2 -2
  34. package/dist/xstate-actors.umd.min.js +1 -1
  35. package/dist/xstate-actors.umd.min.js.map +1 -1
  36. package/dist/xstate-durable.cjs.js +2 -2
  37. package/dist/xstate-durable.development.cjs.js +2 -2
  38. package/dist/xstate-durable.development.esm.js +2 -2
  39. package/dist/xstate-durable.esm.js +2 -2
  40. package/dist/xstate-durable.umd.min.js +1 -1
  41. package/dist/xstate-durable.umd.min.js.map +1 -1
  42. package/dist/xstate-graph.cjs.js +8 -4
  43. package/dist/xstate-graph.cjs.mjs +1 -0
  44. package/dist/xstate-graph.development.cjs.js +8 -4
  45. package/dist/xstate-graph.development.cjs.mjs +1 -0
  46. package/dist/xstate-graph.development.esm.js +8 -4
  47. package/dist/xstate-graph.esm.js +8 -4
  48. package/dist/xstate-graph.umd.min.js +1 -1
  49. package/dist/xstate-graph.umd.min.js.map +1 -1
  50. package/dist/xstate-validation.cjs.js +24 -7
  51. package/dist/xstate-validation.development.cjs.js +24 -7
  52. package/dist/xstate-validation.development.esm.js +24 -7
  53. package/dist/xstate-validation.esm.js +24 -7
  54. package/dist/xstate-validation.umd.min.js +1 -1
  55. package/dist/xstate-validation.umd.min.js.map +1 -1
  56. package/dist/xstate.cjs.js +475 -265
  57. package/dist/xstate.development.cjs.js +497 -268
  58. package/dist/xstate.development.esm.js +499 -270
  59. package/dist/xstate.esm.js +477 -267
  60. package/dist/xstate.umd.min.js +1 -1
  61. package/dist/xstate.umd.min.js.map +1 -1
  62. package/package.json +1 -1
@@ -451,83 +451,9 @@ function createSubscriptionLogic() {
451
451
  /** @public */
452
452
  const subscriptionLogic = /* #__PURE__ */createSubscriptionLogic();
453
453
 
454
- /** Marks the internal lazy scope without imposing structure on custom runtimes. @internal */
455
- const lazyActorScope = Symbol();
456
- /** Whether actor capabilities are represented by a lazy pure-transition scope. @internal */
457
- function isLazyActorScope(actorScope) {
458
- return !!actorScope[lazyActorScope];
459
- }
460
-
461
- /** Reads parent metadata without otherwise touching actor capabilities. @internal */
462
- function getActorScopeParent(actorScope) {
463
- return isLazyActorScope(actorScope) ? actorScope._parent : actorScope.self._parent;
464
- }
465
-
466
- /** Adds only `self`, preserving callback surfaces that expose no other capabilities. @internal */
467
- function withActorSelf(args, actorScope) {
468
- if (!isLazyActorScope(actorScope)) {
469
- return Object.assign(args, {
470
- self: actorScope.self
471
- });
472
- }
473
- Object.defineProperty(args, 'self', {
474
- enumerable: true,
475
- get: () => actorScope.self
476
- });
477
- return args;
478
- }
479
-
480
- /** Adds the actor reference and parent without exposing the system. @internal */
481
- function withActorSelfAndParent(args, actorScope) {
482
- if (!isLazyActorScope(actorScope)) {
483
- const self = actorScope.self;
484
- return Object.assign(args, {
485
- self,
486
- parent: self._parent
487
- });
488
- }
489
- Object.defineProperties(args, {
490
- self: {
491
- enumerable: true,
492
- get: () => actorScope.self
493
- },
494
- parent: {
495
- enumerable: true,
496
- get: () => getActorScopeParent(actorScope)
497
- }
498
- });
499
- return args;
500
- }
501
-
502
- /** Adds actor capabilities without reading them until the callback does. @internal */
503
- function withActorScope(args, actorScope) {
504
- if (!isLazyActorScope(actorScope)) {
505
- const actorArgs = args;
506
- const self = actorScope.self;
507
- actorArgs.self = self;
508
- actorArgs.system = actorScope.system;
509
- actorArgs.parent = self._parent;
510
- return actorArgs;
511
- }
512
- Object.defineProperties(args, {
513
- self: {
514
- enumerable: true,
515
- get: () => actorScope.self
516
- },
517
- system: {
518
- enumerable: true,
519
- get: () => actorScope.system
520
- },
521
- parent: {
522
- enumerable: true,
523
- get: () => getActorScopeParent(actorScope)
524
- }
525
- });
526
- return args;
527
- }
528
-
529
454
  function execCustomEffect() {
530
- return this.action?.(...this.args);
455
+ const result = this.action?.(...this.args);
456
+ return result;
531
457
  }
532
458
  function execEmitEffect(runtime = this.source.system) {
533
459
  return runtime.emitEvent(this.source, this.event);
@@ -1049,6 +975,13 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
1049
975
  props[key] = guard(props[key]);
1050
976
  }
1051
977
  const enqueue = createEnqueueObject(props, guard((action, ...args) => {
978
+ // A named action is `undefined` when it has no implementation. Skip it:
979
+ // an `{ action, args }` record without a function would be emitted as
980
+ // an event. Speculative passes (`can()`, conflict resolution) run with
981
+ // `createActors: false`; warn only when the transition really runs.
982
+ if (typeof action !== 'function') {
983
+ return;
984
+ }
1052
985
  pushBuiltInAction(actions, action, ...args);
1053
986
  }));
1054
987
  enqueueClosers.set(enqueue, () => {
@@ -1308,28 +1241,10 @@ async function executeEffects(effects, runtime) {
1308
1241
  await effect.exec(runtime);
1309
1242
  }
1310
1243
  }
1311
- function resolveActionsWithContext(currentSnapshot, event, actorScope, actions) {
1244
+ function resolveActionsWithContext(currentSnapshot, _event, actorScope, actions) {
1312
1245
  let intermediateSnapshot = currentSnapshot;
1313
1246
  const executableActions = [];
1314
1247
  for (const action of actions) {
1315
- const actionArgs = isLazyActorScope(actorScope) ? withActorScope({
1316
- context: intermediateSnapshot.context,
1317
- event,
1318
- output: getEventOutput(event),
1319
- children: intermediateSnapshot.children,
1320
- actions: currentSnapshot.machine.sources.actions,
1321
- actors: currentSnapshot.machine.sources.actors
1322
- }, actorScope) : {
1323
- context: intermediateSnapshot.context,
1324
- event,
1325
- output: getEventOutput(event),
1326
- self: actorScope.self,
1327
- system: actorScope.system,
1328
- parent: actorScope.self._parent,
1329
- children: intermediateSnapshot.children,
1330
- actions: currentSnapshot.machine.sources.actions,
1331
- actors: currentSnapshot.machine.sources.actors
1332
- };
1333
1248
  const isInline = typeof action === 'function';
1334
1249
  const actionRecord = getTransitionActionRecord(action);
1335
1250
  let resolvedActionArgs = actionRecord?.args;
@@ -1357,33 +1272,6 @@ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions)
1357
1272
  if (actionRecord?.childUpdate) {
1358
1273
  intermediateSnapshot = applyChildUpdate(intermediateSnapshot, actionRecord.childUpdate, actorScope);
1359
1274
  }
1360
- if (resolvedAction && '_special' in resolvedAction) {
1361
- executableActions.push({
1362
- kind: 'action',
1363
- exec: execCustomEffect,
1364
- type: typeof action === 'object' ? 'action' in action && typeof action.action === 'function' ? action.action.name ?? '(anonymous)' : action.type ?? '(anonymous)' : action.name || '(anonymous)',
1365
- params: actionParams,
1366
- args: [],
1367
- action: undefined
1368
- });
1369
- const specialAction = resolvedAction;
1370
- const res = specialAction(actionArgs, emptyEnqueueObject);
1371
- if (res && ('context' in res || 'children' in res)) {
1372
- // Special-action patches never change `nodes`, so a shallow clone is
1373
- // equivalent to `cloneMachineSnapshot` — and keeps this module (and
1374
- // non-machine logic like `createFSM`) independent of State.ts.
1375
- intermediateSnapshot = {
1376
- ...intermediateSnapshot,
1377
- ...(res.context !== undefined ? {
1378
- context: mergeContextPatch(intermediateSnapshot.context, res.context)
1379
- } : {}),
1380
- ...('children' in res ? {
1381
- children: res.children
1382
- } : {})
1383
- };
1384
- }
1385
- continue;
1386
- }
1387
1275
  if (!resolvedAction || !('resolve' in resolvedAction)) {
1388
1276
  const builtInFields = typeof action === 'object' && action !== null && 'action' in action && typeof action.action === 'function' ? getBuiltInActionFields(action.action, resolvedActionArgs) : undefined;
1389
1277
  const isEmittedEvent = typeof action === 'object' && action !== null && !actionRecord;
@@ -1431,7 +1319,6 @@ function createEnqueueObject(props, action) {
1431
1319
  }
1432
1320
  const noop = () => {};
1433
1321
  const emptyActor = () => ({});
1434
- const emptyEnqueueObject = createEnqueueObject({}, noop);
1435
1322
 
1436
1323
  function parseDurationToMilliseconds(duration) {
1437
1324
  const normalizedDuration = duration.trim();
@@ -1763,6 +1650,81 @@ function setSnapshotActorRef(snapshot, actor, baseSystem = actor.system, previou
1763
1650
  });
1764
1651
  }
1765
1652
 
1653
+ /** Marks the internal lazy scope without imposing structure on custom runtimes. @internal */
1654
+ const lazyActorScope = Symbol();
1655
+ /** Whether actor capabilities are represented by a lazy pure-transition scope. @internal */
1656
+ function isLazyActorScope(actorScope) {
1657
+ return !!actorScope[lazyActorScope];
1658
+ }
1659
+
1660
+ /** Reads parent metadata without otherwise touching actor capabilities. @internal */
1661
+ function getActorScopeParent(actorScope) {
1662
+ return isLazyActorScope(actorScope) ? actorScope._parent : actorScope.self._parent;
1663
+ }
1664
+
1665
+ /** Adds only `self`, preserving callback surfaces that expose no other capabilities. @internal */
1666
+ function withActorSelf(args, actorScope) {
1667
+ if (!isLazyActorScope(actorScope)) {
1668
+ return Object.assign(args, {
1669
+ self: actorScope.self
1670
+ });
1671
+ }
1672
+ Object.defineProperty(args, 'self', {
1673
+ enumerable: true,
1674
+ get: () => actorScope.self
1675
+ });
1676
+ return args;
1677
+ }
1678
+
1679
+ /** Adds the actor reference and parent without exposing the system. @internal */
1680
+ function withActorSelfAndParent(args, actorScope) {
1681
+ if (!isLazyActorScope(actorScope)) {
1682
+ const self = actorScope.self;
1683
+ return Object.assign(args, {
1684
+ self,
1685
+ parent: self._parent
1686
+ });
1687
+ }
1688
+ Object.defineProperties(args, {
1689
+ self: {
1690
+ enumerable: true,
1691
+ get: () => actorScope.self
1692
+ },
1693
+ parent: {
1694
+ enumerable: true,
1695
+ get: () => getActorScopeParent(actorScope)
1696
+ }
1697
+ });
1698
+ return args;
1699
+ }
1700
+
1701
+ /** Adds actor capabilities without reading them until the callback does. @internal */
1702
+ function withActorScope(args, actorScope) {
1703
+ if (!isLazyActorScope(actorScope)) {
1704
+ const actorArgs = args;
1705
+ const self = actorScope.self;
1706
+ actorArgs.self = self;
1707
+ actorArgs.system = actorScope.system;
1708
+ actorArgs.parent = self._parent;
1709
+ return actorArgs;
1710
+ }
1711
+ Object.defineProperties(args, {
1712
+ self: {
1713
+ enumerable: true,
1714
+ get: () => actorScope.self
1715
+ },
1716
+ system: {
1717
+ enumerable: true,
1718
+ get: () => actorScope.system
1719
+ },
1720
+ parent: {
1721
+ enumerable: true,
1722
+ get: () => getActorScopeParent(actorScope)
1723
+ }
1724
+ });
1725
+ return args;
1726
+ }
1727
+
1766
1728
  /** @internal */
1767
1729
  function setInertActorScopeSnapshot(actorScope, snapshot, attachActorRef = true) {
1768
1730
  const lazyState = getLazyInertActorState(actorScope);
@@ -2266,6 +2228,10 @@ function getDelayedTransitions(stateNode) {
2266
2228
  }));
2267
2229
  }
2268
2230
  function formatTransition(stateNode, descriptor, transitionConfig) {
2231
+ const removedKey = getRemovedTransitionKey(transitionConfig);
2232
+ if (removedKey) {
2233
+ throw new Error(`Transition "${descriptor || 'always'}" in state "${stateNode.id}" uses ${removedKey}, which was removed in v6. Use a transition function.`);
2234
+ }
2269
2235
  const normalizedTarget = normalizeTarget(transitionConfig.target);
2270
2236
  const reenter = transitionConfig.reenter ?? false;
2271
2237
  const target = resolveTarget(stateNode, normalizedTarget);
@@ -2276,14 +2242,46 @@ function formatTransition(stateNode, descriptor, transitionConfig) {
2276
2242
  source: stateNode,
2277
2243
  reenter,
2278
2244
  eventType: descriptor,
2279
- toJSON: () => ({
2280
- ...transition,
2281
- source: `#${stateNode.id}`,
2282
- target: target ? target.map(t => `#${t.id}`) : undefined
2283
- })
2245
+ toJSON: () => transitionDefinitionToJSON(transition)
2284
2246
  };
2285
2247
  return transition;
2286
2248
  }
2249
+ function transitionDefinitionToJSON(transition) {
2250
+ const {
2251
+ definition: _,
2252
+ ...serializableTransition
2253
+ } = transition;
2254
+ return {
2255
+ ...serializableTransition,
2256
+ source: `#${transition.source.id}`,
2257
+ target: transition.target?.map(target => `#${target.id}`)
2258
+ };
2259
+ }
2260
+
2261
+ /**
2262
+ * Returns the v5 key of a transition object that v6 would misread, in every
2263
+ * build: `cond` is never read, so the transition would always be taken,
2264
+ * transition `actions` would not run as written, and only a function `guard`
2265
+ * can be called. Development builds explain each key earlier, in
2266
+ * `diagnoseAuthorConfig`. Compiled configs (JSON, SCXML) never set them.
2267
+ */
2268
+ function getRemovedTransitionKey(transitionConfig) {
2269
+ const {
2270
+ cond,
2271
+ actions,
2272
+ guard
2273
+ } = transitionConfig;
2274
+ if (cond !== undefined) {
2275
+ return '"cond"';
2276
+ }
2277
+ if (actions !== undefined) {
2278
+ return '"actions"';
2279
+ }
2280
+ if (guard !== undefined && typeof guard !== 'function') {
2281
+ return 'an object-form "guard"';
2282
+ }
2283
+ return undefined;
2284
+ }
2287
2285
  function isStateNodeDescendantOf(stateNode, ancestor) {
2288
2286
  for (let current = stateNode.parent; current; current = current.parent) {
2289
2287
  if (current === ancestor) {
@@ -2626,12 +2624,18 @@ function removeConflictingTransitions(enabledTransitions, stateNodeSet, snapshot
2626
2624
  }
2627
2625
  function createTransitionResultResolver(snapshot, event, actorScope, resolveActions, selectionResults) {
2628
2626
  let cache;
2629
- return transition => {
2630
- if (!transition.to) {
2631
- return getTransitionResult(transition, snapshot, event, actorScope, {
2632
- resolveActions,
2633
- selectionResult: selectionResults?.get(transition)
2634
- });
2627
+ return (transition, applied) => {
2628
+ if (!transition.to && !applied) {
2629
+ // An object transition's targets are static, so reading them must not
2630
+ // run its `context` and `input` mappers
2631
+ return {
2632
+ targets: transition.target,
2633
+ context: undefined,
2634
+ reenter: transition.reenter,
2635
+ actions: undefined,
2636
+ internalEvents: undefined,
2637
+ input: undefined
2638
+ };
2635
2639
  }
2636
2640
  let result = cache?.get(transition);
2637
2641
  if (!result) {
@@ -2807,68 +2811,47 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2807
2811
  return !!targets?.length || !!reenter;
2808
2812
  });
2809
2813
  const getStateActionsAndContext = (transitionFn, context, children, input, kind, stateNode) => {
2810
- if (transitionFn.length === 2) {
2811
- // enqueue action; retrieve
2812
- const actions = [];
2813
- const internalEvents = [];
2814
- let updatedContext;
2815
- const enqueue = createTransitionEnqueue(actorScope, actions, internalEvents, true);
2816
- const args = isLazyActorScope(actorScope) ? withActorScope({
2817
- context,
2818
- event,
2819
- children,
2820
- actions: currentSnapshot.machine.sources.actions,
2821
- actors: currentSnapshot.machine.sources.actors,
2822
- guards: currentSnapshot.machine.sources.guards,
2823
- delays: currentSnapshot.machine.sources.delays,
2824
- input,
2825
- stateNode
2826
- }, actorScope) : {
2827
- context,
2828
- event,
2829
- parent: actorScope.self._parent,
2830
- self: actorScope.self,
2831
- children,
2832
- system: actorScope.system,
2833
- actions: currentSnapshot.machine.sources.actions,
2834
- actors: currentSnapshot.machine.sources.actors,
2835
- guards: currentSnapshot.machine.sources.guards,
2836
- delays: currentSnapshot.machine.sources.delays,
2837
- input,
2838
- stateNode
2839
- };
2840
- let res;
2841
- try {
2842
- res = transitionFn(args, enqueue);
2843
- } finally {
2844
- closeTransitionEnqueue(enqueue);
2845
- }
2846
- assertSyncTransitionResult(res, event, stateNode.id, kind);
2847
- if (res?.context !== undefined) {
2848
- updatedContext = mergeContextPatch(context, res.context);
2849
- }
2850
- return [actions, updatedContext, internalEvents];
2851
- }
2852
-
2853
- // For 1-argument actions, wrap them to include input
2854
- // Preserve _special flag if present (for entry/exit actions)
2855
- const wrappedAction = Object.assign((args, enqueue) => transitionFn(isLazyActorScope(actorScope) ? withActorScope({
2856
- context: args.context,
2857
- event: args.event,
2858
- output: args.output,
2859
- children: args.children,
2860
- actions: args.actions,
2861
- actors: args.actors,
2814
+ const actions = [];
2815
+ const internalEvents = [];
2816
+ let updatedContext;
2817
+ const enqueue = createTransitionEnqueue(actorScope, actions, internalEvents, true);
2818
+ const args = isLazyActorScope(actorScope) ? withActorScope({
2819
+ context,
2820
+ event,
2821
+ output: getEventOutput(event),
2822
+ children,
2823
+ actions: currentSnapshot.machine.sources.actions,
2824
+ actors: currentSnapshot.machine.sources.actors,
2825
+ guards: currentSnapshot.machine.sources.guards,
2826
+ delays: currentSnapshot.machine.sources.delays,
2862
2827
  input,
2863
2828
  stateNode
2864
2829
  }, actorScope) : {
2865
- ...args,
2830
+ context,
2831
+ event,
2832
+ output: getEventOutput(event),
2833
+ parent: actorScope.self._parent,
2834
+ self: actorScope.self,
2835
+ children,
2836
+ system: actorScope.system,
2837
+ actions: currentSnapshot.machine.sources.actions,
2838
+ actors: currentSnapshot.machine.sources.actors,
2839
+ guards: currentSnapshot.machine.sources.guards,
2840
+ delays: currentSnapshot.machine.sources.delays,
2866
2841
  input,
2867
2842
  stateNode
2868
- }, enqueue), '_special' in transitionFn ? {
2869
- _special: true
2870
- } : {});
2871
- return [[wrappedAction], undefined, undefined];
2843
+ };
2844
+ let res;
2845
+ try {
2846
+ res = transitionFn(args, enqueue);
2847
+ } finally {
2848
+ closeTransitionEnqueue(enqueue);
2849
+ }
2850
+ assertSyncTransitionResult(res, event, stateNode.id, kind);
2851
+ if (res?.context !== undefined) {
2852
+ updatedContext = mergeContextPatch(context, res.context);
2853
+ }
2854
+ return [actions, updatedContext, internalEvents, res?.children];
2872
2855
  };
2873
2856
  let nextState = currentSnapshot;
2874
2857
  const getInvokeStopActions = (stateNode, children) => {
@@ -2903,13 +2886,18 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2903
2886
  }
2904
2887
  for (const exitStateNode of statesToExit) {
2905
2888
  const stateInput = getStateInput(currentSnapshot, exitStateNode.id);
2906
- const [exitActions, nextContext, internalEvents] = exitStateNode.exit ? getStateActionsAndContext(exitStateNode.exit, nextState.context, currentSnapshot.children, stateInput, 'exit', exitStateNode) : [[], undefined, undefined];
2889
+ const [exitActions, nextContext, internalEvents, nextChildren] = exitStateNode.exit ? getStateActionsAndContext(exitStateNode.exit, nextState.context, nextState.children, stateInput, 'exit', exitStateNode) : [[], undefined, undefined, undefined];
2907
2890
  if (internalEvents?.length) {
2908
2891
  internalQueue.push(...internalEvents);
2909
2892
  }
2910
- if (nextContext) {
2893
+ if (nextContext || nextChildren) {
2911
2894
  nextState = cloneMachineSnapshot(nextState, {
2912
- context: nextContext
2895
+ ...(nextContext ? {
2896
+ context: nextContext
2897
+ } : {}),
2898
+ ...(nextChildren ? {
2899
+ children: nextChildren
2900
+ } : {})
2913
2901
  });
2914
2902
  }
2915
2903
  const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, exitActions);
@@ -2933,11 +2921,20 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2933
2921
  let context = nextState.context;
2934
2922
  const transitionActions = [];
2935
2923
  const internalEvents = [];
2924
+ const resolvedTransitions = [];
2936
2925
  for (const t of filteredTransitions) {
2937
- if (t.actions) {
2938
- transitionActions.push(...toArray(t.actions));
2926
+ const res = getCurrentTransitionResult(t, true);
2927
+ if (t.to) {
2928
+ const resolvedTransition = {
2929
+ ...t,
2930
+ target: res.targets,
2931
+ definition: t
2932
+ };
2933
+ resolvedTransition.toJSON = () => transitionDefinitionToJSON(resolvedTransition);
2934
+ resolvedTransitions.push(resolvedTransition);
2935
+ } else {
2936
+ resolvedTransitions.push(t);
2939
2937
  }
2940
- const res = getCurrentTransitionResult(t);
2941
2938
  if (res.context !== undefined) {
2942
2939
  context = mergeContextPatch(context, res.context);
2943
2940
  }
@@ -3093,7 +3090,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3093
3090
  const {
3094
3091
  targets,
3095
3092
  input
3096
- } = getCurrentTransitionResult(transition);
3093
+ } = getCurrentTransitionResult(transition, true);
3097
3094
  if (input && targets) {
3098
3095
  for (const targetNode of targets.filter(targetNode => enteredTargetsByTransition.get(transition)?.has(targetNode))) {
3099
3096
  stateInputMap[targetNode.id] = input;
@@ -3102,12 +3099,12 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3102
3099
  }
3103
3100
  }
3104
3101
  const completedNodes = new Set();
3105
- const children = {
3106
- ...nextState.children
3107
- };
3108
3102
  for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
3109
3103
  mutStateNodeSet.add(stateNodeToEnter);
3110
3104
  const actions = [];
3105
+ // Read the children for each state: the entry actions of the states
3106
+ // entered before this one may have spawned or stopped children.
3107
+ let children = nextState.children;
3111
3108
 
3112
3109
  // Final states are inert, so (as in SCXML) their invocations never
3113
3110
  // start.
@@ -3146,7 +3143,10 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3146
3143
  args: [actor]
3147
3144
  });
3148
3145
  if (invokeDef.id) {
3149
- children[invokeDef.id] = actor;
3146
+ children = {
3147
+ ...children,
3148
+ [invokeDef.id]: actor
3149
+ };
3150
3150
  }
3151
3151
  }
3152
3152
  if (invoked) {
@@ -3158,7 +3158,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3158
3158
  let contextChangedByEntry = false;
3159
3159
  const stateInput = stateInputMap[stateNodeToEnter.id];
3160
3160
  if (stateNodeToEnter.entry) {
3161
- const [resultActions, nextContext, nextInternalEvents] = getStateActionsAndContext(stateNodeToEnter.entry, context, children, stateInput, 'entry', stateNodeToEnter);
3161
+ const [resultActions, nextContext, nextInternalEvents, nextChildren] = getStateActionsAndContext(stateNodeToEnter.entry, context, children, stateInput, 'entry', stateNodeToEnter);
3162
3162
  actions.push(...resultActions);
3163
3163
  if (nextInternalEvents?.length) {
3164
3164
  internalQueue.push(...nextInternalEvents);
@@ -3167,6 +3167,11 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3167
3167
  context = nextContext;
3168
3168
  contextChangedByEntry = true;
3169
3169
  }
3170
+ if (nextChildren) {
3171
+ nextState = cloneMachineSnapshot(nextState, {
3172
+ children: nextChildren
3173
+ });
3174
+ }
3170
3175
  }
3171
3176
  if (contextChangedByEntry) {
3172
3177
  nextState.context = context;
@@ -3266,7 +3271,17 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3266
3271
  nextStateNodesToExit.forEach(stateNode => {
3267
3272
  if (stateNode.exit) {
3268
3273
  const stateInput = getStateInput(nextState, stateNode.id);
3269
- const [exitActions,, nextInternalEvents] = getStateActionsAndContext(stateNode.exit, nextState.context, nextState.children, stateInput, 'exit', stateNode);
3274
+ const [exitActions, nextContext, nextInternalEvents, nextChildren] = getStateActionsAndContext(stateNode.exit, nextState.context, nextState.children, stateInput, 'exit', stateNode);
3275
+ if (nextContext || nextChildren) {
3276
+ nextState = cloneMachineSnapshot(nextState, {
3277
+ ...(nextContext ? {
3278
+ context: nextContext
3279
+ } : {}),
3280
+ ...(nextChildren ? {
3281
+ children: nextChildren
3282
+ } : {})
3283
+ });
3284
+ }
3270
3285
  allExitActions.push(...exitActions);
3271
3286
  if (nextInternalEvents?.length) {
3272
3287
  internalQueue.push(...nextInternalEvents);
@@ -3287,14 +3302,14 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3287
3302
  // If context was changed (e.g. by entry actions during self-transition),
3288
3303
  // clone to ensure reference inequality for eventless transition re-evaluation
3289
3304
  if (nextState.context !== originalContext) {
3290
- return [nextState === currentSnapshot ? cloneMachineSnapshot(nextState) : nextState, executableActions];
3305
+ return [nextState === currentSnapshot ? cloneMachineSnapshot(nextState) : nextState, executableActions, resolvedTransitions];
3291
3306
  }
3292
- return [nextState, executableActions];
3307
+ return [nextState, executableActions, resolvedTransitions];
3293
3308
  }
3294
3309
  return [cloneMachineSnapshot(nextState, {
3295
3310
  _nodes: nextStateNodes,
3296
3311
  historyValue
3297
- }), executableActions];
3312
+ }), executableActions, resolvedTransitions];
3298
3313
  }
3299
3314
  }
3300
3315
 
@@ -3404,6 +3419,15 @@ class InfiniteTransitionError extends Error {
3404
3419
  }
3405
3420
  }
3406
3421
  function macrostep(snapshot, event, actorScope, internalQueue, initialMicrosteps = []) {
3422
+ // A done, error or stopped snapshot processes no events, as in an actor:
3423
+ // every event is unhandled. The initial macrostep passes `initialMicrosteps`
3424
+ // and may start from a snapshot that is already done.
3425
+ if (snapshot.status !== 'active' && !initialMicrosteps.length) {
3426
+ return {
3427
+ snapshot,
3428
+ microsteps: [[snapshot, [], []]]
3429
+ };
3430
+ }
3407
3431
  let nextSnapshot = snapshot;
3408
3432
  const microsteps = initialMicrosteps.slice();
3409
3433
  // Whether a transition handled the external event. A handled event always
@@ -3472,6 +3496,7 @@ function macrostep(snapshot, event, actorScope, internalQueue, initialMicrosteps
3472
3496
  };
3473
3497
  }
3474
3498
  function addMicrostep(step, transitions) {
3499
+ transitions = step[2] ?? transitions;
3475
3500
  // collect microsteps; their transitions are surfaced on the enclosing
3476
3501
  // '@xstate.transition' event via its `microsteps[]` facet (there is no
3477
3502
  // standalone microstep event) and returned by `getMicrosteps()`
@@ -3882,6 +3907,17 @@ function isMachineSnapshot(value) {
3882
3907
  return !!value && typeof value === 'object' && 'machine' in value && 'value' in value;
3883
3908
  }
3884
3909
 
3910
+ /**
3911
+ * The values that `snapshot.matches(...)` accepts for a state value: a state
3912
+ * key, a dot-delimited path to a descendant state, or a partial state value.
3913
+ * Non-literal state values accept any value.
3914
+ */
3915
+
3916
+ /**
3917
+ * `unknown` if `matches(...)` accepts `TTestStateValue` for `TStateValue`,
3918
+ * otherwise `never`. Non-literal test values are always accepted.
3919
+ */
3920
+
3885
3921
  /** @public */
3886
3922
 
3887
3923
  const machineSnapshotMatches = function matches(testValue) {
@@ -4382,7 +4418,14 @@ function getAllOwnEventDescriptors(snapshot) {
4382
4418
  return [...new Set([...snapshot.nodes.flatMap(sn => sn.ownEvents)])];
4383
4419
  }
4384
4420
 
4385
- /** @internal Events synthesized from active transition descriptors. */
4421
+ /**
4422
+ * Returns one event per transition of the active state nodes of `snapshot`,
4423
+ * including the internal events for invoked actors (`xstate.done.actor`,
4424
+ * `xstate.error.actor`) and delayed transitions (`xstate.after`). Graph
4425
+ * traversal sends these when the `events` option is not given.
4426
+ *
4427
+ * @public
4428
+ */
4386
4429
  function getAllOwnEvents(snapshot) {
4387
4430
  const events = snapshot.nodes.flatMap(stateNode => [...stateNode.transitions.values()].flatMap(transitions => transitions.map(transition => {
4388
4431
  const event = {
@@ -5931,7 +5974,10 @@ class Actor {
5931
5974
  const [nextSnapshot, effects] = nextState;
5932
5975
  if (nextSnapshot === previousSnapshot && !effects.length &&
5933
5976
  // State machine snapshots only; other logic may ignore events freely.
5934
- 'machine' in previousSnapshot && !event.type.startsWith('xstate.')) {
5977
+ 'machine' in previousSnapshot && (
5978
+ // `xstate.route` is sent by users, so a route that matches nothing is
5979
+ // reported like any other unhandled event.
5980
+ !event.type.startsWith('xstate.') || event.type === 'xstate.route')) {
5935
5981
  this._reportUnhandledEvent(event);
5936
5982
  }
5937
5983
  snapshot = nextSnapshot;