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
@@ -156,7 +156,7 @@ function toStatePath(stateId) {
156
156
  if (isArray(stateId)) {
157
157
  return stateId;
158
158
  }
159
- let result = [];
159
+ const result = [];
160
160
  let segment = '';
161
161
  for (let i = 0; i < stateId.length; i++) {
162
162
  const char = stateId.charCodeAt(i);
@@ -421,6 +421,7 @@ function createSystem(rootActor, options) {
421
421
  return system;
422
422
  }
423
423
 
424
+ let executingCustomAction = false;
424
425
  const $$ACTOR_TYPE = 1;
425
426
 
426
427
  // those values are currently used by @xstate/react directly so it's important to keep the assigned values in sync
@@ -541,10 +542,37 @@ class Actor {
541
542
  if (!listeners && !wildcardListener) {
542
543
  return;
543
544
  }
544
- const allListeners = new Set([...(listeners ? listeners.values() : []), ...(wildcardListener ? wildcardListener.values() : [])]);
545
- for (const handler of Array.from(allListeners)) {
545
+ const allListeners = [...(listeners ? listeners.values() : []), ...(wildcardListener ? wildcardListener.values() : [])];
546
+ for (const handler of allListeners) {
546
547
  handler(emittedEvent);
547
548
  }
549
+ },
550
+ actionExecutor: action => {
551
+ const exec = () => {
552
+ this._actorScope.system._sendInspectionEvent({
553
+ type: '@xstate.action',
554
+ actorRef: this,
555
+ action: {
556
+ type: action.type,
557
+ params: action.params
558
+ }
559
+ });
560
+ if (!action.exec) {
561
+ return;
562
+ }
563
+ const saveExecutingCustomAction = executingCustomAction;
564
+ try {
565
+ executingCustomAction = true;
566
+ action.exec(action.info, action.params);
567
+ } finally {
568
+ executingCustomAction = saveExecutingCustomAction;
569
+ }
570
+ };
571
+ if (this._processingStatus === ProcessingStatus.Running) {
572
+ exec();
573
+ } else {
574
+ this._deferred.push(exec);
575
+ }
548
576
  }
549
577
  };
550
578
 
@@ -1073,11 +1101,13 @@ function resolveCancel(_, snapshot, actionArgs, actionParams, {
1073
1101
  sendId
1074
1102
  }) {
1075
1103
  const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs, actionParams) : sendId;
1076
- return [snapshot, resolvedSendId];
1104
+ return [snapshot, {
1105
+ sendId: resolvedSendId
1106
+ }, undefined];
1077
1107
  }
1078
- function executeCancel(actorScope, resolvedSendId) {
1108
+ function executeCancel(actorScope, params) {
1079
1109
  actorScope.defer(() => {
1080
- actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId);
1110
+ actorScope.system.scheduler.cancel(actorScope.self, params.sendId);
1081
1111
  });
1082
1112
  }
1083
1113
  /**
@@ -1113,7 +1143,7 @@ function executeCancel(actorScope, resolvedSendId) {
1113
1143
  * @param sendId The `id` of the `sendTo(...)` action to cancel.
1114
1144
  */
1115
1145
  function cancel(sendId) {
1116
- function cancel(args, params) {
1146
+ function cancel(_args, _params) {
1117
1147
  }
1118
1148
  cancel.type = 'xstate.cancel';
1119
1149
  cancel.sendId = sendId;
@@ -1132,18 +1162,20 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1132
1162
  const logic = typeof src === 'string' ? resolveReferencedActor(snapshot.machine, src) : src;
1133
1163
  const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
1134
1164
  let actorRef;
1165
+ let resolvedInput = undefined;
1135
1166
  if (logic) {
1167
+ resolvedInput = typeof input === 'function' ? input({
1168
+ context: snapshot.context,
1169
+ event: actionArgs.event,
1170
+ self: actorScope.self
1171
+ }) : input;
1136
1172
  actorRef = createActor(logic, {
1137
1173
  id: resolvedId,
1138
1174
  src,
1139
1175
  parent: actorScope.self,
1140
1176
  syncSnapshot,
1141
1177
  systemId,
1142
- input: typeof input === 'function' ? input({
1143
- context: snapshot.context,
1144
- event: actionArgs.event,
1145
- self: actorScope.self
1146
- }) : input
1178
+ input: resolvedInput
1147
1179
  });
1148
1180
  }
1149
1181
  return [cloneMachineSnapshot(snapshot, {
@@ -1153,11 +1185,13 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1153
1185
  }
1154
1186
  }), {
1155
1187
  id,
1156
- actorRef
1157
- }];
1188
+ systemId,
1189
+ actorRef,
1190
+ src,
1191
+ input: resolvedInput
1192
+ }, undefined];
1158
1193
  }
