xstate 4.27.0-pr2674-2021926101023 → 4.29.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 (65) hide show
  1. package/CHANGELOG.md +210 -94
  2. package/README.md +5 -5
  3. package/dist/xstate.interpreter.js +1 -1
  4. package/dist/xstate.js +1 -1
  5. package/dist/xstate.web.js +2 -2
  6. package/es/Actor.js +5 -5
  7. package/es/Machine.d.ts +4 -4
  8. package/es/State.d.ts +9 -6
  9. package/es/State.js +10 -3
  10. package/es/StateNode.d.ts +12 -11
  11. package/es/StateNode.js +61 -71
  12. package/es/_virtual/_tslib.js +8 -4
  13. package/es/actions.d.ts +18 -2
  14. package/es/actions.js +22 -8
  15. package/es/devTools.d.ts +4 -6
  16. package/es/devTools.js +4 -0
  17. package/es/index.d.ts +9 -3
  18. package/es/index.js +3 -2
  19. package/es/interpreter.d.ts +6 -10
  20. package/es/interpreter.js +40 -27
  21. package/es/model.types.d.ts +3 -3
  22. package/es/registry.js +1 -1
  23. package/es/schema.d.ts +1 -0
  24. package/es/schema.js +2 -1
  25. package/es/scxml.d.ts +1 -1
  26. package/es/stateUtils.d.ts +4 -4
  27. package/es/stateUtils.js +1 -1
  28. package/es/typegenTypes.d.ts +66 -37
  29. package/es/types.d.ts +42 -41
  30. package/es/utils.d.ts +6 -1
  31. package/es/utils.js +17 -10
  32. package/lib/Actor.js +4 -4
  33. package/lib/Machine.d.ts +4 -4
  34. package/lib/SimulatedClock.js +9 -5
  35. package/lib/State.d.ts +9 -6
  36. package/lib/State.js +10 -3
  37. package/lib/StateNode.d.ts +12 -11
  38. package/lib/StateNode.js +59 -69
  39. package/lib/_virtual/_tslib.js +8 -4
  40. package/lib/actions.d.ts +18 -2
  41. package/lib/actions.js +22 -7
  42. package/lib/devTools.d.ts +4 -6
  43. package/lib/devTools.js +4 -0
  44. package/lib/index.d.ts +9 -3
  45. package/lib/index.js +2 -0
  46. package/lib/interpreter.d.ts +6 -10
  47. package/lib/interpreter.js +38 -25
  48. package/lib/json.js +7 -7
  49. package/lib/model.js +14 -10
  50. package/lib/model.types.d.ts +3 -3
  51. package/lib/patterns.js +2 -2
  52. package/lib/registry.js +1 -1
  53. package/lib/schema.d.ts +1 -0
  54. package/lib/schema.js +2 -0
  55. package/lib/scxml.d.ts +1 -1
  56. package/lib/scxml.js +29 -25
  57. package/lib/stateUtils.d.ts +4 -4
  58. package/lib/stateUtils.js +1 -1
  59. package/lib/typegenTypes.d.ts +66 -37
  60. package/lib/types.d.ts +42 -41
  61. package/lib/utils.d.ts +6 -1
  62. package/lib/utils.js +18 -9
  63. package/package.json +7 -11
  64. package/dist/xstate.cjs.d.ts +0 -11
  65. package/dist/xstate.cjs.js +0 -16
package/es/StateNode.js CHANGED
@@ -1,7 +1,7 @@
1
- import { __assign, __values, __spreadArray, __read, __rest } from './_virtual/_tslib.js';
1
+ import { __assign, __spreadArray, __read, __values, __rest } from './_virtual/_tslib.js';
2
2
  import { STATE_DELIMITER } from './constants.js';
3
3
  import { IS_PRODUCTION } from './environment.js';
4
- import { isFunction, mapValues, isArray, flatten, keys, toArray, toStateValue, isString, getEventType, matchesState, path, evaluateGuard, mapContext, toSCXMLEvent, pathToStateValue, isBuiltInEvent, partition, updateHistoryValue, toStatePath, mapFilterValues, warn, toStatePaths, nestedPath, normalizeTarget, toGuard, toTransitionConfigArray, isMachine } from './utils.js';
4
+ import { isFunction, mapValues, isArray, flatten, keys, toArray, toStateValue, isString, getEventType, toSCXMLEvent, matchesState, path, evaluateGuard, mapContext, pathToStateValue, isBuiltInEvent, partition, updateHistoryValue, toStatePath, mapFilterValues, warn, toStatePaths, nestedPath, normalizeTarget, toGuard, toTransitionConfigArray, isMachine, createInvokeId } from './utils.js';
5
5
  import { SpecialTargets } from './types.js';
6
6
  import { getAllStateNodes, getConfiguration, isInFinalState, getTagsFromConfiguration, has, getChildren, getValue, isLeafNode } from './stateUtils.js';
