xstate 4.22.0 → 4.23.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 (88) hide show
  1. package/CHANGELOG.md +58 -4
  2. package/dist/xstate.interpreter.js +1 -1
  3. package/dist/xstate.js +1 -1
  4. package/dist/xstate.web.js +2 -2
  5. package/es/Actor.js +1 -6
  6. package/es/Machine.d.ts +3 -3
  7. package/es/Machine.js +1 -2
  8. package/es/State.js +1 -3
  9. package/es/StateNode.js +3 -2
  10. package/es/_virtual/_tslib.js +59 -73
  11. package/es/actionTypes.js +3 -2
  12. package/es/actions.d.ts +1 -1
  13. package/es/actions.js +51 -37
  14. package/es/behaviors.js +4 -2
  15. package/es/constants.js +2 -1
  16. package/es/devTools.js +1 -1
  17. package/es/environment.js +2 -1
  18. package/es/index.js +3 -1
  19. package/es/interpreter.d.ts +8 -2
  20. package/es/interpreter.js +8 -6
  21. package/es/invokeUtils.js +4 -3
  22. package/es/mapState.js +1 -1
  23. package/es/match.js +1 -1
  24. package/es/model.d.ts +2 -37
  25. package/es/model.types.d.ts +37 -0
  26. package/es/registry.js +2 -1
  27. package/es/scheduler.js +2 -1
  28. package/es/schema.js +1 -1
  29. package/es/serviceScope.js +1 -3
  30. package/es/stateUtils.js +1 -9
  31. package/es/types.d.ts +12 -2
  32. package/es/types.js +1 -1
  33. package/es/utils.js +1 -41
  34. package/lib/Actor.d.ts +25 -25
  35. package/lib/Actor.js +85 -66
  36. package/lib/Machine.d.ts +17 -17
  37. package/lib/Machine.js +14 -8
  38. package/lib/SimulatedClock.d.ts +16 -16
  39. package/lib/State.d.ts +108 -108
  40. package/lib/State.js +246 -236
  41. package/lib/StateNode.d.ts +279 -279
  42. package/lib/StateNode.js +1535 -1357
  43. package/lib/_virtual/_tslib.js +81 -0
  44. package/lib/actionTypes.d.ts +19 -19
  45. package/lib/actionTypes.js +43 -23
  46. package/lib/actions.d.ts +138 -138
  47. package/lib/actions.js +465 -387
  48. package/lib/behaviors.d.ts +36 -36
  49. package/lib/behaviors.js +65 -106
  50. package/lib/constants.d.ts +5 -5
  51. package/lib/constants.js +13 -7
  52. package/lib/devTools.d.ts +15 -15
  53. package/lib/devTools.js +37 -26
  54. package/lib/each.d.ts +3 -3
  55. package/lib/environment.d.ts +1 -1
  56. package/lib/environment.js +7 -4
  57. package/lib/index.d.ts +30 -30
  58. package/lib/index.js +67 -57
  59. package/lib/interpreter.d.ts +205 -199
  60. package/lib/interpreter.js +1306 -1060
  61. package/lib/invoke.d.ts +10 -10
  62. package/lib/invokeUtils.d.ts +6 -6
  63. package/lib/invokeUtils.js +40 -37
  64. package/lib/json.d.ts +30 -30
  65. package/lib/mapState.d.ts +3 -3
  66. package/lib/mapState.js +31 -32
  67. package/lib/match.d.ts +8 -8
  68. package/lib/match.js +33 -47
  69. package/lib/model.d.ts +4 -39
  70. package/lib/model.types.d.ts +37 -0
  71. package/lib/model.types.js +2 -0
  72. package/lib/patterns.d.ts +13 -13
  73. package/lib/registry.d.ts +8 -8
  74. package/lib/registry.js +21 -18
  75. package/lib/scheduler.d.ts +16 -16
  76. package/lib/scheduler.js +79 -70
  77. package/lib/schema.d.ts +1 -1
  78. package/lib/schema.js +6 -4
  79. package/lib/scxml.d.ts +5 -5
  80. package/lib/serviceScope.d.ts +3 -3
  81. package/lib/serviceScope.js +16 -12
  82. package/lib/stateUtils.d.ts +14 -14
  83. package/lib/stateUtils.js +231 -199
  84. package/lib/types.d.ts +928 -918
  85. package/lib/types.js +29 -29
  86. package/lib/utils.d.ts +68 -68
  87. package/lib/utils.js +528 -534
  88. package/package.json +5 -5
