xstate 5.18.2 → 5.19.1

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 (52) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  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/actors/dist/xstate-actors.umd.min.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dev/dist/xstate-dev.cjs.js +1 -1
  14. package/dev/dist/xstate-dev.development.cjs.js +1 -1
  15. package/dev/dist/xstate-dev.development.esm.js +1 -1
  16. package/dev/dist/xstate-dev.esm.js +1 -1
  17. package/dev/dist/xstate-dev.umd.min.js.map +1 -1
  18. package/dist/declarations/src/State.d.ts +1 -1
  19. package/dist/declarations/src/actions/raise.d.ts +9 -1
  20. package/dist/declarations/src/actions/send.d.ts +10 -1
  21. package/dist/declarations/src/createActor.d.ts +2 -1
  22. package/dist/declarations/src/getNextSnapshot.d.ts +2 -0
  23. package/dist/declarations/src/index.d.ts +1 -0
  24. package/dist/declarations/src/inspection.d.ts +1 -1
  25. package/dist/declarations/src/spawn.d.ts +19 -11
  26. package/dist/declarations/src/stateUtils.d.ts +17 -7
  27. package/dist/declarations/src/transition.d.ts +16 -0
  28. package/dist/declarations/src/types.d.ts +48 -14
  29. package/dist/declarations/src/utils.d.ts +1 -1
  30. package/dist/{log-38475d87.development.esm.js → log-0acd9069.development.esm.js} +32 -33
  31. package/dist/{log-15d0f775.esm.js → log-3d9d72a9.esm.js} +31 -29
  32. package/dist/{log-b7ed1b61.development.cjs.js → log-8aa651a0.development.cjs.js} +32 -33
  33. package/dist/{log-98fcce74.cjs.js → log-a019fbd0.cjs.js} +31 -29
  34. package/dist/{raise-5ea71f04.development.esm.js → raise-1db27a82.development.esm.js} +98 -72
  35. package/dist/{raise-e919c5d4.development.cjs.js → raise-4acdb210.development.cjs.js} +98 -72
  36. package/dist/{raise-b1e0b9a9.cjs.js → raise-60cebf03.cjs.js} +94 -70
  37. package/dist/{raise-0f7cf128.esm.js → raise-c17ec2bc.esm.js} +94 -70
  38. package/dist/xstate.cjs.js +57 -15
  39. package/dist/xstate.cjs.mjs +2 -0
  40. package/dist/xstate.development.cjs.js +57 -15
  41. package/dist/xstate.development.cjs.mjs +2 -0
  42. package/dist/xstate.development.esm.js +58 -18
  43. package/dist/xstate.esm.js +58 -18
  44. package/dist/xstate.umd.min.js +1 -1
  45. package/dist/xstate.umd.min.js.map +1 -1
  46. package/guards/dist/xstate-guards.cjs.js +1 -1
  47. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  48. package/guards/dist/xstate-guards.development.esm.js +1 -1
  49. package/guards/dist/xstate-guards.esm.js +1 -1
  50. package/guards/dist/xstate-guards.umd.min.js +1 -1
  51. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  52. package/package.json +1 -1
