xstate 5.25.1 → 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 (50) 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 +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +1 -1
  8. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  9. package/actors/dist/xstate-actors.development.esm.js +1 -1
  10. package/actors/dist/xstate-actors.esm.js +1 -1
  11. package/dist/{StateMachine-d0e98d09.development.esm.js → StateMachine-93271b59.development.esm.js} +8 -13
  12. package/dist/{StateMachine-6c48f805.esm.js → StateMachine-a4db5a91.esm.js} +8 -13
  13. package/dist/{StateMachine-6d24a98f.development.cjs.js → StateMachine-c3376229.development.cjs.js} +8 -13
  14. package/dist/{StateMachine-fee05db8.cjs.js → StateMachine-e93afc18.cjs.js} +8 -13
  15. package/dist/{assign-37a2fc1e.esm.js → assign-227b928a.esm.js} +1 -1
  16. package/dist/{assign-577bb842.development.cjs.js → assign-8ef0e332.development.cjs.js} +1 -1
  17. package/dist/{assign-541a432d.development.esm.js → assign-d5291869.development.esm.js} +1 -1
  18. package/dist/{assign-592716a8.cjs.js → assign-e36553db.cjs.js} +1 -1
  19. package/dist/declarations/src/StateMachine.d.ts +0 -5
  20. package/dist/declarations/src/index.d.ts +1 -1
  21. package/dist/declarations/src/stateUtils.d.ts +6 -5
  22. package/dist/declarations/src/transition.d.ts +41 -1
  23. package/dist/{log-3827c227.development.cjs.js → log-72d5ee02.development.cjs.js} +2 -2
  24. package/dist/{log-b8ca474e.development.esm.js → log-7dbbb7c2.development.esm.js} +2 -2
  25. package/dist/{log-9c9f917d.cjs.js → log-e54c2eff.cjs.js} +2 -2
  26. package/dist/{log-ce14fc9a.esm.js → log-fa76d888.esm.js} +2 -2
  27. package/dist/{raise-577e4163.cjs.js → raise-3a84be1f.cjs.js} +70 -45
  28. package/dist/{raise-13a60c49.development.esm.js → raise-a7794093.development.esm.js} +68 -44
  29. package/dist/{raise-f5c7cb5b.esm.js → raise-e5d81555.esm.js} +68 -44
  30. package/dist/{raise-ed7c6f3d.development.cjs.js → raise-ecea0c53.development.cjs.js} +70 -45
  31. package/dist/xstate.cjs.js +96 -4
  32. package/dist/xstate.cjs.mjs +3 -0
  33. package/dist/xstate.development.cjs.js +96 -4
  34. package/dist/xstate.development.cjs.mjs +3 -0
  35. package/dist/xstate.development.esm.js +98 -9
  36. package/dist/xstate.esm.js +98 -9
  37. package/dist/xstate.umd.min.js +1 -1
  38. package/dist/xstate.umd.min.js.map +1 -1
  39. package/graph/dist/xstate-graph.cjs.js +3 -3
  40. package/graph/dist/xstate-graph.development.cjs.js +3 -3
  41. package/graph/dist/xstate-graph.development.esm.js +3 -3
  42. package/graph/dist/xstate-graph.esm.js +3 -3
  43. package/graph/dist/xstate-graph.umd.min.js +1 -1
  44. package/graph/dist/xstate-graph.umd.min.js.map +1 -1
  45. package/guards/dist/xstate-guards.cjs.js +1 -1
  46. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  47. package/guards/dist/xstate-guards.development.esm.js +1 -1
  48. package/guards/dist/xstate-guards.esm.js +1 -1
  49. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  50. package/package.json +1 -1
@@ -1575,7 +1575,9 @@ function evaluateGuard(guard, context, event, snapshot) {
1575
1575
  );
1576
1576
  }
1577
1577
 
1578
- const isAtomicStateNode = stateNode => stateNode.type === 'atomic' || stateNode.type === 'final';
1578
+ function isAtomicStateNode(stateNode) {
1579
+ return stateNode.type === 'atomic' || stateNode.type === 'final';
1580
+ }
1579
1581
  function getChildren(stateNode) {
1580
1582
  return Object.values(stateNode.states).filter(sn => sn.type !== 'history');
1581
1583
  }
