xstate 5.26.0 → 5.27.0

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 (49) hide show
  1. package/actions/dist/xstate-actions.cjs.js +3 -3
  2. package/actions/dist/xstate-actions.development.cjs.js +3 -3
  3. package/actions/dist/xstate-actions.development.esm.js +3 -3
  4. package/actions/dist/xstate-actions.esm.js +3 -3
  5. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  6. package/actors/dist/xstate-actors.cjs.js +1 -1
  7. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  8. package/actors/dist/xstate-actors.development.esm.js +1 -1
  9. package/actors/dist/xstate-actors.esm.js +1 -1
  10. package/dist/{StateMachine-ceed8de1.development.esm.js → StateMachine-93271b59.development.esm.js} +8 -13
  11. package/dist/{StateMachine-d08f7a0b.esm.js → StateMachine-a4db5a91.esm.js} +8 -13
  12. package/dist/{StateMachine-3a7b3253.development.cjs.js → StateMachine-c3376229.development.cjs.js} +8 -13
  13. package/dist/{StateMachine-29d33e13.cjs.js → StateMachine-e93afc18.cjs.js} +8 -13
  14. package/dist/{assign-5d7df46f.esm.js → assign-227b928a.esm.js} +1 -1
  15. package/dist/{assign-6a45790f.development.cjs.js → assign-8ef0e332.development.cjs.js} +1 -1
  16. package/dist/{assign-382e15e2.development.esm.js → assign-d5291869.development.esm.js} +1 -1
  17. package/dist/{assign-9271e302.cjs.js → assign-e36553db.cjs.js} +1 -1
  18. package/dist/declarations/src/StateMachine.d.ts +0 -5
  19. package/dist/declarations/src/index.d.ts +1 -1
  20. package/dist/declarations/src/stateUtils.d.ts +4 -5
  21. package/dist/declarations/src/transition.d.ts +16 -1
  22. package/dist/{log-661c5df8.development.cjs.js → log-72d5ee02.development.cjs.js} +2 -2
  23. package/dist/{log-d09f274f.development.esm.js → log-7dbbb7c2.development.esm.js} +2 -2
  24. package/dist/{log-fe39762e.cjs.js → log-e54c2eff.cjs.js} +2 -2
  25. package/dist/{log-46a8697a.esm.js → log-fa76d888.esm.js} +2 -2
  26. package/dist/{raise-df325116.cjs.js → raise-3a84be1f.cjs.js} +65 -44
  27. package/dist/{raise-13e2f823.development.esm.js → raise-a7794093.development.esm.js} +65 -43
  28. package/dist/{raise-f11495d1.esm.js → raise-e5d81555.esm.js} +65 -43
  29. package/dist/{raise-e47e3273.development.cjs.js → raise-ecea0c53.development.cjs.js} +65 -44
  30. package/dist/xstate.cjs.js +39 -4
  31. package/dist/xstate.cjs.mjs +2 -0
  32. package/dist/xstate.development.cjs.js +39 -4
  33. package/dist/xstate.development.cjs.mjs +2 -0
  34. package/dist/xstate.development.esm.js +42 -9
  35. package/dist/xstate.esm.js +42 -9
  36. package/dist/xstate.umd.min.js +1 -1
  37. package/dist/xstate.umd.min.js.map +1 -1
  38. package/graph/dist/xstate-graph.cjs.js +3 -3
  39. package/graph/dist/xstate-graph.development.cjs.js +3 -3
  40. package/graph/dist/xstate-graph.development.esm.js +3 -3
  41. package/graph/dist/xstate-graph.esm.js +3 -3
  42. package/graph/dist/xstate-graph.umd.min.js +1 -1
  43. package/graph/dist/xstate-graph.umd.min.js.map +1 -1
  44. package/guards/dist/xstate-guards.cjs.js +1 -1
  45. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  46. package/guards/dist/xstate-guards.development.esm.js +1 -1
  47. package/guards/dist/xstate-guards.esm.js +1 -1
  48. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  49. package/package.json +1 -1
@@ -2120,45 +2120,65 @@ function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
2120
2120
  }
2121
2121
  return true;
2122
2122
  }
2123
+ function initialMicrostep(root, preInitialState, actorScope, initEvent, internalQueue) {
2124
+ return microstep([{
2125
+ target: [...getInitialStateNodes(root)],
2126
+ source: root,
2127
+ reenter: true,
2128
+ actions: [],
2129
+ eventType: null,
2130
+ toJSON: null
2131
+ }], preInitialState, actorScope, initEvent, true, internalQueue);
2132
+ }
2123
2133
 
