xstate 4.26.1 → 4.28.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/interpreter.js CHANGED
@@ -1,6 +1,6 @@
1
- import { __values, __assign, __spreadArray, __read } from './_virtual/_tslib.js';
1
+ import { __values, __spreadArray, __read, __assign } from './_virtual/_tslib.js';
2
2
  import { IS_PRODUCTION } from './environment.js';
3
- import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isMachine, isPromiseLike, isObservable, isBehavior, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, isActor, uniqueId, toObserver } from './utils.js';
3
+ import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isMachine, isPromiseLike, isObservable, isBehavior, reportUnhandledExceptionOnInvocation, interopSymbols, symbolObservable, isArray, toEventObject, isString, isActor, toObserver, uniqueId } from './utils.js';
4
4
  import { ActionTypes, SpecialTargets } from './types.js';
5
5
  import { isInFinalState } from './stateUtils.js';
6
6
  import { errorPlatform, log, stop, start, cancel, send, update, error as error$1 } from './actionTypes.js';
@@ -558,7 +558,9 @@ function () {
558
558
  return this;
559
559
  }
560
560
 
561
- this.state.configuration.forEach(function (stateNode) {
561
+ __spreadArray([], __read(this.state.configuration), false).sort(function (a, b) {
562
+ return b.order - a.order;
563
+ }).forEach(function (stateNode) {
562
564
  var e_11, _a;
563
565
 
564
566
  try {
@@ -580,6 +582,7 @@ function () {
580
582
  }
581
583
  }); // Stop all children
582
584
 
585
+
583
586
  this.children.forEach(function (child) {
584
587
  if (isFunction(child.stop)) {
585
588
  child.stop();
@@ -801,6 +804,10 @@ function () {
801
804
 
802
805
  case start:
803
806
  {
807
+ if (this.status !== InterpreterStatus.Running) {
808
+ return;
809
+ }
810
+
804
811
  var activity = action.activity; // If the activity will be stopped right after it's started
805
812
  // (such as in transient states)
806
813
  // don't bother starting the activity.
@@ -1032,7 +1039,8 @@ function () {
1032
1039
  }
1033
1040
  }
1034
1041
  });
1035
- var actor = {
1042
+
1043
+ var actor = __assign({
1036
1044
  id: id,
1037
1045
  send: function () {
1038
1046
  return void 0;
@@ -1076,7 +1084,8 @@ function () {
1076
1084
  getSnapshot: function () {
1077
1085
  return resolvedData;
1078
1086
  }
1079
- };
1087
+ }, interopSymbols);
1088
+
1080
1089
  this.children.set(id, actor);
1081
1090
  return actor;
1082
1091
  };
@@ -1120,7 +1129,7 @@ function () {
1120
1129
  return this.spawnPromise(callbackStop, id);
1121
1130
  }
1122
1131
 
1123
- var actor = {
1132
+ var actor = __assign({
1124
1133
  id: id,
1125
1134
  send: function (event) {
1126
1135
  return receivers.forEach(function (receiver) {
@@ -1150,7 +1159,8 @@ function () {
1150
1159
  getSnapshot: function () {
1151
1160
  return emitted;
1152
1161
  }
1153
- };
1162
+ }, interopSymbols);
1163
+
1154
1164
  this.children.set(id, actor);
1155
1165
  return actor;
1156
1166
  };
@@ -1178,7 +1188,8 @@ function () {
1178
1188
  origin: id
1179
1189
  }));
1180
1190
  });
1181
- var actor = {
1191
+
1192
+ var actor = __assign({
1182
1193
  id: id,
1183
1194
  send: function () {
1184
1195
  return void 0;
@@ -1197,7 +1208,8 @@ function () {
1197
1208
  id: id
1198
1209
  };
1199
1210
  }
1200
- };
1211
+ }, interopSymbols);
1212
+
1201
1213
  this.children.set(id, actor);
1202
1214
  return actor;
1203
1215
  };
@@ -1225,7 +1237,7 @@ function () {
1225
1237
  };
1226
1238
 
1227
1239
  Interpreter.prototype.spawnEffect = function (id, dispose) {
1228
- this.children.set(id, {
1240
+ this.children.set(id, __assign({
1229
1241
  id: id,
1230
1242
  send: function () {
1231
1243
  return void 0;
@@ -1246,7 +1258,7 @@ function () {
1246
1258
  id: id
1247
1259
  };
1248
1260
  }
1249
- });
1261
+ }, interopSymbols));
1250
1262
  };
1251
1263
 
1252
1264
  Interpreter.prototype.attachDev = function () {
@@ -4,7 +4,7 @@ declare type AdjList<TC, TE extends EventObject> = Map<StateNode<TC, any, TE>, A
4
4
  export declare const isLeafNode: (stateNode: StateNode<any, any, any, any>) => boolean;
5
5
  export declare function getChildren<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE>): Array<StateNode<TC, any, TE>>;
6
6
  export declare function getAllStateNodes<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE, any>): Array<StateNode<TC, any, TE, any>>;
7
- export declare function getConfiguration<TC, TE extends EventObject>(prevStateNodes: Iterable<StateNode<TC, any, TE, any>>, stateNodes: Iterable<StateNode<TC, any, TE, any>>): Iterable<StateNode<TC, any, TE, any>>;
7
+ export declare function getConfiguration<TC, TE extends EventObject>(prevStateNodes: Iterable<StateNode<TC, any, TE, any>>, stateNodes: Iterable<StateNode<TC, any, TE, any>>): Set<StateNode<TC, any, TE, any>>;
8
8
  export declare function getAdjList<TC, TE extends EventObject>(configuration: Configuration<TC, TE>): AdjList<TC, TE>;
9
9
  export declare function getValue<TC, TE extends EventObject>(rootNode: StateNode<TC, any, TE, any>, configuration: Configuration<TC, TE>): StateValue;
10
10
  export declare function has<T>(iterable: Iterable<T>, item: T): boolean;
package/es/types.d.ts CHANGED
@@ -4,6 +4,7 @@ import { Interpreter, Clock } from './interpreter';
4
4
  import { IsNever, Model, Prop } from './model.types';
5
5
  declare type AnyFunction = (...args: any[]) => any;
6
6
  declare type ReturnTypeOrValue<T> = T extends AnyFunction ? ReturnType<T> : T;
7
+ export declare type Cast<A, B> = A extends B ? A : B;
7
8
  export declare type EventType = string;
8
9
  export declare type ActionType = string;
9
10
  export declare type MetaObject = Record<string, any>;
@@ -34,7 +35,7 @@ export interface ActionObject<TContext, TEvent extends EventObject> extends Base
34
35
  /**
35
36
  * The implementation for executing the action.
36
37
  */
37
- exec?: ActionFunction<TContext, TEvent>;
38
+ exec?: ActionFunction<TContext, TEvent> | undefined;
38
39
  }
39
40
  export declare type DefaultContext = Record<string, any> | undefined;
40
41
  export declare type EventData = Record<string, any> & {
@@ -106,7 +107,7 @@ export interface TransitionConfig<TContext, TEvent extends EventObject> {
106
107
  actions?: Actions<TContext, TEvent>;
107
108
  in?: StateValue;
108
109
  internal?: boolean;
109
- target?: TransitionTarget<TContext, TEvent>;
110
+ target?: TransitionTarget<TContext, TEvent> | undefined;
110
111
  meta?: Record<string, any>;
111
112
  description?: string;
112
113
  }
@@ -345,7 +346,7 @@ export interface StateNodeConfig<TContext, TStateSchema extends StateSchema, TEv
345
346
  *
346
347
  * This is equivalent to defining a `[done(id)]` transition on this state node's `on` property.
347
348
  */
348
- onDone?: string | SingleOrArray<TransitionConfig<TContext, DoneEventObject>>;
349
+ onDone?: string | SingleOrArray<TransitionConfig<TContext, DoneEventObject>> | undefined;
349
350
  /**
350
351
  * The mapping (or array) of delays (in milliseconds) to their potential transition(s).
351
352
  * The delayed transitions are taken after the specified delay in an interpreter.
@@ -915,18 +916,21 @@ export interface Observer<T> {
915
916
  export interface Subscription {
916
917
  unsubscribe(): void;
917
918
  }
919
+ export interface InteropObservable<T> {
920
+ [Symbol.observable]: () => Subscribable<T>;
921
+ }
918
922
  export interface Subscribable<T> {
919
923
  subscribe(next: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;
920
924
  subscribe(observer: Observer<T>): Subscription;
921
925
  }
922
- export declare type Spawnable = StateMachine<any, any, any> | PromiseLike<any> | InvokeCallback | Subscribable<any> | Behavior<any>;
926
+ export declare type Spawnable = StateMachine<any, any, any> | PromiseLike<any> | InvokeCallback | InteropObservable<any> | Subscribable<any> | Behavior<any>;
923
927
  export declare type ExtractEvent<TEvent extends EventObject, TEventType extends TEvent['type']> = TEvent extends {
924
928
  type: TEventType;
925
929
  } ? TEvent : never;
926
930
  export interface BaseActorRef<TEvent extends EventObject> {
927
931
  send: (event: TEvent) => void;
928
932
  }
929
- export interface ActorRef<TEvent extends EventObject, TEmitted = any> extends Subscribable<TEmitted> {
933
+ export interface ActorRef<TEvent extends EventObject, TEmitted = any> extends Subscribable<TEmitted>, InteropObservable<TEmitted> {
930
934
  send: Sender<TEvent>;
931
935
  id: string;
932
936
  getSnapshot: () => TEmitted | undefined;
@@ -958,7 +962,7 @@ export interface Behavior<TEvent extends EventObject, TEmitted = any> {
958
962
  start?: (actorCtx: ActorContext<TEvent, TEmitted>) => TEmitted;
959
963
  }
960
964
  export declare type EmittedFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends ActorRef<infer _, infer TEmitted> ? TEmitted : R extends Behavior<infer _, infer TEmitted> ? TEmitted : R extends ActorContext<infer _, infer TEmitted> ? TEmitted : never : never;
961
- declare type ResolveEventType<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer _, infer __, infer TEvent, infer ____> ? TEvent : R extends Model<infer _, infer TEvent, infer ___, infer ____> ? TEvent : R extends State<infer _, infer TEvent, infer ___, infer ____> ? TEvent : R extends Interpreter<infer _, infer __, infer TEvent, infer ____> ? TEvent : never : never;
965
+ declare type ResolveEventType<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer _, infer __, infer TEvent, infer ____> ? TEvent : R extends Model<infer _, infer TEvent, infer ___, infer ____> ? TEvent : R extends State<infer _, infer TEvent, infer ___, infer ____> ? TEvent : R extends Interpreter<infer _, infer __, infer TEvent, infer ____> ? TEvent : R extends ActorRef<infer TEvent, infer _> ? TEvent : never : never;
962
966
  export declare type EventFrom<T, K extends Prop<TEvent, 'type'> = never, TEvent = ResolveEventType<T>> = IsNever<K> extends true ? TEvent : Extract<TEvent, {
963
967
  type: K;
964
968
  }>;
package/es/utils.d.ts CHANGED
@@ -52,7 +52,11 @@ export declare function isFunction(value: any): value is Function;
52
52
  export declare function isString(value: any): value is string;
53
53
  export declare function toGuard<TContext, TEvent extends EventObject>(condition?: Condition<TContext, TEvent>, guardMap?: Record<string, ConditionPredicate<TContext, TEvent>>): Guard<TContext, TEvent> | undefined;
54
54
  export declare function isObservable<T>(value: any): value is Subscribable<T>;
55
- export declare const symbolObservable: any;
55
+ export declare const symbolObservable: string | typeof Symbol.observable;
56
+ export declare const interopSymbols: {
57
+ [x: string]: () => any;
58
+ [Symbol.observable]: () => any;
59
+ };
56
60
  export declare function isMachine(value: any): value is StateMachine<any, any, any>;
57
61
  export declare function isActor(value: any): value is Actor;
58
62
  export declare const uniqueId: () => string;
@@ -66,4 +70,5 @@ export declare function reportUnhandledExceptionOnInvocation(originalError: any,
66
70
  export declare function evaluateGuard<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent, any>, guard: Guard<TContext, TEvent>, context: TContext, _event: SCXML.Event<TEvent>, state: State<TContext, TEvent>): boolean;
67
71
  export declare function toInvokeSource(src: string | InvokeSourceDefinition): InvokeSourceDefinition;
68
72
  export declare function toObserver<T>(nextHandler: Observer<T> | ((value: T) => void), errorHandler?: (error: any) => void, completionHandler?: () => void): Observer<T>;
73
+ export declare function createInvokeId(stateNodeId: string, index: number): string;
69
74
  //# sourceMappingURL=utils.d.ts.map
package/es/utils.js CHANGED
@@ -2,6 +2,7 @@ import { __spreadArray, __read, __values, __assign } from './_virtual/_tslib.js'
2
2
  import { DEFAULT_GUARD_TYPE, TARGETLESS_KEY, STATE_DELIMITER } from './constants.js';
3
3
  import { IS_PRODUCTION } from './environment.js';
4
4
 
5
+ var _a;
5
6
  function keys(value) {
6
7
  return Object.keys(value);
7
8
  }
@@ -460,6 +461,9 @@ function isObservable(value) {
460
461
  var symbolObservable = /*#__PURE__*/function () {
461
462
  return typeof Symbol === 'function' && Symbol.observable || '@@observable';
462
463
  }();
464
+ var interopSymbols = (_a = {}, _a[symbolObservable] = function () {
465
+ return this;
466
+ }, _a);
463
467
  function isMachine(value) {
464
468
  try {
465
469
  return '__xstatenode' in value;
@@ -580,5 +584,8 @@ function toObserver(nextHandler, errorHandler, completionHandler) {
580
584
  complete: completionHandler || noop
581
585
  };
582
586
  }
587
+ function createInvokeId(stateNodeId, index) {
588
+ return "".concat(stateNodeId, ":invocation[").concat(index, "]");
589
+ }
583
590
 
584
- export { evaluateGuard, flatten, getEventType, isActor, isArray, isBehavior, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toInvokeSource, toObserver, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
591
+ export { createInvokeId, evaluateGuard, flatten, getEventType, interopSymbols, isActor, isArray, isBehavior, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toInvokeSource, toObserver, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
package/lib/Actor.js CHANGED
@@ -7,7 +7,7 @@ var utils = require('./utils.js');
7
7
  var serviceScope = require('./serviceScope.js');
8
8
 
9
9
  function createNullActor(id) {
10
- return {
10
+ return _tslib.__assign({
11
11
  id: id,
12
12
  send: function () {
13
13
  return void 0;
@@ -27,7 +27,7 @@ function createNullActor(id) {
27
27
  id: id
28
28
  };
29
29
  }
30
- };
30
+ }, utils.interopSymbols);
31
31
  }
32
32
  /**
33
33
  * Creates a deferred actor that is able to be invoked given the provided
@@ -76,7 +76,7 @@ function isSpawnedActor(item) {
76
76
  return isActor(item) && 'id' in item;
77
77
  }
78
78
  function toActorRef(actorRefLike) {
79
- return _tslib.__assign({
79
+ return _tslib.__assign(_tslib.__assign({
80
80
  subscribe: function () {
81
81
  return {
82
82
  unsubscribe: function () {
@@ -88,7 +88,7 @@ function toActorRef(actorRefLike) {
88
88
  getSnapshot: function () {
89
89
  return undefined;
90
90
  }
91
- }, actorRefLike);
91
+ }, utils.interopSymbols), actorRefLike);
92
92
  }
93
93
 
94
94
  exports.createDeferredActor = createDeferredActor;
package/lib/State.d.ts CHANGED
@@ -107,7 +107,10 @@ export declare class State<TContext, TEvent extends EventObject = EventObject, T
107
107
  */
108
108
  hasTag(tag: string): boolean;
109
109
  /**
110
- * Determines whether sending the `event` will cause a transition.
110
+ * Determines whether sending the `event` will cause a non-forbidden transition
111
+ * to be selected, even if the transitions have no actions nor
112
+ * change the state value.
113
+ *
111
114
  * @param event The event to test
112
115
  * @returns Whether the event will cause a transition
113
116
  */
package/lib/State.js CHANGED
@@ -249,7 +249,10 @@ function () {
249
249
  return this.tags.has(tag);
250
250
  };
251
251
  /**
252
- * Determines whether sending the `event` will cause a transition.
252
+ * Determines whether sending the `event` will cause a non-forbidden transition
253
+ * to be selected, even if the transitions have no actions nor
254
+ * change the state value.
255
+ *
253
256
  * @param event The event to test
254
257
  * @returns Whether the event will cause a transition
255
258
  */
@@ -262,7 +265,11 @@ function () {
262
265
  utils.warn(!!this.machine, "state.can(...) used outside of a machine-created State object; this will always return false.");
263
266
  }
264
267
 
265
- return !!((_a = this.machine) === null || _a === void 0 ? void 0 : _a.transition(this, event).changed);
268
+ var transitionData = (_a = this.machine) === null || _a === void 0 ? void 0 : _a.getTransitionData(this, event);
269
+ return !!(transitionData === null || transitionData === void 0 ? void 0 : transitionData.transitions.length) && // Check that at least one transition is not forbidden
270
+ transitionData.transitions.some(function (t) {
271
+ return t.target !== undefined || t.actions.length;
272
+ });
266
273
  };
267
274
 
268
275
  return State;
@@ -1,4 +1,4 @@
1
- import { Event, StateValue, MachineOptions, EventObject, HistoryValue, StateNodeDefinition, TransitionDefinition, DelayedTransitionDefinition, ActivityDefinition, StateNodeConfig, StateSchema, StateNodesConfig, InvokeDefinition, ActionObject, Mapper, PropertyMapper, SCXML, Typestate, TransitionDefinitionMap, MachineSchema } from './types';
1
+ import { Event, StateValue, StateTransition, MachineOptions, EventObject, HistoryValue, StateNodeDefinition, TransitionDefinition, DelayedTransitionDefinition, ActivityDefinition, StateNodeConfig, StateSchema, StateNodesConfig, InvokeDefinition, ActionObject, Mapper, PropertyMapper, SCXML, Typestate, TransitionDefinitionMap, MachineSchema } from './types';
2
2
  import { State } from './State';
3
3
  declare class StateNode<TContext = any, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
4
4
  value: any;
@@ -182,6 +182,7 @@ declare class StateNode<TContext = any, TStateSchema extends StateSchema = any,
182
182
  private transitionCompoundNode;
183
183
  private transitionParallelNode;
184
184
  private _transition;
185
+ getTransitionData(state: State<TContext, TEvent, any, any>, event: Event<TEvent> | SCXML.Event<TEvent>): StateTransition<TContext, TEvent> | undefined;
185
186
  private next;
186
187
  private nodesFromChild;
187
188
  /**
package/lib/StateNode.js CHANGED
@@ -159,29 +159,30 @@ function () {
159
159
  var _a, _b;
160
160
 
161
161
  if (utils.isMachine(invokeConfig)) {
162
- _this.machine.options.services = _tslib.__assign((_a = {}, _a[invokeConfig.id] = invokeConfig, _a), _this.machine.options.services);
162
+ var invokeId = utils.createInvokeId(_this.id, i);
163
+ _this.machine.options.services = _tslib.__assign((_a = {}, _a[invokeId] = invokeConfig, _a), _this.machine.options.services);
163
164
  return invokeUtils.toInvokeDefinition({
164
- src: invokeConfig.id,
165
- id: invokeConfig.id
165
+ src: invokeId,
166
+ id: invokeId
166
167
  });
167
168
  } else if (utils.isString(invokeConfig.src)) {
169
+ var invokeId = invokeConfig.id || utils.createInvokeId(_this.id, i);
168
170
  return invokeUtils.toInvokeDefinition(_tslib.__assign(_tslib.__assign({}, invokeConfig), {
169
- id: invokeConfig.id || invokeConfig.src,
171
+ id: invokeId,
170
172
  src: invokeConfig.src
171
173
  }));
172
174
  } else if (utils.isMachine(invokeConfig.src) || utils.isFunction(invokeConfig.src)) {
173
- var invokeSrc = "".concat(_this.id, ":invocation[").concat(i, "]"); // TODO: util function
174
-
175
- _this.machine.options.services = _tslib.__assign((_b = {}, _b[invokeSrc] = invokeConfig.src, _b), _this.machine.options.services);
175
+ var invokeId = invokeConfig.id || utils.createInvokeId(_this.id, i);
176
+ _this.machine.options.services = _tslib.__assign((_b = {}, _b[invokeId] = invokeConfig.src, _b), _this.machine.options.services);
176
177
  return invokeUtils.toInvokeDefinition(_tslib.__assign(_tslib.__assign({
177
- id: invokeSrc
178
+ id: invokeId
178
179
  }, invokeConfig), {
179
- src: invokeSrc
180
+ src: invokeId
180
181
  }));
181
182
  } else {
182
183
  var invokeSource = invokeConfig.src;
183
184
  return invokeUtils.toInvokeDefinition(_tslib.__assign(_tslib.__assign({
184
- id: invokeSource.type
185
+ id: utils.createInvokeId(_this.id, i)
185
186
  }, invokeConfig), {
186
187
  src: invokeSource
187
188
  }));
@@ -413,15 +414,11 @@ function () {
413
414
  }
414
415
 
415
416
  var subStateKeys = utils.keys(stateValue);
416
- var subStateNodes = subStateKeys.map(function (subStateKey) {
417
- return _this.getStateNode(subStateKey);
418
- });
419
- subStateNodes.push(this);
420
- return subStateNodes.concat(subStateKeys.reduce(function (allSubStateNodes, subStateKey) {
421
- var subStateNode = _this.getStateNode(subStateKey).getStateNodes(stateValue[subStateKey]);
422
-
423
- return allSubStateNodes.concat(subStateNode);
424
- }, []));
417
+ var subStateNodes = [this];
418
+ subStateNodes.push.apply(subStateNodes, _tslib.__spreadArray([], _tslib.__read(utils.flatten(subStateKeys.map(function (subStateKey) {
419
+ return _this.getStateNode(subStateKey).getStateNodes(stateValue[subStateKey]);
420
+ }))), false));
421
+ return subStateNodes;
425
422
  };
426
423
  /**
427
424
  * Returns `true` if this state node explicitly handles the given event.
@@ -560,6 +557,10 @@ function () {
560
557
  return this.transitionParallelNode(stateValue, state, _event);
561
558
  };
562
559
 
560
+ StateNode.prototype.getTransitionData = function (state, event) {
561
+ return this._transition(state.value, state, utils.toSCXMLEvent(event));
562
+ };
563
+
563
564
  StateNode.prototype.next = function (state, _event) {
564
565
  var e_3, _a;
565
566
 
@@ -728,12 +729,6 @@ function () {
728
729
  }
729
730
  }
730
731
 
731
- if (!transition.source) {
732
- transition.exitSet = []; // Ensure that root StateNode (machine) is entered
733
-
734
- transition.entrySet.push(this);
735
- }
736
-
737
732
  var doneEvents = utils.flatten(transition.entrySet.map(function (sn) {
738
733
  var events = [];
739
734
 
@@ -832,7 +827,7 @@ function () {
832
827
  var prevConfig = stateUtils.getConfiguration([], this.getStateNodes(currentState.value));
833
828
  var resolvedConfig = stateTransition.configuration.length ? stateUtils.getConfiguration(prevConfig, stateTransition.configuration) : prevConfig;
834
829
  stateTransition.configuration = _tslib.__spreadArray([], _tslib.__read(resolvedConfig), false);
835
- return this.resolveTransition(stateTransition, currentState, _event);
830
+ return this.resolveTransition(stateTransition, currentState, currentState.context, _event);
836
831
  };
837
832
 
838
833
  StateNode.prototype.resolveRaisedTransition = function (state, _event, originalEvent) {
@@ -850,7 +845,7 @@ function () {
850
845
  return state;
851
846
  };
852
847
 
853
- StateNode.prototype.resolveTransition = function (stateTransition, currentState, _event, context) {
848
+ StateNode.prototype.resolveTransition = function (stateTransition, currentState, context, _event) {
854
849
  var e_6, _a;
855
850
 
856
851
  var _this = this;
@@ -859,10 +854,6 @@ function () {
859
854
  _event = actions.initEvent;
860
855
  }
861
856
 
862
- if (context === void 0) {
863
- context = this.machine.context;
864
- }
865
-
866
857
  var configuration = stateTransition.configuration; // Transition will "apply" if:
867
858
  // - this is the initial state (there is no current state)
868
859
  // - OR there are transitions
@@ -870,8 +861,7 @@ function () {
870
861
  var willTransition = !currentState || stateTransition.transitions.length > 0;
871
862
  var resolvedStateValue = willTransition ? stateUtils.getValue(this.machine, configuration) : undefined;
872
863
  var historyValue = currentState ? currentState.historyValue ? currentState.historyValue : stateTransition.source ? this.machine.historyValue(currentState.value) : undefined : undefined;
873
- var currentContext = currentState ? currentState.context : context;
874
- var actions$1 = this.getActions(stateTransition, currentContext, _event, currentState);
864
+ var actions$1 = this.getActions(stateTransition, context, _event, currentState);
875
865
  var activities = currentState ? _tslib.__assign({}, currentState.activities) : {};
876
866
 
877
867
  try {
@@ -896,7 +886,7 @@ function () {
896
886
  }
897
887
  }
898
888
 
899
- var _b = _tslib.__read(actions.resolveActions(this, currentState, currentContext, _event, actions$1, this.machine.config.preserveActionOrder), 2),
889
+ var _b = _tslib.__read(actions.resolveActions(this, currentState, context, _event, actions$1, this.machine.config.preserveActionOrder), 2),
900
890
  resolvedActions = _b[0],
901
891
  updatedContext = _b[1];
902
892
 
@@ -915,7 +905,7 @@ function () {
915
905
  acc[action.activity.id] = Actor.createInvocableActor(action.activity, _this.machine, updatedContext, _event);
916
906
  return acc;
917
907
  }, currentState ? _tslib.__assign({}, currentState.children) : {});
918
- var resolvedConfiguration = resolvedStateValue ? stateTransition.configuration : currentState ? currentState.configuration : [];
908
+ var resolvedConfiguration = willTransition ? stateTransition.configuration : currentState ? currentState.configuration : [];
919
909
  var isDone = stateUtils.isInFinalState(resolvedConfiguration, this);
920
910
  var nextState = new State.State({
921
911
  value: resolvedStateValue || currentState.value,
@@ -935,7 +925,7 @@ function () {
935
925
  tags: currentState === null || currentState === void 0 ? void 0 : currentState.tags,
936
926
  machine: this
937
927
  });
938
- var didUpdateContext = currentContext !== updatedContext;
928
+ var didUpdateContext = context !== updatedContext;
939
929
  nextState.changed = _event.name === actionTypes.update || didUpdateContext; // Dispose of penultimate histories to prevent memory leaks
940
930
 
941
931
  var history = nextState.history;
@@ -1163,7 +1153,7 @@ function () {
1163
1153
  transitions: [],
1164
1154
  source: undefined,
1165
1155
  actions: []
1166
- }, undefined, undefined, context);
1156
+ }, undefined, context !== null && context !== void 0 ? context : this.machine.context, undefined);
1167
1157
  };
1168
1158
 
1169
1159
  Object.defineProperty(StateNode.prototype, "initialState", {
package/lib/actions.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { Action, Event, EventObject, SingleOrArray, SendAction, SendActionOptions, CancelAction, ActionObject, ActionType, Assigner, PropertyAssigner, AssignAction, ActionFunction, ActionFunctionMap, ActivityActionObject, ActionTypes, ActivityDefinition, RaiseAction, RaiseActionObject, DoneEvent, ErrorPlatformEvent, DoneEventObject, SendExpr, SendActionObject, PureAction, LogExpr, LogAction, LogActionObject, DelayFunctionMap, SCXML, ExprWithMeta, ChooseCondition, ChooseAction, AnyEventObject, Expr } from './types';
1
+ import { Action, Event, EventObject, SingleOrArray, SendAction, SendActionOptions, CancelAction, ActionObject, ActionType, Assigner, PropertyAssigner, AssignAction, ActionFunction, ActionFunctionMap, ActivityActionObject, ActionTypes, ActivityDefinition, RaiseAction, RaiseActionObject, DoneEvent, ErrorPlatformEvent, DoneEventObject, SendExpr, SendActionObject, PureAction, LogExpr, LogAction, LogActionObject, DelayFunctionMap, SCXML, ExprWithMeta, ChooseCondition, ChooseAction, AnyEventObject, Expr, Cast } from './types';
2
2
  import * as actionTypes from './actionTypes';
3
3
  import { State } from './State';
4
4
  import { StateNode } from './StateNode';
5
- import { StopAction, StopActionObject } from '.';
5
+ import { ActorRef, EventFrom, StopAction, StopActionObject } from '.';
6
6
  export { actionTypes };
7
7
  export declare const initEvent: SCXML.Event<{
8
8
  type: ActionTypes;
@@ -38,6 +38,22 @@ export declare function resolveSend<TContext, TEvent extends EventObject, TSentE
38
38
  * @param options Options to pass into the send event.
39
39
  */
40
40
  export declare function sendParent<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject>(event: Event<TSentEvent> | SendExpr<TContext, TEvent, TSentEvent>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, TSentEvent>;
41
+ declare type InferEvent<E extends EventObject> = {
42
+ [T in E['type']]: {
43
+ type: T;
44
+ } & Extract<E, {
45
+ type: T;
46
+ }>;
47
+ }[E['type']];
48
+ /**
49
+ * Sends an event to an actor.
50
+ *
51
+ * @param actor The `ActorRef` to send the event to.
52
+ * @param event The event to send, or an expression that evaluates to the event to send
53
+ * @param options Send action options
54
+ * @returns An XState send action object
55
+ */
56
+ export declare function sendTo<TContext, TEvent extends EventObject, TActor extends ActorRef<EventObject>>(actor: (ctx: TContext) => TActor, event: EventFrom<TActor> | SendExpr<TContext, TEvent, InferEvent<Cast<EventFrom<TActor>, EventObject>>>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, any>;
41
57
  /**
42
58
  * Sends an update event to this machine's parent.
43
59
  */
package/lib/actions.js CHANGED
@@ -156,6 +156,20 @@ function sendParent(event, options) {
156
156
  to: types.SpecialTargets.Parent
157
157
  }));
158
158
  }
159
+ /**
160
+ * Sends an event to an actor.
161
+ *
162
+ * @param actor The `ActorRef` to send the event to.
163
+ * @param event The event to send, or an expression that evaluates to the event to send
164
+ * @param options Send action options
165
+ * @returns An XState send action object
166
+ */
167
+
168
+ function sendTo(actor, event, options) {
169
+ return send(event, _tslib.__assign(_tslib.__assign({}, options), {
170
+ to: actor
171
+ }));
172
+ }
159
173
  /**
160
174
  * Sends an update event to this machine's parent.
161
175
  */
@@ -524,6 +538,7 @@ exports.resolveStop = resolveStop;
524
538
  exports.respond = respond;
525
539
  exports.send = send;
526
540
  exports.sendParent = sendParent;
541
+ exports.sendTo = sendTo;
527
542
  exports.sendUpdate = sendUpdate;
528
543
  exports.start = start;
529
544
  exports.stop = stop;
package/lib/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import { StateNode } from './StateNode';
4
4
  import { State } from './State';
5
5
  import { Machine, createMachine } from './Machine';
6
6
  import { Actor } from './Actor';
7
- import { raise, send, sendParent, sendUpdate, log, start, stop, assign, after, done, respond, doneInvoke, forwardTo, escalate, choose, pure } from './actions';
7
+ import { raise, send, sendParent, sendTo, sendUpdate, log, start, stop, assign, after, done, respond, doneInvoke, forwardTo, escalate, choose, pure } from './actions';
8
8
  import { interpret, Interpreter, spawn, InterpreterStatus } from './interpreter';
9
9
  import { matchState } from './match';
10
10
  import { createSchema } from './schema';
@@ -12,6 +12,7 @@ declare const actions: {
12
12
  raise: typeof raise;
13
13
  send: typeof send;
14
14
  sendParent: typeof sendParent;
15
+ sendTo: typeof sendTo;
15
16
  sendUpdate: typeof sendUpdate;
16
17
  log: typeof log;
17
18
  cancel: (sendId: string | number) => import("./types").CancelAction;
@@ -28,4 +29,9 @@ declare const actions: {
28
29
  };
29
30
  export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, InterpreterStatus, matchState, spawn, doneInvoke, createMachine, createSchema };
30
31
  export * from './types';
32
+ declare global {
33
+ interface SymbolConstructor {
34
+ readonly observable: symbol;
35
+ }
36
+ }
31
37
  //# sourceMappingURL=index.d.ts.map
package/lib/index.js CHANGED
@@ -17,6 +17,7 @@ var actions = {
17
17
  raise: actions$1.raise,
18
18
  send: actions$1.send,
19
19
  sendParent: actions$1.sendParent,
20
+ sendTo: actions$1.sendTo,
20
21
  sendUpdate: actions$1.sendUpdate,
21
22
  log: actions$1.log,
22
23
  cancel: actions$1.cancel,
@@ -21,11 +21,6 @@ export declare enum InterpreterStatus {
21
21
  Running = 1,
22
22
  Stopped = 2
23
23
  }
24
- declare global {
25
- interface SymbolConstructor {
26
- readonly observable: symbol;
27
- }
28
- }
29
24
  export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
30
25
  value: any;
31
26
  context: TContext;