@@ -154,7 +154,7 @@ function toStatePath(stateId) {
154
154
  if (isArray(stateId)) {
155
155
  return stateId;
156
156
  }
157
- let result = [];
157
+ const result = [];
158
158
  let segment = '';
159
159
  for (let i = 0; i < stateId.length; i++) {
160
160
  const char = stateId.charCodeAt(i);
@@ -419,6 +419,7 @@ function createSystem(rootActor, options) {
419
419
  return system;
420
420
  }
421
421
 
422
+ let executingCustomAction = false;
422
423
  const $$ACTOR_TYPE = 1;
423
424
 
424
425
  // those values are currently used by @xstate/react directly so it's important to keep the assigned values in sync
@@ -539,10 +540,37 @@ class Actor {
539
540
  if (!listeners && !wildcardListener) {
540
541
  return;
541
542
  }
542
- const allListeners = new Set([...(listeners ? listeners.values() : []), ...(wildcardListener ? wildcardListener.values() : [])]);
543
- for (const handler of Array.from(allListeners)) {
543
+ const allListeners = [...(listeners ? listeners.values() : []), ...(wildcardListener ? wildcardListener.values() : [])];
544
+ for (const handler of allListeners) {
544
545
  handler(emittedEvent);
545
546
  }
547
+ },
548
+ actionExecutor: action => {
549
+ const exec = () => {
550
+ this._actorScope.system._sendInspectionEvent({
551
+ type: '@xstate.action',
552
+ actorRef: this,
553
+ action: {
554
+ type: action.type,
555
+ params: action.params
556
+ }
557
+ });
558
+ if (!action.exec) {
559
+ return;
560
+ }
561
+ const saveExecutingCustomAction = executingCustomAction;
562
+ try {
563
+ executingCustomAction = true;
564
+ action.exec(action.info, action.params);
565
+ } finally {
566
+ executingCustomAction = saveExecutingCustomAction;
567
+ }
568
+ };
569
+ if (this._processingStatus === ProcessingStatus.Running) {
570
+ exec();
571
+ } else {
572
+ this._deferred.push(exec);
573
+ }
546
574
  }
547
575
  };
548
576
 
@@ -1071,11 +1099,13 @@ function resolveCancel(_, snapshot, actionArgs, actionParams, {
1071
1099
  sendId
1072
1100
  }) {
1073
1101
  const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs, actionParams) : sendId;
1074
- return [snapshot, resolvedSendId];
1102
+ return [snapshot, {
1103
+ sendId: resolvedSendId
1104
+ }, undefined];
1075
1105
  }
1076
- function executeCancel(actorScope, resolvedSendId) {
1106
+ function executeCancel(actorScope, params) {
1077
1107
  actorScope.defer(() => {
1078
- actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId);
1108
+ actorScope.system.scheduler.cancel(actorScope.self, params.sendId);
1079
1109
  });
1080
1110
  }
1081
1111
  /**
@@ -1111,7 +1141,7 @@ function executeCancel(actorScope, resolvedSendId) {
1111
1141
  * @param sendId The `id` of the `sendTo(...)` action to cancel.
1112
1142
  */
1113
1143
  function cancel(sendId) {
1114
- function cancel(args, params) {
1144
+ function cancel(_args, _params) {
1115
1145
  }
1116
1146
  cancel.type = 'xstate.cancel';
1117
1147
  cancel.sendId = sendId;
@@ -1130,18 +1160,20 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1130
1160
  const logic = typeof src === 'string' ? resolveReferencedActor(snapshot.machine, src) : src;
1131
1161
  const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
1132
1162
  let actorRef;
1163
+ let resolvedInput = undefined;
1133
1164
  if (logic) {
1165
+ resolvedInput = typeof input === 'function' ? input({
1166
+ context: snapshot.context,
1167
+ event: actionArgs.event,
1168
+ self: actorScope.self
1169
+ }) : input;
1134
1170
  actorRef = createActor(logic, {
1135
1171
  id: resolvedId,
1136
1172
  src,
1137
1173
  parent: actorScope.self,
1138
1174
  syncSnapshot,
1139
1175
  systemId,
1140
- input: typeof input === 'function' ? input({
1141
- context: snapshot.context,
1142
- event: actionArgs.event,
1143
- self: actorScope.self
1144
- }) : input
1176
+ input: resolvedInput
1145
1177
  });
1146
1178
  }
1147
1179
  return [cloneMachineSnapshot(snapshot, {
@@ -1151,11 +1183,13 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1151
1183
  }
1152
1184
  }), {
1153
1185
  id,
1154
- actorRef
1155
- }];
1186
+ systemId,
1187
+ actorRef,
1188
+ src,
1189
+ input: resolvedInput
1190
+ }, undefined];
1156
1191
  }