2124
2134
  /** https://www.w3.org/TR/scxml/#microstepProcedure */
2125
2135
  function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
2136
+ const actions = [];
2126
2137
  if (!transitions.length) {
2127
- return currentSnapshot;
2138
+ return [currentSnapshot, actions];
2128
2139
  }
2129
- const mutStateNodeSet = new Set(currentSnapshot._nodes);
2130
- let historyValue = currentSnapshot.historyValue;
2131
- const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
2132
- let nextState = currentSnapshot;
2140
+ const originalExecutor = actorScope.actionExecutor;
2141
+ actorScope.actionExecutor = action => {
2142
+ actions.push(action);
2143
+ originalExecutor(action);
2144
+ };
2145
+ try {
2146
+ const mutStateNodeSet = new Set(currentSnapshot._nodes);
2147
+ let historyValue = currentSnapshot.historyValue;
2148
+ const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
2149
+ let nextState = currentSnapshot;
2133
2150
 
2134
- // Exit states
2135
- if (!isInitial) {
2136
- [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2137
- }
2151
+ // Exit states
2152
+ if (!isInitial) {
2153
+ [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2154
+ }
2138
2155
 
2139
- // Execute transition content
2140
- nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue, undefined);
2156
+ // Execute transition content
2157
+ nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue, undefined);
2141
2158
 
2142
- // Enter states
2143
- nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2144
- const nextStateNodes = [...mutStateNodeSet];
2145
- if (nextState.status === 'done') {
2146
- nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2147
- }
2159
+ // Enter states
2160
+ nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2161
+ const nextStateNodes = [...mutStateNodeSet];
2162
+ if (nextState.status === 'done') {
2163
+ nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2164
+ }
2148
2165
 
2149
- // eslint-disable-next-line no-useless-catch
2150
- try {
2151
- if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2152
- return nextState;
2166
+ // eslint-disable-next-line no-useless-catch
2167
+ try {
2168
+ if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2169
+ return [nextState, actions];
2170
+ }
2171
+ return [cloneMachineSnapshot(nextState, {
2172
+ _nodes: nextStateNodes,
2173
+ historyValue
2174
+ }), actions];
2175
+ } catch (e) {
2176
+ // TODO: Refactor this once proper error handling is implemented.
2177
+ // See https://github.com/statelyai/rfcs/pull/4
2178
+ throw e;
2153
2179
  }
2154
- return cloneMachineSnapshot(nextState, {
2155
- _nodes: nextStateNodes,
2156
- historyValue
2157
- });
2158
- } catch (e) {
2159
- // TODO: Refactor this once proper error handling is implemented.
2160
- // See https://github.com/statelyai/rfcs/pull/4
2161
- throw e;
2180
+ } finally {
2181
+ actorScope.actionExecutor = originalExecutor;
2162
2182
  }
2163
2183
  }
2164
2184
  function getMachineOutput(snapshot, event, actorScope, rootNode, rootCompletionNode) {
@@ -2418,16 +2438,16 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2418
2438
  throw new Error(`An event cannot have the wildcard type ('${WILDCARD}')`);
2419
2439
  }
2420
2440
  let nextSnapshot = snapshot;
2421
- const microstates = [];
2422
- function addMicrostate(microstate, event, transitions) {
2441
+ const microsteps = [];
2442
+ function addMicrostep(step, event, transitions) {
2423
2443
  actorScope.system._sendInspectionEvent({
2424
2444
  type: '@xstate.microstep',
2425
2445
  actorRef: actorScope.self,
2426
2446
  event,
2427
- snapshot: microstate,
2447
+ snapshot: step[0],
2428
2448
  _transitions: transitions
2429
2449
  });
2430
- microstates.push(microstate);
2450
+ microsteps.push(step);
2431
2451
  }
2432
2452
 
2433
2453
  // Handle stop event
@@ -2435,10 +2455,10 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2435
2455
  nextSnapshot = cloneMachineSnapshot(stopChildren(nextSnapshot, event, actorScope), {
2436
2456
  status: 'stopped'
2437
2457
  });
2438
- addMicrostate(nextSnapshot, event, []);
2458
+ addMicrostep([nextSnapshot, []], event, []);
2439
2459
  return {
2440
2460
  snapshot: nextSnapshot,
2441
- microstates
2461
+ microsteps
2442
2462
  };
2443
2463
  }
