xstate 5.0.0-beta.32 → 5.0.0-beta.34

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 (42) 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 +31 -20
  8. package/actors/dist/xstate-actors.development.cjs.js +31 -20
  9. package/actors/dist/xstate-actors.development.esm.js +31 -20
  10. package/actors/dist/xstate-actors.esm.js +31 -20
  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/dist/declarations/src/index.d.ts +1 -0
  14. package/dist/declarations/src/interpreter.d.ts +1 -1
  15. package/dist/declarations/src/stateUtils.d.ts +2 -2
  16. package/dist/declarations/src/system.d.ts +22 -0
  17. package/dist/declarations/src/types.d.ts +4 -5
  18. package/dist/declarations/src/utils.d.ts +0 -1
  19. package/dist/{interpreter-05e11c15.cjs.js → interpreter-1301970f.cjs.js} +77 -32
  20. package/dist/{interpreter-a2236840.development.cjs.js → interpreter-70ed62f2.development.cjs.js} +80 -32
  21. package/dist/{interpreter-d5fa7ce0.esm.js → interpreter-83f7f2d4.esm.js} +78 -33
  22. package/dist/{interpreter-e4d2487f.development.esm.js → interpreter-dee56dc8.development.esm.js} +81 -33
  23. package/dist/{raise-6fbd4513.development.esm.js → raise-05f8b2a6.development.esm.js} +52 -33
  24. package/dist/{raise-90808d65.cjs.js → raise-1dd65455.cjs.js} +52 -33
  25. package/dist/{raise-b4bfe138.development.cjs.js → raise-38b707c0.development.cjs.js} +52 -33
  26. package/dist/{raise-6a68d0cc.esm.js → raise-b5cfe1bb.esm.js} +52 -33
  27. package/dist/{send-e5f0f3f6.esm.js → send-0b5eda0c.esm.js} +15 -9
  28. package/dist/{send-4163d2af.development.cjs.js → send-3764c866.development.cjs.js} +15 -9
  29. package/dist/{send-7baeedcb.development.esm.js → send-9526366e.development.esm.js} +15 -9
  30. package/dist/{send-72e85cc6.cjs.js → send-fe94de2b.cjs.js} +15 -9
  31. package/dist/xstate.cjs.js +6 -31
  32. package/dist/xstate.development.cjs.js +6 -31
  33. package/dist/xstate.development.esm.js +9 -34
  34. package/dist/xstate.esm.js +9 -34
  35. package/dist/xstate.umd.min.js +1 -1
  36. package/dist/xstate.umd.min.js.map +1 -1
  37. package/guards/dist/xstate-guards.cjs.js +2 -2
  38. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  39. package/guards/dist/xstate-guards.development.esm.js +2 -2
  40. package/guards/dist/xstate-guards.esm.js +2 -2
  41. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  42. package/package.json +1 -1
