xstate 6.0.0-alpha.53 → 6.0.0-alpha.55

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 (58) hide show
  1. package/dist/{createActor-4d94dd53.development.cjs.js → createActor-2213c48b.development.cjs.js} +67 -15
  2. package/dist/{createActor-41e3bbc3.development.esm.js → createActor-3dc6cdb7.development.esm.js} +67 -15
  3. package/dist/{createActor-20277025.cjs.js → createActor-c4723a2b.cjs.js} +67 -15
  4. package/dist/{createActor-6fa87eea.esm.js → createActor-c7c704c2.esm.js} +67 -15
  5. package/dist/declarations/src/StateMachine.d.ts +14 -6
  6. package/dist/declarations/src/StateNode.d.ts +14 -14
  7. package/dist/declarations/src/createActor.d.ts +11 -2
  8. package/dist/declarations/src/createMachine.d.ts +7 -6
  9. package/dist/declarations/src/graph/types.d.ts +1 -3
  10. package/dist/declarations/src/machineVersions.d.ts +2 -10
  11. package/dist/declarations/src/setup.d.ts +54 -38
  12. package/dist/declarations/src/transitionActions.d.ts +1 -1
  13. package/dist/declarations/src/types.d.ts +68 -14
  14. package/dist/declarations/src/types.v6.d.ts +49 -41
  15. package/dist/{effectDescriptor-fa21710c.cjs.js → effectDescriptor-3121155c.cjs.js} +1 -1
  16. package/dist/{effectDescriptor-e64efd18.development.esm.js → effectDescriptor-95202c94.development.esm.js} +1 -1
  17. package/dist/{effectDescriptor-c56bd9d5.esm.js → effectDescriptor-9cb5c006.esm.js} +1 -1
  18. package/dist/{effectDescriptor-6991ab90.development.cjs.js → effectDescriptor-c0976206.development.cjs.js} +1 -1
  19. package/dist/{types-63cc2400.development.esm.js → types-33de9b3d.development.esm.js} +47 -3
  20. package/dist/{types-ab7eb7a3.esm.js → types-69f39448.esm.js} +47 -3
  21. package/dist/{types-5d934c5c.cjs.js → types-802dc45c.cjs.js} +47 -3
  22. package/dist/{types-84a9c6a1.development.cjs.js → types-ec6c763a.development.cjs.js} +47 -3
  23. package/dist/xstate-actors.cjs.js +1 -1
  24. package/dist/xstate-actors.development.cjs.js +1 -1
  25. package/dist/xstate-actors.development.esm.js +2 -2
  26. package/dist/xstate-actors.esm.js +2 -2
  27. package/dist/xstate-actors.umd.min.js +1 -1
  28. package/dist/xstate-actors.umd.min.js.map +1 -1
  29. package/dist/xstate-durable.cjs.js +2 -2
  30. package/dist/xstate-durable.development.cjs.js +2 -2
  31. package/dist/xstate-durable.development.esm.js +2 -2
  32. package/dist/xstate-durable.esm.js +2 -2
  33. package/dist/xstate-durable.umd.min.js +1 -1
  34. package/dist/xstate-durable.umd.min.js.map +1 -1
  35. package/dist/xstate-graph.cjs.js +1 -1
  36. package/dist/xstate-graph.development.cjs.js +1 -1
  37. package/dist/xstate-graph.development.esm.js +1 -1
  38. package/dist/xstate-graph.esm.js +1 -1
  39. package/dist/xstate-graph.umd.min.js +1 -1
  40. package/dist/xstate-graph.umd.min.js.map +1 -1
  41. package/dist/xstate-scxml.cjs.js +2 -2
  42. package/dist/xstate-scxml.development.cjs.js +2 -2
  43. package/dist/xstate-scxml.development.esm.js +2 -2
  44. package/dist/xstate-scxml.esm.js +2 -2
  45. package/dist/xstate-scxml.umd.min.js +1 -1
  46. package/dist/xstate-scxml.umd.min.js.map +1 -1
  47. package/dist/xstate-validation.cjs.js +1 -1
  48. package/dist/xstate-validation.development.cjs.js +1 -1
  49. package/dist/xstate-validation.development.esm.js +1 -1
  50. package/dist/xstate-validation.esm.js +1 -1
  51. package/dist/xstate-validation.umd.min.js.map +1 -1
  52. package/dist/xstate.cjs.js +7 -3
  53. package/dist/xstate.development.cjs.js +7 -3
  54. package/dist/xstate.development.esm.js +9 -5
  55. package/dist/xstate.esm.js +9 -5
  56. package/dist/xstate.umd.min.js +1 -1
  57. package/dist/xstate.umd.min.js.map +1 -1
  58. package/package.json +1 -1