2444
2464
  let nextEvent = event;
@@ -2457,16 +2477,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2457
2477
  status: 'error',
2458
2478
  error: currentEvent.error
2459
2479
  });
2460
- addMicrostate(nextSnapshot, currentEvent, []);
2480
+ addMicrostep([nextSnapshot, []], currentEvent, []);
2461
2481
  return {
2462
2482
  snapshot: nextSnapshot,
2463
- microstates
2483
+ microsteps
2464
2484
  };
2465
2485
  }
2466
- nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
2486
+ const step = microstep(transitions, snapshot, actorScope, nextEvent, false,
2467
2487
  // isInitial
2468
2488
  internalQueue);
2469
- addMicrostate(nextSnapshot, currentEvent, transitions);
2489
+ nextSnapshot = step[0];
2490
+ addMicrostep(step, currentEvent, transitions);
2470
2491
  }
2471
2492
  let shouldSelectEventlessTransitions = true;
2472
2493
  while (nextSnapshot.status === 'active') {
@@ -2482,16 +2503,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2482
2503
  nextEvent = internalQueue.shift();
2483
2504
  enabledTransitions = selectTransitions(nextEvent, nextSnapshot);
2484
2505
  }
2485
- nextSnapshot = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2506
+ const step = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2507
+ nextSnapshot = step[0];
2486
2508
  shouldSelectEventlessTransitions = nextSnapshot !== previousState;
2487
- addMicrostate(nextSnapshot, nextEvent, enabledTransitions);
2509
+ addMicrostep(step, nextEvent, enabledTransitions);
2488
2510
  }
2489
2511
  if (nextSnapshot.status !== 'active') {
2490
2512
  stopChildren(nextSnapshot, nextEvent, actorScope);
2491
2513
  }
2492
2514
  return {
2493
2515
  snapshot: nextSnapshot,
2494
- microstates
2516
+ microsteps
2495
2517
  };
2496
2518
  }
2497
2519
  function stopChildren(nextState, event, actorScope) {
@@ -2743,4 +2765,4 @@ function raise(eventOrExpr, options) {
2743
2765
  return raise;
2744
2766
  }
2745
2767
 
2746
- export { $$ACTOR_TYPE as $, Actor as A, createInvokeId as B, getDelayedTransitions as C, formatInitialTransition as D, getCandidates as E, resolveStateValue as F, getAllStateNodes as G, createMachineSnapshot as H, isInFinalState as I, macrostep as J, transitionNode as K, resolveActionsAndContext as L, createInitEvent as M, NULL_EVENT as N, microstep as O, getInitialStateNodes as P, toStatePath as Q, isStateId as R, STATE_DELIMITER as S, getStateNodeByPath as T, getPersistedSnapshot as U, resolveReferencedActor as V, executingCustomAction as W, XSTATE_STOP as X, XSTATE_ERROR as Y, createErrorActorEvent as Z, ProcessingStatus as _, cancel as a, cloneMachineSnapshot as a0, spawnChild as b, createActor as c, interpret as d, and as e, stateIn as f, getProperAncestors as g, isMachineSnapshot as h, isAtomicStateNode as i, getStateNodes as j, getAllOwnEventDescriptors as k, matchesState as l, matchesEventDescriptor as m, not as n, or as o, pathToStateValue as p, toObserver as q, raise as r, stopChild as s, toArray as t, stop as u, mapValues as v, formatTransitions as w, toTransitionConfigArray as x, formatTransition as y, evaluateGuard as z };
2768
+ export { $$ACTOR_TYPE as $, Actor as A, toTransitionConfigArray as B, formatTransition as C, evaluateGuard as D, createInvokeId as E, getDelayedTransitions as F, formatInitialTransition as G, getCandidates as H, resolveStateValue as I, getAllStateNodes as J, createMachineSnapshot as K, isInFinalState as L, transitionNode as M, NULL_EVENT as N, resolveActionsAndContext as O, toStatePath as P, isStateId as Q, getStateNodeByPath as R, STATE_DELIMITER as S, getPersistedSnapshot as T, resolveReferencedActor as U, executingCustomAction as V, XSTATE_ERROR as W, XSTATE_STOP as X, createErrorActorEvent as Y, ProcessingStatus as Z, cloneMachineSnapshot as _, cancel as a, spawnChild as b, createActor as c, macrostep as d, createInitEvent as e, isAtomicStateNode as f, getProperAncestors as g, interpret as h, initialMicrostep as i, and as j, stateIn as k, isMachineSnapshot as l, matchesEventDescriptor as m, not as n, or as o, getStateNodes as p, getAllOwnEventDescriptors as q, raise as r, stopChild as s, toArray as t, matchesState as u, pathToStateValue as v, toObserver as w, stop as x, mapValues as y, formatTransitions as z };
@@ -2073,45 +2073,65 @@ function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
2073
2073
  }
