xstate 4.33.1 → 4.33.4

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.
@@ -73,6 +73,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
73
73
  sessionId: string;
74
74
  children: Map<string | number, ActorRef<any>>;
75
75
  private forwardTo;
76
+ private _outgoingQueue;
76
77
  private devTools?;
77
78
  /**
78
79
  * Creates a new Interpreter instance (i.e., service) for the given machine with the provided options, if any.
@@ -134,6 +135,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
134
135
  * @param initialState The state to start the statechart from
135
136
  */
136
137
  start(initialState?: State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta> | StateConfig<TContext, TEvent> | StateValue): this;
138
+ private _stopChildren;
137
139
  private _stop;
138
140
  /**
139
141
  * Stops the interpreter and unsubscribe all listeners.
package/es/interpreter.js CHANGED
@@ -4,7 +4,7 @@ import { isStateConfig, State, bindActionToState } from './State.js';
4
4
  import { raise, send, errorPlatform, update, error as error$1, log, stop, start, cancel } from './actionTypes.js';
5
5
  import { initEvent, doneInvoke, toActionObjects, resolveActions, error, getActionFunction } from './actions.js';
6
6
  import { IS_PRODUCTION } from './environment.js';
7
- import { warn, mapContext, toObserver, toSCXMLEvent, flatten, isFunction, isPromiseLike, isObservable, isMachine, isBehavior, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, isActor, toInvokeSource, uniqueId } from './utils.js';
7
+ import { warn, mapContext, toObserver, isFunction, toSCXMLEvent, flatten, isPromiseLike, isObservable, isMachine, isBehavior, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, isActor, toInvokeSource, uniqueId } from './utils.js';
8
8
  import { Scheduler } from './scheduler.js';
9
9
  import { createDeferredActor, isSpawnedActor } from './Actor.js';
10
10
  import { registry } from './registry.js';
@@ -58,6 +58,7 @@ function () {
58
58
  this.status = InterpreterStatus.NotStarted;
59
59
  this.children = new Map();
60
60
  this.forwardTo = new Set();
61
+ this._outgoingQueue = [];
61
62
  /**
62
63
  * Alias for Interpreter.prototype.start
63
64
  */
@@ -109,7 +110,7 @@ function () {
109
110
  // tslint:disable-next-line:semicolon
110
111
  };
111
112
 
112
- this.sendTo = function (event, to) {
113
+ this.sendTo = function (event, to, immediate) {
113
114
  var isParent = _this.parent && (to === SpecialTargets.Parent || _this.parent.id === to);
114
115
  var target = isParent ? _this.parent : isString(to) ? _this.children.get(to) || registry.get(to) : isActor(to) ? to : undefined;
115
116
 
@@ -132,14 +133,24 @@ function () {
132
133
  if (_this.status !== InterpreterStatus.Stopped || _this.parent !== target || // we need to send events to the parent from exit handlers of a machine that reached its final state
133
134
  _this.state.done) {
134
135
  // Send SCXML events to machines
135
- target.send(__assign(__assign({}, event), {
136
+ var scxmlEvent = __assign(__assign({}, event), {
136
137
  name: event.name === error$1 ? "".concat(error(_this.id)) : event.name,
137
138
  origin: _this.sessionId
138
- }));
139
+ });
140
+
141
+ if (!immediate && _this.machine.config.predictableActionArguments) {
142
+ _this._outgoingQueue.push([target, scxmlEvent]);
143
+ } else {
144
+ target.send(scxmlEvent);
145
+ }
139
146
  }
140
147
  } else {
141
148
  // Send normal events to other targets
142
- target.send(event.data);
149
+ if (!immediate && _this.machine.config.predictableActionArguments) {
150
+ _this._outgoingQueue.push([target, event.data]);
151
+ } else {
152
+ target.send(event.data);
153
+ }
143
154
  }
144
155
  };
145
156
 
@@ -183,7 +194,7 @@ function () {
183
194
  return;
184
195
  } else {
185
196
  if (sendAction.to) {
186
- _this.sendTo(sendAction._event, sendAction.to);
197
+ _this.sendTo(sendAction._event, sendAction.to, _event === initEvent);
187
198
  } else {
188
199
  _this.send(sendAction._event);
189
200
  }
@@ -384,6 +395,12 @@ function () {
384
395
  // we can't just recompute it (and execute actions while doing so) because we try to preserve identity of actors created within initial assigns
385
396
  _event === initEvent) && this.options.execute) {
386
397
  this.execute(this.state);
398
+ } else {
399
+ var item = void 0;
400
+
401
+ while (item = this._outgoingQueue.shift()) {
402
+ item[0].send(item[1]);
403
+ }
387
404
  } // Update children
388
405
 
389
406
 
@@ -474,6 +491,8 @@ function () {
474
491
  }
475
492
 
476
493
  this._stop();
494
+
495
+ this._stopChildren();
477
496
  }