7
7
  import { start as start$1, stop as stop$1, invoke, update, nullEvent, raise as raise$1, send as send$1 } from './actionTypes.js';
@@ -33,8 +33,8 @@ var validateArrayifiedTransitions = function (stateNode, event, transitions) {
33
33
  var hasNonLastUnguardedTarget = transitions.slice(0, -1).some(function (transition) {
34
34
  return !('cond' in transition) && !('in' in transition) && (isString(transition.target) || isMachine(transition.target));
35
35
  });
36
- var eventText = event === NULL_EVENT ? 'the transient event' : "event '" + event + "'";
37
- warn(!hasNonLastUnguardedTarget, "One or more transitions for " + eventText + " on state '" + stateNode.id + "' are unreachable. " + "Make sure that the default transition is the last one defined.");
36
+ var eventText = event === NULL_EVENT ? 'the transient event' : "event '".concat(event, "'");
37
+ warn(!hasNonLastUnguardedTarget, "One or more transitions for ".concat(eventText, " on state '").concat(stateNode.id, "' are unreachable. ") + "Make sure that the default transition is the last one defined.");
38
38
  };
39
39
 
40
40
  var StateNode =
@@ -86,14 +86,14 @@ function () {
86
86
  this.machine = this.parent ? this.parent.machine : this;
87
87
  this.path = this.parent ? this.parent.path.concat(this.key) : [];
88
88
  this.delimiter = this.config.delimiter || (this.parent ? this.parent.delimiter : STATE_DELIMITER);
89
- this.id = this.config.id || __spreadArray([this.machine.key], __read(this.path)).join(this.delimiter);
89
+ this.id = this.config.id || __spreadArray([this.machine.key], __read(this.path), false).join(this.delimiter);
90
90
  this.version = this.parent ? this.parent.version : this.config.version;
91
91
  this.type = this.config.type || (this.config.parallel ? 'parallel' : this.config.states && keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
92
92
  this.schema = this.parent ? this.machine.schema : (_a = this.config.schema) !== null && _a !== void 0 ? _a : {};
93
93
  this.description = this.config.description;
94
94
 
95
95
  if (!IS_PRODUCTION) {
96
- warn(!('parallel' in this.config), "The \"parallel\" property is deprecated and will be removed in version 4.1. " + (this.config.parallel ? "Replace with `type: 'parallel'`" : "Use `type: '" + this.type + "'`") + " in the config for state node '" + this.id + "' instead.");
96
+ warn(!('parallel' in this.config), "The \"parallel\" property is deprecated and will be removed in version 4.1. ".concat(this.config.parallel ? "Replace with `type: 'parallel'`" : "Use `type: '".concat(this.type, "'`"), " in the config for state node '").concat(this.id, "' instead."));
97
97
  }
98
98
 
99
99
  this.initial = this.config.initial;
@@ -155,29 +155,30 @@ function () {
155
155
  var _a, _b;
156
156
 
157
157
  if (isMachine(invokeConfig)) {
158
- _this.machine.options.services = __assign((_a = {}, _a[invokeConfig.id] = invokeConfig, _a), _this.machine.options.services);
158
+ var invokeId = createInvokeId(_this.id, i);
159
+ _this.machine.options.services = __assign((_a = {}, _a[invokeId] = invokeConfig, _a), _this.machine.options.services);
159
160
  return toInvokeDefinition({
160
- src: invokeConfig.id,
161
- id: invokeConfig.id
161
+ src: invokeId,
162
+ id: invokeId
162
163
  });
163
164
  } else if (isString(invokeConfig.src)) {
165
+ var invokeId = invokeConfig.id || createInvokeId(_this.id, i);
164
166
  return toInvokeDefinition(__assign(__assign({}, invokeConfig), {
165
- id: invokeConfig.id || invokeConfig.src,
167
+ id: invokeId,
166
168
  src: invokeConfig.src
167
169
  }));
168
170
  } else if (isMachine(invokeConfig.src) || isFunction(invokeConfig.src)) {
169
- var invokeSrc = _this.id + ":invocation[" + i + "]"; // TODO: util function
170
-
171
- _this.machine.options.services = __assign((_b = {}, _b[invokeSrc] = invokeConfig.src, _b), _this.machine.options.services);
171
+ var invokeId = invokeConfig.id || createInvokeId(_this.id, i);
172
+ _this.machine.options.services = __assign((_b = {}, _b[invokeId] = invokeConfig.src, _b), _this.machine.options.services);
172
173
  return toInvokeDefinition(__assign(__assign({
173
- id: invokeSrc
174
+ id: invokeId
174
175
  }, invokeConfig), {
175
- src: invokeSrc
176
+ src: invokeId
176
177
  }));
177
178
  } else {
178
179
  var invokeSource = invokeConfig.src;
179
180
  return toInvokeDefinition(__assign(__assign({
180
- id: invokeSource.type
181
+ id: createInvokeId(_this.id, i)
181
182
  }, invokeConfig), {
182
183
  src: invokeSource
183
184
  }));
@@ -347,7 +348,7 @@ function () {
347
348
  }
348
349
 
349
350
  var mutateEntryExit = function (delay, i) {
350
- var delayRef = isFunction(delay) ? _this.id + ":delay[" + i + "]" : delay;
351
+ var delayRef = isFunction(delay) ? "".concat(_this.id, ":delay[").concat(i, "]") : delay;
351
352
  var eventType = after(delayRef, _this.id);
352
353
 
353
354
  _this.onEntry.push(send(eventType, {
@@ -409,15 +410,11 @@ function () {
409
410
  }
410
411
 
411
412
  var subStateKeys = keys(stateValue);
412
- var subStateNodes = subStateKeys.map(function (subStateKey) {
413
- return _this.getStateNode(subStateKey);
414
- });
415
- subStateNodes.push(this);
416
- return subStateNodes.concat(subStateKeys.reduce(function (allSubStateNodes, subStateKey) {
417
- var subStateNode = _this.getStateNode(subStateKey).getStateNodes(stateValue[subStateKey]);
418
-
419
- return allSubStateNodes.concat(subStateNode);
420
- }, []));
413
+ var subStateNodes = [this];
414
+ subStateNodes.push.apply(subStateNodes, __spreadArray([], __read(flatten(subStateKeys.map(function (subStateKey) {
415
+ return _this.getStateNode(subStateKey).getStateNodes(stateValue[subStateKey]);
416
+ }))), false));
417
+ return subStateNodes;
421
418
  };
422
419
  /**
423
420
  * Returns `true` if this state node explicitly handles the given event.
@@ -556,6 +553,10 @@ function () {
556
553
  return this.transitionParallelNode(stateValue, state, _event);
557
554
  };
558
555
 
556
+ StateNode.prototype.getTransitionData = function (state, event) {
557
+ return this._transition(state.value, state, toSCXMLEvent(event));
558
+ };
559
+
559
560
  StateNode.prototype.next = function (state, _event) {
560
561
  var e_3, _a;
561
562
 
@@ -580,7 +581,7 @@ function () {
580
581
  try {
581
582
  guardPassed = !cond || evaluateGuard(this.machine, cond, resolvedContext, _event, state);
582
583
  } catch (err) {
583
- throw new Error("Unable to evaluate guard '" + (cond.name || cond.type) + "' in transition for event '" + eventName + "' in state node '" + this.id + "':\n" + err.message);
584
+ throw new Error("Unable to evaluate guard '".concat(cond.name || cond.type, "' in transition for event '").concat(eventName, "' in state node '").concat(this.id, "':\n").concat(err.message));
584
585
  }
585
586
 
586
587
  if (guardPassed && isInState) {
@@ -588,7 +589,7 @@ function () {
588
589
  nextStateNodes = candidate.target;
589
590
  }
590
591
 
591
- actions.push.apply(actions, __spreadArray([], __read(candidate.actions)));
592
+ actions.push.apply(actions, __spreadArray([], __read(candidate.actions), false));
592
593
  selectedTransition = candidate;
593
594
  break;
594
595
  }
@@ -724,12 +725,6 @@ function () {
724
725
  }
725
726
  }
726
727
 
727
- if (!transition.source) {
728
- transition.exitSet = []; // Ensure that root StateNode (machine) is entered
729
-
730
- transition.entrySet.push(this);
731
- }
732
-
733
728
  var doneEvents = flatten(transition.entrySet.map(function (sn) {
734
729
  var events = [];
735
730
 
@@ -769,11 +764,11 @@ function () {
769
764
  var _c = __read([flatten(Array.from(entryStates).map(function (stateNode) {
770
765
  return __spreadArray(__spreadArray([], __read(stateNode.activities.map(function (activity) {
771
766
  return start(activity);
772
- }))), __read(stateNode.onEntry));
767
+ })), false), __read(stateNode.onEntry), false);
773
768
  })).concat(doneEvents.map(raise)), flatten(Array.from(exitStates).map(function (stateNode) {
774
- return __spreadArray(__spreadArray([], __read(stateNode.onExit)), __read(stateNode.activities.map(function (activity) {
769
+ return __spreadArray(__spreadArray([], __read(stateNode.onExit), false), __read(stateNode.activities.map(function (activity) {
775
770
  return stop(activity);
776
- })));
771
+ })), false);
777
772
  }))], 2),
778
773
  entryActions = _c[0],
779
774
  exitActions = _c[1];
@@ -808,12 +803,12 @@ function () {
808
803
  }
809
804
 
810
805
  if (!IS_PRODUCTION && _event.name === WILDCARD) {
811
- throw new Error("An event cannot have the wildcard type ('" + WILDCARD + "')");
806
+ throw new Error("An event cannot have the wildcard type ('".concat(WILDCARD, "')"));
812
807
  }
813
808
 
814
809
  if (this.strict) {
815
810
  if (!this.events.includes(_event.name) && !isBuiltInEvent(_event.name)) {
816
- throw new Error("Machine '" + this.id + "' does not accept event '" + _event.name + "'");
811
+ throw new Error("Machine '".concat(this.id, "' does not accept event '").concat(_event.name, "'"));
817
812
  }
818
813
  }
819
814
 
@@ -827,8 +822,8 @@ function () {
827
822
  };
828
823
  var prevConfig = getConfiguration([], this.getStateNodes(currentState.value));
829
824
  var resolvedConfig = stateTransition.configuration.length ? getConfiguration(prevConfig, stateTransition.configuration) : prevConfig;
830
- stateTransition.configuration = __spreadArray([], __read(resolvedConfig));
831
- return this.resolveTransition(stateTransition, currentState, _event);
825
+ stateTransition.configuration = __spreadArray([], __read(resolvedConfig), false);
826
+ return this.resolveTransition(stateTransition, currentState, currentState.context, _event);
832
827
  };
833
828
 
834
829
  StateNode.prototype.resolveRaisedTransition = function (state, _event, originalEvent) {
@@ -841,12 +836,12 @@ function () {
841
836
  state._event = originalEvent;
842
837
  state.event = originalEvent.data;
843
838
 
844
- (_a = state.actions).unshift.apply(_a, __spreadArray([], __read(currentActions)));
839
+ (_a = state.actions).unshift.apply(_a, __spreadArray([], __read(currentActions), false));
845
840
 
846
841
  return state;
847
842
  };
848
843
 
849
- StateNode.prototype.resolveTransition = function (stateTransition, currentState, _event, context) {
844
+ StateNode.prototype.resolveTransition = function (stateTransition, currentState, context, _event) {
850
845
  var e_6, _a;
851
846
 
852
847
  var _this = this;
@@ -855,10 +850,6 @@ function () {
855
850
  _event = initEvent;
856
851
  }
857
852
 
858
- if (context === void 0) {
859
- context = this.machine.context;
860
- }
861
-
862
853
  var configuration = stateTransition.configuration; // Transition will "apply" if:
863
854
  // - this is the initial state (there is no current state)
864
855
  // - OR there are transitions
@@ -866,8 +857,7 @@ function () {
866
857
  var willTransition = !currentState || stateTransition.transitions.length > 0;
867
858
  var resolvedStateValue = willTransition ? getValue(this.machine, configuration) : undefined;
868
859
  var historyValue = currentState ? currentState.historyValue ? currentState.historyValue : stateTransition.source ? this.machine.historyValue(currentState.value) : undefined : undefined;
869
- var currentContext = currentState ? currentState.context : context;
870
- var actions = this.getActions(stateTransition, currentContext, _event, currentState);
860
+ var actions = this.getActions(stateTransition, context, _event, currentState);
871
861
  var activities = currentState ? __assign({}, currentState.activities) : {};
872
862
 
873
863
  try {
@@ -892,7 +882,7 @@ function () {
892
882
  }
893
883
  }
894
884
 
895
- var _b = __read(resolveActions(this, currentState, currentContext, _event, actions, this.machine.config.preserveActionOrder), 2),
885
+ var _b = __read(resolveActions(this, currentState, context, _event, actions, this.machine.config.preserveActionOrder), 2),
896
886
  resolvedActions = _b[0],
897
887
  updatedContext = _b[1];
898
888
 
@@ -911,7 +901,7 @@ function () {
911
901
  acc[action.activity.id] = createInvocableActor(action.activity, _this.machine, updatedContext, _event);
912
902
  return acc;
913
903
  }, currentState ? __assign({}, currentState.children) : {});
914
- var resolvedConfiguration = resolvedStateValue ? stateTransition.configuration : currentState ? currentState.configuration : [];
904
+ var resolvedConfiguration = willTransition ? stateTransition.configuration : currentState ? currentState.configuration : [];
915
905
  var isDone = isInFinalState(resolvedConfiguration, this);
916
906
  var nextState = new State({
917
907
  value: resolvedStateValue || currentState.value,
@@ -931,7 +921,7 @@ function () {
931
921
  tags: currentState === null || currentState === void 0 ? void 0 : currentState.tags,
932
922
  machine: this
933
923
  });
934
- var didUpdateContext = currentContext !== updatedContext;
924
+ var didUpdateContext = context !== updatedContext;
935
925
  nextState.changed = _event.name === update || didUpdateContext; // Dispose of penultimate histories to prevent memory leaks
936
926
 
937
927
  var history = nextState.history;
@@ -992,13 +982,13 @@ function () {
992
982
  }
993
983
 
994
984
  if (!this.states) {
995
- throw new Error("Unable to retrieve child state '" + stateKey + "' from '" + this.id + "'; no child states exist.");
985
+ throw new Error("Unable to retrieve child state '".concat(stateKey, "' from '").concat(this.id, "'; no child states exist."));
996
986
  }
997
987
 
998
988
  var result = this.states[stateKey];
999
989
 
1000
990
  if (!result) {
1001
- throw new Error("Child state '" + stateKey + "' does not exist on '" + this.id + "'");
991
+ throw new Error("Child state '".concat(stateKey, "' does not exist on '").concat(this.id, "'"));
1002
992
  }
1003
993
 
1004
994
  return result;
@@ -1020,7 +1010,7 @@ function () {
1020
1010
  var stateNode = this.machine.idMap[resolvedStateId];
1021
1011
 
1022
1012
  if (!stateNode) {
1023
- throw new Error("Child state node '#" + resolvedStateId + "' does not exist on machine '" + this.id + "'");
1013
+ throw new Error("Child state node '#".concat(resolvedStateId, "' does not exist on machine '").concat(this.id, "'"));
1024
1014
  }
1025
1015
 
1026
1016
  return stateNode;
@@ -1107,7 +1097,7 @@ function () {
1107
1097
  var stateNode = this.machine.idMap[stateIdentifier.slice(STATE_IDENTIFIER.length)];
1108
1098
 
1109
1099
  if (!stateNode) {
1110
- throw new Error("Unable to find state node '" + stateIdentifier + "'");
1100
+ throw new Error("Unable to find state node '".concat(stateIdentifier, "'"));
1111
1101
  }
1112
1102
 
1113
1103
  return stateNode.path;
@@ -1134,7 +1124,7 @@ function () {
1134
1124
  });
1135
1125
  } else if (this.initial !== undefined) {
1136
1126
  if (!this.states[this.initial]) {
1137
- throw new Error("Initial state '" + this.initial + "' not found on '" + this.key + "'");
1127
+ throw new Error("Initial state '".concat(this.initial, "' not found on '").concat(this.key, "'"));
1138
1128
  }
1139
1129
 
1140
1130
  initialStateValue = isLeafNode(this.states[this.initial]) ? this.initial : (_a = {}, _a[this.initial] = this.states[this.initial].initialStateValue, _a);
@@ -1159,7 +1149,7 @@ function () {
1159
1149
  transitions: [],
1160
1150
  source: undefined,
1161
1151
  actions: []
1162
- }, undefined, undefined, context);
1152
+ }, undefined, context !== null && context !== void 0 ? context : this.machine.context, undefined);
1163
1153
  };
1164
1154
 
1165
1155
  Object.defineProperty(StateNode.prototype, "initialState", {
@@ -1174,7 +1164,7 @@ function () {
1174
1164
  var initialStateValue = this.initialStateValue;
1175
1165
 
1176
1166
  if (!initialStateValue) {
1177
- throw new Error("Cannot retrieve initial state from simple state '" + this.id + "'.");
1167
+ throw new Error("Cannot retrieve initial state from simple state '".concat(this.id, "'."));
1178
1168
  }
1179
1169
 
1180
1170
  return this.getInitialState(initialStateValue);
@@ -1232,7 +1222,7 @@ function () {
1232
1222
 
1233
1223
  if (this.type === 'compound' && !this.initial) {
1234
1224
  if (!IS_PRODUCTION) {
1235
- warn(false, "Compound state node '" + this.id + "' has no initial state.");
1225
+ warn(false, "Compound state node '".concat(this.id, "' has no initial state."));
1236
1226
  }
1237
1227
 
1238
1228
  return [this];
@@ -1263,7 +1253,7 @@ function () {
1263
1253
  childStatePath = _a.slice(1);
1264
1254
 
1265
1255
  if (!this.states) {
1266
- throw new Error("Cannot retrieve subPath '" + stateKey + "' from node with no states");
1256
+ throw new Error("Cannot retrieve subPath '".concat(stateKey, "' from node with no states"));
1267
1257
  }
1268
1258
 
1269
1259
  var childStateNode = this.getStateNode(stateKey);
@@ -1273,7 +1263,7 @@ function () {
1273
1263
  }
1274
1264
 
1275
1265
  if (!this.states[stateKey]) {
1276
- throw new Error("Child state '" + stateKey + "' does not exist on '" + this.id + "'");
1266
+ throw new Error("Child state '".concat(stateKey, "' does not exist on '").concat(this.id, "'"));
1277
1267
  }
1278
1268
 
1279
1269
  return this.states[stateKey].getFromRelativePath(childStatePath);
@@ -1372,7 +1362,7 @@ function () {
1372
1362
  try {
1373
1363
  for (var _e = (e_8 = void 0, __values(state.events)), _f = _e.next(); !_f.done; _f = _e.next()) {
1374
1364
  var event_1 = _f.value;
1375
- events.add("" + event_1);
1365
+ events.add("".concat(event_1));
1376
1366
  }
1377
1367
  } catch (e_8_1) {
1378
1368
  e_8 = {
@@ -1451,7 +1441,7 @@ function () {
1451
1441
 
1452
1442
  return targetStateNode;
1453
1443
  } catch (err) {
1454
- throw new Error("Invalid transition definition for state node '" + _this.id + "':\n" + err.message);
1444
+ throw new Error("Invalid transition definition for state node '".concat(_this.id, "':\n").concat(err.message));
1455
1445
  }
1456
1446
  } else {
1457
1447
  return _this.getStateNodeByPath(resolvedTarget);
@@ -1479,9 +1469,9 @@ function () {
1479
1469
  toJSON: function () {
1480
1470
  return __assign(__assign({}, transition), {
1481
1471
  target: transition.target ? transition.target.map(function (t) {
1482
- return "#" + t.id;
1472
+ return "#".concat(t.id);
1483
1473
  }) : undefined,
1484
- source: "#" + _this.id
1474
+ source: "#".concat(_this.id)
1485
1475
  });
1486
1476
  }
1487
1477
  });
@@ -1509,7 +1499,7 @@ function () {
1509
1499
 
1510
1500
  onConfig = flatten(keys(strictTransitionConfigs_1).map(function (key) {
1511
1501
  if (!IS_PRODUCTION && key === NULL_EVENT) {
1512
- warn(false, "Empty string transition configs (e.g., `{ on: { '': ... }}`) for transient transitions are deprecated. Specify the transition in the `{ always: ... }` property instead. " + ("Please check the `on` configuration for \"#" + _this.id + "\"."));
1502
+ warn(false, "Empty string transition configs (e.g., `{ on: { '': ... }}`) for transient transitions are deprecated. Specify the transition in the `{ always: ... }` property instead. " + "Please check the `on` configuration for \"#".concat(_this.id, "\"."));
1513
1503
  }
1514
1504
 
1515
1505
  var transitionConfigArray = toTransitionConfigArray(key, strictTransitionConfigs_1[key]);
@@ -1526,24 +1516,24 @@ function () {
1526
1516
  var doneConfig = this.config.onDone ? toTransitionConfigArray(String(done(this.id)), this.config.onDone) : [];
1527
1517
 
1528
1518
  if (!IS_PRODUCTION) {
1529
- warn(!(this.config.onDone && !this.parent), "Root nodes cannot have an \".onDone\" transition. Please check the config of \"" + this.id + "\".");
1519
+ warn(!(this.config.onDone && !this.parent), "Root nodes cannot have an \".onDone\" transition. Please check the config of \"".concat(this.id, "\"."));
1530
1520
  }
1531
1521
 
1532
1522
  var invokeConfig = flatten(this.invoke.map(function (invokeDef) {
1533
1523
  var settleTransitions = [];
1534
1524
 
1535
1525
  if (invokeDef.onDone) {
1536
- settleTransitions.push.apply(settleTransitions, __spreadArray([], __read(toTransitionConfigArray(String(doneInvoke(invokeDef.id)), invokeDef.onDone))));
1526
+ settleTransitions.push.apply(settleTransitions, __spreadArray([], __read(toTransitionConfigArray(String(doneInvoke(invokeDef.id)), invokeDef.onDone)), false));
1537
1527
  }
1538
1528
 
1539
1529
  if (invokeDef.onError) {
1540
- settleTransitions.push.apply(settleTransitions, __spreadArray([], __read(toTransitionConfigArray(String(error(invokeDef.id)), invokeDef.onError))));
1530
+ settleTransitions.push.apply(settleTransitions, __spreadArray([], __read(toTransitionConfigArray(String(error(invokeDef.id)), invokeDef.onError)), false));
1541
1531
  }
1542
1532
 
1543
1533
  return settleTransitions;
1544
1534
  }));
1545
1535
  var delayedTransitions = this.after;
1546
- var formattedTransitions = flatten(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(doneConfig)), __read(invokeConfig)), __read(onConfig)), __read(eventlessConfig)).map(function (transitionConfig) {
1536
+ var formattedTransitions = flatten(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(doneConfig), false), __read(invokeConfig), false), __read(onConfig), false), __read(eventlessConfig), false).map(function (transitionConfig) {
1547
1537
  return toArray(transitionConfig).map(function (transition) {
1548
1538
  return _this.formatTransition(transition);
1549
1539
  });
@@ -65,10 +65,14 @@ function __read(o, n) {
65
65
  return ar;
66
66
  }
67
67
 
68
- function __spreadArray(to, from) {
69
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
70
- to[j] = from[i];
71
- return to;
68
+ function __spreadArray(to, from, pack) {
69
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
70
+ if (ar || !(i in from)) {
71
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
72
+ ar[i] = from[i];
73
+ }
74
+ }
75
+ return to.concat(ar || Array.prototype.slice.call(from));
72
76
  }
73
77
 
74
78
  export { __assign, __read, __rest, __spreadArray, __values };
package/es/actions.d.ts CHANGED
@@ -1,4 +1,4 @@
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, StopAction, StopActionObject } 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, StopAction, StopActionObject, Cast, ActorRef, EventFrom } from './types';
2
2
  import * as actionTypes from './actionTypes';
3
3
  import { State } from './State';
4
4
  import { StateNode } from './StateNode';
@@ -37,6 +37,22 @@ export declare function resolveSend<TContext, TEvent extends EventObject, TSentE
37
37
  * @param options Options to pass into the send event.
38
38
  */
39
39
  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>;
40
+ declare type InferEvent<E extends EventObject> = {
41
+ [T in E['type']]: {
42
+ type: T;
43
+ } & Extract<E, {
44
+ type: T;
45
+ }>;
46
+ }[E['type']];
47
+ /**
48
+ * Sends an event to an actor.
49
+ *
50
+ * @param actor The `ActorRef` to send the event to.
51
+ * @param event The event to send, or an expression that evaluates to the event to send
52
+ * @param options Send action options
53
+ * @returns An XState send action object
54
+ */
55
+ 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>;
40
56
  /**
41
57
  * Sends an update event to this machine's parent.
42
58
  */
@@ -134,5 +150,5 @@ export declare function forwardTo<TContext, TEvent extends EventObject>(target:
134
150
  */
135
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>;
136
152
  export declare function choose<TContext, TEvent extends EventObject>(conds: Array<ChooseCondition<TContext, TEvent>>): ChooseAction<TContext, TEvent>;
137
- export declare function resolveActions<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent, any, any>, currentState: State<TContext, TEvent> | undefined, currentContext: TContext, _event: SCXML.Event<TEvent>, actions: Array<ActionObject<TContext, TEvent>>, 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> | undefined, currentContext: TContext, _event: SCXML.Event<TEvent>, actions: Array<ActionObject<TContext, TEvent>>, preserveActionOrder?: boolean): [Array<ActionObject<TContext, TEvent>>, TContext];
138
154
  //# sourceMappingURL=actions.d.ts.map
package/es/actions.js CHANGED
@@ -152,6 +152,20 @@ function sendParent(event, options) {
152
152
  to: SpecialTargets.Parent
153
153
  }));
154
154
  }
155
+ /**
156
+ * Sends an event to an actor.
157
+ *
158
+ * @param actor The `ActorRef` to send the event to.
159
+ * @param event The event to send, or an expression that evaluates to the event to send
160
+ * @param options Send action options
161
+ * @returns An XState send action object
162
+ */
163
+
164
+ function sendTo(actor, event, options) {
165
+ return send(event, __assign(__assign({}, options), {
166
+ to: actor
167
+ }));
168
+ }
155
169
  /**
156
170
  * Sends an update event to this machine's parent.
157
171
  */
@@ -283,8 +297,8 @@ var assign = function (assignment) {
283
297
  */
284
298
 
285
299
  function after(delayRef, id) {
286
- var idSuffix = id ? "#" + id : '';
287
- return ActionTypes.After + "(" + delayRef + ")" + idSuffix;
300
+ var idSuffix = id ? "#".concat(id) : '';
301
+ return "".concat(ActionTypes.After, "(").concat(delayRef, ")").concat(idSuffix);
288
302
  }
289
303
  /**
290
304
  * Returns an event that represents that a final state node
@@ -295,7 +309,7 @@ function after(delayRef, id) {
295
309
  */
296
310
 
297
311
  function done(id, data) {
298
- var type = ActionTypes.DoneState + "." + id;
312
+ var type = "".concat(ActionTypes.DoneState, ".").concat(id);
299
313
  var eventObject = {
300
314
  type: type,
301
315
  data: data
@@ -318,7 +332,7 @@ function done(id, data) {
318
332
  */
319
333
 
320
334
  function doneInvoke(id, data) {
321
- var type = ActionTypes.DoneInvoke + "." + id;
335
+ var type = "".concat(ActionTypes.DoneInvoke, ".").concat(id);
322
336
  var eventObject = {
323
337
  type: type,
324
338
  data: data
@@ -331,7 +345,7 @@ function doneInvoke(id, data) {
331
345
  return eventObject;
332
346
  }
333
347
  function error(id, data) {
334
- var type = ActionTypes.ErrorPlatform + "." + id;
348
+ var type = "".concat(ActionTypes.ErrorPlatform, ".").concat(id);
335
349
  var eventObject = {
336
350
  type: type,
337
351
  data: data
@@ -413,7 +427,7 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
413
427
  if (!IS_PRODUCTION) {
414
428
  // warn after resolving as we can create better contextual message here
415
429
  warn(!isString(actionObject.delay) || typeof sendAction.delay === 'number', // tslint:disable-next-line:max-line-length
416
- "No delay reference for delay expression '" + actionObject.delay + "' was found on machine '" + machine.id + "'");
430
+ "No delay reference for delay expression '".concat(actionObject.delay, "' was found on machine '").concat(machine.id, "'"));
417
431
  }
418
432
 
419
433
  return sendAction;
@@ -485,7 +499,7 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
485
499
  args[_i - 1] = arguments[_i];
486
500
  }
487
501
 
488
- exec_1.apply(void 0, __spreadArray([preservedContexts[contextIndex_1]], __read(args)));
502
+ exec_1.apply(void 0, __spreadArray([preservedContexts[contextIndex_1]], __read(args), false));
489
503
  }
490
504
  });
491
505
  }
@@ -498,4 +512,4 @@ function resolveActions(machine, currentState, currentContext, _event, actions,
498
512
  return [resolvedActions, updatedContext];
499
513
  }
500
514
 
501
- export { after, assign, cancel, choose, done, doneInvoke, error, escalate, forwardTo, getActionFunction, initEvent, log, pure, raise, resolveActions, resolveLog, resolveRaise, resolveSend, resolveStop, respond, send, sendParent, sendUpdate, start, stop, toActionObject, toActionObjects, toActivityDefinition };
515
+ export { after, assign, cancel, choose, done, doneInvoke, error, escalate, forwardTo, getActionFunction, initEvent, log, pure, raise, resolveActions, resolveLog, resolveRaise, resolveSend, resolveStop, respond, send, sendParent, sendTo, sendUpdate, start, stop, toActionObject, toActionObjects, toActivityDefinition };
package/es/devTools.d.ts CHANGED
@@ -1,16 +1,14 @@
1
- /// <reference types="node" />
2
1
  import { AnyInterpreter } from './types';
3
- import { Interpreter } from './interpreter';
4
2
  declare type ServiceListener = (service: AnyInterpreter) => void;
5
3
  export interface XStateDevInterface {
6
- register: (service: Interpreter<any>) => void;
7
- unregister: (service: Interpreter<any>) => void;
4
+ register: (service: AnyInterpreter) => void;
5
+ unregister: (service: AnyInterpreter) => void;
8
6
  onRegister: (listener: ServiceListener) => {
9
7
  unsubscribe: () => void;
10
8
  };
11
- services: Set<Interpreter<any>>;
9
+ services: Set<AnyInterpreter>;
12
10
  }
13
- export declare function getGlobal(): (Window & typeof globalThis) | (NodeJS.Global & typeof globalThis) | undefined;
11
+ export declare function getGlobal(): typeof globalThis | undefined;
14
12
  export declare function registerService(service: AnyInterpreter): void;
15
13
  export {};
16
14
  //# sourceMappingURL=devTools.d.ts.map
package/es/devTools.js CHANGED
@@ -1,5 +1,9 @@
1
1
  // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
2
2
  function getGlobal() {
3
+ if (typeof globalThis !== 'undefined') {
4
+ return globalThis;
5
+ }
6
+
3
7
  if (typeof self !== 'undefined') {
4
8
  return self;
5
9
  }
package/es/index.d.ts CHANGED
@@ -4,14 +4,15 @@ 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
- import { createSchema } from './schema';
10
+ import { createSchema, t } from './schema';
11
11
  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;
@@ -26,7 +27,12 @@ declare const actions: {
26
27
  choose: typeof choose;
27
28
  pure: typeof pure;
28
29
  };
29
- export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, InterpreterStatus, matchState, spawn, doneInvoke, createMachine, createSchema };
30
+ export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, InterpreterStatus, matchState, spawn, doneInvoke, createMachine, createSchema, t };
30
31
  export * from './types';
31
32
  export * from './typegenTypes';
33
+ declare global {
34
+ interface SymbolConstructor {
35
+ readonly observable: symbol;
36
+ }
37
+ }
32
38
  //# sourceMappingURL=index.d.ts.map
package/es/index.js CHANGED
@@ -1,19 +1,20 @@
1
1
  export { matchesState } from './utils.js';
2
2
  export { mapState } from './mapState.js';
3
3
  export { ActionTypes, SpecialTargets } from './types.js';
4
- import { raise, send, sendParent, sendUpdate, log, cancel, start, stop, assign, after, done, respond, forwardTo, escalate, choose, pure } from './actions.js';
4
+ import { raise, send, sendParent, sendTo, sendUpdate, log, cancel, start, stop, assign, after, done, respond, forwardTo, escalate, choose, pure } from './actions.js';
5
5
  export { assign, doneInvoke, forwardTo, send, sendParent, sendUpdate } from './actions.js';
6
6
  export { State } from './State.js';
7
7
  export { StateNode } from './StateNode.js';
8
8
  export { Machine, createMachine } from './Machine.js';
9
9
  export { Interpreter, InterpreterStatus, interpret, spawn } from './interpreter.js';
10
10
  export { matchState } from './match.js';
11
- export { createSchema } from './schema.js';
11
+ export { createSchema, t } from './schema.js';
12
12
 
13
13
  var actions = {
14
14
  raise: raise,
15
15
  send: send,
16
16
  sendParent: sendParent,
17
+ sendTo: sendTo,
17
18
  sendUpdate: sendUpdate,
18
19
  log: log,
19
20
  cancel: cancel,