1159
1194
  function executeSpawn(actorScope, {
1160
- id,
1161
1195
  actorRef
1162
1196
  }) {
1163
1197
  if (!actorRef) {
@@ -1176,9 +1210,9 @@ function spawnChild(...[src, {
1176
1210
  input,
1177
1211
  syncSnapshot = false
1178
1212
  } = {}]) {
1179
- function spawnChild(args, params) {
1213
+ function spawnChild(_args, _params) {
1180
1214
  }
1181
- spawnChild.type = 'snapshot.spawnChild';
1215
+ spawnChild.type = 'xstate.spawnChild';
1182
1216
  spawnChild.id = id;
1183
1217
  spawnChild.systemId = systemId;
1184
1218
  spawnChild.src = src;
@@ -1203,7 +1237,7 @@ function resolveStop(_, snapshot, args, actionParams, {
1203
1237
  }
1204
1238
  return [cloneMachineSnapshot(snapshot, {
1205
1239
  children
1206
- }), resolvedActorRef];
1240
+ }), resolvedActorRef, undefined];
1207
1241
  }
1208
1242
  function executeStop(actorScope, actorRef) {
1209
1243
  if (!actorRef) {
@@ -1235,7 +1269,7 @@ function executeStop(actorScope, actorRef) {
1235
1269
  * @param actorRef The actor to stop.
1236
1270
  */
1237
1271
  function stopChild(actorRef) {
1238
- function stop(args, params) {
1272
+ function stop(_args, _params) {
1239
1273
  }
1240
1274
  stop.type = 'xstate.stopChild';
1241
1275
  stop.actorRef = actorRef;
@@ -1262,7 +1296,7 @@ function checkStateIn(snapshot, _, {
1262
1296
  return snapshot.matches(stateValue);
1263
1297
  }
1264
1298
  function stateIn(stateValue) {
1265
- function stateIn(args, params) {
1299
+ function stateIn() {
1266
1300
  return false;
1267
1301
  }
1268
1302
  stateIn.check = checkStateIn;
@@ -1308,7 +1342,7 @@ function checkNot(snapshot, {
1308
1342
  * @returns A guard
1309
1343
  */
1310
1344
  function not(guard) {
1311
- function not(args, params) {
1345
+ function not(_args, _params) {
1312
1346
  return false;
1313
1347
  }
1314
1348
  not.check = checkNot;
@@ -1354,7 +1388,7 @@ function checkAnd(snapshot, {
1354
1388
  * @returns A guard action object
1355
1389
  */
1356
1390
  function and(guards) {
1357
- function and(args, params) {
1391
+ function and(_args, _params) {
1358
1392
  return false;
1359
1393
  }
1360
1394
  and.check = checkAnd;
@@ -1400,7 +1434,7 @@ function checkOr(snapshot, {
1400
1434
  * @returns A guard action object
1401
1435
  */
1402
1436
  function or(guards) {
1403
- function or(args, params) {
1437
+ function or(_args, _params) {
1404
1438
  return false;
1405
1439
  }
1406
1440
  or.check = checkOr;
@@ -1578,7 +1612,7 @@ function getDelayedTransitions(stateNode) {
1578
1612
  if (!afterConfig) {
1579
1613
  return [];
1580
1614
  }
1581
- const mutateEntryExit = (delay, i) => {
1615
+ const mutateEntryExit = delay => {
1582
1616
  const afterEvent = createAfterEvent(delay, stateNode.id);
1583
1617
  const eventType = afterEvent.type;
1584
1618
  stateNode.entry.push(raise(afterEvent, {
@@ -1588,7 +1622,7 @@ function getDelayedTransitions(stateNode) {
1588
1622
  stateNode.exit.push(cancel(eventType));
1589
1623
  return eventType;
1590
1624
  };
1591
- const delayedTransitions = Object.keys(afterConfig).flatMap((delay, i) => {
1625
+ const delayedTransitions = Object.keys(afterConfig).flatMap(delay => {
1592
1626
  const configTransition = afterConfig[delay];
1593
1627
  const resolvedTransition = typeof configTransition === 'string' ? {
1594
1628
  target: configTransition
@@ -1673,7 +1707,9 @@ function formatTransitions(stateNode) {
1673
1707
  function formatInitialTransition(stateNode, _target) {
1674
1708
  const resolvedTarget = typeof _target === 'string' ? stateNode.states[_target] : _target ? stateNode.states[_target.target] : undefined;
1675
1709
  if (!resolvedTarget && _target) {
1676
- throw new Error(`Initial state node "${_target}" not found on parent state node #${stateNode.id}`);
1710
+ throw new Error(
1711
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string
1712
+ `Initial state node "${_target}" not found on parent state node #${stateNode.id}`);
1677
1713
  }
1678
1714
  const transition = {
1679
1715
  source: stateNode,
@@ -1783,7 +1819,7 @@ function getStateNodeByPath(stateNode, statePath) {
1783
1819
  if (typeof statePath === 'string' && isStateId(statePath)) {
1784
1820
  try {
1785
1821
  return stateNode.machine.getStateNodeById(statePath);
1786
- } catch (e) {
1822
+ } catch {
1787
1823
  // try individual paths
1788
1824
  // throw e;
1789
1825
  }
@@ -2012,18 +2048,20 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2012
2048
 
2013
2049
  // Exit states
2014
2050
  if (!isInitial) {
2015
- [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue);
2051
+ [nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
2016
2052
  }
2017
2053
 
2018
2054
  // Execute transition content
2019
- nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue);
2055
+ nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue, undefined);
2020
2056
 
2021
2057
  // Enter states
2022
2058
  nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
2023
2059
  const nextStateNodes = [...mutStateNodeSet];
2024
2060
  if (nextState.status === 'done') {
2025
- nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue);
2061
+ nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
2026
2062
  }
2063
+
2064
+ // eslint-disable-next-line no-useless-catch
2027
2065
  try {
2028
2066
  if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
2029
2067
  return nextState;
@@ -2193,7 +2231,7 @@ function addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultE
2193
2231
  function addProperAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, historyValue, statesForDefaultEntry) {
2194
2232
  addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultEntry, getProperAncestors(stateNode, toStateNode));
2195
2233
  }
2196
- function exitStates(currentSnapshot, event, actorScope, transitions, mutStateNodeSet, historyValue, internalQueue) {
2234
+ function exitStates(currentSnapshot, event, actorScope, transitions, mutStateNodeSet, historyValue, internalQueue, _actionExecutor) {
2197
2235
  let nextSnapshot = currentSnapshot;
2198
2236
  const statesToExit = computeExitSet(transitions, mutStateNodeSet, historyValue);
2199
2237
  statesToExit.sort((a, b) => b.order - a.order);
@@ -2217,12 +2255,14 @@ function exitStates(currentSnapshot, event, actorScope, transitions, mutStateNod
2217
2255
  }
2218
2256
  }
2219
2257
  for (const s of statesToExit) {
2220
- nextSnapshot = resolveActionsAndContext(nextSnapshot, event, actorScope, [...s.exit, ...s.invoke.map(def => stopChild(def.id))], internalQueue);
2258
+ nextSnapshot = resolveActionsAndContext(nextSnapshot, event, actorScope, [...s.exit, ...s.invoke.map(def => stopChild(def.id))], internalQueue, undefined);
2221
2259
  mutStateNodeSet.delete(s);
2222
2260
  }
2223
2261
  return [nextSnapshot, changedHistory || historyValue];
2224
2262
  }
2225
- let executingCustomAction = false;
2263
+ function getAction(machine, actionType) {
2264
+ return machine.implementations.actions[actionType];
2265
+ }
2226
2266
  function resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope, actions, extra, retries) {
2227
2267
  const {
2228
2268
  machine
@@ -2234,10 +2274,8 @@ function resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope,
2234
2274
  // the existing type of `.actions` assumes non-nullable `TExpressionAction`
2235
2275
  // it's fine to cast this here to get a common type and lack of errors in the rest of the code
2236
2276
  // our logic below makes sure that we call those 2 "variants" correctly
2237
- machine.implementations.actions[typeof action === 'string' ? action : action.type];
2238
- if (!resolvedAction) {
2239
- continue;
2240
- }
2277
+
2278
+ getAction(machine, typeof action === 'string' ? action : action.type);
2241
2279
  const actionArgs = {
2242
2280
  context: intermediateSnapshot.context,
2243
2281
  event,
@@ -2248,30 +2286,13 @@ function resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope,
2248
2286
  context: intermediateSnapshot.context,
2249
2287
  event
2250
2288
  }) : action.params : undefined;
2251
- function executeAction() {
2252
- actorScope.system._sendInspectionEvent({
2253
- type: '@xstate.action',
2254
- actorRef: actorScope.self,
2255
- action: {
2256
- type: typeof action === 'string' ? action : typeof action === 'object' ? action.type : action.name || '(anonymous)',
2257
- params: actionParams
2258
- }
2289
+ if (!resolvedAction || !('resolve' in resolvedAction)) {
2290
+ actorScope.actionExecutor({
2291
+ type: typeof action === 'string' ? action : typeof action === 'object' ? action.type : action.name || '(anonymous)',
2292
+ info: actionArgs,
2293
+ params: actionParams,
2294
+ exec: resolvedAction
2259
2295
  });
2260
- try {
2261
- executingCustomAction = resolvedAction;
2262
- resolvedAction(actionArgs, actionParams);
2263
- } finally {
2264
- executingCustomAction = false;
2265
- }
2266
- }
2267
- if (!('resolve' in resolvedAction)) {
2268
- if (actorScope.self._processingStatus === ProcessingStatus.Running) {
2269
- executeAction();
2270
- } else {
2271
- actorScope.defer(() => {
2272
- executeAction();
2273
- });
2274
- }
2275
2296
  continue;
2276
2297
  }
2277
2298
  const builtinAction = resolvedAction;
@@ -2283,11 +2304,12 @@ function resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope,
2283
2304
  retries?.push([builtinAction, params]);
2284
2305
  }
2285
2306
  if ('execute' in builtinAction) {
2286
- if (actorScope.self._processingStatus === ProcessingStatus.Running) {
2287
- builtinAction.execute(actorScope, params);
2288
- } else {
2289
- actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
2290
- }
2307
+ actorScope.actionExecutor({
2308
+ type: builtinAction.type,
2309
+ info: actionArgs,
2310
+ params,
2311
+ exec: builtinAction.execute.bind(null, actorScope, params)
2312
+ });
2291
2313
  }
2292
2314
  if (actions) {
2293
2315
  intermediateSnapshot = resolveAndExecuteActionsWithContext(intermediateSnapshot, event, actorScope, actions, extra, retries);
@@ -2306,7 +2328,7 @@ function resolveActionsAndContext(currentSnapshot, event, actorScope, actions, i
2306
2328
  });
2307
2329
  return nextState;
2308
2330
  }
2309
- function macrostep(snapshot, event, actorScope, internalQueue = []) {
2331
+ function macrostep(snapshot, event, actorScope, internalQueue) {
2310
2332
  let nextSnapshot = snapshot;
2311
2333
  const microstates = [];
2312
2334
  function addMicrostate(microstate, event, transitions) {
@@ -2385,7 +2407,7 @@ function macrostep(snapshot, event, actorScope, internalQueue = []) {
2385
2407
  };
2386
2408
  }
2387
2409
  function stopChildren(nextState, event, actorScope) {
2388
- return resolveActionsAndContext(nextState, event, actorScope, Object.values(nextState.children).map(child => stopChild(child)), []);
2410
+ return resolveActionsAndContext(nextState, event, actorScope, Object.values(nextState.children).map(child => stopChild(child)), [], undefined);
2389
2411
  }
2390
2412
  function selectTransitions(event, nextState) {
2391
2413
  return nextState.machine.getTransitionData(nextState, event);
@@ -2552,7 +2574,9 @@ function resolveRaise(_, snapshot, args, actionParams, {
2552
2574
  }) {
2553
2575
  const delaysMap = snapshot.machine.implementations.delays;
2554
2576
  if (typeof eventOrExpr === 'string') {
2555
- throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
2577
+ throw new Error(
2578
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
2579
+ `Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
2556
2580
  }
2557
2581
  const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
2558
2582
  let resolvedDelay;
@@ -2569,7 +2593,7 @@ function resolveRaise(_, snapshot, args, actionParams, {
2569
2593
  event: resolvedEvent,
2570
2594
  id,
2571
2595
  delay: resolvedDelay
2572
- }];
2596
+ }, undefined];
2573
2597
  }
2574
2598
  function executeRaise(actorScope, params) {
2575
2599
  const {
@@ -2592,7 +2616,7 @@ function executeRaise(actorScope, params) {
2592
2616
  * @param eventType The event to raise.
2593
2617
  */
2594
2618
  function raise(eventOrExpr, options) {
2595
- function raise(args, params) {
2619
+ function raise(_args, _params) {
2596
2620
  }
2597
2621
  raise.type = 'xstate.raise';
2598
2622
  raise.event = eventOrExpr;