478
497
  };
479
498
  /*
@@ -632,6 +651,16 @@ function () {
632
651
  return this;
633
652
  };
634
653
 
654
+ Interpreter.prototype._stopChildren = function () {
655
+ // TODO: think about converting those to actions
656
+ this.children.forEach(function (child) {
657
+ if (isFunction(child.stop)) {
658
+ child.stop();
659
+ }
660
+ });
661
+ this.children.clear();
662
+ };
663
+
635
664
  Interpreter.prototype._stop = function () {
636
665
  var e_6, _a, e_7, _b, e_8, _c, e_9, _d, e_10, _e;
637
666
 
@@ -769,7 +798,7 @@ function () {
769
798
  return toActionObjects(stateNode.onExit, _this.machine.options.actions);
770
799
  }));
771
800
 
772
- var _a = __read(resolveActions(_this.machine, _this.state, _this.state.context, _event, exitActions, _this.machine.config.predictableActionArguments ? _this._exec : undefined, _this.machine.config.predictableActionArguments || _this.machine.config.preserveActionOrder), 2),
801
+ var _a = __read(resolveActions(_this.machine, _this.state, _this.state.context, _event, [exitActions], _this.machine.config.predictableActionArguments ? _this._exec : undefined, _this.machine.config.predictableActionArguments || _this.machine.config.preserveActionOrder), 2),
773
802
  resolvedActions = _a[0],
774
803
  updatedContext = _a[1];
775
804
 
@@ -796,17 +825,9 @@ function () {
796
825
  return newState;
797
826
  });
798
827
 
799
- _this.update(nextState, _event); // TODO: think about converting those to actions
800
- // Stop all children
801
-
802
-
803
- _this.children.forEach(function (child) {
804
- if (isFunction(child.stop)) {
805
- child.stop();
806
- }
807
- });
828
+ _this.update(nextState, _event);
808
829
 
809
- _this.children.clear();
830
+ _this._stopChildren();
810
831
 
811
832
  registry.free(_this.sessionId);
812
833
  });
@@ -947,7 +968,7 @@ function () {
947
968
 
948
969
  this.delayedEventsMap[sendAction.id] = this.clock.setTimeout(function () {
949
970
  if (sendAction.to) {
950
- _this.sendTo(sendAction._event, sendAction.to);
971
+ _this.sendTo(sendAction._event, sendAction.to, true);
951
972
  } else {
952
973
  _this.send(sendAction._event);
953
974
  }
package/lib/StateNode.js CHANGED
@@ -671,9 +671,11 @@ function () {
671
671
  return nodes;
672
672
  };
673
673
 
674
- StateNode.prototype.getActions = function (resolvedConfig, isDone, transition, currentContext, _event, prevState) {
674
+ StateNode.prototype.getActions = function (resolvedConfig, isDone, transition, currentContext, _event, prevState, predictableExec) {
675
675
  var e_4, _a, e_5, _b;
676
676
 
677
+ var _this = this;
678
+
677
679
  var prevConfig = stateUtils.getConfiguration([], prevState ? this.getStateNodes(prevState.value) : [this]);
678
680
 
679
681
  try {
@@ -751,20 +753,19 @@ function () {
751
753
  });
752
754
  var entryStates = new Set(transition.entrySet);
753
755
  var exitStates = new Set(transition.exitSet);
754
-
755
- var _c = _tslib.__read([utils.flatten(Array.from(entryStates).map(function (stateNode) {
756
- return _tslib.__spreadArray(_tslib.__spreadArray([], _tslib.__read(stateNode.activities.map(function (activity) {
756
+ var entryActions = Array.from(entryStates).map(function (stateNode) {
757
+ var entryActions = stateNode.onEntry;
758
+ var invokeActions = stateNode.activities.map(function (activity) {
757
759
  return actions.start(activity);
758
- })), false), _tslib.__read(stateNode.onEntry), false);
759
- })).concat(doneEvents.map(actions.raise)), utils.flatten(Array.from(exitStates).map(function (stateNode) {
760
- return _tslib.__spreadArray(_tslib.__spreadArray([], _tslib.__read(stateNode.onExit), false), _tslib.__read(stateNode.activities.map(function (activity) {
760
+ });
761
+ return actions.toActionObjects(predictableExec ? _tslib.__spreadArray(_tslib.__spreadArray([], _tslib.__read(entryActions), false), _tslib.__read(invokeActions), false) : _tslib.__spreadArray(_tslib.__spreadArray([], _tslib.__read(invokeActions), false), _tslib.__read(entryActions), false), _this.machine.options.actions);
762
+ }).concat([doneEvents.map(actions.raise)]);
763
+ var exitActions = Array.from(exitStates).map(function (stateNode) {
764
+ return actions.toActionObjects(_tslib.__spreadArray(_tslib.__spreadArray([], _tslib.__read(stateNode.onExit), false), _tslib.__read(stateNode.activities.map(function (activity) {
761
765
  return actions.stop(activity);
762
- })), false);
763
- }))], 2),
764
- entryActions = _c[0],
765
- exitActions = _c[1];
766
-
767
- var actions$1 = actions.toActionObjects(exitActions.concat(transition.actions).concat(entryActions), this.machine.options.actions);
766
+ })), false), _this.machine.options.actions);
767
+ });
768
+ var actions$1 = exitActions.concat([actions.toActionObjects(transition.actions, this.machine.options.actions)]).concat(entryActions);
768
769
 
769
770
  if (isDone) {
770
771
  var stopActions = actions.toActionObjects(utils.flatten(_tslib.__spreadArray([], _tslib.__read(resolvedConfig), false).sort(function (a, b) {
@@ -774,7 +775,7 @@ function () {
774
775
  })), this.machine.options.actions).filter(function (action) {
775
776
  return action.type !== actionTypes.raise && (action.type !== actionTypes.send || !!action.to && action.to !== types.SpecialTargets.Internal);
776
777
  });
777
- return actions$1.concat(stopActions);
778
+ return actions$1.concat([stopActions]);
778
779
  }
779
780
 
780
781
  return actions$1;
@@ -845,7 +846,7 @@ function () {
845
846
  };
846
847
 
847
848
  StateNode.prototype.resolveTransition = function (stateTransition, currentState, context, predictableExec, _event) {
848
- var e_6, _a;
849
+ var e_6, _a, e_7, _b;
849
850
 
850
851
  var _this = this;
851
852
 
@@ -862,17 +863,33 @@ function () {
862
863
  var isDone = stateUtils.isInFinalState(resolvedConfiguration, this);
863
864
  var resolvedStateValue = willTransition ? stateUtils.getValue(this.machine, configuration) : undefined;
864
865
  var historyValue = currentState ? currentState.historyValue ? currentState.historyValue : stateTransition.source ? this.machine.historyValue(currentState.value) : undefined : undefined;
865
- var actions$1 = this.getActions(new Set(resolvedConfiguration), isDone, stateTransition, context, _event, currentState);
866
+ var actionBlocks = this.getActions(new Set(resolvedConfiguration), isDone, stateTransition, context, _event, currentState, predictableExec);
866
867
  var activities = currentState ? _tslib.__assign({}, currentState.activities) : {};
867
868
 
868
869
  try {
869
- for (var actions_1 = _tslib.__values(actions$1), actions_1_1 = actions_1.next(); !actions_1_1.done; actions_1_1 = actions_1.next()) {
870
- var action = actions_1_1.value;
870
+ for (var actionBlocks_1 = _tslib.__values(actionBlocks), actionBlocks_1_1 = actionBlocks_1.next(); !actionBlocks_1_1.done; actionBlocks_1_1 = actionBlocks_1.next()) {
871
+ var block = actionBlocks_1_1.value;
871
872
 
872
- if (action.type === actionTypes.start) {
873
- activities[action.activity.id || action.activity.type] = action;
874
- } else if (action.type === actionTypes.stop) {
875
- activities[action.activity.id || action.activity.type] = false;
873
+ try {
874
+ for (var block_1 = (e_7 = void 0, _tslib.__values(block)), block_1_1 = block_1.next(); !block_1_1.done; block_1_1 = block_1.next()) {
875
+ var action = block_1_1.value;
876
+
877
+ if (action.type === actionTypes.start) {
878
+ activities[action.activity.id || action.activity.type] = action;
879
+ } else if (action.type === actionTypes.stop) {
880
+ activities[action.activity.id || action.activity.type] = false;
881
+ }
882
+ }
883
+ } catch (e_7_1) {
884
+ e_7 = {
885
+ error: e_7_1
886
+ };
887
+ } finally {
888
+ try {
889
+ if (block_1_1 && !block_1_1.done && (_b = block_1.return)) _b.call(block_1);
890
+ } finally {
891
+ if (e_7) throw e_7.error;
892
+ }
876
893
  }
877
894
  }
878
895
  } catch (e_6_1) {
@@ -881,21 +898,21 @@ function () {
881
898
  };
882
899
  } finally {
883
900
  try {
884
- if (actions_1_1 && !actions_1_1.done && (_a = actions_1.return)) _a.call(actions_1);
901
+ if (actionBlocks_1_1 && !actionBlocks_1_1.done && (_a = actionBlocks_1.return)) _a.call(actionBlocks_1);
885
902
  } finally {
886
903
  if (e_6) throw e_6.error;
887
904
  }
888
905
  }
889
906
 
890
- var _b = _tslib.__read(actions.resolveActions(this, currentState, context, _event, actions$1, predictableExec, this.machine.config.predictableActionArguments || this.machine.config.preserveActionOrder), 2),
891
- resolvedActions = _b[0],
892
- updatedContext = _b[1];
907
+ var _c = _tslib.__read(actions.resolveActions(this, currentState, context, _event, actionBlocks, predictableExec, this.machine.config.predictableActionArguments || this.machine.config.preserveActionOrder), 2),
908
+ resolvedActions = _c[0],
909
+ updatedContext = _c[1];
893
910
 
894
- var _c = _tslib.__read(utils.partition(resolvedActions, function (action) {
911
+ var _d = _tslib.__read(utils.partition(resolvedActions, function (action) {
895
912
  return action.type === actionTypes.raise || action.type === actionTypes.send && action.to === types.SpecialTargets.Internal;
896
913
  }), 2),
897
- raisedEvents = _c[0],
898
- nonRaisedActions = _c[1];
914
+ raisedEvents = _d[0],
915
+ nonRaisedActions = _d[1];
899
916
 
900
917
  var invokeActions = resolvedActions.filter(function (action) {
901
918
  var _a;
@@ -1344,7 +1361,7 @@ function () {
1344
1361
  * All the event types accepted by this state node and its descendants.
1345
1362
  */