@@ -1179,7 +1179,7 @@ async function executeEffects(effects, runtime) {
1179
1179
  await effect.exec(runtime);
1180
1180
  }
1181
1181
  }
1182
- function resolveActionsWithContext(currentSnapshot, event, actorScope, actions) {
1182
+ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions, internalEvents) {
1183
1183
  let intermediateSnapshot = currentSnapshot;
1184
1184
  const executableActions = [];
1185
1185
  for (const action of actions) {
@@ -1203,7 +1203,20 @@ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions)
1203
1203
  };
1204
1204
  const isInline = typeof action === 'function';
1205
1205
  const actionRecord = getTransitionActionRecord(action);
1206
- const resolvedAction = isInline ? action : actionRecord ? actionRecord.action.bind(null, ...actionRecord.args) : false;
1206
+ let resolvedActionArgs = actionRecord?.args;
1207
+ if (actionRecord?.action === builtInActions['@xstate.sendTo'] && typeof actionRecord.args[1] === 'string') {
1208
+ const childId = actionRecord.args[1];
1209
+ const target = Object.hasOwn(intermediateSnapshot.children, childId) ? intermediateSnapshot.children[childId] : undefined;
1210
+ if (!target) {
1211
+ internalEvents?.push(createErrorPlatformEvent('communication', {
1212
+ message: `Unable to send event to unknown child '${childId}'`,
1213
+ event: actionRecord.args[2]
1214
+ }));
1215
+ continue;
1216
+ }
1217
+ resolvedActionArgs = [actionRecord.args[0], target, ...actionRecord.args.slice(2)];
1218
+ }
1219
+ const resolvedAction = isInline ? action : actionRecord ? actionRecord.action.bind(null, ...resolvedActionArgs) : false;
1207
1220
  let actionParams = undefined;
1208
1221
  if (typeof action === 'object' && action !== null) {
1209
1222
  const {
@@ -1244,13 +1257,13 @@ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions)
1244
1257
  continue;
1245
1258
  }
1246
1259
  if (!resolvedAction || !('resolve' in resolvedAction)) {
1247
- const builtInFields = typeof action === 'object' && action !== null && 'action' in action && typeof action.action === 'function' ? getBuiltInActionFields(action.action, action.args) : undefined;
1260
+ const builtInFields = typeof action === 'object' && action !== null && 'action' in action && typeof action.action === 'function' ? getBuiltInActionFields(action.action, resolvedActionArgs) : undefined;
1248
1261
  const isEmittedEvent = typeof action === 'object' && action !== null && !actionRecord;
1249
1262
  const executableAction = {
1250
1263
  kind: builtInFields ? 'builtin' : isEmittedEvent ? 'emit' : 'action',
1251
1264
  type: typeof action === 'object' ? 'action' in action && typeof action.action === 'function' ? action.action.name ?? '(anonymous)' : action.type : action.name || '(anonymous)',
1252
1265
  params: builtInFields ? undefined : actionParams,
1253
- args: typeof action === 'object' && 'action' in action ? action.args : [],
1266
+ args: typeof action === 'object' && 'action' in action ? resolvedActionArgs : [],
1254
1267
  ...(builtInFields ? {} : isEmittedEvent ? {
1255
1268
  source: actorScope.self,
1256
1269
  event: action
@@ -2798,12 +2811,12 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2798
2811
  context: nextContext
2799
2812
  });
2800
2813
  }
2801
- const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, exitActions);
2814
+ const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, exitActions, internalQueue);
2802
2815
  nextState = resolvedState;
2803
2816
  executableActions.push(...resolvedActions);
2804
2817
  const invokeStopActions = getInvokeStopActions(exitStateNode, nextState.children);
2805
2818
  if (invokeStopActions.length) {
2806
- const [stoppedState, stopEffects] = resolveActionsWithContext(nextState, event, actorScope, invokeStopActions);
2819
+ const [stoppedState, stopEffects] = resolveActionsWithContext(nextState, event, actorScope, invokeStopActions, internalQueue);
2807
2820
  nextState = stoppedState;
2808
2821
  executableActions.push(...stopEffects);
2809
2822
  }
@@ -2934,14 +2947,18 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2934
2947
  }
2935
2948
  }
2936
2949
  };