@@ -147,13 +147,14 @@ function reportUnhandledError(err) {
147
147
 
148
148
  const symbolObservable = (() => typeof Symbol === 'function' && Symbol.observable || '@@observable')();
149
149
 
150
- function createSystem() {
151
- let sessionIdCounter = 0;
150
+ let idCounter = 0;
151
+ function createSystem(rootActor) {
152
152
  const children = new Map();
153
153
  const keyedActors = new Map();
154
154
  const reverseKeyedActors = new WeakMap();
155
+ const observers = new Set();
155
156
  const system = {
156
- _bookId: () => `x:${sessionIdCounter++}`,
157
+ _bookId: () => `x:${idCounter++}`,
157
158
  _register: (sessionId, actorRef) => {
158
159
  children.set(sessionId, actorRef);
159
160
  return sessionId;
@@ -176,6 +177,25 @@ function createSystem() {
176
177
  }
177
178
  keyedActors.set(systemId, actorRef);
178
179
  reverseKeyedActors.set(actorRef, systemId);
180
+ },
181
+ inspect: observer => {
182
+ observers.add(observer);
183
+ },
184
+ _sendInspectionEvent: event => {
185
+ const resolvedInspectionEvent = {
186
+ ...event,
187
+ rootId: rootActor.sessionId
188
+ };
189
+ observers.forEach(observer => observer.next?.(resolvedInspectionEvent));
190
+ },
191
+ _relay: (source, target, event) => {
192
+ system._sendInspectionEvent({
193
+ type: '@xstate.event',
194
+ sourceRef: source,
195
+ targetRef: target,
196
+ event
197
+ });
198
+ target._send(event);
179
199
  }
180
200
  };
181
201
  return system;
@@ -406,10 +426,14 @@ class Actor {
406
426
  logger,
407
427
  parent,
408
428
  id,
409
- systemId
429
+ systemId,
430
+ inspect
410
431
  } = resolvedOptions;
411
- const self = this;
412
- this.system = parent?.system ?? createSystem();
432
+ this.system = parent?.system ?? createSystem(this);
433
+ if (inspect && !parent) {
434
+ // Always inspect at the system-level
435
+ this.system.inspect(toObserver(inspect));
436
+ }
413
437
  if (systemId) {
414
438
  this._systemId = systemId;
415
439
  this.system._set(systemId, this);
@@ -423,7 +447,7 @@ class Actor {
423
447
  this.src = resolvedOptions.src;
424
448
  this.ref = this;
425
449
  this._actorContext = {
426
- self,
450
+ self: this,
427
451
  id: this.id,
428
452
  sessionId: this.sessionId,
429
453
  logger: this.logger,
@@ -442,6 +466,10 @@ class Actor {
442
466
  // Ensure that the send method is bound to this Actor instance
443
467
  // if destructured
444
468
  this.send = this.send.bind(this);
469
+ this.system._sendInspectionEvent({
470
+ type: '@xstate.actor',
471
+ actorRef: this
472
+ });
445
473
  this._initState();
446
474
  }
447
475
  _initState() {
@@ -450,7 +478,7 @@ class Actor {
450
478
 
451
479
  // array of functions to defer
452
480
 
453
- update(snapshot) {
481
+ update(snapshot, event) {
454
482
  // Update state
455
483
  this._state = snapshot;
456
484
 
@@ -472,14 +500,24 @@ class Actor {
472
500
  this._stopProcedure();
473
501
  this._complete();
474
502
  this._doneEvent = createDoneActorEvent(this.id, this._state.output);
475
- this._parent?.send(this._doneEvent);
503
+ if (this._parent) {
504
+ this.system._relay(this, this._parent, this._doneEvent);
505
+ }
476
506
  break;
477
507
  case 'error':
478
508
  this._stopProcedure();
479
509
  this._error(this._state.error);
480
- this._parent?.send(createErrorActorEvent(this.id, this._state.error));
510
+ if (this._parent) {
511
+ this.system._relay(this, this._parent, createErrorActorEvent(this.id, this._state.error));
512
+ }
481
513
  break;
482
514
  }
515
+ this.system._sendInspectionEvent({
516
+ type: '@xstate.snapshot',
517
+ actorRef: this,
518
+ event,
519
+ snapshot
520
+ });
483
521
  }
484
522
  subscribe(nextListenerOrObserver, errorListener, completeListener) {
485
523
  const observer = toObserver(nextListenerOrObserver, errorListener, completeListener);
@@ -512,12 +550,19 @@ class Actor {
512
550
  this.system._set(this._systemId, this);
513
551
  }
514
552
  this.status = ActorStatus.Running;
553
+ const initEvent = createInitEvent(this.options.input);
554
+ this.system._sendInspectionEvent({
555
+ type: '@xstate.event',
556
+ sourceRef: this._parent,
557
+ targetRef: this,
558
+ event: initEvent
559
+ });
515
560
  const status = this._state.status;
516
561
  switch (status) {
517
562
  case 'done':
518
- // a state machine can be "done" upon intialization (it could reach a final state using initial microsteps)
563
+ // a state machine can be "done" upon initialization (it could reach a final state using initial microsteps)
519
564
  // we still need to complete observers, flush deferreds etc
520
- this.update(this._state);
565
+ this.update(this._state, initEvent);
521
566
  // fallthrough
522
567
  case 'error':
523
568
  // TODO: rethink cleanup of observers, mailbox, etc
@@ -537,7 +582,7 @@ class Actor {
537
582
  // TODO: this notifies all subscribers but usually this is redundant
538
583
  // there is no real change happening here
539
584
  // we need to rethink if this needs to be refactored
540
- this.update(this._state);
585
+ this.update(this._state, initEvent);
541
586
  if (this.options.devTools) {
542
587
  this.attachDevTools();
543
588
  }
@@ -565,7 +610,7 @@ class Actor {
565
610
  this._parent?.send(createErrorActorEvent(this.id, err));
566
611
  return;
567
612
  }
568
- this.update(nextState);
613
+ this.update(nextState, event);
569
614
  if (event.type === XSTATE_STOP) {
570
615
  this._stopProcedure();
571
616
  this._complete();
@@ -651,14 +696,9 @@ class Actor {
651
696
  }
652
697
 
653
698
  /**
654
- * Sends an event to the running Actor to trigger a transition.
655
- *
656
- * @param event The event to send
699
+ * @internal
657
700
  */
658
- send(event) {
659
- if (typeof event === 'string') {
660
- throw new Error(`Only event objects may be sent to actors; use .send({ type: "${event}" }) instead`);
661
- }
701
+ _send(event) {
662
702
  if (this.status === ActorStatus.Stopped) {
663
703
  // do nothing
664
704
  {
@@ -670,19 +710,27 @@ class Actor {
670
710
  this.mailbox.enqueue(event);
671
711
  }
672
712
 
713
+ /**
714
+ * Sends an event to the running Actor to trigger a transition.
715
+ *
716
+ * @param event The event to send
717
+ */
718
+ send(event) {
719
+ if (typeof event === 'string') {
720
+ throw new Error(`Only event objects may be sent to actors; use .send({ type: "${event}" }) instead`);
721
+ }
722
+ this.system._relay(undefined, this, event);
723
+ }
724
+
673
725
  // TODO: make private (and figure out a way to do this within the machine)
674
- delaySend({
675
- event,
676
- id,
677
- delay,
678
- to
679
- }) {
726
+ delaySend(params) {
727
+ const {
728
+ event,
729
+ id,
730
+ delay
731
+ } = params;
680
732
  const timerId = this.clock.setTimeout(() => {
681
- if (to) {
682
- to.send(event);
683
- } else {
684
- this.send(event);
685
- }
733
+ this.system._relay(this, params.to ?? this, event);
686
734
  }, delay);
687
735
 
688
736
  // TODO: consider the rehydration story here
@@ -744,4 +792,4 @@ const interpret = createActor;
744
792
  * @deprecated Use `Actor` instead.
745
793
  */
746
794
 
747
- export { Actor as A, InterpreterStatus as I, NULL_EVENT as N, STATE_DELIMITER as S, WILDCARD as W, XSTATE_INIT as X, toTransitionConfigArray as a, createInitEvent as b, createInvokeId as c, createActor as d, matchesState as e, ActorStatus as f, interpret as g, toObserver as h, isErrorActorEvent as i, XSTATE_STOP as j, createErrorActorEvent as k, toStateValue as l, mapValues as m, STATE_IDENTIFIER as n, normalizeTarget as o, pathToStateValue as p, toStatePath as q, resolveReferencedActor as r, createDoneStateEvent as s, toArray as t, resolveOutput as u, isArray as v, createAfterEvent as w, flatten as x, XSTATE_ERROR as y };
795
+ export { Actor as A, InterpreterStatus as I, NULL_EVENT as N, STATE_DELIMITER as S, WILDCARD as W, XSTATE_STOP as X, toTransitionConfigArray as a, createInitEvent as b, createInvokeId as c, createActor as d, matchesState as e, ActorStatus as f, interpret as g, toObserver as h, isErrorActorEvent as i, createErrorActorEvent as j, toStateValue as k, STATE_IDENTIFIER as l, mapValues as m, normalizeTarget as n, toStatePath as o, pathToStateValue as p, createDoneStateEvent as q, resolveReferencedActor as r, resolveOutput as s, toArray as t, XSTATE_INIT as u, isArray as v, createAfterEvent as w, flatten as x, XSTATE_ERROR as y };
@@ -1,4 +1,4 @@
1
- import { r as resolveReferencedActor, d as createActor, f as ActorStatus, k as createErrorActorEvent, l as toStateValue, n as STATE_IDENTIFIER, o as normalizeTarget, t as toArray, N as NULL_EVENT, a as toTransitionConfigArray, S as STATE_DELIMITER, q as toStatePath, s as createDoneStateEvent, u as resolveOutput, W as WILDCARD, j as XSTATE_STOP, X as XSTATE_INIT, v as isArray, w as createAfterEvent, x as flatten, e as matchesState } from './interpreter-e4d2487f.development.esm.js';
1
+ import { r as resolveReferencedActor, d as createActor, f as ActorStatus, j as createErrorActorEvent, k as toStateValue, l as STATE_IDENTIFIER, n as normalizeTarget, t as toArray, N as NULL_EVENT, a as toTransitionConfigArray, S as STATE_DELIMITER, o as toStatePath, q as createDoneStateEvent, s as resolveOutput, W as WILDCARD, X as XSTATE_STOP, u as XSTATE_INIT, v as isArray, w as createAfterEvent, x as flatten, e as matchesState } from './interpreter-dee56dc8.development.esm.js';
2
2
 
3
3
  const cache = new WeakMap();
4
4
  function memo(object, key, fn) {
@@ -916,31 +916,33 @@ function microstep(transitions, currentState, actorCtx, event, isInitial) {
916
916
  }
917
917
 
918
918
  function microstepProcedure(transitions, currentState, mutConfiguration, event, actorCtx, isInitial) {
919
- const actions = [];
920
919
  const historyValue = {
921
920
  ...currentState.historyValue
922
921
  };
923
922
  const filteredTransitions = removeConflictingTransitions(transitions, mutConfiguration, historyValue);
924
923
  const internalQueue = [...currentState._internalQueue];
924
+ // TODO: this `cloneState` is really just a hack to prevent infinite loops
925
+ // we need to take another look at how internal queue is managed
926
+ let nextState = cloneState(currentState, {
927
+ _internalQueue: []
928
+ });
925
929
 
926
930
  // Exit states
927
931
  if (!isInitial) {
928
- exitStates(filteredTransitions, mutConfiguration, historyValue, actions);
932
+ nextState = exitStates(nextState, event, actorCtx, filteredTransitions, mutConfiguration, historyValue);
929
933
  }
930
934
 
931
935
  // Execute transition content
932
- actions.push(...filteredTransitions.flatMap(t => t.actions));
936
+ nextState = resolveActionsAndContext(nextState, event, actorCtx, filteredTransitions.flatMap(t => t.actions));
933
937
 
934
938
  // Enter states
935
- enterStates(event, filteredTransitions, mutConfiguration, actions, internalQueue, currentState, historyValue, isInitial, actorCtx);
939
+ nextState = enterStates(nextState, event, actorCtx, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial);
936
940
  const nextConfiguration = [...mutConfiguration];
937
941
  const done = isInFinalState(nextConfiguration);
938
942
  if (done) {
939
- const finalActions = nextConfiguration.sort((a, b) => b.order - a.order).flatMap(state => state.exit);
940
- actions.push(...finalActions);
943
+ nextState = resolveActionsAndContext(nextState, event, actorCtx, nextConfiguration.sort((a, b) => b.order - a.order).flatMap(state => state.exit));
941
944
  }
942
945
  try {
943
- const nextState = resolveActionsAndContext(actions, event, currentState, actorCtx);
944
946
  const output = done ? getOutput(nextConfiguration, nextState.context, event, actorCtx.self) : undefined;
945
947
  internalQueue.push(...nextState._internalQueue);
946
948
  return cloneState(currentState, {
@@ -958,7 +960,8 @@ function microstepProcedure(transitions, currentState, mutConfiguration, event,
958
960
  throw e;
959
961
  }
960
962
  }
961
- function enterStates(event, filteredTransitions, mutConfiguration, actions, internalQueue, currentState, historyValue, isInitial, actorContext) {
963
+ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial) {
964
+ let nextState = currentState;
962
965
  const statesToEnter = new Set();
963
966
  const statesForDefaultEntry = new Set();
964
967
  computeEntrySet(filteredTransitions, historyValue, statesForDefaultEntry, statesToEnter);
@@ -969,24 +972,26 @@ function enterStates(event, filteredTransitions, mutConfiguration, actions, inte
969
972
  }
970
973
  for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
971
974
  mutConfiguration.add(stateNodeToEnter);
972
- for (const invokeDef of stateNodeToEnter.invoke) {
973
- actions.push(invoke(invokeDef));
974
- }
975
+ const actions = [];
975
976
 
976
977
  // Add entry actions
977
978
  actions.push(...stateNodeToEnter.entry);
979
+ for (const invokeDef of stateNodeToEnter.invoke) {
980
+ actions.push(invoke(invokeDef));
981
+ }
978
982
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
979
983
  for (const stateNode of statesForDefaultEntry) {
980
984
  const initialActions = stateNode.initial.actions;
981
985
  actions.push(...initialActions);
982
986
  }
983
987
  }
988
+ nextState = resolveActionsAndContext(nextState, event, actorCtx, actions, stateNodeToEnter.invoke.map(invokeDef => invokeDef.id));
984
989
  if (stateNodeToEnter.type === 'final') {
985
990
  const parent = stateNodeToEnter.parent;
986
991
  if (!parent.parent) {
987
992
  continue;
988
993
  }
989
- internalQueue.push(createDoneStateEvent(parent.id, stateNodeToEnter.output ? resolveOutput(stateNodeToEnter.output, currentState.context, event, actorContext.self) : undefined));
994
+ internalQueue.push(createDoneStateEvent(parent.id, stateNodeToEnter.output ? resolveOutput(stateNodeToEnter.output, nextState.context, event, actorCtx.self) : undefined));
990
995
  if (parent.parent) {
991
996
  const grandparent = parent.parent;
992
997
  if (grandparent.type === 'parallel') {
@@ -997,6 +1002,7 @@ function enterStates(event, filteredTransitions, mutConfiguration, actions, inte
997
1002
  }
998
1003
  }
999
1004
  }
1005
+ return nextState;
1000
1006
  }
1001
1007
  function computeEntrySet(transitions, historyValue, statesForDefaultEntry, statesToEnter) {
1002
1008
  for (const t of transitions) {
@@ -1070,7 +1076,8 @@ function addAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, history
1070
1076
  }
1071
1077
  }
1072
1078
  }
1073
- function exitStates(transitions, mutConfiguration, historyValue, actions) {
1079
+ function exitStates(currentState, event, actorCtx, transitions, mutConfiguration, historyValue) {
1080
+ let nextState = currentState;
1074
1081
  const statesToExit = computeExitSet(transitions, mutConfiguration, historyValue);
1075
1082
  statesToExit.sort((a, b) => b.order - a.order);
1076
1083
 
@@ -1089,27 +1096,24 @@ function exitStates(transitions, mutConfiguration, historyValue, actions) {
1089
1096
  }
1090
1097
  }
1091
1098
  for (const s of statesToExit) {
1092
- actions.push(...s.exit, ...s.invoke.map(def => stop(def.id)));
1099
+ nextState = resolveActionsAndContext(nextState, event, actorCtx, [...s.exit, ...s.invoke.map(def => stop(def.id))]);
1093
1100
  mutConfiguration.delete(s);
1094
1101
  }
1102
+ return nextState;
1095
1103
  }
1096
- function resolveActionsAndContext(actions, event, currentState, actorCtx) {
1104
+ function resolveActionsAndContextWorker(currentState, event, actorCtx, actions, extra, retries) {
1097
1105
  const {
1098
1106
  machine
1099
1107
  } = currentState;
1100
- // TODO: this `cloneState` is really just a hack to prevent infinite loops
1101
- // we need to take another look at how internal queue is managed
1102
- let intermediateState = cloneState(currentState, {
1103
- _internalQueue: []
1104
- });
1108
+ let intermediateState = currentState;
1105
1109
  for (const action of actions) {
1106
1110
  const isInline = typeof action === 'function';
1107
- const resolved = isInline ? action :
1111
+ const resolvedAction = isInline ? action :
1108
1112
  // the existing type of `.actions` assumes non-nullable `TExpressionAction`
1109
1113
  // it's fine to cast this here to get a common type and lack of errors in the rest of the code
1110
1114
  // our logic below makes sure that we call those 2 "variants" correctly
1111
1115
  machine.implementations.actions[typeof action === 'string' ? action : action.type];
1112
- if (!resolved) {
1116
+ if (!resolvedAction) {
1113
1117
  continue;
1114
1118
  }
1115
1119
  const actionArgs = {
@@ -1129,20 +1133,25 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
1129
1133
  // TS isn't able to narrow it down here
1130
1134
  action
1131
1135
  };
1132
- if (!('resolve' in resolved)) {
1136
+ if (!('resolve' in resolvedAction)) {
1133
1137
  if (actorCtx?.self.status === ActorStatus.Running) {
1134
- resolved(actionArgs);
1138
+ resolvedAction(actionArgs);
1135
1139
  } else {
1136
- actorCtx?.defer(() => resolved(actionArgs));
1140
+ actorCtx?.defer(() => {
1141
+ resolvedAction(actionArgs);
1142
+ });
1137
1143
  }
1138
1144
  continue;
1139
1145
  }
1140
- const builtinAction = resolved;
1141
- const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolved // this holds all params
1142
- );
1143
-
1146
+ const builtinAction = resolvedAction;
1147
+ const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolvedAction,
1148
+ // this holds all params
1149
+ extra);
1144
1150
  intermediateState = nextState;
1145
- if ('execute' in resolved) {
1151
+ if ('retryResolve' in builtinAction) {
1152
+ retries?.push([builtinAction, params]);
1153
+ }
1154
+ if ('execute' in builtinAction) {
1146
1155
  if (actorCtx?.self.status === ActorStatus.Running) {
1147
1156
  builtinAction.execute(actorCtx, params);
1148
1157
  } else {
@@ -1150,11 +1159,21 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
1150
1159
  }
1151
1160
  }
1152
1161
  if (actions) {
1153
- intermediateState = resolveActionsAndContext(actions, event, intermediateState, actorCtx);
1162
+ intermediateState = resolveActionsAndContextWorker(intermediateState, event, actorCtx, actions, extra, retries);
1154
1163
  }
1155
1164
  }
1156
1165
  return intermediateState;
1157
1166
  }
1167
+ function resolveActionsAndContext(currentState, event, actorCtx, actions, deferredActorIds) {
1168
+ const retries = deferredActorIds ? [] : undefined;
1169
+ const nextState = resolveActionsAndContextWorker(currentState, event, actorCtx, actions, deferredActorIds && {
1170
+ deferredActorIds
1171
+ }, retries);
1172
+ retries?.forEach(([builtinAction, params]) => {
1173
+ builtinAction.retryResolve(actorCtx, nextState, params);
1174
+ });
1175
+ return nextState;
1176
+ }
1158
1177
  function macrostep(state, event, actorCtx) {
1159
1178
  if (event.type === WILDCARD) {
1160
1179
  throw new Error(`An event cannot have the wildcard type ('${WILDCARD}')`);
@@ -1214,7 +1233,7 @@ function stopStep(event, nextState, actorCtx) {
1214
1233
  for (const child of Object.values(nextState.children)) {
1215
1234
  actions.push(stop(child));
1216
1235
  }
1217
- return resolveActionsAndContext(actions, event, nextState, actorCtx);
1236
+ return resolveActionsAndContext(nextState, event, actorCtx, actions);
1218
1237
  }
1219
1238
  function selectTransitions(event, nextState) {
1220
1239
  return nextState.machine.getTransitionData(nextState, event);
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var interpreter = require('./interpreter-05e11c15.cjs.js');
3
+ var interpreter = require('./interpreter-1301970f.cjs.js');
4
4
 
5
5
  const cache = new WeakMap();
6
6
  function memo(object, key, fn) {
@@ -887,31 +887,33 @@ function microstep(transitions, currentState, actorCtx, event, isInitial) {
887
887
  }
888
888
 
889
889
  function microstepProcedure(transitions, currentState, mutConfiguration, event, actorCtx, isInitial) {
890
- const actions = [];
891
890
  const historyValue = {
892
891
  ...currentState.historyValue
893
892
  };
894
893
  const filteredTransitions = removeConflictingTransitions(transitions, mutConfiguration, historyValue);
895
894
  const internalQueue = [...currentState._internalQueue];
895
+ // TODO: this `cloneState` is really just a hack to prevent infinite loops
896
+ // we need to take another look at how internal queue is managed
897
+ let nextState = cloneState(currentState, {
898
+ _internalQueue: []
899
+ });
896
900
 
897
901
  // Exit states
898
902
  if (!isInitial) {
899
- exitStates(filteredTransitions, mutConfiguration, historyValue, actions);
903
+ nextState = exitStates(nextState, event, actorCtx, filteredTransitions, mutConfiguration, historyValue);
900
904
  }
901
905
 
902
906
  // Execute transition content
903
- actions.push(...filteredTransitions.flatMap(t => t.actions));
907
+ nextState = resolveActionsAndContext(nextState, event, actorCtx, filteredTransitions.flatMap(t => t.actions));
904
908
 
905
909
  // Enter states
906
- enterStates(event, filteredTransitions, mutConfiguration, actions, internalQueue, currentState, historyValue, isInitial, actorCtx);
910
+ nextState = enterStates(nextState, event, actorCtx, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial);
907
911
  const nextConfiguration = [...mutConfiguration];
908
912
  const done = isInFinalState(nextConfiguration);
909
913
  if (done) {
910
- const finalActions = nextConfiguration.sort((a, b) => b.order - a.order).flatMap(state => state.exit);
911
- actions.push(...finalActions);
914
+ nextState = resolveActionsAndContext(nextState, event, actorCtx, nextConfiguration.sort((a, b) => b.order - a.order).flatMap(state => state.exit));
912
915
  }
913
916
  try {
914
- const nextState = resolveActionsAndContext(actions, event, currentState, actorCtx);
915
917
  const output = done ? getOutput(nextConfiguration, nextState.context, event, actorCtx.self) : undefined;
916
918
  internalQueue.push(...nextState._internalQueue);
917
919
  return cloneState(currentState, {
@@ -929,7 +931,8 @@ function microstepProcedure(transitions, currentState, mutConfiguration, event,
929
931
  throw e;
930
932
  }
931
933
  }
932
- function enterStates(event, filteredTransitions, mutConfiguration, actions, internalQueue, currentState, historyValue, isInitial, actorContext) {
934
+ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial) {
935
+ let nextState = currentState;
933
936
  const statesToEnter = new Set();
934
937
  const statesForDefaultEntry = new Set();
935
938
  computeEntrySet(filteredTransitions, historyValue, statesForDefaultEntry, statesToEnter);
@@ -940,24 +943,26 @@ function enterStates(event, filteredTransitions, mutConfiguration, actions, inte
940
943
  }
941
944
  for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
942
945
  mutConfiguration.add(stateNodeToEnter);
943
- for (const invokeDef of stateNodeToEnter.invoke) {
944
- actions.push(invoke(invokeDef));
945
- }
946
+ const actions = [];
946
947
 
947
948
  // Add entry actions
948
949
  actions.push(...stateNodeToEnter.entry);
950
+ for (const invokeDef of stateNodeToEnter.invoke) {
951
+ actions.push(invoke(invokeDef));
952
+ }
949
953
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
950
954
  for (const stateNode of statesForDefaultEntry) {
951
955
  const initialActions = stateNode.initial.actions;
952
956
  actions.push(...initialActions);
953
957
  }
954
958
  }
959
+ nextState = resolveActionsAndContext(nextState, event, actorCtx, actions, stateNodeToEnter.invoke.map(invokeDef => invokeDef.id));
955
960
  if (stateNodeToEnter.type === 'final') {
956
961
  const parent = stateNodeToEnter.parent;
957
962
  if (!parent.parent) {
958
963
  continue;
959
964
  }
960
- internalQueue.push(interpreter.createDoneStateEvent(parent.id, stateNodeToEnter.output ? interpreter.resolveOutput(stateNodeToEnter.output, currentState.context, event, actorContext.self) : undefined));
965
+ internalQueue.push(interpreter.createDoneStateEvent(parent.id, stateNodeToEnter.output ? interpreter.resolveOutput(stateNodeToEnter.output, nextState.context, event, actorCtx.self) : undefined));
961
966
  if (parent.parent) {
962
967
  const grandparent = parent.parent;
963
968
  if (grandparent.type === 'parallel') {
@@ -968,6 +973,7 @@ function enterStates(event, filteredTransitions, mutConfiguration, actions, inte
968
973
  }
969
974
  }
970
975
  }
976
+ return nextState;
971
977
  }
972
978
  function computeEntrySet(transitions, historyValue, statesForDefaultEntry, statesToEnter) {
973
979
  for (const t of transitions) {
@@ -1041,7 +1047,8 @@ function addAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, history
1041
1047
  }
1042
1048
  }
1043
1049
  }
1044
- function exitStates(transitions, mutConfiguration, historyValue, actions) {
1050
+ function exitStates(currentState, event, actorCtx, transitions, mutConfiguration, historyValue) {
1051
+ let nextState = currentState;
1045
1052
  const statesToExit = computeExitSet(transitions, mutConfiguration, historyValue);
1046
1053
  statesToExit.sort((a, b) => b.order - a.order);
1047
1054
 
@@ -1060,27 +1067,24 @@ function exitStates(transitions, mutConfiguration, historyValue, actions) {
1060
1067
  }
1061
1068
  }
1062
1069
  for (const s of statesToExit) {
1063
- actions.push(...s.exit, ...s.invoke.map(def => stop(def.id)));
1070
+ nextState = resolveActionsAndContext(nextState, event, actorCtx, [...s.exit, ...s.invoke.map(def => stop(def.id))]);
1064
1071
  mutConfiguration.delete(s);
1065
1072
  }
1073
+ return nextState;
1066
1074
  }
1067
- function resolveActionsAndContext(actions, event, currentState, actorCtx) {
1075
+ function resolveActionsAndContextWorker(currentState, event, actorCtx, actions, extra, retries) {
1068
1076
  const {
1069
1077
  machine
1070
1078
  } = currentState;
1071
- // TODO: this `cloneState` is really just a hack to prevent infinite loops
1072
- // we need to take another look at how internal queue is managed
1073
- let intermediateState = cloneState(currentState, {
1074
- _internalQueue: []
1075
- });
1079
+ let intermediateState = currentState;
1076
1080
  for (const action of actions) {
1077
1081
  const isInline = typeof action === 'function';
1078
- const resolved = isInline ? action :
1082
+ const resolvedAction = isInline ? action :
1079
1083
  // the existing type of `.actions` assumes non-nullable `TExpressionAction`
1080
1084
  // it's fine to cast this here to get a common type and lack of errors in the rest of the code
1081
1085
  // our logic below makes sure that we call those 2 "variants" correctly
1082
1086
  machine.implementations.actions[typeof action === 'string' ? action : action.type];
1083
- if (!resolved) {
1087
+ if (!resolvedAction) {
1084
1088
  continue;
1085
1089
  }
1086
1090
  const actionArgs = {
@@ -1100,20 +1104,25 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
1100
1104
  // TS isn't able to narrow it down here
1101
1105
  action
1102
1106
  };
1103
- if (!('resolve' in resolved)) {
1107
+ if (!('resolve' in resolvedAction)) {
1104
1108
  if (actorCtx?.self.status === interpreter.ActorStatus.Running) {
1105
- resolved(actionArgs);
1109
+ resolvedAction(actionArgs);
1106
1110
  } else {
1107
- actorCtx?.defer(() => resolved(actionArgs));
1111
+ actorCtx?.defer(() => {
1112
+ resolvedAction(actionArgs);
1113
+ });
1108
1114
  }
1109
1115
  continue;
1110
1116
  }
1111
- const builtinAction = resolved;
1112
- const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolved // this holds all params
1113
- );
1114
-
1117
+ const builtinAction = resolvedAction;
1118
+ const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolvedAction,
1119
+ // this holds all params
1120
+ extra);
1115
1121
  intermediateState = nextState;
1116
- if ('execute' in resolved) {
1122
+ if ('retryResolve' in builtinAction) {
1123
+ retries?.push([builtinAction, params]);
1124
+ }
1125
+ if ('execute' in builtinAction) {
1117
1126
  if (actorCtx?.self.status === interpreter.ActorStatus.Running) {
1118
1127
  builtinAction.execute(actorCtx, params);
1119
1128
  } else {
@@ -1121,11 +1130,21 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
1121
1130
  }
1122
1131
  }
1123
1132
  if (actions) {
1124
- intermediateState = resolveActionsAndContext(actions, event, intermediateState, actorCtx);
1133
+ intermediateState = resolveActionsAndContextWorker(intermediateState, event, actorCtx, actions, extra, retries);
1125
1134
  }
1126
1135
  }
1127
1136
  return intermediateState;
1128
1137
  }
1138
+ function resolveActionsAndContext(currentState, event, actorCtx, actions, deferredActorIds) {
1139
+ const retries = deferredActorIds ? [] : undefined;
1140
+ const nextState = resolveActionsAndContextWorker(currentState, event, actorCtx, actions, deferredActorIds && {
1141
+ deferredActorIds
1142
+ }, retries);
1143
+ retries?.forEach(([builtinAction, params]) => {
1144
+ builtinAction.retryResolve(actorCtx, nextState, params);
1145
+ });
1146
+ return nextState;
1147
+ }
1129
1148
  function macrostep(state, event, actorCtx) {
1130
1149
  let nextState = state;
1131
1150
  const states = [];
@@ -1182,7 +1201,7 @@ function stopStep(event, nextState, actorCtx) {
1182
1201
  for (const child of Object.values(nextState.children)) {
1183
1202
  actions.push(stop(child));
1184
1203
  }
1185
- return resolveActionsAndContext(actions, event, nextState, actorCtx);
1204
+ return resolveActionsAndContext(nextState, event, actorCtx, actions);
1186
1205
  }
1187
1206
  function selectTransitions(event, nextState) {
1188
1207
  return nextState.machine.getTransitionData(nextState, event);