1346
1363
  get: function () {
1347
- var e_7, _a, e_8, _b;
1364
+ var e_8, _a, e_9, _b;
1348
1365
 
1349
1366
  if (this.__cache.events) {
1350
1367
  return this.__cache.events;
@@ -1361,32 +1378,32 @@ function () {
1361
1378
 
1362
1379
  if (state.states) {
1363
1380
  try {
1364
- for (var _e = (e_8 = void 0, _tslib.__values(state.events)), _f = _e.next(); !_f.done; _f = _e.next()) {
1381
+ for (var _e = (e_9 = void 0, _tslib.__values(state.events)), _f = _e.next(); !_f.done; _f = _e.next()) {
1365
1382
  var event_1 = _f.value;
1366
1383
  events.add("".concat(event_1));
1367
1384
  }
1368
- } catch (e_8_1) {
1369
- e_8 = {
1370
- error: e_8_1
1385
+ } catch (e_9_1) {
1386
+ e_9 = {
1387
+ error: e_9_1
1371
1388
  };
1372
1389
  } finally {
1373
1390
  try {
1374
1391
  if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
1375
1392
  } finally {
1376
- if (e_8) throw e_8.error;
1393
+ if (e_9) throw e_9.error;
1377
1394
  }
1378
1395
  }
1379
1396
  }
1380
1397
  }
1381
- } catch (e_7_1) {
1382
- e_7 = {
1383
- error: e_7_1
1398
+ } catch (e_8_1) {
1399
+ e_8 = {
1400
+ error: e_8_1
1384
1401
  };
1385
1402
  } finally {
1386
1403
  try {
1387
1404
  if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
1388
1405
  } finally {
1389
- if (e_7) throw e_7.error;
1406
+ if (e_8) throw e_8.error;
1390
1407
  }
1391
1408
  }
1392
1409
  }
@@ -1481,7 +1498,7 @@ function () {
1481
1498
  };
1482
1499
 
1483
1500
  StateNode.prototype.formatTransitions = function () {
1484
- var e_9, _a;
1501
+ var e_10, _a;
1485
1502
 
1486
1503
  var _this = this;
1487
1504
 
@@ -1545,15 +1562,15 @@ function () {
1545
1562
  var delayedTransition = delayedTransitions_1_1.value;
1546
1563
  formattedTransitions.push(delayedTransition);
1547
1564
  }
1548
- } catch (e_9_1) {
1549
- e_9 = {
1550
- error: e_9_1
1565
+ } catch (e_10_1) {
1566
+ e_10 = {
1567
+ error: e_10_1
1551
1568
  };
1552
1569
  } finally {
1553
1570
  try {
1554
1571
  if (delayedTransitions_1_1 && !delayedTransitions_1_1.done && (_a = delayedTransitions_1.return)) _a.call(delayedTransitions_1);
1555
1572
  } finally {
1556
- if (e_9) throw e_9.error;
1573
+ if (e_10) throw e_10.error;
1557
1574
  }
1558
1575
  }
1559
1576
 
package/lib/actions.d.ts CHANGED
@@ -150,5 +150,5 @@ export declare function forwardTo<TContext, TEvent extends EventObject>(target:
150
150
  */
151
151
  export declare function escalate<TContext, TEvent extends EventObject, TErrorData = any>(errorData: TErrorData | ExprWithMeta<TContext, TEvent, TErrorData>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, AnyEventObject>;
152
152
  export declare function choose<TContext, TEvent extends EventObject>(conds: Array<ChooseCondition<TContext, TEvent>>): ChooseAction<TContext, TEvent>;
153
- export declare function resolveActions<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent, any, any, any>, currentState: State<TContext, TEvent, any, any, any> | undefined, currentContext: TContext, _event: SCXML.Event<TEvent>, actions: Array<ActionObject<TContext, TEvent>>, predictableExec?: PredictableActionArgumentsExec, preserveActionOrder?: boolean): [Array<ActionObject<TContext, TEvent>>, TContext];
153
+ export declare function resolveActions<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent, any, any, any>, currentState: State<TContext, TEvent, any, any, any> | undefined, currentContext: TContext, _event: SCXML.Event<TEvent>, actionBlocks: Array<Array<ActionObject<TContext, TEvent>>>, predictableExec?: PredictableActionArgumentsExec, preserveActionOrder?: boolean): [Array<ActionObject<TContext, TEvent>>, TContext];
154
154
  //# sourceMappingURL=actions.d.ts.map
package/lib/actions.js CHANGED
@@ -428,20 +428,53 @@ function choose(conds) {
428
428
  conds: conds
429
429
  };
430
430
  }
431
- function resolveActions(machine, currentState, currentContext, _event, actions, predictableExec, preserveActionOrder) {
431
+
432
+ var pluckAssigns = function (actionBlocks) {
433
+ var e_1, _a;
434
+
435
+ var assignActions = [];
436
+
437
+ try {
438
+ for (var actionBlocks_1 = _tslib.__values(actionBlocks), actionBlocks_1_1 = actionBlocks_1.next(); !actionBlocks_1_1.done; actionBlocks_1_1 = actionBlocks_1.next()) {
439
+ var block = actionBlocks_1_1.value;
440
+ var i = 0;
441
+
442
+ while (i < block.length) {
443
+ if (block[i].type === actionTypes.assign) {
444
+ assignActions.push(block[i]);
445
+ block.splice(i, 1);
446
+ continue;
447
+ }
448
+
449
+ i++;
450
+ }
451
+ }
452
+ } catch (e_1_1) {
453
+ e_1 = {
454
+ error: e_1_1
455
+ };
456
+ } finally {
457
+ try {
458
+ if (actionBlocks_1_1 && !actionBlocks_1_1.done && (_a = actionBlocks_1.return)) _a.call(actionBlocks_1);
459
+ } finally {
460
+ if (e_1) throw e_1.error;
461
+ }
462
+ }
463
+
464
+ return assignActions;
465
+ };
466
+
467
+ function resolveActions(machine, currentState, currentContext, _event, actionBlocks, predictableExec, preserveActionOrder) {
432
468
  if (preserveActionOrder === void 0) {
433
469
  preserveActionOrder = false;
434
470
  }
435
471
 
436
- var _a = _tslib.__read(preserveActionOrder ? [[], actions] : utils.partition(actions, function (action) {
437
- return action.type === actionTypes.assign;
438
- }), 2),
439
- assignActions = _a[0],
440
- otherActions = _a[1];
441
-
472
+ var assignActions = preserveActionOrder ? [] : pluckAssigns(actionBlocks);
442
473
  var updatedContext = assignActions.length ? utils.updateContext(currentContext, _event, assignActions, currentState) : currentContext;
443
474
  var preservedContexts = preserveActionOrder ? [currentContext] : undefined;
444
- var resolvedActions = utils.flatten(otherActions.map(function (actionObject) {
475
+ var deferredToBlockEnd = [];
476
+
477
+ function handleAction(actionObject) {
445
478
  var _a;
446
479
 
447
480
  switch (actionObject.type) {
@@ -459,8 +492,8 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
459
492
  "No delay reference for delay expression '".concat(actionObject.delay, "' was found on machine '").concat(machine.id, "'"));
460
493
  }
461
494
 
462
- if (sendAction.to !== types.SpecialTargets.Internal) {
463
- predictableExec === null || predictableExec === void 0 ? void 0 : predictableExec(sendAction, updatedContext, _event);
495
+ if (predictableExec && sendAction.to !== types.SpecialTargets.Internal) {
496
+ deferredToBlockEnd.push(sendAction);
464
497
  }
465
498
 
466
499
  return sendAction;
@@ -484,7 +517,7 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
484
517
  return [];
485
518
  }
486
519
 
487
- var _b = _tslib.__read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(utils.toArray(matchedActions), machine.options.actions), predictableExec, preserveActionOrder), 2),
520
+ var _b = _tslib.__read(resolveActions(machine, currentState, updatedContext, _event, [toActionObjects(utils.toArray(matchedActions), machine.options.actions)], predictableExec, preserveActionOrder), 2),
488
521
  resolvedActionsFromChoose = _b[0],
489
522
  resolvedContextFromChoose = _b[1];
490
523
 
@@ -501,7 +534,7 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
501
534
  return [];
502
535
  }
503
536
 
504
- var _c = _tslib.__read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(utils.toArray(matchedActions), machine.options.actions), predictableExec, preserveActionOrder), 2),
537
+ var _c = _tslib.__read(resolveActions(machine, currentState, updatedContext, _event, [toActionObjects(utils.toArray(matchedActions), machine.options.actions)], predictableExec, preserveActionOrder), 2),
505
538
  resolvedActionsFromPure = _c[0],
506
539
  resolvedContext = _c[1];
507
540
 
@@ -513,7 +546,7 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
513
546
  case actionTypes.stop:
514
547
  {
515
548
  var resolved = resolveStop(actionObject, updatedContext, _event);
516
- predictableExec === null || predictableExec === void 0 ? void 0 : predictableExec(resolved, updatedContext, _event);
549
+ predictableExec === null || predictableExec === void 0 ? void 0 : predictableExec(resolved, currentContext, _event);
517
550
  return resolved;
518
551
  }
519
552
 
@@ -547,9 +580,42 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
547
580
 
548
581
  return resolvedActionObject;
549
582
  }
550
- }).filter(function (a) {
551
- return !!a;
552
- }));
583
+ }
584
+
585
+ function processBlock(block) {
586
+ var e_2, _a;
587
+
588
+ var resolvedActions = [];
589
+
590
+ try {
591
+ for (var block_1 = _tslib.__values(block), block_1_1 = block_1.next(); !block_1_1.done; block_1_1 = block_1.next()) {
592
+ var action = block_1_1.value;
593
+ var resolved = handleAction(action);
594
+
595
+ if (resolved) {
596
+ resolvedActions = resolvedActions.concat(resolved);
597
+ }
598
+ }
599
+ } catch (e_2_1) {
600
+ e_2 = {
601
+ error: e_2_1
602
+ };
603
+ } finally {
604
+ try {
605
+ if (block_1_1 && !block_1_1.done && (_a = block_1.return)) _a.call(block_1);
606
+ } finally {
607
+ if (e_2) throw e_2.error;
608
+ }
609
+ }
610
+
611
+ deferredToBlockEnd.forEach(function (action) {
612
+ predictableExec(action, updatedContext, _event);
613
+ });
614
+ deferredToBlockEnd.length = 0;
615
+ return resolvedActions;
616
+ }
617
+
618
+ var resolvedActions = utils.flatten(actionBlocks.map(processBlock));
553
619
  return [resolvedActions, updatedContext];