2950
+ const enteredTargetsByTransition = new Map();
2937
2951
  for (const transition of filteredTransitions) {
2938
2952
  const domain = getTransitionDomain(transition, currentSnapshot, getCurrentTransitionResult);
2939
2953
  const {
2940
2954
  targets,
2941
2955
  reenter
2942
2956
  } = getCurrentTransitionResult(transition);
2957
+ const enteredTargets = new Set();
2958
+ enteredTargetsByTransition.set(transition, enteredTargets);
2943
2959
  for (const targetNode of targets ?? []) {
2944
2960
  if (!isHistoryNode(targetNode) && (transition.source !== targetNode || transition.source !== domain || reenter)) {
2961
+ enteredTargets.add(targetNode);
2945
2962
  statesToEnter.add(targetNode);
2946
2963
  statesForDefaultEntry.add(targetNode);
2947
2964
  }
@@ -2977,7 +2994,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2977
2994
  input
2978
2995
  } = getCurrentTransitionResult(transition);
2979
2996
  if (input && targets) {
2980
- for (const targetNode of targets) {
2997
+ for (const targetNode of targets.filter(targetNode => enteredTargetsByTransition.get(transition)?.has(targetNode))) {
2981
2998
  stateInputMap[targetNode.id] = input;
2982
2999
  stateInputsChanged = true;
2983
3000
  }
@@ -3076,7 +3093,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3076
3093
  }
3077
3094
  }
3078
3095
  }
3079
- const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, actions);
3096
+ const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, actions, internalQueue);
3080
3097
  nextState = resolvedState;
3081
3098
  actions.length = 0;
3082
3099
  executableActions.push(...resolvedActions);
@@ -3125,7 +3142,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3125
3142
  };
3126
3143
 
3127
3144
  // Execute transition content
3128
- const [resolvedTransitionState, transitionExecutableActions] = resolveActionsWithContext(nextState, event, actorScope, transitionActions);
3145
+ const [resolvedTransitionState, transitionExecutableActions] = resolveActionsWithContext(nextState, event, actorScope, transitionActions, internalQueue);
3129
3146
  nextState = resolvedTransitionState;
3130
3147
  executableActions.push(...transitionExecutableActions);
3131
3148
  if (context && context !== currentSnapshot.context) {
@@ -3152,7 +3169,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3152
3169
  }
3153
3170
  }
3154
3171
  });
3155
- const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, allExitActions);
3172
+ const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, allExitActions, internalQueue);
3156
3173
  nextState = resolvedState;
3157
3174
  executableActions.push(...resolvedActions);
3158
3175
  const [stoppedState, stopEffects] = stopChildren(nextState, event, actorScope);
@@ -4127,7 +4144,10 @@ function toObserver(nextHandler, errorHandler, completionHandler) {
4127
4144
  return {
4128
4145
  next: (isObserver ? nextHandler.next : nextHandler)?.bind(self),
4129
4146
  error: (isObserver ? nextHandler.error : errorHandler)?.bind(self),
4130
- complete: (isObserver ? nextHandler.complete : completionHandler)?.bind(self)
4147
+ complete: (isObserver ? nextHandler.complete : completionHandler)?.bind(self),
4148
+ ...(isObserver && nextHandler.passive ? {
4149
+ passive: true
4150
+ } : {})
4131
4151
  };
4132
4152
  }