@@ -2118,45 +2120,65 @@ function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
2118
2120
  }
2119
2121
  return true;
2120
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
+ }
2121
2133
 
2122
2134
  /** https://www.w3.org/TR/scxml/#microstepProcedure */
2123
2135
  function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
2136
+ const actions = [];
2124
2137
  if (!transitions.length) {
2125
- return currentSnapshot;
2138
+ return [currentSnapshot, actions];
2126
2139
  }
2127
- const mutStateNodeSet = new Set(currentSnapshot._nodes);
2128
- let historyValue = currentSnapshot.historyValue;
2129
- const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
2130
- 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;
2131
2150
 
2132
- // Exit states
2133
- if (!isInitial) {
2134
- [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2135
- }
2151
+ // Exit states
2152
+ if (!isInitial) {
2153
+ [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2154
+ }
2136
2155
 
2137
- // Execute transition content
2138
- 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);
2139
2158
 
2140
- // Enter states
2141
- nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2142
- const nextStateNodes = [...mutStateNodeSet];
2143
- if (nextState.status === 'done') {
2144
- nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2145
- }
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
+ }
2146
2165
 
2147
- // eslint-disable-next-line no-useless-catch
2148
- try {
2149
- if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2150
- 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;
2151
2179
  }
2152
- return cloneMachineSnapshot(nextState, {
2153
- _nodes: nextStateNodes,
2154
- historyValue
2155
- });
2156
- } catch (e) {
2157
- // TODO: Refactor this once proper error handling is implemented.
2158
- // See https://github.com/statelyai/rfcs/pull/4
2159
- throw e;
2180
+ } finally {
2181
+ actorScope.actionExecutor = originalExecutor;
2160
2182
  }
2161
2183
  }
2162
2184
  function getMachineOutput(snapshot, event, actorScope, rootNode, rootCompletionNode) {
@@ -2416,16 +2438,16 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2416
2438
  throw new Error(`An event cannot have the wildcard type ('${WILDCARD}')`);
2417
2439
  }
2418
2440
  let nextSnapshot = snapshot;
2419
- const microstates = [];
2420
- function addMicrostate(microstate, event, transitions) {
2441
+ const microsteps = [];
2442
+ function addMicrostep(step, event, transitions) {
2421
2443
  actorScope.system._sendInspectionEvent({
2422
2444
  type: '@xstate.microstep',
2423
2445
  actorRef: actorScope.self,
2424
2446
  event,
2425
- snapshot: microstate,
2447
+ snapshot: step[0],
2426
2448
  _transitions: transitions
2427
2449
  });
2428
- microstates.push(microstate);
2450
+ microsteps.push(step);
2429
2451
  }
2430
2452
 
2431
2453
  // Handle stop event
@@ -2433,10 +2455,10 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2433
2455
  nextSnapshot = cloneMachineSnapshot(stopChildren(nextSnapshot, event, actorScope), {
2434
2456
  status: 'stopped'
2435
2457
  });
2436
- addMicrostate(nextSnapshot, event, []);
2458
+ addMicrostep([nextSnapshot, []], event, []);
2437
2459
  return {
2438
2460
  snapshot: nextSnapshot,
2439
- microstates
2461
+ microsteps
2440
2462
  };
2441
2463
  }
2442
2464
  let nextEvent = event;
@@ -2455,16 +2477,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2455
2477
  status: 'error',
2456
2478
  error: currentEvent.error
2457
2479
  });
2458
- addMicrostate(nextSnapshot, currentEvent, []);
2480
+ addMicrostep([nextSnapshot, []], currentEvent, []);
2459
2481
  return {
2460
2482
  snapshot: nextSnapshot,
2461
- microstates
2483
+ microsteps
2462
2484
  };
2463
2485
  }
2464
- nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
2486
+ const step = microstep(transitions, snapshot, actorScope, nextEvent, false,
2465
2487
  // isInitial
2466
2488
  internalQueue);
2467
- addMicrostate(nextSnapshot, currentEvent, transitions);
2489
+ nextSnapshot = step[0];
2490
+ addMicrostep(step, currentEvent, transitions);
2468
2491
  }
