xstate 4.10.0 → 4.14.0

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 (64) hide show
  1. package/CHANGELOG.md +205 -0
  2. package/LICENSE +22 -0
  3. package/README.md +2 -2
  4. package/dist/xstate.interpreter.js +1 -1
  5. package/dist/xstate.js +1 -1
  6. package/dist/xstate.web.js +2 -2
  7. package/es/Actor.d.ts +5 -3
  8. package/es/Actor.js +26 -4
  9. package/es/State.d.ts +5 -2
  10. package/es/StateNode.d.ts +8 -8
  11. package/es/StateNode.js +61 -41
  12. package/es/actions.d.ts +9 -5
  13. package/es/actions.js +28 -13
  14. package/es/index.d.ts +2 -2
  15. package/es/index.js +1 -1
  16. package/es/interpreter.d.ts +29 -15
  17. package/es/interpreter.js +99 -112
  18. package/es/invokeUtils.d.ts +7 -0
  19. package/es/invokeUtils.js +39 -0
  20. package/es/match.d.ts +4 -1
  21. package/es/serviceScope.d.ts +10 -0
  22. package/es/serviceScope.js +18 -0
  23. package/es/stateUtils.d.ts +1 -1
  24. package/es/types.d.ts +79 -32
  25. package/es/utils.d.ts +4 -3
  26. package/es/utils.js +12 -3
  27. package/lib/Actor.d.ts +5 -3
  28. package/lib/Actor.js +24 -3
  29. package/lib/Machine.js +1 -0
  30. package/lib/SimulatedClock.js +1 -0
  31. package/lib/State.d.ts +5 -2
  32. package/lib/State.js +1 -0
  33. package/lib/StateNode.d.ts +8 -8
  34. package/lib/StateNode.js +69 -42
  35. package/lib/actionTypes.js +1 -0
  36. package/lib/actions.d.ts +9 -5
  37. package/lib/actions.js +27 -19
  38. package/lib/constants.js +1 -0
  39. package/lib/devTools.js +1 -0
  40. package/lib/each.js +1 -0
  41. package/lib/environment.js +1 -0
  42. package/lib/index.d.ts +2 -2
  43. package/lib/index.js +29 -20
  44. package/lib/interpreter.d.ts +28 -14
  45. package/lib/interpreter.js +82 -88
  46. package/lib/invokeUtils.d.ts +7 -0
  47. package/lib/invokeUtils.js +42 -0
  48. package/lib/json.js +1 -0
  49. package/lib/mapState.js +1 -0
  50. package/lib/match.d.ts +4 -1
  51. package/lib/match.js +1 -0
  52. package/lib/patterns.js +1 -0
  53. package/lib/registry.js +1 -0
  54. package/lib/scheduler.js +1 -0
  55. package/lib/scxml.js +1 -0
  56. package/lib/serviceScope.d.ts +10 -0
  57. package/lib/serviceScope.js +15 -0
  58. package/lib/stateUtils.d.ts +1 -1
  59. package/lib/stateUtils.js +1 -0
  60. package/lib/types.d.ts +79 -32
  61. package/lib/types.js +1 -0
  62. package/lib/utils.d.ts +4 -3
  63. package/lib/utils.js +9 -2
  64. package/package.json +6 -6
package/lib/StateNode.js CHANGED
@@ -53,6 +53,7 @@ var __values = (this && this.__values) || function(o) {
53
53
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
54
54
  };
55
55
  Object.defineProperty(exports, "__esModule", { value: true });
56
+ exports.StateNode = void 0;
56
57
  var utils_1 = require("./utils");
57
58
  var types_1 = require("./types");
58
59
  var utils_2 = require("./utils");
@@ -63,6 +64,7 @@ var environment_1 = require("./environment");
63
64
  var constants_1 = require("./constants");
64
65
  var stateUtils_1 = require("./stateUtils");
65
66
  var Actor_1 = require("./Actor");
67
+ var invokeUtils_1 = require("./invokeUtils");
66
68
  var NULL_EVENT = '';
67
69
  var STATE_IDENTIFIER = '#';
68
70
  var WILDCARD = '*';