4133
4153
  function createInvokeId(stateNodeId, index) {
@@ -5034,6 +5054,8 @@ class Actor {
5034
5054
  this.mailbox = void 0;
5035
5055
  this._mailboxStarted = false;
5036
5056
  this.observers = void 0;
5057
+ /** Whether a consumer subscribed to this actor's error after it errored. */
5058
+ this._errorObserved = false;
5037
5059
  this.eventListeners = void 0;
5038
5060
  this.logger = void 0;
5039
5061
  /** @internal */
@@ -5228,7 +5250,15 @@ class Actor {
5228
5250
  const saveExecutingCustomAction = executingCustomAction;
5229
5251
  try {
5230
5252
  executingCustomAction = true;
5231
- void action.exec();
5253
+ const result = action.exec();
5254
+ if (result && typeof result.then === 'function') {
5255
+ void Promise.resolve(result).catch(err => {
5256
+ if (this._processingStatus === ProcessingStatus.Stopped) {
5257
+ return;
5258
+ }
5259
+ this._recoverOrError(err);
5260
+ });
5261
+ }
5232
5262
  } finally {
5233
5263
  executingCustomAction = saveExecutingCustomAction;
5234
5264
  }
@@ -5438,6 +5468,9 @@ class Actor {
5438
5468
  if (!observer.error) {
5439
5469
  reportUnhandledError(err);
5440
5470
  } else {
5471
+ if (!observer.passive) {
5472
+ this._errorObserved = true;
5473
+ }
5441
5474
  safeCall(observer.error, err);
5442
5475
  }
5443
5476
  break;
@@ -5692,22 +5725,41 @@ class Actor {
5692
5725
  this.observers?.clear();
5693
5726
  this.eventListeners?.clear();
5694
5727
  }
5728
+
5729
+ /**
5730
+ * Reports an unhandled error unless a consumer observes it before the
5731
+ * report runs. An actor can error before its creator has a chance to
5732
+ * subscribe, so the check waits one macrotask for a subscriber with an
5733
+ * `error` callback.
5734
+ */
5735
+ _reportUnlessObserved(err) {
5736
+ setTimeout(() => {
5737
+ if (!this._errorObserved) {
5738
+ reportUnhandledError(err);
5739
+ }
5740
+ });
5741
+ }
5695
5742
  _error(err) {
5696
5743
  this._stopProcedure();
5697
5744
  if (!this.observers?.size) {
5698
5745
  if (!this._parent) {
5699
- reportUnhandledError(err);
5746
+ this._reportUnlessObserved(err);
5700
5747
  }
5701
5748
  } else {
5702
5749
  let reportError = false;
5750
+ let handled = false;
5703
5751
  for (const observer of this.observers) {
5704
5752
  const errorListener = observer.error;
5705
- reportError ||= !errorListener;
5753
+ if (!observer.passive) {
5754
+ reportError ||= !errorListener;
5755
+ handled ||= !!errorListener;
5756
+ }
5706
5757
  safeCall(errorListener, err);
5707
5758
  }
5759
+ reportError ||= !handled;
5708
5760
  this.observers.clear();
5709
5761
  if (reportError) {
5710
- reportUnhandledError(err);
5762
+ this._reportUnlessObserved(err);
5711
5763
  }
5712
5764
  }
5713
5765
  this.eventListeners?.clear();
@@ -1177,7 +1177,7 @@ async function executeEffects(effects, runtime) {
1177
1177
  await effect.exec(runtime);
1178
1178
  }
1179
1179
  }
1180
- function resolveActionsWithContext(currentSnapshot, event, actorScope, actions) {
1180
+ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions, internalEvents) {
1181
1181
  let intermediateSnapshot = currentSnapshot;
1182
1182
  const executableActions = [];
1183
1183
  for (const action of actions) {
@@ -1201,7 +1201,20 @@ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions)
1201
1201
  };
1202
1202
  const isInline = typeof action === 'function';
1203
1203
  const actionRecord = getTransitionActionRecord(action);
1204
- const resolvedAction = isInline ? action : actionRecord ? actionRecord.action.bind(null, ...actionRecord.args) : false;
1204
+ let resolvedActionArgs = actionRecord?.args;
1205
+ if (actionRecord?.action === builtInActions['@xstate.sendTo'] && typeof actionRecord.args[1] === 'string') {
1206
+ const childId = actionRecord.args[1];
1207
+ const target = Object.hasOwn(intermediateSnapshot.children, childId) ? intermediateSnapshot.children[childId] : undefined;
1208
+ if (!target) {
1209
+ internalEvents?.push(createErrorPlatformEvent('communication', {
1210
+ message: `Unable to send event to unknown child '${childId}'`,
1211
+ event: actionRecord.args[2]
1212
+ }));
1213
+ continue;
1214
+ }
1215
+ resolvedActionArgs = [actionRecord.args[0], target, ...actionRecord.args.slice(2)];
1216
+ }
1217
+ const resolvedAction = isInline ? action : actionRecord ? actionRecord.action.bind(null, ...resolvedActionArgs) : false;
1205
1218
  let actionParams = undefined;
1206
1219
  if (typeof action === 'object' && action !== null) {
1207
1220
  const {
@@ -1242,13 +1255,13 @@ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions)
1242
1255
  continue;
1243
1256
  }
1244
1257
  if (!resolvedAction || !('resolve' in resolvedAction)) {
1245
- const builtInFields = typeof action === 'object' && action !== null && 'action' in action && typeof action.action === 'function' ? getBuiltInActionFields(action.action, action.args) : undefined;
1258
+ const builtInFields = typeof action === 'object' && action !== null && 'action' in action && typeof action.action === 'function' ? getBuiltInActionFields(action.action, resolvedActionArgs) : undefined;
1246
1259
  const isEmittedEvent = typeof action === 'object' && action !== null && !actionRecord;
1247
1260
  const executableAction = {
1248
1261
  kind: builtInFields ? 'builtin' : isEmittedEvent ? 'emit' : 'action',
1249
1262
  type: typeof action === 'object' ? 'action' in action && typeof action.action === 'function' ? action.action.name ?? '(anonymous)' : action.type : action.name || '(anonymous)',
1250
1263
  params: builtInFields ? undefined : actionParams,
1251
- args: typeof action === 'object' && 'action' in action ? action.args : [],
1264
+ args: typeof action === 'object' && 'action' in action ? resolvedActionArgs : [],
1252
1265
  ...(builtInFields ? {} : isEmittedEvent ? {
1253
1266
  source: actorScope.self,
1254
1267
  event: action
@@ -2796,12 +2809,12 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2796
2809
  context: nextContext
2797
2810
  });
2798
2811
  }
2799
- const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, exitActions);
2812
+ const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, exitActions, internalQueue);
2800
2813
  nextState = resolvedState;
2801
2814
  executableActions.push(...resolvedActions);
2802
2815
  const invokeStopActions = getInvokeStopActions(exitStateNode, nextState.children);
2803
2816
  if (invokeStopActions.length) {
2804
- const [stoppedState, stopEffects] = resolveActionsWithContext(nextState, event, actorScope, invokeStopActions);
2817
+ const [stoppedState, stopEffects] = resolveActionsWithContext(nextState, event, actorScope, invokeStopActions, internalQueue);
2805
2818
  nextState = stoppedState;
2806
2819
  executableActions.push(...stopEffects);
2807
2820
  }
@@ -2932,14 +2945,18 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2932
2945
  }
2933
2946
  }
2934
2947
  };