2074
2074
  return true;
2075
2075
  }
2076
+ function initialMicrostep(root, preInitialState, actorScope, initEvent, internalQueue) {
2077
+ return microstep([{
2078
+ target: [...getInitialStateNodes(root)],
2079
+ source: root,
2080
+ reenter: true,
2081
+ actions: [],
2082
+ eventType: null,
2083
+ toJSON: null
2084
+ }], preInitialState, actorScope, initEvent, true, internalQueue);
2085
+ }
2076
2086
 
2077
2087
  /** https://www.w3.org/TR/scxml/#microstepProcedure */
2078
2088
  function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
2089
+ const actions = [];
2079
2090
  if (!transitions.length) {
2080
- return currentSnapshot;
2091
+ return [currentSnapshot, actions];
2081
2092
  }
2082
- const mutStateNodeSet = new Set(currentSnapshot._nodes);
2083
- let historyValue = currentSnapshot.historyValue;
2084
- const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
2085
- let nextState = currentSnapshot;
2093
+ const originalExecutor = actorScope.actionExecutor;
2094
+ actorScope.actionExecutor = action => {
2095
+ actions.push(action);
2096
+ originalExecutor(action);
2097
+ };
2098
+ try {
2099
+ const mutStateNodeSet = new Set(currentSnapshot._nodes);
2100
+ let historyValue = currentSnapshot.historyValue;
2101
+ const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
2102
+ let nextState = currentSnapshot;
2086
2103
 
2087
- // Exit states
2088
- if (!isInitial) {
2089
- [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2090
- }
2104
+ // Exit states
2105
+ if (!isInitial) {
2106
+ [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2107
+ }
2091
2108
 
2092
- // Execute transition content
2093
- nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue, undefined);
2109
+ // Execute transition content
2110
+ nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue, undefined);
2094
2111
 
2095
- // Enter states
2096
- nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2097
- const nextStateNodes = [...mutStateNodeSet];
2098
- if (nextState.status === 'done') {
2099
- nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2100
- }
2112
+ // Enter states
2113
+ nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2114
+ const nextStateNodes = [...mutStateNodeSet];
2115
+ if (nextState.status === 'done') {
2116
+ nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2117
+ }
2101
2118
 
2102
- // eslint-disable-next-line no-useless-catch
2103
- try {
2104
- if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2105
- return nextState;
2119
+ // eslint-disable-next-line no-useless-catch
2120
+ try {
2121
+ if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2122
+ return [nextState, actions];
2123
+ }
2124
+ return [cloneMachineSnapshot(nextState, {
2125
+ _nodes: nextStateNodes,
2126
+ historyValue
2127
+ }), actions];
2128
+ } catch (e) {
2129
+ // TODO: Refactor this once proper error handling is implemented.
2130
+ // See https://github.com/statelyai/rfcs/pull/4
2131
+ throw e;
2106
2132
  }
2107
- return cloneMachineSnapshot(nextState, {
2108
- _nodes: nextStateNodes,
2109
- historyValue
2110
- });
2111
- } catch (e) {
2112
- // TODO: Refactor this once proper error handling is implemented.
2113
- // See https://github.com/statelyai/rfcs/pull/4
2114
- throw e;
2133
+ } finally {
2134
+ actorScope.actionExecutor = originalExecutor;
2115
2135
  }
2116
2136
  }