554
620
  }
555
621
 
@@ -73,6 +73,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
73
73
  sessionId: string;
74
74
  children: Map<string | number, ActorRef<any>>;
75
75
  private forwardTo;
76
+ private _outgoingQueue;
76
77
  private devTools?;
77
78
  /**
78
79
  * Creates a new Interpreter instance (i.e., service) for the given machine with the provided options, if any.
@@ -134,6 +135,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
134
135
  * @param initialState The state to start the statechart from
135
136
  */
136
137
  start(initialState?: State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta> | StateConfig<TContext, TEvent> | StateValue): this;
138
+ private _stopChildren;
137
139
  private _stop;
138
140
  /**
139
141
  * Stops the interpreter and unsubscribe all listeners.
@@ -62,6 +62,7 @@ function () {
62
62
  this.status = exports.InterpreterStatus.NotStarted;
63
63
  this.children = new Map();
64
64
  this.forwardTo = new Set();
65
+ this._outgoingQueue = [];
65
66
  /**
66
67
  * Alias for Interpreter.prototype.start
67
68
  */
@@ -113,7 +114,7 @@ function () {
113
114
  // tslint:disable-next-line:semicolon
114
115
  };
115
116
 
116
- this.sendTo = function (event, to) {
117
+ this.sendTo = function (event, to, immediate) {
117
118
  var isParent = _this.parent && (to === types.SpecialTargets.Parent || _this.parent.id === to);
118
119
  var target = isParent ? _this.parent : utils.isString(to) ? _this.children.get(to) || registry.registry.get(to) : utils.isActor(to) ? to : undefined;
119
120
 
@@ -136,14 +137,24 @@ function () {
136
137
  if (_this.status !== exports.InterpreterStatus.Stopped || _this.parent !== target || // we need to send events to the parent from exit handlers of a machine that reached its final state
137
138
  _this.state.done) {
138
139
  // Send SCXML events to machines
139
- target.send(_tslib.__assign(_tslib.__assign({}, event), {
140
+ var scxmlEvent = _tslib.__assign(_tslib.__assign({}, event), {
140
141
  name: event.name === actionTypes.error ? "".concat(actions.error(_this.id)) : event.name,
141
142
  origin: _this.sessionId
142
- }));
143
+ });
144
+
145
+ if (!immediate && _this.machine.config.predictableActionArguments) {
146
+ _this._outgoingQueue.push([target, scxmlEvent]);
147
+ } else {
148
+ target.send(scxmlEvent);
149
+ }
143
150
  }
144
151
  } else {
145
152
  // Send normal events to other targets
146
- target.send(event.data);
153
+ if (!immediate && _this.machine.config.predictableActionArguments) {
154
+ _this._outgoingQueue.push([target, event.data]);
155
+ } else {
156
+ target.send(event.data);
157
+ }
147
158
  }
148
159
  };
149
160
 
@@ -187,7 +198,7 @@ function () {
187
198
  return;
188
199
  } else {
189
200
  if (sendAction.to) {
190
- _this.sendTo(sendAction._event, sendAction.to);
201
+ _this.sendTo(sendAction._event, sendAction.to, _event === actions.initEvent);
191
202
  } else {
192
203
  _this.send(sendAction._event);
193
204
  }
@@ -388,6 +399,12 @@ function () {
388
399
  // we can't just recompute it (and execute actions while doing so) because we try to preserve identity of actors created within initial assigns
389
400
  _event === actions.initEvent) && this.options.execute) {
390
401
  this.execute(this.state);
402
+ } else {
403
+ var item = void 0;
404
+
405
+ while (item = this._outgoingQueue.shift()) {
406
+ item[0].send(item[1]);
407
+ }
391
408
  } // Update children
392
409
 
393
410
 
@@ -478,6 +495,8 @@ function () {
478
495
  }
479
496
 
480
497
  this._stop();
498
+
499
+ this._stopChildren();
481
500
  }