2948
+ const enteredTargetsByTransition = new Map();
2935
2949
  for (const transition of filteredTransitions) {
2936
2950
  const domain = getTransitionDomain(transition, currentSnapshot, getCurrentTransitionResult);
2937
2951
  const {
2938
2952
  targets,
2939
2953
  reenter
2940
2954
  } = getCurrentTransitionResult(transition);
2955
+ const enteredTargets = new Set();
2956
+ enteredTargetsByTransition.set(transition, enteredTargets);
2941
2957
  for (const targetNode of targets ?? []) {
2942
2958
  if (!isHistoryNode(targetNode) && (transition.source !== targetNode || transition.source !== domain || reenter)) {
2959
+ enteredTargets.add(targetNode);
2943
2960
  statesToEnter.add(targetNode);
2944
2961
  statesForDefaultEntry.add(targetNode);
2945
2962
  }
@@ -2975,7 +2992,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2975
2992
  input
2976
2993
  } = getCurrentTransitionResult(transition);
2977
2994
  if (input && targets) {
2978
- for (const targetNode of targets) {
2995
+ for (const targetNode of targets.filter(targetNode => enteredTargetsByTransition.get(transition)?.has(targetNode))) {
2979
2996
  stateInputMap[targetNode.id] = input;
2980
2997
  stateInputsChanged = true;
2981
2998
  }
@@ -3074,7 +3091,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3074
3091
  }
3075
3092
  }
3076
3093
  }
3077
- const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, actions);
3094
+ const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, actions, internalQueue);
3078
3095
  nextState = resolvedState;
3079
3096
  actions.length = 0;
3080
3097
  executableActions.push(...resolvedActions);
@@ -3123,7 +3140,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3123
3140
  };
3124
3141
 
3125
3142
  // Execute transition content
3126
- const [resolvedTransitionState, transitionExecutableActions] = resolveActionsWithContext(nextState, event, actorScope, transitionActions);
3143
+ const [resolvedTransitionState, transitionExecutableActions] = resolveActionsWithContext(nextState, event, actorScope, transitionActions, internalQueue);
3127
3144
  nextState = resolvedTransitionState;
3128
3145
  executableActions.push(...transitionExecutableActions);
3129
3146
  if (context && context !== currentSnapshot.context) {
@@ -3150,7 +3167,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3150
3167
  }
3151
3168
  }
3152
3169
  });
3153
- const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, allExitActions);
3170
+ const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, allExitActions, internalQueue);
3154
3171
  nextState = resolvedState;
3155
3172
  executableActions.push(...resolvedActions);
3156
3173
  const [stoppedState, stopEffects] = stopChildren(nextState, event, actorScope);
@@ -4125,7 +4142,10 @@ function toObserver(nextHandler, errorHandler, completionHandler) {
4125
4142
  return {
4126
4143
  next: (isObserver ? nextHandler.next : nextHandler)?.bind(self),
4127
4144
  error: (isObserver ? nextHandler.error : errorHandler)?.bind(self),
4128
- complete: (isObserver ? nextHandler.complete : completionHandler)?.bind(self)
4145
+ complete: (isObserver ? nextHandler.complete : completionHandler)?.bind(self),
4146
+ ...(isObserver && nextHandler.passive ? {
4147
+ passive: true
4148
+ } : {})
4129
4149
  };
4130
4150
  }