@@ -179,21 +181,23 @@ var StateNode = /** @class */ (function () {
179
181
  // History config
180
182
  this.history =
181
183
  this.config.history === true ? 'shallow' : this.config.history || false;
182
- this._transient = !this.config.on
183
- ? false
184
- : Array.isArray(this.config.on)
185
- ? this.config.on.some(function (_a) {
186
- var event = _a.event;
187
- return event === NULL_EVENT;
188
- })
189
- : NULL_EVENT in this.config.on;
184
+ this._transient =
185
+ !!this.config.always ||
186
+ (!this.config.on
187
+ ? false
188
+ : Array.isArray(this.config.on)
189
+ ? this.config.on.some(function (_a) {
190
+ var event = _a.event;
191
+ return event === NULL_EVENT;
192
+ })
193
+ : NULL_EVENT in this.config.on);
190
194
  this.strict = !!this.config.strict;
191
195
  // TODO: deprecate (entry)
192
196
  this.onEntry = utils_1.toArray(this.config.entry || this.config.onEntry).map(function (action) { return actions_1.toActionObject(action); });
193
197
  // TODO: deprecate (exit)
194
198
  this.onExit = utils_1.toArray(this.config.exit || this.config.onExit).map(function (action) { return actions_1.toActionObject(action); });
195
199
  this.meta = this.config.meta;
196
- this.data =
200
+ this.doneData =
197
201
  this.type === 'final'
198
202
  ? this.config.data
199
203
  : undefined;
@@ -201,25 +205,33 @@ var StateNode = /** @class */ (function () {
201
205
  var _a, _b;
202
206
  if (utils_1.isMachine(invokeConfig)) {
203
207
  _this.machine.options.services = __assign((_a = {}, _a[invokeConfig.id] = invokeConfig, _a), _this.machine.options.services);
204
- return {
205
- type: actionTypes.invoke,
208
+ return invokeUtils_1.toInvokeDefinition({
206
209
  src: invokeConfig.id,
207
210
  id: invokeConfig.id
208
- };
211
+ });
212
+ }
213
+ else if (utils_1.isString(invokeConfig.src)) {
214
+ return invokeUtils_1.toInvokeDefinition(__assign(__assign({}, invokeConfig), { id: invokeConfig.id || invokeConfig.src, src: invokeConfig.src }));
209
215
  }
210
- else if (typeof invokeConfig.src !== 'string') {
216
+ else if (utils_1.isMachine(invokeConfig.src) || utils_1.isFunction(invokeConfig.src)) {
211
217
  var invokeSrc = _this.id + ":invocation[" + i + "]"; // TODO: util function
212
218
  _this.machine.options.services = __assign((_b = {}, _b[invokeSrc] = invokeConfig.src, _b), _this.machine.options.services);
213
- return __assign(__assign({ type: actionTypes.invoke, id: invokeSrc }, invokeConfig), { src: invokeSrc });
219
+ return invokeUtils_1.toInvokeDefinition(__assign(__assign({ id: invokeSrc }, invokeConfig), { src: invokeSrc }));
214
220
  }
215
221
  else {
216
- return __assign(__assign({}, invokeConfig), { type: actionTypes.invoke, id: invokeConfig.id || invokeConfig.src, src: invokeConfig.src });
222
+ var invokeSource = invokeConfig.src;
223
+ return invokeUtils_1.toInvokeDefinition(__assign(__assign({ id: invokeSource.type }, invokeConfig), { src: invokeSource }));
217
224
  }
218
225
  });
219
226
  this.activities = utils_1.toArray(this.config.activities)
220
227
  .concat(this.invoke)
221
228
  .map(function (activity) { return actions_1.toActivityDefinition(activity); });
222
229
  this.transition = this.transition.bind(this);
230
+ // TODO: this is the real fix for initialization once
231
+ // state node getters are deprecated
232
+ // if (!this.parent) {
233
+ // this._init();
234
+ // }
223
235
  }
224
236
  StateNode.prototype._init = function () {
225
237
  if (this.__cache.transitions) {
@@ -273,11 +285,11 @@ var StateNode = /** @class */ (function () {
273
285
  activities: this.activities || [],
274
286
  meta: this.meta,
275
287
  order: this.order || -1,
276
- data: this.data,
288
+ data: this.doneData,
277
289
  invoke: this.invoke
278
290
  };
279
291
  },
280
- enumerable: true,
292
+ enumerable: false,
281
293
  configurable: true
282
294
  });
283
295
  StateNode.prototype.toJSON = function () {
@@ -298,7 +310,7 @@ var StateNode = /** @class */ (function () {
298
310
  return map;
299
311
  }, {}));
300
312
  },
301
- enumerable: true,
313
+ enumerable: false,
302
314
  configurable: true
303
315
  });