package/lib/State.js CHANGED
@@ -1,245 +1,255 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
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
- var __read = (this && this.__read) || function (o, n) {
25
- var m = typeof Symbol === "function" && o[Symbol.iterator];
26
- if (!m) return o;
27
- var i = m.call(o), r, ar = [], e;
28
- try {
29
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
30
- }
31
- catch (error) { e = { error: error }; }
32
- finally {
33
- try {
34
- if (r && !r.done && (m = i["return"])) m.call(i);
35
- }
36
- finally { if (e) throw e.error; }
37
- }
38
- return ar;
39
- };
40
- var __spreadArray = (this && this.__spreadArray) || function (to, from) {
41
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
42
- to[j] = from[i];
43
- return to;
44
- };
45
- Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.State = exports.bindActionToState = exports.isState = exports.stateValuesEqual = void 0;
47
- var constants_1 = require("./constants");
48
- var utils_1 = require("./utils");
49
- var stateUtils_1 = require("./stateUtils");
50
- var actions_1 = require("./actions");
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('./_virtual/_tslib.js');
6
+ var constants = require('./constants.js');
7
+ var utils = require('./utils.js');
8
+ var stateUtils = require('./stateUtils.js');
9
+ var actions = require('./actions.js');
10
+
51
11
  function stateValuesEqual(a, b) {
52
- if (a === b) {
53
- return true;
54
- }
55
- if (a === undefined || b === undefined) {
56
- return false;
57
- }
58
- if (utils_1.isString(a) || utils_1.isString(b)) {
59
- return a === b;
60
- }
61
- var aKeys = utils_1.keys(a);
62
- var bKeys = utils_1.keys(b);
63
- return (aKeys.length === bKeys.length &&
64
- aKeys.every(function (key) { return stateValuesEqual(a[key], b[key]); }));
12
+ if (a === b) {
13
+ return true;
14
+ }
15
+
16
+ if (a === undefined || b === undefined) {
17
+ return false;
18
+ }
19
+
20
+ if (utils.isString(a) || utils.isString(b)) {
21
+ return a === b;
22
+ }
23
+
24
+ var aKeys = utils.keys(a);
25
+ var bKeys = utils.keys(b);
26
+ return aKeys.length === bKeys.length && aKeys.every(function (key) {
27
+ return stateValuesEqual(a[key], b[key]);
28
+ });
65
29
  }
66
- exports.stateValuesEqual = stateValuesEqual;
67
30
  function isState(state) {
68
- if (utils_1.isString(state)) {
69
- return false;
70
- }
71
- return 'value' in state && 'history' in state;
31
+ if (utils.isString(state)) {
32
+ return false;
33
+ }
34
+
35
+ return 'value' in state && 'history' in state;
72
36
  }
73
- exports.isState = isState;
74
37
  function bindActionToState(action, state) {
75
- var exec = action.exec;
76
- var boundAction = __assign(__assign({}, action), { exec: exec !== undefined
77
- ? function () {
78
- return exec(state.context, state.event, {
79
- action: action,
80
- state: state,
81
- _event: state._event
82
- });
83
- }
84
- : undefined });
85
- return boundAction;
38
+ var exec = action.exec;
39
+
40
+ var boundAction = _tslib.__assign(_tslib.__assign({}, action), {
41
+ exec: exec !== undefined ? function () {
42
+ return exec(state.context, state.event, {
43
+ action: action,
44
+ state: state,
45
+ _event: state._event
46
+ });
47
+ } : undefined
48
+ });
49
+
50
+ return boundAction;
86
51
  }