1157
1192
  function executeSpawn(actorScope, {
1158
- id,
1159
1193
  actorRef
1160
1194
  }) {
1161
1195
  if (!actorRef) {
@@ -1174,9 +1208,9 @@ function spawnChild(...[src, {
1174
1208
  input,
1175
1209
  syncSnapshot = false
1176
1210
  } = {}]) {
1177
- function spawnChild(args, params) {
1211
+ function spawnChild(_args, _params) {
1178
1212
  }
1179
- spawnChild.type = 'snapshot.spawnChild';
1213
+ spawnChild.type = 'xstate.spawnChild';
1180
1214
  spawnChild.id = id;
1181
1215
  spawnChild.systemId = systemId;
1182
1216
  spawnChild.src = src;
@@ -1201,7 +1235,7 @@ function resolveStop(_, snapshot, args, actionParams, {
1201
1235
  }
1202
1236
  return [cloneMachineSnapshot(snapshot, {
1203
1237
  children
1204
- }), resolvedActorRef];
1238
+ }), resolvedActorRef, undefined];
1205
1239
  }
1206
1240
  function executeStop(actorScope, actorRef) {
1207
1241
  if (!actorRef) {
@@ -1233,7 +1267,7 @@ function executeStop(actorScope, actorRef) {
1233
1267
  * @param actorRef The actor to stop.
1234
1268
  */
1235
1269
  function stopChild(actorRef) {
1236
- function stop(args, params) {
1270
+ function stop(_args, _params) {
1237
1271
  }
1238
1272
  stop.type = 'xstate.stopChild';
1239
1273
  stop.actorRef = actorRef;
@@ -1260,7 +1294,7 @@ function checkStateIn(snapshot, _, {
1260
1294
  return snapshot.matches(stateValue);
1261
1295
  }
1262
1296
  function stateIn(stateValue) {
1263
- function stateIn(args, params) {
1297
+ function stateIn() {
1264
1298
  return false;
1265
1299
  }
1266
1300
  stateIn.check = checkStateIn;
@@ -1306,7 +1340,7 @@ function checkNot(snapshot, {
1306
1340
  * @returns A guard
1307
1341
  */
1308
1342
  function not(guard) {
1309
- function not(args, params) {
1343
+ function not(_args, _params) {
1310
1344
  return false;
1311
1345
  }
1312
1346
  not.check = checkNot;
@@ -1352,7 +1386,7 @@ function checkAnd(snapshot, {
1352
1386
  * @returns A guard action object
1353
1387
  */
1354
1388
  function and(guards) {
1355
- function and(args, params) {
1389
+ function and(_args, _params) {
1356
1390
  return false;
1357
1391
  }
1358
1392
  and.check = checkAnd;
@@ -1398,7 +1432,7 @@ function checkOr(snapshot, {
1398
1432
  * @returns A guard action object
1399
1433
  */
1400
1434
  function or(guards) {
1401
- function or(args, params) {
1435
+ function or(_args, _params) {
1402
1436
  return false;
1403
1437
  }
1404
1438
  or.check = checkOr;
@@ -1576,7 +1610,7 @@ function getDelayedTransitions(stateNode) {
1576
1610
  if (!afterConfig) {
1577
1611
  return [];
1578
1612
  }
1579
- const mutateEntryExit = (delay, i) => {
1613
+ const mutateEntryExit = delay => {
1580
1614
  const afterEvent = createAfterEvent(delay, stateNode.id);
1581
1615
  const eventType = afterEvent.type;
1582
1616
  stateNode.entry.push(raise(afterEvent, {
@@ -1586,7 +1620,7 @@ function getDelayedTransitions(stateNode) {
1586
1620
  stateNode.exit.push(cancel(eventType));
1587
1621
  return eventType;
1588
1622
  };
1589
- const delayedTransitions = Object.keys(afterConfig).flatMap((delay, i) => {
1623
+ const delayedTransitions = Object.keys(afterConfig).flatMap(delay => {
1590
1624
  const configTransition = afterConfig[delay];
1591
1625
  const resolvedTransition = typeof configTransition === 'string' ? {
1592
1626
  target: configTransition
@@ -1671,7 +1705,9 @@ function formatTransitions(stateNode) {
1671
1705
  function formatInitialTransition(stateNode, _target) {
1672
1706
  const resolvedTarget = typeof _target === 'string' ? stateNode.states[_target] : _target ? stateNode.states[_target.target] : undefined;
1673
1707
  if (!resolvedTarget && _target) {
1674
- throw new Error(`Initial state node "${_target}" not found on parent state node #${stateNode.id}`);
1708
+ throw new Error(
1709
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string
1710
+ `Initial state node "${_target}" not found on parent state node #${stateNode.id}`);
1675
1711
  }
1676
1712
  const transition = {
1677
1713
  source: stateNode,
@@ -1781,7 +1817,7 @@ function getStateNodeByPath(stateNode, statePath) {
1781
1817
  if (typeof statePath === 'string' && isStateId(statePath)) {
1782
1818
  try {
1783
1819
  return stateNode.machine.getStateNodeById(statePath);
1784
- } catch (e) {
1820
+ } catch {
1785
1821
  // try individual paths
1786
1822
  // throw e;
1787
1823
  }
@@ -2010,18 +2046,20 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2010
2046
 
2011
2047
  // Exit states
2012
2048
  if (!isInitial) {
2013
- [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue);
2049
+ [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2014
2050
  }
2015
2051
 
2016
2052
  // Execute transition content
2017
- nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue);
2053
+ nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue, undefined);
2018
2054
 
2019
2055
  // Enter states
2020
2056
  nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2021
2057
  const nextStateNodes = [...mutStateNodeSet];
2022
2058
  if (nextState.status === 'done') {
2023
- nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue);
2059
+ nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2024
2060
  }
2061
+
2062
+ // eslint-disable-next-line no-useless-catch
2025
2063
  try {
2026
2064
  if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2027
2065
  return nextState;
@@ -2191,7 +2229,7 @@ function addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultE
2191
2229
  function addProperAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, historyValue, statesForDefaultEntry) {
2192
2230
  addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultEntry, getProperAncestors(stateNode, toStateNode));
2193
2231
  }
2194
- function exitStates(currentSnapshot, event, actorScope, transitions, mutStateNodeSet, historyValue, internalQueue) {
2232
+ function exitStates(currentSnapshot, event, actorScope, transitions, mutStateNodeSet, historyValue, internalQueue, _actionExecutor) {
2195
2233
  let nextSnapshot = currentSnapshot;
2196
2234
  const statesToExit = computeExitSet(transitions, mutStateNodeSet, historyValue);
2197
2235
  statesToExit.sort((a, b) => b.order - a.order);
@@ -2215,12 +2253,14 @@ function exitStates(currentSnapshot, event, actorScope, transitions, mutStateNod
2215
2253
  }
2216
2254
  }
2217
2255
  for (const s of statesToExit) {
2218
- nextSnapshot = resolveActionsAndContext(nextSnapshot, event, actorScope, [...s.exit, ...s.invoke.map(def => stopChild(def.id))], internalQueue);
2256
+ nextSnapshot = resolveActionsAndContext(nextSnapshot, event, actorScope, [...s.exit, ...s.invoke.map(def => stopChild(def.id))], internalQueue, undefined);
2219
2257
  mutStateNodeSet.delete(s);
2220
2258
  }
2221
2259
  return [nextSnapshot, changedHistory || historyValue];
2222
2260
  }
2223
- let executingCustomAction = false;
2261
+ function getAction(machine, actionType) {
2262
+ return machine.implementations.actions[actionType];
2263
+ }
2224
2264
  function resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope, actions, extra, retries) {
2225
2265
  const {
2226
2266
  machine
@@ -2232,10 +2272,8 @@ function resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope,
2232
2272
  // the existing type of `.actions` assumes non-nullable `TExpressionAction`
2233
2273
  // it's fine to cast this here to get a common type and lack of errors in the rest of the code
2234
2274
  // our logic below makes sure that we call those 2 "variants" correctly
2235
- machine.implementations.actions[typeof action === 'string' ? action : action.type];
2236
- if (!resolvedAction) {
2237
- continue;
2238
- }
2275
+
2276
+ getAction(machine, typeof action === 'string' ? action : action.type);
2239
2277
  const actionArgs = {
2240
2278
  context: intermediateSnapshot.context,
2241
2279
  event,
@@ -2246,30 +2284,13 @@ function resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope,
2246
2284
  context: intermediateSnapshot.context,
2247
2285
  event
2248
2286
  }) : action.params : undefined;
2249
- function executeAction() {
2250
- actorScope.system._sendInspectionEvent({
2251
- type: '@xstate.action',
2252
- actorRef: actorScope.self,
2253
- action: {
2254
- type: typeof action === 'string' ? action : typeof action === 'object' ? action.type : action.name || '(anonymous)',
2255
- params: actionParams
2256
- }
2287
+ if (!resolvedAction || !('resolve' in resolvedAction)) {
2288
+ actorScope.actionExecutor({
2289
+ type: typeof action === 'string' ? action : typeof action === 'object' ? action.type : action.name || '(anonymous)',
2290
+ info: actionArgs,
2291
+ params: actionParams,
2292
+ exec: resolvedAction
2257
2293
  });
2258
- try {
2259
- executingCustomAction = resolvedAction;
2260
- resolvedAction(actionArgs, actionParams);
2261
- } finally {
2262
- executingCustomAction = false;
2263
- }
2264
- }
2265
- if (!('resolve' in resolvedAction)) {
2266
- if (actorScope.self._processingStatus === ProcessingStatus.Running) {
2267
- executeAction();
2268
- } else {
2269
- actorScope.defer(() => {
2270
- executeAction();
2271
- });
2272
- }
2273
2294
  continue;
2274
2295
  }
2275
2296
  const builtinAction = resolvedAction;
@@ -2281,11 +2302,12 @@ function resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope,
2281
2302
  retries?.push([builtinAction, params]);
2282
2303
  }
2283
2304
  if ('execute' in builtinAction) {
2284
- if (actorScope.self._processingStatus === ProcessingStatus.Running) {
2285
- builtinAction.execute(actorScope, params);
2286
- } else {
2287
- actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
2288
- }
2305
+ actorScope.actionExecutor({
2306
+ type: builtinAction.type,
2307
+ info: actionArgs,
2308
+ params,
2309
+ exec: builtinAction.execute.bind(null, actorScope, params)
2310
+ });
2289
2311
  }
2290
2312
  if (actions) {
2291
2313
  intermediateSnapshot = resolveAndExecuteActionsWithContext(intermediateSnapshot, event, actorScope, actions, extra, retries);
@@ -2304,7 +2326,7 @@ function resolveActionsAndContext(currentSnapshot, event, actorScope, actions, i
2304
2326
  });
2305
2327
  return nextState;
2306
2328
  }
2307
- function macrostep(snapshot, event, actorScope, internalQueue = []) {
2329
+ function macrostep(snapshot, event, actorScope, internalQueue) {
2308
2330
  let nextSnapshot = snapshot;
2309
2331
  const microstates = [];
2310
2332
  function addMicrostate(microstate, event, transitions) {
@@ -2383,7 +2405,7 @@ function macrostep(snapshot, event, actorScope, internalQueue = []) {
2383
2405
  };
2384
2406
  }
2385
2407
  function stopChildren(nextState, event, actorScope) {
2386
- return resolveActionsAndContext(nextState, event, actorScope, Object.values(nextState.children).map(child => stopChild(child)), []);
2408
+ return resolveActionsAndContext(nextState, event, actorScope, Object.values(nextState.children).map(child => stopChild(child)), [], undefined);
2387
2409
  }
2388
2410
  function selectTransitions(event, nextState) {
2389
2411
  return nextState.machine.getTransitionData(nextState, event);
@@ -2550,7 +2572,9 @@ function resolveRaise(_, snapshot, args, actionParams, {
2550
2572
  }) {
2551
2573
  const delaysMap = snapshot.machine.implementations.delays;
2552
2574
  if (typeof eventOrExpr === 'string') {
2553
- throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
2575
+ throw new Error(
2576
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
2577
+ `Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
2554
2578
  }
2555
2579
  const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
2556
2580
  let resolvedDelay;
@@ -2567,7 +2591,7 @@ function resolveRaise(_, snapshot, args, actionParams, {
2567
2591
  event: resolvedEvent,
2568
2592
  id,
2569
2593
  delay: resolvedDelay
2570
- }];
2594
+ }, undefined];
2571
2595
  }
2572
2596
  function executeRaise(actorScope, params) {
2573
2597
  const {
@@ -2590,7 +2614,7 @@ function executeRaise(actorScope, params) {
2590
2614
  * @param eventType The event to raise.
2591
2615
  */
2592
2616
  function raise(eventOrExpr, options) {
2593
- function raise(args, params) {
2617
+ function raise(_args, _params) {
2594
2618
  }
2595
2619
  raise.type = 'xstate.raise';
2596
2620
  raise.event = eventOrExpr;
@@ -3,8 +3,8 @@
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-b1e0b9a9.cjs.js');
7
- var log = require('./log-98fcce74.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-60cebf03.cjs.js');
7
+ var log = require('./log-a019fbd0.cjs.js');
8
8
  require('../dev/dist/xstate-dev.cjs.js');
9
9
 
10
10
  /**
@@ -224,10 +224,10 @@ class StateNode {
224
224
  systemId
225
225
  } = invokeConfig;
226
226
  const resolvedId = invokeConfig.id ?? guards_dist_xstateGuards.createInvokeId(this.id, i);
227
- const resolvedSrc = typeof src === 'string' ? src : `xstate.invoke.${guards_dist_xstateGuards.createInvokeId(this.id, i)}`;
227
+ const sourceName = typeof src === 'string' ? src : `xstate.invoke.${guards_dist_xstateGuards.createInvokeId(this.id, i)}`;
228
228
  return {
229
229
  ...invokeConfig,
230
- src: resolvedSrc,
230
+ src: sourceName,
231
231
  id: resolvedId,
232
232
  systemId: systemId,
233
233
  toJSON() {
@@ -239,7 +239,7 @@ class StateNode {
239
239
  return {
240
240
  ...invokeDefValues,
241
241
  type: 'xstate.invoke',
242
- src: resolvedSrc,
242
+ src: sourceName,
243
243
  id: resolvedId
244
244
  };
245
245
  }
@@ -422,7 +422,7 @@ class StateMachine {
422
422
  * @param event The received event
423
423
  */
424
424
  transition(snapshot, event, actorScope) {
425
- return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope).snapshot;
425
+ return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope, []).snapshot;
426
426
  }
427
427
 
428
428
  /**
@@ -433,7 +433,7 @@ class StateMachine {
433
433
  * @param event The received event
434
434
  */
435
435
  microstep(snapshot, event, actorScope) {
436
- return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope).microstates;
436
+ return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope, []).microstates;
437
437
  }
438
438
  getTransitionData(snapshot, event) {
439
439
  return guards_dist_xstateGuards.transitionNode(this.root, snapshot.value, snapshot, event) || [];
@@ -463,7 +463,7 @@ class StateMachine {
463
463
  input: event.input,
464
464
  self
465
465
  });
466
- return guards_dist_xstateGuards.resolveActionsAndContext(preInitial, initEvent, actorScope, [log.assign(assignment)], internalQueue);
466
+ return guards_dist_xstateGuards.resolveActionsAndContext(preInitial, initEvent, actorScope, [log.assign(assignment)], internalQueue, undefined);
467
467
  }
468
468
  return preInitial;
469
469
  }
@@ -541,13 +541,13 @@ class StateMachine {
541
541
  children,
542
542
  _nodes: Array.from(guards_dist_xstateGuards.getAllStateNodes(guards_dist_xstateGuards.getStateNodes(this.root, snapshot.value)))
543
543
  }, this);
544
- let seen = new Set();
544
+ const seen = new Set();
545
545
  function reviveContext(contextPart, children) {
546
546
  if (seen.has(contextPart)) {
547
547
  return;
548
548
  }
549
549
  seen.add(contextPart);
550
- for (let key in contextPart) {
550
+ for (const key in contextPart) {
551
551
  const value = contextPart[key];
552
552
  if (value && typeof value === 'object') {
553
553
  if ('xstate$$type' in value && value.xstate$$type === guards_dist_xstateGuards.$$ACTOR_TYPE) {
@@ -563,10 +563,6 @@ class StateMachine {
563
563
  }
564
564
  }
565
565
 
566
- // this is not 100% accurate since we can't make parallel regions required in the result
567
- // `TTestValue` doesn't encode this information anyhow for us to be able to do that
568
- // this is fine for most practical use cases anyway though
569
-
570
566
  /**
571
567
  * Creates a state machine (statechart) with the given configuration.
572
568
  *
@@ -624,10 +620,13 @@ function createInertActorScope(actorLogic) {
624
620
  sessionId: '',
625
621
  stopChild: () => {},
626
622
  system: self.system,
627
- emit: () => {}
623
+ emit: () => {},
624
+ actionExecutor: () => {}
628
625
  };
629
626
  return inertActorScope;
630
627
  }
628
+
629
+ /** @deprecated Use `initialTransition(…)` instead. */
631
630
  function getInitialSnapshot(actorLogic, ...[input]) {
632
631
  const actorScope = createInertActorScope(actorLogic);
633
632
  return actorLogic.getInitialSnapshot(actorScope, input);
@@ -640,6 +639,7 @@ function getInitialSnapshot(actorLogic, ...[input]) {
640
639
  * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
641
640
  * used.
642
641
  *
642
+ * @deprecated Use `transition(…)` instead.
643
643
  * @example
644
644
  *
645
645
  * ```ts
@@ -694,6 +694,9 @@ function setup({
694
694
  };
695
695
  }
696
696
 
697
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
698
+
699
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
697
700
  class SimulatedClock {
698
701
  constructor() {
699
702
  this.timeouts = new Map();
@@ -794,6 +797,39 @@ function toPromise(actor) {
794
797
  });
795
798
  }
796
799
 
800
+ /**
801
+ * Given actor `logic`, a `snapshot`, and an `event`, returns a tuple of the
802
+ * `nextSnapshot` and `actions` to execute.
803
+ *
804
+ * This is a pure function that does not execute `actions`.
805
+ */
806
+ function transition(logic, snapshot, event) {
807
+ const executableActions = [];
808
+ const actorScope = createInertActorScope(logic);
809
+ actorScope.actionExecutor = action => {
810
+ executableActions.push(action);
811
+ };
812
+ const nextSnapshot = logic.transition(snapshot, event, actorScope);
813
+ return [nextSnapshot, executableActions];
814
+ }
815
+
816
+ /**
817
+ * Given actor `logic` and optional `input`, returns a tuple of the
818
+ * `nextSnapshot` and `actions` to execute from the initial transition (no
819
+ * previous state).
820
+ *
821
+ * This is a pure function that does not execute `actions`.
822
+ */
823
+ function initialTransition(logic, ...[input]) {
824
+ const executableActions = [];
825
+ const actorScope = createInertActorScope(logic);
826
+ actorScope.actionExecutor = action => {
827
+ executableActions.push(action);
828
+ };
829
+ const nextSnapshot = logic.getInitialSnapshot(actorScope, input);
830
+ return [nextSnapshot, executableActions];
831
+ }
832
+
797
833
  const defaultWaitForOptions = {
798
834
  timeout: Infinity // much more than 10 seconds
799
835
  };
@@ -829,6 +865,7 @@ function waitFor(actorRef, predicate, options) {
829
865
  signal
830
866
  } = resolvedOptions;
831
867
  if (signal?.aborted) {
868
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
832
869
  rej(signal.reason);
833
870
  return;
834
871
  }
@@ -857,6 +894,7 @@ function waitFor(actorRef, predicate, options) {
857
894
  * `abort` event
858
895
  */
859
896
  let abortListener;
897
+ // eslint-disable-next-line prefer-const
860
898
  let sub; // avoid TDZ when disposing synchronously
861
899
 
862
900
  // See if the current snapshot already matches the predicate
@@ -870,6 +908,7 @@ function waitFor(actorRef, predicate, options) {
870
908
  abortListener = () => {
871
909
  dispose();
872
910
  // XState does not "own" the signal, so we should reject with its reason (if any)
911
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
873
912
  rej(signal.reason);
874
913
  };
875
914
  signal.addEventListener('abort', abortListener);
@@ -878,6 +917,7 @@ function waitFor(actorRef, predicate, options) {
878
917
  next: checkEmitted,
879
918
  error: err => {
880
919
  dispose();
920
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
881
921
  rej(err);
882
922
  },
883
923
  complete: () => {
@@ -930,6 +970,8 @@ exports.assertEvent = assertEvent;
930
970
  exports.createMachine = createMachine;
931
971
  exports.getInitialSnapshot = getInitialSnapshot;
932
972
  exports.getNextSnapshot = getNextSnapshot;
973
+ exports.initialTransition = initialTransition;
933
974
  exports.setup = setup;
934
975
  exports.toPromise = toPromise;
976
+ exports.transition = transition;
935
977
  exports.waitFor = waitFor;
@@ -23,6 +23,7 @@ export {
23
23
  getInitialSnapshot,
24
24
  getNextSnapshot,
25
25
  getStateNodes,
26
+ initialTransition,
26
27
  interpret,
27
28
  isMachineSnapshot,
28
29
  log,
@@ -40,5 +41,6 @@ export {
40
41
  stopChild,
41
42
  toObserver,
42
43
  toPromise,
44
+ transition,
43
45
  waitFor
44
46
  } from "./xstate.cjs.js";