2117
2137
  function getMachineOutput(snapshot, event, actorScope, rootNode, rootCompletionNode) {
@@ -2368,16 +2388,16 @@ function resolveActionsAndContext(currentSnapshot, event, actorScope, actions, i
2368
2388
  }
2369
2389
  function macrostep(snapshot, event, actorScope, internalQueue) {
2370
2390
  let nextSnapshot = snapshot;
2371
- const microstates = [];
2372
- function addMicrostate(microstate, event, transitions) {
2391
+ const microsteps = [];
2392
+ function addMicrostep(step, event, transitions) {
2373
2393
  actorScope.system._sendInspectionEvent({
2374
2394
  type: '@xstate.microstep',
2375
2395
  actorRef: actorScope.self,
2376
2396
  event,
2377
- snapshot: microstate,
2397
+ snapshot: step[0],
2378
2398
  _transitions: transitions
2379
2399
  });
2380
- microstates.push(microstate);
2400
+ microsteps.push(step);
2381
2401
  }
2382
2402
 
2383
2403
  // Handle stop event
@@ -2385,10 +2405,10 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2385
2405
  nextSnapshot = cloneMachineSnapshot(stopChildren(nextSnapshot, event, actorScope), {
2386
2406
  status: 'stopped'
2387
2407
  });
2388
- addMicrostate(nextSnapshot, event, []);
2408
+ addMicrostep([nextSnapshot, []], event, []);
2389
2409
  return {
2390
2410
  snapshot: nextSnapshot,
2391
- microstates
2411
+ microsteps
2392
2412
  };
2393
2413
  }
2394
2414
  let nextEvent = event;
@@ -2407,16 +2427,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2407
2427
  status: 'error',
2408
2428
  error: currentEvent.error
2409
2429
  });
2410
- addMicrostate(nextSnapshot, currentEvent, []);
2430
+ addMicrostep([nextSnapshot, []], currentEvent, []);
2411
2431
  return {
2412
2432
  snapshot: nextSnapshot,
2413
- microstates
2433
+ microsteps
2414
2434
  };
2415
2435
  }
2416
- nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
2436
+ const step = microstep(transitions, snapshot, actorScope, nextEvent, false,
2417
2437
  // isInitial
2418
2438
  internalQueue);
2419
- addMicrostate(nextSnapshot, currentEvent, transitions);
2439
+ nextSnapshot = step[0];
2440
+ addMicrostep(step, currentEvent, transitions);
2420
2441
  }
2421
2442
  let shouldSelectEventlessTransitions = true;
2422
2443
  while (nextSnapshot.status === 'active') {
@@ -2432,16 +2453,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2432
2453
  nextEvent = internalQueue.shift();
2433
2454
  enabledTransitions = selectTransitions(nextEvent, nextSnapshot);
2434
2455
  }
2435
- nextSnapshot = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2456
+ const step = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2457
+ nextSnapshot = step[0];
2436
2458
  shouldSelectEventlessTransitions = nextSnapshot !== previousState;
2437
- addMicrostate(nextSnapshot, nextEvent, enabledTransitions);
2459
+ addMicrostep(step, nextEvent, enabledTransitions);
2438
2460
  }
2439
2461
  if (nextSnapshot.status !== 'active') {
2440
2462
  stopChildren(nextSnapshot, nextEvent, actorScope);
2441
2463
  }
2442
2464
  return {
2443
2465
  snapshot: nextSnapshot,
2444
- microstates
2466
+ microsteps
2445
2467
  };
2446
2468
  }
2447
2469
  function stopChildren(nextState, event, actorScope) {
@@ -2681,4 +2703,4 @@ function raise(eventOrExpr, options) {
2681
2703
  return raise;
2682
2704
  }
2683
2705
 
2684
- export { $$ACTOR_TYPE as $, Actor as A, createInvokeId as B, getDelayedTransitions as C, formatInitialTransition as D, getCandidates as E, resolveStateValue as F, getAllStateNodes as G, createMachineSnapshot as H, isInFinalState as I, macrostep as J, transitionNode as K, resolveActionsAndContext as L, createInitEvent as M, NULL_EVENT as N, microstep as O, getInitialStateNodes as P, toStatePath as Q, isStateId as R, STATE_DELIMITER as S, getStateNodeByPath as T, getPersistedSnapshot as U, resolveReferencedActor as V, XSTATE_ERROR as W, XSTATE_STOP as X, createErrorActorEvent as Y, ProcessingStatus as Z, cloneMachineSnapshot as _, cancel as a, spawnChild as b, createActor as c, interpret as d, and as e, stateIn as f, getProperAncestors as g, isMachineSnapshot as h, isAtomicStateNode as i, getStateNodes as j, getAllOwnEventDescriptors as k, matchesState as l, matchesEventDescriptor as m, not as n, or as o, pathToStateValue as p, toObserver as q, raise as r, stopChild as s, toArray as t, stop as u, mapValues as v, formatTransitions as w, toTransitionConfigArray as x, formatTransition as y, evaluateGuard as z };
2706
+ export { $$ACTOR_TYPE as $, Actor as A, toTransitionConfigArray as B, formatTransition as C, evaluateGuard as D, createInvokeId as E, getDelayedTransitions as F, formatInitialTransition as G, getCandidates as H, resolveStateValue as I, getAllStateNodes as J, createMachineSnapshot as K, isInFinalState as L, transitionNode as M, NULL_EVENT as N, resolveActionsAndContext as O, toStatePath as P, isStateId as Q, getStateNodeByPath as R, STATE_DELIMITER as S, getPersistedSnapshot as T, resolveReferencedActor as U, XSTATE_ERROR as V, createErrorActorEvent as W, XSTATE_STOP as X, ProcessingStatus as Y, cloneMachineSnapshot as Z, cancel as a, spawnChild as b, createActor as c, macrostep as d, createInitEvent as e, isAtomicStateNode as f, getProperAncestors as g, interpret as h, initialMicrostep as i, and as j, stateIn as k, isMachineSnapshot as l, matchesEventDescriptor as m, not as n, or as o, getStateNodes as p, getAllOwnEventDescriptors as q, raise as r, stopChild as s, toArray as t, matchesState as u, pathToStateValue as v, toObserver as w, stop as x, mapValues as y, formatTransitions as z };
@@ -2122,45 +2122,65 @@ function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
2122
2122
  }