2469
2492
  let shouldSelectEventlessTransitions = true;
2470
2493
  while (nextSnapshot.status === 'active') {
@@ -2480,16 +2503,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2480
2503
  nextEvent = internalQueue.shift();
2481
2504
  enabledTransitions = selectTransitions(nextEvent, nextSnapshot);
2482
2505
  }
2483
- nextSnapshot = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2506
+ const step = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2507
+ nextSnapshot = step[0];
2484
2508
  shouldSelectEventlessTransitions = nextSnapshot !== previousState;
2485
- addMicrostate(nextSnapshot, nextEvent, enabledTransitions);
2509
+ addMicrostep(step, nextEvent, enabledTransitions);
2486
2510
  }
2487
2511
  if (nextSnapshot.status !== 'active') {
2488
2512
  stopChildren(nextSnapshot, nextEvent, actorScope);
2489
2513
  }
2490
2514
  return {
2491
2515
  snapshot: nextSnapshot,
2492
- microstates
2516
+ microsteps
2493
2517
  };
2494
2518
  }
2495
2519
  function stopChildren(nextState, event, actorScope) {
@@ -2741,4 +2765,4 @@ function raise(eventOrExpr, options) {
2741
2765
  return raise;
2742
2766
  }
2743
2767
 
2744
- export { $$ACTOR_TYPE as $, Actor as A, formatInitialTransition as B, getCandidates as C, resolveStateValue as D, getAllStateNodes as E, createMachineSnapshot as F, isInFinalState as G, macrostep as H, transitionNode as I, resolveActionsAndContext as J, createInitEvent as K, microstep as L, getInitialStateNodes as M, NULL_EVENT as N, toStatePath as O, isStateId as P, getStateNodeByPath as Q, getPersistedSnapshot as R, STATE_DELIMITER as S, resolveReferencedActor as T, executingCustomAction 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, and as d, stateIn as e, isMachineSnapshot as f, getStateNodes as g, getAllOwnEventDescriptors as h, interpret as i, matchesState as j, toObserver as k, stop as l, matchesEventDescriptor as m, not as n, or as o, pathToStateValue as p, mapValues as q, raise as r, stopChild as s, toArray as t, formatTransitions as u, toTransitionConfigArray as v, formatTransition as w, evaluateGuard as x, createInvokeId as y, getDelayedTransitions 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 };
@@ -1533,7 +1533,9 @@ function evaluateGuard(guard, context, event, snapshot) {
1533
1533
  );
1534
1534
  }
1535
1535
 
1536
- const isAtomicStateNode = stateNode => stateNode.type === 'atomic' || stateNode.type === 'final';
1536
+ function isAtomicStateNode(stateNode) {
1537
+ return stateNode.type === 'atomic' || stateNode.type === 'final';
1538
+ }
1537
1539
  function getChildren(stateNode) {
1538
1540
  return Object.values(stateNode.states).filter(sn => sn.type !== 'history');
1539
1541
  }
@@ -2071,45 +2073,65 @@ function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
2071
2073
  }
2072
2074
  return true;
2073
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
+ }
2074
2086
 
2075
2087
  /** https://www.w3.org/TR/scxml/#microstepProcedure */
2076
2088
  function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
2089
+ const actions = [];
2077
2090
  if (!transitions.length) {
2078
- return currentSnapshot;
2091
+ return [currentSnapshot, actions];
2079
2092
  }
2080
- const mutStateNodeSet = new Set(currentSnapshot._nodes);
2081
- let historyValue = currentSnapshot.historyValue;
2082
- const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
2083
- 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;
2084
2103
 
2085
- // Exit states
2086
- if (!isInitial) {
2087
- [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2088
- }
2104
+ // Exit states
2105
+ if (!isInitial) {
2106
+ [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2107
+ }
2089
2108
 
2090
- // Execute transition content
2091
- 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);
2092
2111
 
2093
- // Enter states
2094
- nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2095
- const nextStateNodes = [...mutStateNodeSet];
2096
- if (nextState.status === 'done') {
2097
- nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2098
- }
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
+ }
2099
2118
 