304
316
  Object.defineProperty(StateNode.prototype, "after", {
@@ -307,7 +319,7 @@ var StateNode = /** @class */ (function () {
307
319
  ((this.__cache.delayedTransitions = this.getDelayedTransitions()),
308
320
  this.__cache.delayedTransitions));
309
321
  },
310
- enumerable: true,
322
+ enumerable: false,
311
323
  configurable: true
312
324
  });
313
325
  Object.defineProperty(StateNode.prototype, "transitions", {
@@ -319,7 +331,7 @@ var StateNode = /** @class */ (function () {
319
331
  ((this.__cache.transitions = this.formatTransitions()),
320
332
  this.__cache.transitions));
321
333
  },
322
- enumerable: true,
334
+ enumerable: false,
323
335
  configurable: true
324
336
  });
325
337
  StateNode.prototype.getCandidates = function (eventName) {
@@ -645,14 +657,16 @@ var StateNode = /** @class */ (function () {
645
657
  if (!parent.parent) {
646
658
  return events;
647
659
  }
648
- events.push(actions_1.done(sn.id, sn.data), // TODO: deprecate - final states should not emit done events for their own state.
649
- actions_1.done(parent.id, sn.data ? utils_1.mapContext(sn.data, currentContext, _event) : undefined));
660
+ events.push(actions_1.done(sn.id, sn.doneData), // TODO: deprecate - final states should not emit done events for their own state.
661
+ actions_1.done(parent.id, sn.doneData
662
+ ? utils_1.mapContext(sn.doneData, currentContext, _event)
663
+ : undefined));
650
664
  var grandparent = parent.parent;
651
665
  if (grandparent.type === 'parallel') {
652
666
  if (stateUtils_1.getChildren(grandparent).every(function (parentNode) {
653
667
  return stateUtils_1.isInFinalState(transition.configuration, parentNode);
654
668
  })) {
655
- events.push(actions_1.done(grandparent.id, grandparent.data));
669
+ events.push(actions_1.done(grandparent.id));
656
670
  }
657
671
  }
658
672
  return events;
@@ -678,7 +692,8 @@ var StateNode = /** @class */ (function () {
678
692
  * @param context The current context (extended state) of the current state
679
693
  */
680
694
  StateNode.prototype.transition = function (state, event, context) {
681
- if (state === void 0) { state = this.initialState; }
695
+ if (state === void 0) { state = this
696
+ .initialState; }
682
697
  var _event = utils_1.toSCXMLEvent(event);
683
698
  var currentState;
684
699
  if (state instanceof State_1.State) {
@@ -722,6 +737,7 @@ var StateNode = /** @class */ (function () {
722
737
  var currentActions = state.actions;
723
738
  state = this.transition(state, _event);
724
739
  // Save original event to state
740
+ // TODO: this should be the raised event! Delete in V5 (breaking)
725
741
  state._event = originalEvent;
726
742
  state.event = originalEvent.data;
727
743
  (_a = state.actions).unshift.apply(_a, __spread(currentActions));
@@ -729,6 +745,7 @@ var StateNode = /** @class */ (function () {
729
745
  };
730
746
  StateNode.prototype.resolveTransition = function (stateTransition, currentState, _event, context) {
731
747
  var e_6, _a;
748
+ var _this = this;
732
749
  if (_event === void 0) { _event = actions_1.initEvent; }
733
750
  if (context === void 0) { context = this.machine.context; }
734
751
  var configuration = stateTransition.configuration;
@@ -775,12 +792,13 @@ var StateNode = /** @class */ (function () {
775
792
  types_1.SpecialTargets.Internal);
776
793
  }), 2), raisedEvents = _c[0], nonRaisedActions = _c[1];
777
794
  var invokeActions = resolvedActions.filter(function (action) {
795
+ var _a;
778
796
  return (action.type === actionTypes.start &&
779
- action.activity.type ===
797
+ ((_a = action.activity) === null || _a === void 0 ? void 0 : _a.type) ===
780
798
  actionTypes.invoke);
781
799
  });
782
800
  var children = invokeActions.reduce(function (acc, action) {
783
- acc[action.activity.id] = Actor_1.createInvocableActor(action.activity);
801
+ acc[action.activity.id] = Actor_1.createInvocableActor(action.activity, _this.machine, updatedContext, _event);
784
802
  return acc;
785
803
  }, currentState
786
804
  ? __assign({}, currentState.children) : {});
@@ -842,7 +860,9 @@ var StateNode = /** @class */ (function () {
842
860
  var maybeNextState = nextState;
843
861
  if (!isDone) {
844
862
  var isTransient = this._transient ||
845
- configuration.some(function (stateNode) { return stateNode._transient; });
863
+ configuration.some(function (stateNode) {
864
+ return stateNode._transient;
865
+ });
846
866
  if (isTransient) {
847
867
  maybeNextState = this.resolveRaisedTransition(maybeNextState, {
848
868
  type: actionTypes.nullEvent
@@ -999,7 +1019,7 @@ var StateNode = /** @class */ (function () {
999
1019
  this.__cache.initialStateValue = initialStateValue;
1000
1020
  return this.__cache.initialStateValue;
1001
1021
  },
1002
- enumerable: true,
1022
+ enumerable: false,
1003
1023
  configurable: true
1004
1024
  });
1005
1025
  StateNode.prototype.getInitialState = function (stateValue, context) {
@@ -1019,14 +1039,14 @@ var StateNode = /** @class */ (function () {
1019
1039
  * entering the initial state.
1020
1040
  */
1021
1041
  get: function () {
1022
- this._init();
1042
+ this._init(); // TODO: this should be in the constructor (see note in constructor)
1023
1043
  var initialStateValue = this.initialStateValue;
1024
1044
  if (!initialStateValue) {
1025
1045
  throw new Error("Cannot retrieve initial state from simple state '" + this.id + "'.");
1026
1046
  }
1027
1047
  return this.getInitialState(initialStateValue);
1028
1048
  },
1029
- enumerable: true,
1049
+ enumerable: false,
1030
1050
  configurable: true
1031
1051
  });
1032
1052
  Object.defineProperty(StateNode.prototype, "target", {
@@ -1051,7 +1071,7 @@ var StateNode = /** @class */ (function () {
1051
1071
  }
1052
1072
  return target;
1053
1073
  },
1054
- enumerable: true,
1074
+ enumerable: false,
1055
1075
  configurable: true
1056
1076
  });
1057
1077
  /**
@@ -1087,7 +1107,7 @@ var StateNode = /** @class */ (function () {
1087
1107
  return _this.getFromRelativePath(initialPath);
1088
1108
  }));
1089
1109
  },
1090
- enumerable: true,
1110
+ enumerable: false,
1091
1111
  configurable: true
1092
1112
  });
1093
1113
  /**
@@ -1171,7 +1191,7 @@ var StateNode = /** @class */ (function () {
1171
1191
  }));
1172
1192
  return [this.id].concat(childStateIds);
1173
1193
  },
1174
- enumerable: true,
1194
+ enumerable: false,
1175
1195
  configurable: true
1176
1196
  });
1177
1197
  Object.defineProperty(StateNode.prototype, "events", {
@@ -1217,7 +1237,7 @@ var StateNode = /** @class */ (function () {
1217
1237
  }
1218
1238
  return (this.__cache.events = Array.from(events));
1219
1239
  },
1220
- enumerable: true,
1240
+ enumerable: false,
1221
1241
  configurable: true
1222
1242
  });
1223
1243
  Object.defineProperty(StateNode.prototype, "ownEvents", {
@@ -1236,7 +1256,7 @@ var StateNode = /** @class */ (function () {
1236
1256
  .map(function (transition) { return transition.eventType; }));
1237
1257
  return Array.from(events);
1238
1258
  },
1239
- enumerable: true,
1259
+ enumerable: false,
1240
1260
  configurable: true
1241
1261
  });
1242
1262
  StateNode.prototype.resolveTarget = function (_target) {
@@ -1282,7 +1302,7 @@ var StateNode = /** @class */ (function () {
1282
1302
  var target = this.resolveTarget(normalizedTarget);
1283
1303
  var transition = __assign(__assign({}, transitionConfig), { actions: actions_1.toActionObjects(utils_1.toArray(transitionConfig.actions)), cond: utils_1.toGuard(transitionConfig.cond, guards), target: target, source: this, internal: internal, eventType: transitionConfig.event, toJSON: function () { return (__assign(__assign({}, transition), { target: transition.target
1284
1304
  ? transition.target.map(function (t) { return "#" + t.id; })
1285
- : undefined, source: "#{this.id}" })); } });
1305
+ : undefined, source: "#" + _this.id })); } });
1286
1306
  return transition;
1287
1307
  };
1288
1308
  StateNode.prototype.formatTransitions = function () {
@@ -1296,17 +1316,24 @@ var StateNode = /** @class */ (function () {
1296
1316
  onConfig = this.config.on;
1297
1317
  }
1298
1318
  else {
1299
- var _b = this.config.on, _c = WILDCARD, _d = _b[_c], wildcardConfigs = _d === void 0 ? [] : _d, strictOnConfigs_1 = __rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
1300
- onConfig = utils_1.flatten(utils_1.keys(strictOnConfigs_1)
1319
+ var _b = this.config.on, _c = WILDCARD, _d = _b[_c], wildcardConfigs = _d === void 0 ? [] : _d, strictTransitionConfigs_1 = __rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
1320
+ onConfig = utils_1.flatten(utils_1.keys(strictTransitionConfigs_1)
1301
1321
  .map(function (key) {
1302
- var arrayified = utils_1.toTransitionConfigArray(key, strictOnConfigs_1[key]);
1322
+ if (!environment_1.IS_PRODUCTION && key === NULL_EVENT) {
1323
+ utils_1.warn(false, "Empty string transition configs (e.g., `{ on: { '': ... }}`) for transient transitions are deprecated. Specify the transition in the `{ always: ... }` property instead. " +
1324
+ ("Please check the `on` configuration for \"#" + _this.id + "\"."));
1325
+ }
1326
+ var transitionConfigArray = utils_1.toTransitionConfigArray(key, strictTransitionConfigs_1[key]);
1303
1327
  if (!environment_1.IS_PRODUCTION) {
1304
- validateArrayifiedTransitions(_this, key, arrayified);
1328
+ validateArrayifiedTransitions(_this, key, transitionConfigArray);
1305
1329
  }
1306
- return arrayified;
1330
+ return transitionConfigArray;
1307
1331
  })
1308
1332
  .concat(utils_1.toTransitionConfigArray(WILDCARD, wildcardConfigs)));
1309
1333
  }
1334
+ var eventlessConfig = this.config.always
1335
+ ? utils_1.toTransitionConfigArray('', this.config.always)
1336
+ : [];
1310
1337
  var doneConfig = this.config.onDone
1311
1338
  ? utils_1.toTransitionConfigArray(String(actions_1.done(this.id)), this.config.onDone)
1312
1339
  : [];
@@ -1324,7 +1351,7 @@ var StateNode = /** @class */ (function () {
1324
1351
  return settleTransitions;
1325
1352
  }));
1326
1353
  var delayedTransitions = this.after;
1327
- var formattedTransitions = utils_1.flatten(__spread(doneConfig, invokeConfig, onConfig).map(function (transitionConfig) {
1354
+ var formattedTransitions = utils_1.flatten(__spread(doneConfig, invokeConfig, onConfig, eventlessConfig).map(function (transitionConfig) {
1328
1355
  return utils_1.toArray(transitionConfig).map(function (transition) {
1329
1356
  return _this.formatTransition(transition);
1330
1357
  });
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pure = exports.choose = exports.update = exports.error = exports.errorPlatform = exports.errorExecution = exports.invoke = exports.init = exports.log = exports.doneState = exports.after = exports.assign = exports.nullEvent = exports.cancel = exports.send = exports.raise = exports.stop = exports.start = void 0;
3
4
  var types_1 = require("./types");
4
5
  // xstate-specific action types
5
6
  exports.start = types_1.ActionTypes.Start;
package/lib/actions.d.ts CHANGED
@@ -1,14 +1,15 @@
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, ChooseConditon, ChooseAction, AnyEventObject } 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, ChooseConditon, ChooseAction, AnyEventObject, Expr } 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
6
  export { actionTypes };
6
7
  export declare const initEvent: SCXML.Event<{
7
8
  type: ActionTypes;
8
9
  }>;
9
10
  export declare function getActionFunction<TContext, TEvent extends EventObject>(actionType: ActionType, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | undefined;
10
11
  export declare function toActionObject<TContext, TEvent extends EventObject>(action: Action<TContext, TEvent>, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent>;
11
- export declare const toActionObjects: <TContext, TEvent extends EventObject>(action?: string | RaiseAction<AnyEventObject> | ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | AssignAction<Required<TContext>, TEvent> | SendAction<TContext, TEvent, AnyEventObject> | ChooseAction<TContext, TEvent> | Action<TContext, TEvent>[] | undefined, actionFunctionMap?: Record<string, ActionFunction<TContext, TEvent> | ActionObject<TContext, TEvent>> | undefined) => ActionObject<TContext, TEvent>[];
12
+ export declare const toActionObjects: <TContext, TEvent extends EventObject>(action?: string | ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | Action<TContext, TEvent>[] | undefined, actionFunctionMap?: Record<string, ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent>> | undefined) => ActionObject<TContext, TEvent>[];
12
13
  export declare function toActivityDefinition<TContext, TEvent extends EventObject>(action: string | ActivityDefinition<TContext, TEvent>): ActivityDefinition<TContext, TEvent>;
13
14
  /**
14
15
  * Raises an event. This places the event in the internal event queue, so that
@@ -16,7 +17,7 @@ export declare function toActivityDefinition<TContext, TEvent extends EventObjec
16
17
  *
17
18
  * @param eventType The event to raise.
18
19
  */
19
- export declare function raise<TContext, TEvent extends EventObject>(event: Event<TEvent>): RaiseAction<TEvent> | SendAction<TContext, TEvent, TEvent>;
20
+ export declare function raise<TContext, TEvent extends EventObject>(event: Event<TEvent>): RaiseAction<TEvent> | SendAction<TContext, AnyEventObject, TEvent>;
20
21
  export declare function resolveRaise<TEvent extends EventObject>(action: RaiseAction<TEvent>): RaiseActionObject<TEvent>;
21
22
  /**
22
23
  * Sends an event. This returns an action that will be read by an interpreter to
@@ -77,9 +78,12 @@ export declare function start<TContext, TEvent extends EventObject>(activity: st
77
78
  /**
78
79
  * Stops an activity.
79
80
  *
80
- * @param activity The activity to stop.
81
+ * @param actorRef The activity to stop.
81
82
  */
82
- export declare function stop<TContext, TEvent extends EventObject>(activity: string | ActivityDefinition<TContext, TEvent>): ActivityActionObject<TContext, TEvent>;
83
+ export declare function stop<TContext, TEvent extends EventObject>(actorRef: string | ActivityDefinition<TContext, TEvent> | Expr<TContext, TEvent, string | {
84
+ id: string;
85
+ }>): StopAction<TContext, TEvent>;
86
+ export declare function resolveStop<TContext, TEvent extends EventObject>(action: StopAction<TContext, TEvent>, context: TContext, _event: SCXML.Event<TEvent>): StopActionObject;
83
87
  /**
84
88
  * Updates the current context of the machine.
85
89
  *
package/lib/actions.js CHANGED
@@ -10,17 +10,6 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
- var __rest = (this && this.__rest) || function (s, e) {
14
- var t = {};
15
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
- t[p] = s[p];
17
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
- t[p[i]] = s[p[i]];
21
- }
22
- return t;
23
- };
24
13
  var __read = (this && this.__read) || function (o, n) {
25
14
  var m = typeof Symbol === "function" && o[Symbol.iterator];
26
15
  if (!m) return o;
@@ -38,11 +27,11 @@ var __read = (this && this.__read) || function (o, n) {
38
27
  return ar;
39
28
  };
40
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.resolveActions = exports.choose = exports.escalate = exports.forwardTo = exports.pure = exports.error = exports.doneInvoke = exports.done = exports.after = exports.isActionObject = exports.assign = exports.resolveStop = exports.stop = exports.start = exports.cancel = exports.resolveLog = exports.log = exports.respond = exports.sendUpdate = exports.sendParent = exports.resolveSend = exports.send = exports.resolveRaise = exports.raise = exports.toActivityDefinition = exports.toActionObjects = exports.toActionObject = exports.getActionFunction = exports.initEvent = exports.actionTypes = void 0;
41
31
  var types_1 = require("./types");
42
32
  var actionTypes = require("./actionTypes");
43
33
  exports.actionTypes = actionTypes;
44
34
  var utils_1 = require("./utils");
45
- var utils_2 = require("./utils");
46
35
  var environment_1 = require("./environment");
47
36
  exports.initEvent = utils_1.toSCXMLEvent({ type: actionTypes.init });
48
37
  function getActionFunction(actionType, actionFunctionMap) {
@@ -81,8 +70,8 @@ function toActionObject(action, actionFunctionMap) {
81
70
  actionObject = __assign(__assign({}, action), { exec: exec });
82
71
  }
83
72
  else if (exec) {
84
- var type = action.type, other = __rest(action, ["type"]);
85
- actionObject = __assign(__assign({ type: type }, exec), other);
73
+ var actionType = exec.type || action.type;
74
+ actionObject = __assign(__assign(__assign({}, exec), action), { type: actionType });
86
75
  }
87
76
  else {
88
77
  actionObject = action;
@@ -100,7 +89,7 @@ exports.toActionObjects = function (action, actionFunctionMap) {
100
89
  if (!action) {
101
90
  return [];
102
91
  }
103
- var actions = utils_2.isArray(action) ? action : [action];
92
+ var actions = utils_1.isArray(action) ? action : [action];
104
93
  return actions.map(function (subAction) {
105
94
  return toActionObject(subAction, actionFunctionMap);
106
95
  });
@@ -269,17 +258,33 @@ exports.start = start;
269
258
  /**
270
259
  * Stops an activity.
271
260
  *
272
- * @param activity The activity to stop.
261
+ * @param actorRef The activity to stop.
273
262
  */
274
- function stop(activity) {
275
- var activityDef = toActivityDefinition(activity);
263
+ function stop(actorRef) {
264
+ var activity = utils_1.isFunction(actorRef)
265
+ ? actorRef
266
+ : toActivityDefinition(actorRef);
276
267
  return {
277
268
  type: types_1.ActionTypes.Stop,
278
- activity: activityDef,
269
+ activity: activity,
279
270
  exec: undefined
280
271
  };
281
272
  }
282
273
  exports.stop = stop;
274
+ function resolveStop(action, context, _event) {
275
+ var actorRefOrString = utils_1.isFunction(action.activity)
276
+ ? action.activity(context, _event.data)
277
+ : action.activity;
278
+ var resolvedActorRef = typeof actorRefOrString === 'string'
279
+ ? { id: actorRefOrString }
280
+ : actorRefOrString;
281
+ var actionObject = {
282
+ type: types_1.ActionTypes.Stop,
283
+ activity: resolvedActorRef
284
+ };
285
+ return actionObject;
286
+ }
287
+ exports.resolveStop = resolveStop;
283
288
  /**
284
289
  * Updates the current context of the machine.
285
290
  *
@@ -439,6 +444,9 @@ function resolveActions(machine, currentState, currentContext, _event, actions)
439
444
  updatedContext = resolved[1];
440
445
  return resolved[0];
441
446
  }
447
+ case actionTypes.stop: {
448
+ return resolveStop(actionObject, updatedContext, _event);
449
+ }
442
450
  default:
443
451
  return toActionObject(actionObject, machine.options.actions);
444
452
  }
package/lib/constants.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TARGETLESS_KEY = exports.DEFAULT_GUARD_TYPE = exports.EMPTY_ACTIVITY_MAP = exports.STATE_DELIMITER = void 0;
3
4
  exports.STATE_DELIMITER = '.';
4
5
  exports.EMPTY_ACTIVITY_MAP = {};
5
6
  exports.DEFAULT_GUARD_TYPE = 'xstate.guard';
package/lib/devTools.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerService = void 0;
3
4
  var environment_1 = require("./environment");
4
5
  function getDevTools() {
5
6
  var w = window;
package/lib/each.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.each = void 0;
3
4
  function each(collection, item, indexOrActions, maybeActions) {
4
5
  var actions = maybeActions || indexOrActions;
5
6
  var index = maybeActions ? indexOrActions : undefined;
@@ -1,3 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IS_PRODUCTION = void 0;
3
4
  exports.IS_PRODUCTION = process.env.NODE_ENV === 'production';
package/lib/index.d.ts CHANGED
@@ -5,7 +5,7 @@ import { State } from './State';
5
5
  import { Machine, createMachine } from './Machine';
6
6
  import { Actor } from './Actor';
7
7
  import { raise, send, sendParent, sendUpdate, log, start, stop, assign, after, done, respond, doneInvoke, forwardTo, escalate, choose, pure } from './actions';
8
- import { interpret, Interpreter, spawn } from './interpreter';
8
+ import { interpret, Interpreter, spawn, InterpreterStatus } from './interpreter';
9
9
  import { matchState } from './match';
10
10
  declare const actions: {
11
11
  raise: typeof raise;
@@ -25,6 +25,6 @@ declare const actions: {
25
25
  choose: typeof choose;
26
26
  pure: typeof pure;
27
27
  };
28
- export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, matchState, spawn, doneInvoke, createMachine };
28
+ export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, InterpreterStatus, matchState, spawn, doneInvoke, createMachine };
29
29
  export * from './types';
30
30
  //# sourceMappingURL=index.d.ts.map
package/lib/index.js CHANGED
@@ -1,32 +1,41 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
5
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.createMachine = exports.doneInvoke = exports.spawn = exports.matchState = exports.InterpreterStatus = exports.Interpreter = exports.interpret = exports.forwardTo = exports.sendUpdate = exports.sendParent = exports.send = exports.assign = exports.actions = exports.mapState = exports.matchesState = exports.State = exports.StateNode = exports.Machine = void 0;
6
14
  var utils_1 = require("./utils");
7
- exports.matchesState = utils_1.matchesState;
15
+ Object.defineProperty(exports, "matchesState", { enumerable: true, get: function () { return utils_1.matchesState; } });
8
16
  var mapState_1 = require("./mapState");
9
- exports.mapState = mapState_1.mapState;
17
+ Object.defineProperty(exports, "mapState", { enumerable: true, get: function () { return mapState_1.mapState; } });
10
18
  var StateNode_1 = require("./StateNode");
11
- exports.StateNode = StateNode_1.StateNode;
19
+ Object.defineProperty(exports, "StateNode", { enumerable: true, get: function () { return StateNode_1.StateNode; } });
12
20
  var State_1 = require("./State");
13
- exports.State = State_1.State;
21
+ Object.defineProperty(exports, "State", { enumerable: true, get: function () { return State_1.State; } });
14
22
  var Machine_1 = require("./Machine");
15
- exports.Machine = Machine_1.Machine;
16
- exports.createMachine = Machine_1.createMachine;
23
+ Object.defineProperty(exports, "Machine", { enumerable: true, get: function () { return Machine_1.Machine; } });
24
+ Object.defineProperty(exports, "createMachine", { enumerable: true, get: function () { return Machine_1.createMachine; } });
17
25
  var actions_1 = require("./actions");
18
- exports.send = actions_1.send;
19
- exports.sendParent = actions_1.sendParent;
20
- exports.sendUpdate = actions_1.sendUpdate;
21
- exports.assign = actions_1.assign;
22
- exports.doneInvoke = actions_1.doneInvoke;
23
- exports.forwardTo = actions_1.forwardTo;
26
+ Object.defineProperty(exports, "send", { enumerable: true, get: function () { return actions_1.send; } });
27
+ Object.defineProperty(exports, "sendParent", { enumerable: true, get: function () { return actions_1.sendParent; } });
28
+ Object.defineProperty(exports, "sendUpdate", { enumerable: true, get: function () { return actions_1.sendUpdate; } });
29
+ Object.defineProperty(exports, "assign", { enumerable: true, get: function () { return actions_1.assign; } });
30
+ Object.defineProperty(exports, "doneInvoke", { enumerable: true, get: function () { return actions_1.doneInvoke; } });
31
+ Object.defineProperty(exports, "forwardTo", { enumerable: true, get: function () { return actions_1.forwardTo; } });
24
32
  var interpreter_1 = require("./interpreter");
25
- exports.interpret = interpreter_1.interpret;
26
- exports.Interpreter = interpreter_1.Interpreter;
27
- exports.spawn = interpreter_1.spawn;
33
+ Object.defineProperty(exports, "interpret", { enumerable: true, get: function () { return interpreter_1.interpret; } });
34
+ Object.defineProperty(exports, "Interpreter", { enumerable: true, get: function () { return interpreter_1.Interpreter; } });
35
+ Object.defineProperty(exports, "spawn", { enumerable: true, get: function () { return interpreter_1.spawn; } });
36
+ Object.defineProperty(exports, "InterpreterStatus", { enumerable: true, get: function () { return interpreter_1.InterpreterStatus; } });
28
37
  var match_1 = require("./match");
29
- exports.matchState = match_1.matchState;
38
+ Object.defineProperty(exports, "matchState", { enumerable: true, get: function () { return match_1.matchState; } });
30
39
  var actions = {
31
40
  raise: actions_1.raise,
32
41
  send: actions_1.send,
@@ -46,4 +55,4 @@ var actions = {
46
55
  pure: actions_1.pure
47
56
  };
48
57
  exports.actions = actions;
49
- __export(require("./types"));
58
+ __exportStar(require("./types"), exports);