4131
4151
  function createInvokeId(stateNodeId, index) {
@@ -5032,6 +5052,8 @@ class Actor {
5032
5052
  this.mailbox = void 0;
5033
5053
  this._mailboxStarted = false;
5034
5054
  this.observers = void 0;
5055
+ /** Whether a consumer subscribed to this actor's error after it errored. */
5056
+ this._errorObserved = false;
5035
5057
  this.eventListeners = void 0;
5036
5058
  this.logger = void 0;
5037
5059
  /** @internal */
@@ -5226,7 +5248,15 @@ class Actor {
5226
5248
  const saveExecutingCustomAction = executingCustomAction;
5227
5249
  try {
5228
5250
  executingCustomAction = true;
5229
- void action.exec();
5251
+ const result = action.exec();
5252
+ if (result && typeof result.then === 'function') {
5253
+ void Promise.resolve(result).catch(err => {
5254
+ if (this._processingStatus === ProcessingStatus.Stopped) {
5255
+ return;
5256
+ }
5257
+ this._recoverOrError(err);
5258
+ });
5259
+ }
5230
5260
  } finally {
5231
5261
  executingCustomAction = saveExecutingCustomAction;
5232
5262
  }
@@ -5436,6 +5466,9 @@ class Actor {
5436
5466
  if (!observer.error) {
5437
5467
  reportUnhandledError(err);
5438
5468
  } else {
5469
+ if (!observer.passive) {
5470
+ this._errorObserved = true;
5471
+ }
5439
5472
  safeCall(observer.error, err);
5440
5473
  }
5441
5474
  break;
@@ -5690,22 +5723,41 @@ class Actor {
5690
5723
  this.observers?.clear();
5691
5724
  this.eventListeners?.clear();
5692
5725
  }
5726
+
5727
+ /**
5728
+ * Reports an unhandled error unless a consumer observes it before the
5729
+ * report runs. An actor can error before its creator has a chance to
5730
+ * subscribe, so the check waits one macrotask for a subscriber with an
5731
+ * `error` callback.
5732
+ */
5733
+ _reportUnlessObserved(err) {
5734
+ setTimeout(() => {
5735
+ if (!this._errorObserved) {
5736
+ reportUnhandledError(err);
5737
+ }
5738
+ });
5739
+ }
5693
5740
  _error(err) {
5694
5741
  this._stopProcedure();
5695
5742
  if (!this.observers?.size) {
5696
5743
  if (!this._parent) {
5697
- reportUnhandledError(err);
5744
+ this._reportUnlessObserved(err);
5698
5745
  }
5699
5746
  } else {
5700
5747
  let reportError = false;
5748
+ let handled = false;
5701
5749
  for (const observer of this.observers) {
5702
5750
  const errorListener = observer.error;
5703
- reportError ||= !errorListener;
5751
+ if (!observer.passive) {
5752
+ reportError ||= !errorListener;
5753
+ handled ||= !!errorListener;
5754
+ }
5704
5755
  safeCall(errorListener, err);
5705
5756
  }
5757
+ reportError ||= !handled;
5706
5758
  this.observers.clear();
5707
5759
  if (reportError) {
5708
- reportUnhandledError(err);
5760
+ this._reportUnlessObserved(err);
5709
5761
  }
5710
5762
  }
5711
5763
  this.eventListeners?.clear();
@@ -1171,7 +1171,7 @@ async function executeEffects(effects, runtime) {
1171
1171
  await effect.exec(runtime);
1172
1172
  }
1173
1173
  }
1174
- function resolveActionsWithContext(currentSnapshot, event, actorScope, actions) {
1174
+ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions, internalEvents) {
1175
1175
  let intermediateSnapshot = currentSnapshot;
1176
1176
  const executableActions = [];
1177
1177
  for (const action of actions) {
@@ -1195,7 +1195,20 @@ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions)
1195
1195
  };
1196
1196
  const isInline = typeof action === 'function';
1197
1197
  const actionRecord = getTransitionActionRecord(action);
1198
- const resolvedAction = isInline ? action : actionRecord ? actionRecord.action.bind(null, ...actionRecord.args) : false;
1198
+ let resolvedActionArgs = actionRecord?.args;
1199
+ if (actionRecord?.action === builtInActions['@xstate.sendTo'] && typeof actionRecord.args[1] === 'string') {
1200
+ const childId = actionRecord.args[1];
1201
+ const target = Object.hasOwn(intermediateSnapshot.children, childId) ? intermediateSnapshot.children[childId] : undefined;
1202
+ if (!target) {
1203
+ internalEvents?.push(createErrorPlatformEvent('communication', {
1204
+ message: `Unable to send event to unknown child '${childId}'`,
1205
+ event: actionRecord.args[2]
1206
+ }));
1207
+ continue;
1208
+ }
1209
+ resolvedActionArgs = [actionRecord.args[0], target, ...actionRecord.args.slice(2)];
1210
+ }
1211
+ const resolvedAction = isInline ? action : actionRecord ? actionRecord.action.bind(null, ...resolvedActionArgs) : false;
1199
1212
  let actionParams = undefined;
1200
1213
  if (typeof action === 'object' && action !== null) {
1201
1214
  const {
@@ -1236,13 +1249,13 @@ function resolveActionsWithContext(currentSnapshot, event, actorScope, actions)
1236
1249
  continue;
1237
1250
  }
1238
1251
  if (!resolvedAction || !('resolve' in resolvedAction)) {
1239
- const builtInFields = typeof action === 'object' && action !== null && 'action' in action && typeof action.action === 'function' ? getBuiltInActionFields(action.action, action.args) : undefined;
1252
+ const builtInFields = typeof action === 'object' && action !== null && 'action' in action && typeof action.action === 'function' ? getBuiltInActionFields(action.action, resolvedActionArgs) : undefined;
1240
1253
  const isEmittedEvent = typeof action === 'object' && action !== null && !actionRecord;
1241
1254
  const executableAction = {
1242
1255
  kind: builtInFields ? 'builtin' : isEmittedEvent ? 'emit' : 'action',
1243
1256
  type: typeof action === 'object' ? 'action' in action && typeof action.action === 'function' ? action.action.name ?? '(anonymous)' : action.type : action.name || '(anonymous)',
1244
1257
  params: builtInFields ? undefined : actionParams,
1245
- args: typeof action === 'object' && 'action' in action ? action.args : [],
1258
+ args: typeof action === 'object' && 'action' in action ? resolvedActionArgs : [],
1246
1259
  ...(builtInFields ? {} : isEmittedEvent ? {
1247
1260
  source: actorScope.self,
1248
1261
  event: action
@@ -2780,12 +2793,12 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2780
2793
  context: nextContext
2781
2794
  });
2782
2795
  }
2783
- const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, exitActions);
2796
+ const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, exitActions, internalQueue);
2784
2797
  nextState = resolvedState;
2785
2798
  executableActions.push(...resolvedActions);
2786
2799
  const invokeStopActions = getInvokeStopActions(exitStateNode, nextState.children);
2787
2800
  if (invokeStopActions.length) {
2788
- const [stoppedState, stopEffects] = resolveActionsWithContext(nextState, event, actorScope, invokeStopActions);
2801
+ const [stoppedState, stopEffects] = resolveActionsWithContext(nextState, event, actorScope, invokeStopActions, internalQueue);
2789
2802
  nextState = stoppedState;
2790
2803
  executableActions.push(...stopEffects);
2791
2804
  }
@@ -2916,14 +2929,18 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2916
2929
  }
2917
2930
  }
2918
2931
  };
2932
+ const enteredTargetsByTransition = new Map();
2919
2933
  for (const transition of filteredTransitions) {
2920
2934
  const domain = getTransitionDomain(transition, currentSnapshot, getCurrentTransitionResult);
2921
2935
  const {
2922
2936
  targets,
2923
2937
  reenter
2924
2938
  } = getCurrentTransitionResult(transition);
2939
+ const enteredTargets = new Set();
2940
+ enteredTargetsByTransition.set(transition, enteredTargets);
2925
2941
  for (const targetNode of targets ?? []) {
2926
2942
  if (!isHistoryNode(targetNode) && (transition.source !== targetNode || transition.source !== domain || reenter)) {
2943
+ enteredTargets.add(targetNode);
2927
2944
  statesToEnter.add(targetNode);
2928
2945
  statesForDefaultEntry.add(targetNode);
2929
2946
  }
@@ -2959,7 +2976,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
2959
2976
  input
2960
2977
  } = getCurrentTransitionResult(transition);
2961
2978
  if (input && targets) {
2962
- for (const targetNode of targets) {
2979
+ for (const targetNode of targets.filter(targetNode => enteredTargetsByTransition.get(transition)?.has(targetNode))) {
2963
2980
  stateInputMap[targetNode.id] = input;
2964
2981
  stateInputsChanged = true;
2965
2982
  }
@@ -3058,7 +3075,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3058
3075
  }
3059
3076
  }
3060
3077
  }
3061
- const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, actions);
3078
+ const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, actions, internalQueue);
3062
3079
  nextState = resolvedState;