2100
- // eslint-disable-next-line no-useless-catch
2101
- try {
2102
- if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2103
- 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;
2104
2132
  }
2105
- return cloneMachineSnapshot(nextState, {
2106
- _nodes: nextStateNodes,
2107
- historyValue
2108
- });
2109
- } catch (e) {
2110
- // TODO: Refactor this once proper error handling is implemented.
2111
- // See https://github.com/statelyai/rfcs/pull/4
2112
- throw e;
2133
+ } finally {
2134
+ actorScope.actionExecutor = originalExecutor;
2113
2135
  }
2114
2136
  }
2115
2137
  function getMachineOutput(snapshot, event, actorScope, rootNode, rootCompletionNode) {
@@ -2366,16 +2388,16 @@ function resolveActionsAndContext(currentSnapshot, event, actorScope, actions, i
2366
2388
  }
2367
2389
  function macrostep(snapshot, event, actorScope, internalQueue) {
2368
2390
  let nextSnapshot = snapshot;
2369
- const microstates = [];
2370
- function addMicrostate(microstate, event, transitions) {
2391
+ const microsteps = [];
2392
+ function addMicrostep(step, event, transitions) {
2371
2393
  actorScope.system._sendInspectionEvent({
2372
2394
  type: '@xstate.microstep',
2373
2395
  actorRef: actorScope.self,
2374
2396
  event,
2375
- snapshot: microstate,
2397
+ snapshot: step[0],
2376
2398
  _transitions: transitions
2377
2399
  });
2378
- microstates.push(microstate);
2400
+ microsteps.push(step);
2379
2401
  }
2380
2402
 
2381
2403
  // Handle stop event
@@ -2383,10 +2405,10 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2383
2405
  nextSnapshot = cloneMachineSnapshot(stopChildren(nextSnapshot, event, actorScope), {
2384
2406
  status: 'stopped'
2385
2407
  });
2386
- addMicrostate(nextSnapshot, event, []);
2408
+ addMicrostep([nextSnapshot, []], event, []);
2387
2409
  return {
2388
2410
  snapshot: nextSnapshot,
2389
- microstates
2411
+ microsteps
2390
2412
  };
2391
2413
  }
2392
2414
  let nextEvent = event;
@@ -2405,16 +2427,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2405
2427
  status: 'error',
2406
2428
  error: currentEvent.error
2407
2429
  });
2408
- addMicrostate(nextSnapshot, currentEvent, []);
2430
+ addMicrostep([nextSnapshot, []], currentEvent, []);
2409
2431
  return {
2410
2432
  snapshot: nextSnapshot,
2411
- microstates
2433
+ microsteps
2412
2434
  };
2413
2435
  }
2414
- nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
2436
+ const step = microstep(transitions, snapshot, actorScope, nextEvent, false,
2415
2437
  // isInitial
2416
2438
  internalQueue);
2417
- addMicrostate(nextSnapshot, currentEvent, transitions);
2439
+ nextSnapshot = step[0];
2440
+ addMicrostep(step, currentEvent, transitions);
2418
2441
  }
2419
2442
  let shouldSelectEventlessTransitions = true;
2420
2443
  while (nextSnapshot.status === 'active') {
@@ -2430,16 +2453,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2430
2453
  nextEvent = internalQueue.shift();
2431
2454
  enabledTransitions = selectTransitions(nextEvent, nextSnapshot);
2432
2455
  }
2433
- nextSnapshot = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2456
+ const step = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2457
+ nextSnapshot = step[0];
2434
2458
  shouldSelectEventlessTransitions = nextSnapshot !== previousState;
2435
- addMicrostate(nextSnapshot, nextEvent, enabledTransitions);
2459
+ addMicrostep(step, nextEvent, enabledTransitions);
2436
2460
  }
2437
2461
  if (nextSnapshot.status !== 'active') {
2438
2462
  stopChildren(nextSnapshot, nextEvent, actorScope);
2439
2463
  }
2440
2464
  return {
2441
2465
  snapshot: nextSnapshot,
2442
- microstates
2466
+ microsteps
2443
2467
  };
2444
2468
  }