87
- exports.bindActionToState = bindActionToState;
88
- var State = /** @class */ (function () {
89
- /**
90
- * Creates a new State instance.
91
- * @param value The state value
92
- * @param context The extended state
93
- * @param historyValue The tree representing historical values of the state nodes
94
- * @param history The previous state
95
- * @param actions An array of action objects to execute as side-effects
96
- * @param activities A mapping of activities and whether they are started (`true`) or stopped (`false`).
97
- * @param meta
98
- * @param events Internal event queue. Should be empty with run-to-completion semantics.
99
- * @param configuration
100
- */
101
- function State(config) {
102
- var _this = this;
103
- var _a;
104
- this.actions = [];
105
- this.activities = constants_1.EMPTY_ACTIVITY_MAP;
106
- this.meta = {};
107
- this.events = [];
108
- this.value = config.value;
109
- this.context = config.context;
110
- this._event = config._event;
111
- this._sessionid = config._sessionid;
112
- this.event = this._event.data;
113
- this.historyValue = config.historyValue;
114
- this.history = config.history;
115
- this.actions = config.actions || [];
116
- this.activities = config.activities || constants_1.EMPTY_ACTIVITY_MAP;
117
- this.meta = stateUtils_1.getMeta(config.configuration);
118
- this.events = config.events || [];
119
- this.matches = this.matches.bind(this);
120
- this.toStrings = this.toStrings.bind(this);
121
- this.configuration = config.configuration;
122
- this.transitions = config.transitions;
123
- this.children = config.children;
124
- this.done = !!config.done;
125
- this.tags = (_a = config.tags) !== null && _a !== void 0 ? _a : new Set();
126
- Object.defineProperty(this, 'nextEvents', {
127
- get: function () {
128
- return stateUtils_1.nextEvents(_this.configuration);
129
- }
130
- });
131
- }
132
- /**
133
- * Creates a new State instance for the given `stateValue` and `context`.
134
- * @param stateValue
135
- * @param context
136
- */
137
- State.from = function (stateValue, context) {
138
- if (stateValue instanceof State) {
139
- if (stateValue.context !== context) {
140
- return new State({
141
- value: stateValue.value,
142
- context: context,
143
- _event: stateValue._event,
144
- _sessionid: null,
145
- historyValue: stateValue.historyValue,
146
- history: stateValue.history,
147
- actions: [],
148
- activities: stateValue.activities,
149
- meta: {},
150
- events: [],
151
- configuration: [],
152
- transitions: [],
153
- children: {}
154
- });
155
- }
156
- return stateValue;
157
- }
158
- var _event = actions_1.initEvent;
52
+
53
+ var State =
54
+ /*#__PURE__*/
55
+
56
+ /** @class */
57
+ function () {
58
+ /**
59
+ * Creates a new State instance.
60
+ * @param value The state value
61
+ * @param context The extended state
62
+ * @param historyValue The tree representing historical values of the state nodes
63
+ * @param history The previous state
64
+ * @param actions An array of action objects to execute as side-effects
65
+ * @param activities A mapping of activities and whether they are started (`true`) or stopped (`false`).
66
+ * @param meta
67
+ * @param events Internal event queue. Should be empty with run-to-completion semantics.
68
+ * @param configuration
69
+ */
70
+ function State(config) {
71
+ var _this = this;
72
+
73
+ var _a;
74
+
75
+ this.actions = [];
76
+ this.activities = constants.EMPTY_ACTIVITY_MAP;
77
+ this.meta = {};
78
+ this.events = [];
79
+ this.value = config.value;
80
+ this.context = config.context;
81
+ this._event = config._event;
82
+ this._sessionid = config._sessionid;
83
+ this.event = this._event.data;
84
+ this.historyValue = config.historyValue;
85
+ this.history = config.history;
86
+ this.actions = config.actions || [];
87
+ this.activities = config.activities || constants.EMPTY_ACTIVITY_MAP;
88
+ this.meta = stateUtils.getMeta(config.configuration);
89
+ this.events = config.events || [];
90
+ this.matches = this.matches.bind(this);
91
+ this.toStrings = this.toStrings.bind(this);
92
+ this.configuration = config.configuration;
93
+ this.transitions = config.transitions;
94
+ this.children = config.children;
95
+ this.done = !!config.done;
96
+ this.tags = (_a = config.tags) !== null && _a !== void 0 ? _a : new Set();
97
+ Object.defineProperty(this, 'nextEvents', {
98
+ get: function () {
99
+ return stateUtils.nextEvents(_this.configuration);
100
+ }
101
+ });
102
+ }
103
+ /**
104
+ * Creates a new State instance for the given `stateValue` and `context`.
105
+ * @param stateValue
106
+ * @param context
107
+ */
108
+
109
+
110
+ State.from = function (stateValue, context) {
111
+ if (stateValue instanceof State) {
112
+ if (stateValue.context !== context) {
159
113
  return new State({
160
- value: stateValue,
161
- context: context,
162
- _event: _event,
163
- _sessionid: null,
164
- historyValue: undefined,
165
- history: undefined,
166
- actions: [],
167
- activities: undefined,
168
- meta: undefined,
169
- events: [],
170
- configuration: [],
171
- transitions: [],
172
- children: {}
114
+ value: stateValue.value,
115
+ context: context,
116
+ _event: stateValue._event,
117
+ _sessionid: null,
118
+ historyValue: stateValue.historyValue,
119
+ history: stateValue.history,
120
+ actions: [],
121
+ activities: stateValue.activities,
122
+ meta: {},
123
+ events: [],
124
+ configuration: [],
125
+ transitions: [],
126
+ children: {}
173
127
  });
174
- };
175
- /**
176
- * Creates a new State instance for the given `config`.
177
- * @param config The state config
178
- */
179
- State.create = function (config) {
180
- return new State(config);
181
- };
182
- /**
183
- * Creates a new `State` instance for the given `stateValue` and `context` with no actions (side-effects).
184
- * @param stateValue
185
- * @param context
186
- */
187
- State.inert = function (stateValue, context) {
188
- if (stateValue instanceof State) {
189
- if (!stateValue.actions.length) {
190
- return stateValue;
191
- }
192
- var _event = actions_1.initEvent;
193
- return new State({
194
- value: stateValue.value,
195
- context: context,
196
- _event: _event,
197
- _sessionid: null,
198
- historyValue: stateValue.historyValue,
199
- history: stateValue.history,
200
- activities: stateValue.activities,
201
- configuration: stateValue.configuration,
202
- transitions: [],
203
- children: {}
204
- });
205
- }
206
- return State.from(stateValue, context);
207
- };
208
- /**
209
- * Returns an array of all the string leaf state node paths.
210
- * @param stateValue
211
- * @param delimiter The character(s) that separate each subpath in the string state node path.
212
- */
213
- State.prototype.toStrings = function (stateValue, delimiter) {
214
- var _this = this;
215
- if (stateValue === void 0) { stateValue = this.value; }
216
- if (delimiter === void 0) { delimiter = '.'; }
217
- if (utils_1.isString(stateValue)) {
218
- return [stateValue];
219
- }
220
- var valueKeys = utils_1.keys(stateValue);
221
- return valueKeys.concat.apply(valueKeys, __spreadArray([], __read(valueKeys.map(function (key) {
222
- return _this.toStrings(stateValue[key], delimiter).map(function (s) { return key + delimiter + s; });
223
- }))));
224
- };
225
- State.prototype.toJSON = function () {
226
- var _a = this, configuration = _a.configuration, transitions = _a.transitions, tags = _a.tags, jsonValues = __rest(_a, ["configuration", "transitions", "tags"]);
227
- return __assign(__assign({}, jsonValues), { tags: Array.from(tags) });
228
- };
229
- /**
230
- * Whether the current state value is a subset of the given parent state value.
231
- * @param parentStateValue
232
- */
233
- State.prototype.matches = function (parentStateValue) {
234
- return utils_1.matchesState(parentStateValue, this.value);
235
- };
236
- /**
237
- * Whether the current state configuration has a state node with the specified `tag`.
238
- * @param tag
239
- */
240
- State.prototype.hasTag = function (tag) {
241
- return this.tags.has(tag);
242
- };
243
- return State;
244
- }());
128
+ }
129
+
130
+ return stateValue;
131
+ }
132
+
133
+ var _event = actions.initEvent;
134
+ return new State({
135
+ value: stateValue,
136
+ context: context,
137
+ _event: _event,
138
+ _sessionid: null,
139
+ historyValue: undefined,
140
+ history: undefined,
141
+ actions: [],
142
+ activities: undefined,
143
+ meta: undefined,
144
+ events: [],
145
+ configuration: [],
146
+ transitions: [],
147
+ children: {}
148
+ });
149
+ };
150
+ /**
151
+ * Creates a new State instance for the given `config`.
152
+ * @param config The state config
153
+ */
154
+
155
+
156
+ State.create = function (config) {
157
+ return new State(config);
158
+ };
159
+ /**
160
+ * Creates a new `State` instance for the given `stateValue` and `context` with no actions (side-effects).
161
+ * @param stateValue
162
+ * @param context
163
+ */
164
+
165
+
166
+ State.inert = function (stateValue, context) {
167
+ if (stateValue instanceof State) {
168
+ if (!stateValue.actions.length) {
169
+ return stateValue;
170
+ }
171
+
172
+ var _event = actions.initEvent;
173
+ return new State({
174
+ value: stateValue.value,
175
+ context: context,
176
+ _event: _event,
177
+ _sessionid: null,
178
+ historyValue: stateValue.historyValue,
179
+ history: stateValue.history,
180
+ activities: stateValue.activities,
181
+ configuration: stateValue.configuration,
182
+ transitions: [],
183
+ children: {}
184
+ });
185
+ }
186
+
187
+ return State.from(stateValue, context);
188
+ };
189
+ /**
190
+ * Returns an array of all the string leaf state node paths.
191
+ * @param stateValue
192
+ * @param delimiter The character(s) that separate each subpath in the string state node path.
193
+ */
194
+
195
+
196
+ State.prototype.toStrings = function (stateValue, delimiter) {
197
+ var _this = this;
198
+
199
+ if (stateValue === void 0) {
200
+ stateValue = this.value;
201
+ }
202
+
203
+ if (delimiter === void 0) {
204
+ delimiter = '.';
205
+ }
206
+
207
+ if (utils.isString(stateValue)) {
208
+ return [stateValue];
209
+ }
210
+
211
+ var valueKeys = utils.keys(stateValue);
212
+ return valueKeys.concat.apply(valueKeys, _tslib.__spreadArray([], _tslib.__read(valueKeys.map(function (key) {
213
+ return _this.toStrings(stateValue[key], delimiter).map(function (s) {
214
+ return key + delimiter + s;
215
+ });
216
+ }))));
217
+ };
218
+
219
+ State.prototype.toJSON = function () {
220
+ var _a = this,
221
+ configuration = _a.configuration,
222
+ transitions = _a.transitions,
223
+ tags = _a.tags,
224
+ jsonValues = _tslib.__rest(_a, ["configuration", "transitions", "tags"]);
225
+
226
+ return _tslib.__assign(_tslib.__assign({}, jsonValues), {
227
+ tags: Array.from(tags)
228
+ });
229
+ };
230
+ /**
231
+ * Whether the current state value is a subset of the given parent state value.
232
+ * @param parentStateValue
233
+ */
234
+
235
+
236
+ State.prototype.matches = function (parentStateValue) {
237
+ return utils.matchesState(parentStateValue, this.value);
238
+ };
239
+ /**
240
+ * Whether the current state configuration has a state node with the specified `tag`.
241
+ * @param tag
242
+ */
243
+
244
+
245
+ State.prototype.hasTag = function (tag) {
246
+ return this.tags.has(tag);
247
+ };
248
+
249
+ return State;
250
+ }();
251
+
245
252
  exports.State = State;
253
+ exports.bindActionToState = bindActionToState;
254
+ exports.isState = isState;
255
+ exports.stateValuesEqual = stateValuesEqual;