3063
3080
  actions.length = 0;
3064
3081
  executableActions.push(...resolvedActions);
@@ -3107,7 +3124,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3107
3124
  };
3108
3125
 
3109
3126
  // Execute transition content
3110
- const [resolvedTransitionState, transitionExecutableActions] = resolveActionsWithContext(nextState, event, actorScope, transitionActions);
3127
+ const [resolvedTransitionState, transitionExecutableActions] = resolveActionsWithContext(nextState, event, actorScope, transitionActions, internalQueue);
3111
3128
  nextState = resolvedTransitionState;
3112
3129
  executableActions.push(...transitionExecutableActions);
3113
3130
  if (context && context !== currentSnapshot.context) {
@@ -3134,7 +3151,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
3134
3151
  }
3135
3152
  }
3136
3153
  });
3137
- const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, allExitActions);
3154
+ const [resolvedState, resolvedActions] = resolveActionsWithContext(nextState, event, actorScope, allExitActions, internalQueue);
3138
3155
  nextState = resolvedState;
3139
3156
  executableActions.push(...resolvedActions);
3140
3157
  const [stoppedState, stopEffects] = stopChildren(nextState, event, actorScope);
@@ -4103,7 +4120,10 @@ function toObserver(nextHandler, errorHandler, completionHandler) {
4103
4120
  return {
4104
4121
  next: (isObserver ? nextHandler.next : nextHandler)?.bind(self),
4105
4122
  error: (isObserver ? nextHandler.error : errorHandler)?.bind(self),
4106
- complete: (isObserver ? nextHandler.complete : completionHandler)?.bind(self)
4123
+ complete: (isObserver ? nextHandler.complete : completionHandler)?.bind(self),
4124
+ ...(isObserver && nextHandler.passive ? {
4125
+ passive: true
4126
+ } : {})
4107
4127
  };
4108
4128
  }