2123
2123
  return true;
2124
2124
  }
2125
+ function initialMicrostep(root, preInitialState, actorScope, initEvent, internalQueue) {
2126
+ return microstep([{
2127
+ target: [...getInitialStateNodes(root)],
2128
+ source: root,
2129
+ reenter: true,
2130
+ actions: [],
2131
+ eventType: null,
2132
+ toJSON: null
2133
+ }], preInitialState, actorScope, initEvent, true, internalQueue);
2134
+ }
2125
2135
 
2126
2136
  /** https://www.w3.org/TR/scxml/#microstepProcedure */
2127
2137
  function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
2138
+ const actions = [];
2128
2139
  if (!transitions.length) {
2129
- return currentSnapshot;
2140
+ return [currentSnapshot, actions];
2130
2141
  }
2131
- const mutStateNodeSet = new Set(currentSnapshot._nodes);
2132
- let historyValue = currentSnapshot.historyValue;
2133
- const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
2134
- let nextState = currentSnapshot;
2142
+ const originalExecutor = actorScope.actionExecutor;
2143
+ actorScope.actionExecutor = action => {
2144
+ actions.push(action);
2145
+ originalExecutor(action);
2146
+ };
2147
+ try {
2148
+ const mutStateNodeSet = new Set(currentSnapshot._nodes);
2149
+ let historyValue = currentSnapshot.historyValue;
2150
+ const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
2151
+ let nextState = currentSnapshot;
2135
2152
 
2136
- // Exit states
2137
- if (!isInitial) {
2138
- [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2139
- }
2153
+ // Exit states
2154
+ if (!isInitial) {
2155
+ [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2156
+ }
2140
2157
 
2141
- // Execute transition content
2142
- nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue, undefined);
2158
+ // Execute transition content
2159
+ nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue, undefined);
2143
2160
 
2144
- // Enter states
2145
- nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2146
- const nextStateNodes = [...mutStateNodeSet];
2147
- if (nextState.status === 'done') {
2148
- nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2149
- }
2161
+ // Enter states
2162
+ nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2163
+ const nextStateNodes = [...mutStateNodeSet];
2164
+ if (nextState.status === 'done') {
2165
+ nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2166
+ }
2150
2167
 
2151
- // eslint-disable-next-line no-useless-catch
2152
- try {
2153
- if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2154
- return nextState;
2168
+ // eslint-disable-next-line no-useless-catch
2169
+ try {
2170
+ if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2171
+ return [nextState, actions];
2172
+ }
2173
+ return [cloneMachineSnapshot(nextState, {
2174
+ _nodes: nextStateNodes,
2175
+ historyValue
2176
+ }), actions];
2177
+ } catch (e) {
2178
+ // TODO: Refactor this once proper error handling is implemented.
2179
+ // See https://github.com/statelyai/rfcs/pull/4
2180
+ throw e;
2155
2181
  }
2156
- return cloneMachineSnapshot(nextState, {
2157
- _nodes: nextStateNodes,
2158
- historyValue
2159
- });
2160
- } catch (e) {
2161
- // TODO: Refactor this once proper error handling is implemented.
2162
- // See https://github.com/statelyai/rfcs/pull/4
2163
- throw e;
2182
+ } finally {
2183
+ actorScope.actionExecutor = originalExecutor;
2164
2184
  }