2445
2469
  function stopChildren(nextState, event, actorScope) {
@@ -2679,4 +2703,4 @@ function raise(eventOrExpr, options) {
2679
2703
  return raise;
2680
2704
  }
2681
2705
 
2682
- export { $$ACTOR_TYPE as $, Actor as A, formatInitialTransition as B, getCandidates as C, resolveStateValue as D, getAllStateNodes as E, createMachineSnapshot as F, isInFinalState as G, macrostep as H, transitionNode as I, resolveActionsAndContext as J, createInitEvent as K, microstep as L, getInitialStateNodes as M, NULL_EVENT as N, toStatePath as O, isStateId as P, getStateNodeByPath as Q, getPersistedSnapshot as R, STATE_DELIMITER as S, resolveReferencedActor as T, XSTATE_ERROR as U, createErrorActorEvent as V, ProcessingStatus as W, XSTATE_STOP as X, cloneMachineSnapshot as Y, cancel as a, spawnChild as b, createActor as c, and as d, stateIn as e, isMachineSnapshot as f, getStateNodes as g, getAllOwnEventDescriptors as h, interpret as i, matchesState as j, toObserver as k, stop as l, matchesEventDescriptor as m, not as n, or as o, pathToStateValue as p, mapValues as q, raise as r, stopChild as s, toArray as t, formatTransitions as u, toTransitionConfigArray as v, formatTransition as w, evaluateGuard as x, createInvokeId as y, getDelayedTransitions 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 };
@@ -1577,7 +1577,9 @@ function evaluateGuard(guard, context, event, snapshot) {
1577
1577
  );
1578
1578
  }
1579
1579
 
1580
- const isAtomicStateNode = stateNode => stateNode.type === 'atomic' || stateNode.type === 'final';
1580
+ function isAtomicStateNode(stateNode) {
1581
+ return stateNode.type === 'atomic' || stateNode.type === 'final';
1582
+ }
1581
1583
  function getChildren(stateNode) {
1582
1584
  return Object.values(stateNode.states).filter(sn => sn.type !== 'history');
1583
1585
  }
@@ -2120,45 +2122,65 @@ function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
2120
2122
  }
2121
2123
  return true;
2122
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
+ }
2123
2135
 
2124
2136
  /** https://www.w3.org/TR/scxml/#microstepProcedure */
2125
2137
  function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
2138
+ const actions = [];
2126
2139
  if (!transitions.length) {
2127
- return currentSnapshot;
2140
+ return [currentSnapshot, actions];
2128
2141
  }
2129
- const mutStateNodeSet = new Set(currentSnapshot._nodes);
2130
- let historyValue = currentSnapshot.historyValue;
2131
- const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
2132
- 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;
2133
2152
 
2134
- // Exit states
2135
- if (!isInitial) {
2136
- [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2137
- }
2153
+ // Exit states
2154
+ if (!isInitial) {
2155
+ [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2156
+ }
2138
2157
 
2139
- // Execute transition content
2140
- 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);
2141
2160
 
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
- }
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
+ }
2148
2167
 
2149
- // eslint-disable-next-line no-useless-catch
2150
- try {
2151
- if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2152
- 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;
2153
2181
  }
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;
2182
+ } finally {
2183
+ actorScope.actionExecutor = originalExecutor;
2162
2184
  }
2163
2185
  }
2164
2186
  function getMachineOutput(snapshot, event, actorScope, rootNode, rootCompletionNode) {
@@ -2418,16 +2440,16 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2418
2440
  throw new Error(`An event cannot have the wildcard type ('${WILDCARD}')`);
2419
2441
  }
2420
2442
  let nextSnapshot = snapshot;
2421
- const microstates = [];
2422
- function addMicrostate(microstate, event, transitions) {
2443
+ const microsteps = [];
2444
+ function addMicrostep(step, event, transitions) {
2423
2445
  actorScope.system._sendInspectionEvent({
2424
2446
  type: '@xstate.microstep',
2425
2447
  actorRef: actorScope.self,
2426
2448
  event,
2427
- snapshot: microstate,
2449
+ snapshot: step[0],
2428
2450
  _transitions: transitions
2429
2451
  });
2430
- microstates.push(microstate);
2452
+ microsteps.push(step);
2431
2453
  }
2432
2454
 
2433
2455
  // Handle stop event
@@ -2435,10 +2457,10 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2435
2457
  nextSnapshot = cloneMachineSnapshot(stopChildren(nextSnapshot, event, actorScope), {
2436
2458
  status: 'stopped'
2437
2459
  });
2438
- addMicrostate(nextSnapshot, event, []);
2460
+ addMicrostep([nextSnapshot, []], event, []);
2439
2461
  return {
2440
2462
  snapshot: nextSnapshot,
2441
- microstates
2463
+ microsteps
2442
2464
  };
2443
2465
  }