4109
4129
  function createInvokeId(stateNodeId, index) {
@@ -5001,6 +5021,8 @@ class Actor {
5001
5021
  this.mailbox = void 0;
5002
5022
  this._mailboxStarted = false;
5003
5023
  this.observers = void 0;
5024
+ /** Whether a consumer subscribed to this actor's error after it errored. */
5025
+ this._errorObserved = false;
5004
5026
  this.eventListeners = void 0;
5005
5027
  this.logger = void 0;
5006
5028
  /** @internal */
@@ -5195,7 +5217,15 @@ class Actor {
5195
5217
  const saveExecutingCustomAction = executingCustomAction;
5196
5218
  try {
5197
5219
  executingCustomAction = true;
5198
- void action.exec();
5220
+ const result = action.exec();
5221
+ if (result && typeof result.then === 'function') {
5222
+ void Promise.resolve(result).catch(err => {
5223
+ if (this._processingStatus === ProcessingStatus.Stopped) {
5224
+ return;
5225
+ }
5226
+ this._recoverOrError(err);
5227
+ });
5228
+ }
5199
5229
  } finally {
5200
5230
  executingCustomAction = saveExecutingCustomAction;
5201
5231
  }
@@ -5405,6 +5435,9 @@ class Actor {
5405
5435
  if (!observer.error) {
5406
5436
  reportUnhandledError(err);
5407
5437
  } else {
5438
+ if (!observer.passive) {
5439
+ this._errorObserved = true;
5440
+ }
5408
5441
  safeCall(observer.error, err);
5409
5442
  }
5410
5443
  break;
@@ -5659,22 +5692,41 @@ class Actor {
5659
5692
  this.observers?.clear();
5660
5693
  this.eventListeners?.clear();
5661
5694
  }
5695
+
5696
+ /**
5697
+ * Reports an unhandled error unless a consumer observes it before the
5698
+ * report runs. An actor can error before its creator has a chance to
5699
+ * subscribe, so the check waits one macrotask for a subscriber with an
5700
+ * `error` callback.
5701
+ */
5702
+ _reportUnlessObserved(err) {
5703
+ setTimeout(() => {
5704
+ if (!this._errorObserved) {
5705
+ reportUnhandledError(err);
5706
+ }
5707
+ });
5708
+ }
5662
5709
  _error(err) {
5663
5710
  this._stopProcedure();
5664
5711
  if (!this.observers?.size) {
5665
5712
  if (!this._parent) {
5666
- reportUnhandledError(err);
5713
+ this._reportUnlessObserved(err);
5667
5714
  }
5668
5715
  } else {
5669
5716
  let reportError = false;
5717
+ let handled = false;
5670
5718
  for (const observer of this.observers) {
5671
5719
  const errorListener = observer.error;
5672
- reportError ||= !errorListener;
5720
+ if (!observer.passive) {
5721
+ reportError ||= !errorListener;
5722
+ handled ||= !!errorListener;
5723
+ }
5673
5724
  safeCall(errorListener, err);
5674
5725
  }
5726
+ reportError ||= !handled;
5675
5727
  this.observers.clear();
5676
5728
  if (reportError) {
5677
- reportUnhandledError(err);
5729
+ this._reportUnlessObserved(err);
5678
5730
  }
5679
5731
  }
5680
5732
  this.eventListeners?.clear();