2165
2185
  }
2166
2186
  function getMachineOutput(snapshot, event, actorScope, rootNode, rootCompletionNode) {
@@ -2420,16 +2440,16 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2420
2440
  throw new Error(`An event cannot have the wildcard type ('${WILDCARD}')`);
2421
2441
  }
2422
2442
  let nextSnapshot = snapshot;
2423
- const microstates = [];
2424
- function addMicrostate(microstate, event, transitions) {
2443
+ const microsteps = [];
2444
+ function addMicrostep(step, event, transitions) {
2425
2445
  actorScope.system._sendInspectionEvent({
2426
2446
  type: '@xstate.microstep',
2427
2447
  actorRef: actorScope.self,
2428
2448
  event,
2429
- snapshot: microstate,
2449
+ snapshot: step[0],
2430
2450
  _transitions: transitions
2431
2451
  });
2432
- microstates.push(microstate);
2452
+ microsteps.push(step);
2433
2453
  }
2434
2454
 
2435
2455
  // Handle stop event
@@ -2437,10 +2457,10 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2437
2457
  nextSnapshot = cloneMachineSnapshot(stopChildren(nextSnapshot, event, actorScope), {
2438
2458
  status: 'stopped'
2439
2459
  });
2440
- addMicrostate(nextSnapshot, event, []);
2460
+ addMicrostep([nextSnapshot, []], event, []);
2441
2461
  return {
2442
2462
  snapshot: nextSnapshot,
2443
- microstates
2463
+ microsteps
2444
2464
  };
2445
2465
  }
2446
2466
  let nextEvent = event;
@@ -2459,16 +2479,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2459
2479
  status: 'error',
2460
2480
  error: currentEvent.error
2461
2481
  });
2462
- addMicrostate(nextSnapshot, currentEvent, []);
2482
+ addMicrostep([nextSnapshot, []], currentEvent, []);
2463
2483
  return {
2464
2484
  snapshot: nextSnapshot,
2465
- microstates
2485
+ microsteps
2466
2486
  };
2467
2487
  }
2468
- nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
2488
+ const step = microstep(transitions, snapshot, actorScope, nextEvent, false,
2469
2489
  // isInitial
2470
2490
  internalQueue);
2471
- addMicrostate(nextSnapshot, currentEvent, transitions);
2491
+ nextSnapshot = step[0];
2492
+ addMicrostep(step, currentEvent, transitions);
2472
2493
  }
2473
2494
  let shouldSelectEventlessTransitions = true;
2474
2495
  while (nextSnapshot.status === 'active') {
@@ -2484,16 +2505,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2484
2505
  nextEvent = internalQueue.shift();
2485
2506
  enabledTransitions = selectTransitions(nextEvent, nextSnapshot);
2486
2507
  }
2487
- nextSnapshot = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2508
+ const step = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2509
+ nextSnapshot = step[0];
2488
2510
  shouldSelectEventlessTransitions = nextSnapshot !== previousState;
2489
- addMicrostate(nextSnapshot, nextEvent, enabledTransitions);
2511
+ addMicrostep(step, nextEvent, enabledTransitions);
2490
2512
  }
2491
2513
  if (nextSnapshot.status !== 'active') {
2492
2514
  stopChildren(nextSnapshot, nextEvent, actorScope);
2493
2515
  }
2494
2516
  return {
2495
2517
  snapshot: nextSnapshot,
2496
- microstates
2518
+ microsteps
2497
2519
  };
2498
2520
  }
