xstate 4.32.0 → 4.33.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/actions.js CHANGED
@@ -376,6 +376,26 @@ function pure(getActions) {
376
376
  */
377
377
 
378
378
  function forwardTo(target, options) {
379
+ if (!IS_PRODUCTION && (!target || typeof target === 'function')) {
380
+ var originalTarget_1 = target;
381
+
382
+ target = function () {
383
+ var args = [];
384
+
385
+ for (var _i = 0; _i < arguments.length; _i++) {
386
+ args[_i] = arguments[_i];
387
+ }
388
+
389
+ var resolvedTarget = typeof originalTarget_1 === 'function' ? originalTarget_1.apply(void 0, __spreadArray([], __read(args), false)) : originalTarget_1;
390
+
391
+ if (!resolvedTarget) {
392
+ throw new Error("Attempted to forward event to undefined actor. This risks an infinite loop in the sender.");
393
+ }
394
+
395
+ return resolvedTarget;
396
+ };
397
+ }
398
+
379
399
  return send(function (_, event) {
380
400
  return event;
381
401
  }, __assign(__assign({}, options), {
@@ -406,7 +426,7 @@ function choose(conds) {
406
426
  conds: conds
407
427
  };
408
428
  }
409
- function resolveActions(machine, currentState, currentContext, _event, actions, preserveActionOrder) {
429
+ function resolveActions(machine, currentState, currentContext, _event, actions, predictableExec, preserveActionOrder) {
410
430
  if (preserveActionOrder === void 0) {
411
431
  preserveActionOrder = false;
412
432
  }
@@ -424,7 +444,9 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
424
444
 
425
445
  switch (actionObject.type) {
426
446
  case raise$1:
427
- return resolveRaise(actionObject);
447
+ {
448
+ return resolveRaise(actionObject);
449
+ }
428
450
 
429
451
  case send$1:
430
452
  var sendAction = resolveSend(actionObject, updatedContext, _event, machine.options.delays); // TODO: fix ActionTypes.Init
@@ -435,24 +457,32 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
435
457
  "No delay reference for delay expression '".concat(actionObject.delay, "' was found on machine '").concat(machine.id, "'"));
436
458
  }
437
459
 
460
+ if (sendAction.to !== SpecialTargets.Internal) {
461
+ predictableExec === null || predictableExec === void 0 ? void 0 : predictableExec(sendAction, updatedContext, _event);
462
+ }
463
+
438
464
  return sendAction;
439
465
 
440
466
  case log$1:
441
- return resolveLog(actionObject, updatedContext, _event);
467
+ {
468
+ var resolved = resolveLog(actionObject, updatedContext, _event);
469
+ predictableExec === null || predictableExec === void 0 ? void 0 : predictableExec(resolved, updatedContext, _event);
470
+ return resolved;
471
+ }
442
472
 
443
473
  case choose$1:
444
474
  {
445
475
  var chooseAction = actionObject;
446
476
  var matchedActions = (_a = chooseAction.conds.find(function (condition) {
447
477
  var guard = toGuard(condition.cond, machine.options.guards);
448
- return !guard || evaluateGuard(machine, guard, updatedContext, _event, currentState);
478
+ return !guard || evaluateGuard(machine, guard, updatedContext, _event, !predictableExec ? currentState : undefined);
449
479
  })) === null || _a === void 0 ? void 0 : _a.actions;
450
480
 
451
481
  if (!matchedActions) {
452
482
  return [];
453
483
  }
454
484
 
455
- var _b = __read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions), preserveActionOrder), 2),
485
+ var _b = __read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions), predictableExec, preserveActionOrder), 2),
456
486
  resolvedActionsFromChoose = _b[0],
457
487
  resolvedContextFromChoose = _b[1];
458
488
 
@@ -469,7 +499,7 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
469
499
  return [];
470
500
  }
471
501
 
472
- var _c = __read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions), preserveActionOrder), 2),
502
+ var _c = __read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions), predictableExec, preserveActionOrder), 2),
473
503
  resolvedActionsFromPure = _c[0],
474
504
  resolvedContext = _c[1];
475
505
 
@@ -480,12 +510,14 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
480
510
 
481
511
  case stop$1:
482
512
  {
483
- return resolveStop(actionObject, updatedContext, _event);
513
+ var resolved = resolveStop(actionObject, updatedContext, _event);
514
+ predictableExec === null || predictableExec === void 0 ? void 0 : predictableExec(resolved, updatedContext, _event);
515
+ return resolved;
484
516
  }
485
517
 
486
518
  case assign$1:
487
519
  {
488
- updatedContext = updateContext(updatedContext, _event, [actionObject], currentState);
520
+ updatedContext = updateContext(updatedContext, _event, [actionObject], !predictableExec ? currentState : undefined);
489
521
  preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
490
522
  break;
491
523
  }
@@ -494,7 +526,9 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
494
526
  var resolvedActionObject = toActionObject(actionObject, machine.options.actions);
495
527
  var exec_1 = resolvedActionObject.exec;
496
528
 
497
- if (exec_1 && preservedContexts) {
529
+ if (predictableExec) {
530
+ predictableExec(resolvedActionObject, updatedContext, _event);
531
+ } else if (exec_1 && preservedContexts) {
498
532
  var contextIndex_1 = preservedContexts.length - 1;
499
533
  resolvedActionObject = __assign(__assign({}, resolvedActionObject), {
500
534
  exec: function (_ctx) {
@@ -93,7 +93,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
93
93
  execute(state: State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
94
94
  private update;
95
95
  onTransition(listener: StateListener<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>): this;
96
- subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>>): Subscription;
96
+ subscribe(observer: Partial<Observer<State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>>>): Subscription;
97
97
  subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Subscription;
98
98
  /**
99
99
  * Adds an event listener that is notified whenever an event is sent to the running interpreter.
@@ -134,6 +134,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
134
134
  * @param initialState The state to start the statechart from
135
135
  */
136
136
  start(initialState?: State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta> | StateConfig<TContext, TEvent> | StateValue): this;
137
+ private _stop;
137
138
  /**
138
139
  * Stops the interpreter and unsubscribe all listeners.
139
140
  *
@@ -158,6 +159,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
158
159
  */
159
160
  sender(event: Event<TEvent>): () => State<TContext, TEvent, TStateSchema, TTypestate>;
160
161
  private sendTo;
162
+ private _nextState;
161
163
  /**
162
164
  * Returns the next state given the interpreter's current state and the event.
163
165
  *
@@ -169,6 +171,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
169
171
  private forward;
170
172
  private defer;
171
173
  private cancel;
174
+ private _exec;
172
175
  private exec;
173
176
  private removeChild;
174
177
  private stopChild;