2444
2466
  let nextEvent = event;
@@ -2457,16 +2479,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2457
2479
  status: 'error',
2458
2480
  error: currentEvent.error
2459
2481
  });
2460
- addMicrostate(nextSnapshot, currentEvent, []);
2482
+ addMicrostep([nextSnapshot, []], currentEvent, []);
2461
2483
  return {
2462
2484
  snapshot: nextSnapshot,
2463
- microstates
2485
+ microsteps
2464
2486
  };
2465
2487
  }
2466
- nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
2488
+ const step = microstep(transitions, snapshot, actorScope, nextEvent, false,
2467
2489
  // isInitial
2468
2490
  internalQueue);
2469
- addMicrostate(nextSnapshot, currentEvent, transitions);
2491
+ nextSnapshot = step[0];
2492
+ addMicrostep(step, currentEvent, transitions);
2470
2493
  }
2471
2494
  let shouldSelectEventlessTransitions = true;
2472
2495
  while (nextSnapshot.status === 'active') {
@@ -2482,16 +2505,17 @@ function macrostep(snapshot, event, actorScope, internalQueue) {
2482
2505
  nextEvent = internalQueue.shift();
2483
2506
  enabledTransitions = selectTransitions(nextEvent, nextSnapshot);
2484
2507
  }
2485
- nextSnapshot = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2508
+ const step = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
2509
+ nextSnapshot = step[0];
2486
2510
  shouldSelectEventlessTransitions = nextSnapshot !== previousState;
2487
- addMicrostate(nextSnapshot, nextEvent, enabledTransitions);
2511
+ addMicrostep(step, nextEvent, enabledTransitions);
2488
2512
  }
2489
2513
  if (nextSnapshot.status !== 'active') {
2490
2514
  stopChildren(nextSnapshot, nextEvent, actorScope);
2491
2515
  }
2492
2516
  return {
2493
2517
  snapshot: nextSnapshot,
2494
- microstates
2518
+ microsteps
2495
2519
  };
2496
2520
  }
2497
2521
  function stopChildren(nextState, event, actorScope) {
@@ -2766,11 +2790,13 @@ exports.getAllOwnEventDescriptors = getAllOwnEventDescriptors;
2766
2790
  exports.getAllStateNodes = getAllStateNodes;
2767
2791
  exports.getCandidates = getCandidates;
2768
2792
  exports.getDelayedTransitions = getDelayedTransitions;
2769
- exports.getInitialStateNodes = getInitialStateNodes;
2770
2793
  exports.getPersistedSnapshot = getPersistedSnapshot;
2794
+ exports.getProperAncestors = getProperAncestors;
2771
2795
  exports.getStateNodeByPath = getStateNodeByPath;
2772
2796
  exports.getStateNodes = getStateNodes;
2797
+ exports.initialMicrostep = initialMicrostep;
2773
2798
  exports.interpret = interpret;
2799
+ exports.isAtomicStateNode = isAtomicStateNode;
2774
2800
  exports.isInFinalState = isInFinalState;
2775
2801
  exports.isMachineSnapshot = isMachineSnapshot;
2776
2802
  exports.isStateId = isStateId;
@@ -2778,7 +2804,6 @@ exports.macrostep = macrostep;
2778
2804
  exports.mapValues = mapValues;
2779
2805
  exports.matchesEventDescriptor = matchesEventDescriptor;
2780
2806
  exports.matchesState = matchesState;
2781
- exports.microstep = microstep;
2782
2807
  exports.not = not;
2783
2808
  exports.or = or;
2784
2809
  exports.pathToStateValue = pathToStateValue;