482
501
  };
483
502
  /*
@@ -636,6 +655,16 @@ function () {
636
655
  return this;
637
656
  };
638
657
 
658
+ Interpreter.prototype._stopChildren = function () {
659
+ // TODO: think about converting those to actions
660
+ this.children.forEach(function (child) {
661
+ if (utils.isFunction(child.stop)) {
662
+ child.stop();
663
+ }
664
+ });
665
+ this.children.clear();
666
+ };
667
+
639
668
  Interpreter.prototype._stop = function () {
640
669
  var e_6, _a, e_7, _b, e_8, _c, e_9, _d, e_10, _e;
641
670
 
@@ -773,7 +802,7 @@ function () {
773
802
  return actions.toActionObjects(stateNode.onExit, _this.machine.options.actions);
774
803
  }));
775
804
 
776
- var _a = _tslib.__read(actions.resolveActions(_this.machine, _this.state, _this.state.context, _event, exitActions, _this.machine.config.predictableActionArguments ? _this._exec : undefined, _this.machine.config.predictableActionArguments || _this.machine.config.preserveActionOrder), 2),
805
+ var _a = _tslib.__read(actions.resolveActions(_this.machine, _this.state, _this.state.context, _event, [exitActions], _this.machine.config.predictableActionArguments ? _this._exec : undefined, _this.machine.config.predictableActionArguments || _this.machine.config.preserveActionOrder), 2),
777
806
  resolvedActions = _a[0],
778
807
  updatedContext = _a[1];
779
808
 
@@ -800,17 +829,9 @@ function () {
800
829
  return newState;
801
830
  });
802
831
 
803
- _this.update(nextState, _event); // TODO: think about converting those to actions
804
- // Stop all children
805
-
806
-
807
- _this.children.forEach(function (child) {
808
- if (utils.isFunction(child.stop)) {
809
- child.stop();
810
- }
811
- });
832
+ _this.update(nextState, _event);
812
833
 
813
- _this.children.clear();
834
+ _this._stopChildren();
814
835
 
815
836
  registry.registry.free(_this.sessionId);
816
837
  });
@@ -951,7 +972,7 @@ function () {
951
972
 
952
973
  this.delayedEventsMap[sendAction.id] = this.clock.setTimeout(function () {
953
974
  if (sendAction.to) {
954
- _this.sendTo(sendAction._event, sendAction.to);
975
+ _this.sendTo(sendAction._event, sendAction.to, true);
955
976
  } else {
956
977
  _this.send(sendAction._event);
957
978
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xstate",
3
- "version": "4.33.1",
3
+ "version": "4.33.4",
4
4
  "description": "Finite State Machines and Statecharts for the Modern Web.",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",