2499
2521
  function stopChildren(nextState, event, actorScope) {
@@ -2768,11 +2790,11 @@ exports.getAllOwnEventDescriptors = getAllOwnEventDescriptors;
2768
2790
  exports.getAllStateNodes = getAllStateNodes;
2769
2791
  exports.getCandidates = getCandidates;
2770
2792
  exports.getDelayedTransitions = getDelayedTransitions;
2771
- exports.getInitialStateNodes = getInitialStateNodes;
2772
2793
  exports.getPersistedSnapshot = getPersistedSnapshot;
2773
2794
  exports.getProperAncestors = getProperAncestors;
2774
2795
  exports.getStateNodeByPath = getStateNodeByPath;
2775
2796
  exports.getStateNodes = getStateNodes;
2797
+ exports.initialMicrostep = initialMicrostep;
2776
2798
  exports.interpret = interpret;
2777
2799
  exports.isAtomicStateNode = isAtomicStateNode;
2778
2800
  exports.isInFinalState = isInFinalState;
@@ -2782,7 +2804,6 @@ exports.macrostep = macrostep;
2782
2804
  exports.mapValues = mapValues;
2783
2805
  exports.matchesEventDescriptor = matchesEventDescriptor;
2784
2806
  exports.matchesState = matchesState;
2785
- exports.microstep = microstep;
2786
2807
  exports.not = not;
2787
2808
  exports.or = or;
2788
2809
  exports.pathToStateValue = pathToStateValue;
@@ -3,10 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var actors_dist_xstateActors = require('../actors/dist/xstate-actors.cjs.js');
6
- var guards_dist_xstateGuards = require('./raise-df325116.cjs.js');
7
- var StateMachine = require('./StateMachine-29d33e13.cjs.js');
8
- var assign = require('./assign-9271e302.cjs.js');
9
- var log = require('./log-fe39762e.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-3a84be1f.cjs.js');
7
+ var StateMachine = require('./StateMachine-e93afc18.cjs.js');
8
+ var assign = require('./assign-e36553db.cjs.js');
9
+ var log = require('./log-e54c2eff.cjs.js');
10
10
  require('../dev/dist/xstate-dev.cjs.js');
11
11
 
12
12
  /**
@@ -338,6 +338,39 @@ function initialTransition(logic, ...[input]) {
338
338
  return [nextSnapshot, executableActions];
339
339
  }
340
340
 
341
+ /**
342
+ * Given a state `machine`, a `snapshot`, and an `event`, returns an array of
343
+ * microsteps, where each microstep is a tuple of `[snapshot, actions]`.
344
+ *
345
+ * This is a pure function that does not execute `actions`.
346
+ */
347
+ function getMicrosteps(machine, snapshot, event) {
348
+ const actorScope = createInertActorScope(machine);
349
+ const {
350
+ microsteps
351
+ } = guards_dist_xstateGuards.macrostep(snapshot, event, actorScope, []);
352
+ return microsteps;
353
+ }
354
+
355
+ /**
356
+ * Given a state `machine` and optional `input`, returns an array of microsteps
357
+ * from the initial transition, where each microstep is a tuple of `[snapshot,
358
+ * actions]`.
359
+ *
360
+ * This is a pure function that does not execute `actions`.
361
+ */
362
+ function getInitialMicrosteps(machine, ...[input]) {
363
+ const actorScope = createInertActorScope(machine);
364
+ const initEvent = guards_dist_xstateGuards.createInitEvent(input);
365
+ const internalQueue = [];
366
+ const preInitialSnapshot = machine._getPreInitialState(actorScope, initEvent, internalQueue);
367
+ const first = guards_dist_xstateGuards.initialMicrostep(machine.root, preInitialSnapshot, actorScope, initEvent, internalQueue);
368
+ const {
369
+ microsteps
370
+ } = guards_dist_xstateGuards.macrostep(first[0], initEvent, actorScope, internalQueue);
371
+ return [first, ...microsteps];
372
+ }
373
+
341
374
  /**
342
375
  * Gets all potential next transitions from the current state.
343
376
  *
@@ -532,7 +565,9 @@ exports.sendTo = log.sendTo;
532
565
  exports.SimulatedClock = SimulatedClock;
533
566
  exports.assertEvent = assertEvent;
534
567
  exports.createMachine = createMachine;
568
+ exports.getInitialMicrosteps = getInitialMicrosteps;
535
569
  exports.getInitialSnapshot = getInitialSnapshot;
570
+ exports.getMicrosteps = getMicrosteps;
536
571
  exports.getNextSnapshot = getNextSnapshot;
537
572
  exports.getNextTransitions = getNextTransitions;
538
573
  exports.initialTransition = initialTransition;
@@ -20,7 +20,9 @@ export {
20
20
  fromObservable,
21
21
  fromPromise,
22
22
  fromTransition,
23
+ getInitialMicrosteps,
23
24
  getInitialSnapshot,
25
+ getMicrosteps,
24
26
  getNextSnapshot,
25
27
  getNextTransitions,
26
28
  getStateNodes,