xstate 4.33.2 → 4.33.5

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.
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.
@@ -128,7 +129,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
128
129
  /**
129
130
  * Alias for Interpreter.prototype.start
130
131
  */
131
- init: (initialState?: StateValue | State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta> | StateConfig<TContext, TEvent> | undefined) => this;
132
+ init: (initialState?: State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta> | StateConfig<TContext, TEvent> | StateValue) => this;
132
133
  /**
133
134
  * Starts the interpreter from the given state, or the initial state.
134
135
  * @param initialState The state to start the statechart from
@@ -151,7 +152,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
151
152
  *
152
153
  * @param event The event(s) to send
153
154
  */
154
- send: (event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>, payload?: EventData | undefined) => State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
155
+ send: (event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>, payload?: EventData) => State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
155
156
  private batch;
156
157
  /**
157
158
  * Returns a send function bound to this interpreter instance.
@@ -177,7 +178,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
177
178
  private removeChild;
178
179
  private stopChild;
179
180
  spawn(entity: Spawnable, name: string, options?: SpawnOptions): ActorRef<any>;
180
- spawnMachine<TChildContext, TChildStateSchema, TChildEvent extends EventObject>(machine: StateMachine<TChildContext, TChildStateSchema, TChildEvent>, options?: {
181
+ spawnMachine<TChildContext, TChildStateSchema extends StateSchema, TChildEvent extends EventObject>(machine: StateMachine<TChildContext, TChildStateSchema, TChildEvent>, options?: {
181
182
  id?: string;
182
183
  autoForward?: boolean;
183
184
  sync?: boolean;
@@ -40,12 +40,12 @@ function () {
40
40
  * @param options Interpreter options
41
41
  */
42
42
  function Interpreter(machine, options) {
43
- var _this = this;
44
-
45
43
  if (options === void 0) {
46
44
  options = Interpreter.defaultOptions;
47
45
  }
48
46
 
47
+ var _this = this;
48
+
49
49
  this.machine = machine;
50
50
  this.delayedEventsMap = {};
51
51
  this.listeners = new Set();
@@ -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
 
@@ -785,7 +802,7 @@ function () {
785
802
  return actions.toActionObjects(stateNode.onExit, _this.machine.options.actions);
786
803
  }));
787
804
 
788
- 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),
789
806
  resolvedActions = _a[0],
790
807
  updatedContext = _a[1];
791
808
 
@@ -834,6 +851,11 @@ function () {
834
851
  "".concat(events.length, " event(s) were sent to uninitialized service \"").concat(this.machine.id, "\". Make sure .start() is called for this service, or set { deferEvents: true } in the service options."));
835
852
  }
836
853
 
854
+ if (!events.length) {
855
+ return;
856
+ }
857
+
858
+ var exec = !!this.machine.config.predictableActionArguments && this._exec;
837
859
  this.scheduler.schedule(function () {
838
860
  var e_11, _a;
839
861
 
@@ -847,9 +869,9 @@ function () {
847
869
  _this.forward(_event);
848
870
 
849
871
  nextState = serviceScope.provide(_this, function () {
850
- return _this.machine.transition(nextState, _event);
872
+ return _this.machine.transition(nextState, _event, undefined, exec || undefined);
851
873
  });
852
- batchedActions.push.apply(batchedActions, _tslib.__spreadArray([], _tslib.__read(nextState.actions.map(function (a) {
874
+ batchedActions.push.apply(batchedActions, _tslib.__spreadArray([], _tslib.__read(_this.machine.config.predictableActionArguments ? nextState.actions : nextState.actions.map(function (a) {
853
875
  return State.bindActionToState(a, nextState);
854
876
  })), false));
855
877
  batchChanged = batchChanged || !!nextState.changed;
@@ -955,7 +977,7 @@ function () {
955
977
 
956
978
  this.delayedEventsMap[sendAction.id] = this.clock.setTimeout(function () {
957
979
  if (sendAction.to) {
958
- _this.sendTo(sendAction._event, sendAction.to);
980
+ _this.sendTo(sendAction._event, sendAction.to, true);
959
981
  } else {
960
982
  _this.send(sendAction._event);
961
983
  }
package/lib/types.d.ts CHANGED
@@ -228,13 +228,13 @@ export declare type DelayedTransitions<TContext, TEvent extends EventObject> = R
228
228
  export declare type StateTypes = 'atomic' | 'compound' | 'parallel' | 'final' | 'history' | string;
229
229
  export declare type SingleOrArray<T> = T[] | T;
230
230
  export declare type StateNodesConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> = {
231
- [K in keyof TStateSchema['states']]: StateNode<TContext, TStateSchema['states'][K], TEvent>;
231
+ [K in keyof TStateSchema['states']]: StateNode<TContext, TStateSchema['states'][K] & {}, TEvent>;
232
232
  };
233
233
  export declare type StatesConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject> = {
234
- [K in keyof TStateSchema['states']]: StateNodeConfig<TContext, TStateSchema['states'][K], TEvent, TAction>;
234
+ [K in keyof TStateSchema['states']]: StateNodeConfig<TContext, TStateSchema['states'][K] & {}, TEvent, TAction>;
235
235
  };
236
236
  export declare type StatesDefinition<TContext, TStateSchema extends StateSchema, TEvent extends EventObject> = {
237
- [K in keyof TStateSchema['states']]: StateNodeDefinition<TContext, TStateSchema['states'][K], TEvent>;
237
+ [K in keyof TStateSchema['states']]: StateNodeDefinition<TContext, TStateSchema['states'][K] & {}, TEvent>;
238
238
  };
239
239
  export declare type TransitionConfigTarget<TContext, TEvent extends EventObject> = string | undefined | StateNode<TContext, any, TEvent>;
240
240
  export declare type TransitionConfigOrTarget<TContext, TEvent extends EventObject> = SingleOrArray<TransitionConfigTarget<TContext, TEvent> | TransitionConfig<TContext, TEvent>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xstate",
3
- "version": "4.33.2",
3
+ "version": "4.33.5",
4
4
  "description": "Finite State Machines and Statecharts for the Modern Web.",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -63,7 +63,7 @@
63
63
  "rxjs": "^7.1.0",
64
64
  "ts-jest": "^26.5.6",
65
65
  "tslib": "^2.3.1",
66
- "typescript": "^4.5.2",
66
+ "typescript": "^4.8.2",
67
67
  "xml-js": "^1.6.11"
68
68
  }
69
69
  }