xstate 4.20.1 → 4.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/CHANGELOG.md +134 -0
  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 +17 -5
  6. package/es/Machine.d.ts +6 -3
  7. package/es/Machine.js +3 -6
  8. package/es/State.js +1 -3
  9. package/es/StateNode.d.ts +4 -3
  10. package/es/StateNode.js +34 -20
  11. package/es/_virtual/_tslib.js +59 -73
  12. package/es/actionTypes.js +3 -2
  13. package/es/actions.d.ts +1 -1
  14. package/es/actions.js +51 -37
  15. package/es/behaviors.d.ts +37 -0
  16. package/es/behaviors.js +65 -0
  17. package/es/constants.js +2 -1
  18. package/es/devTools.js +1 -1
  19. package/es/environment.js +2 -1
  20. package/es/index.js +3 -1
  21. package/es/interpreter.d.ts +10 -3
  22. package/es/interpreter.js +44 -22
  23. package/es/invokeUtils.js +4 -3
  24. package/es/mapState.js +1 -1
  25. package/es/match.js +1 -1
  26. package/es/model.d.ts +2 -36
  27. package/es/model.types.d.ts +37 -0
  28. package/es/registry.js +2 -1
  29. package/es/scheduler.js +2 -1
  30. package/es/schema.js +1 -1
  31. package/es/serviceScope.js +1 -3
  32. package/es/stateUtils.js +1 -9
  33. package/es/types.d.ts +26 -5
  34. package/es/types.js +1 -1
  35. package/es/utils.d.ts +3 -2
  36. package/es/utils.js +4 -40
  37. package/lib/Actor.d.ts +25 -25
  38. package/lib/Actor.js +85 -66
  39. package/lib/Machine.d.ts +17 -14
  40. package/lib/Machine.js +14 -14
  41. package/lib/SimulatedClock.d.ts +16 -16
  42. package/lib/State.d.ts +108 -108
  43. package/lib/State.js +246 -236
  44. package/lib/StateNode.d.ts +279 -278
  45. package/lib/StateNode.js +1535 -1339
  46. package/lib/_virtual/_tslib.js +81 -0
  47. package/lib/actionTypes.d.ts +19 -19
  48. package/lib/actionTypes.js +43 -23
  49. package/lib/actions.d.ts +138 -138
  50. package/lib/actions.js +465 -387
  51. package/lib/behaviors.d.ts +37 -0
  52. package/lib/behaviors.js +69 -0
  53. package/lib/constants.d.ts +5 -5
  54. package/lib/constants.js +13 -7
  55. package/lib/devTools.d.ts +15 -15
  56. package/lib/devTools.js +37 -26
  57. package/lib/each.d.ts +3 -3
  58. package/lib/environment.d.ts +1 -1
  59. package/lib/environment.js +7 -4
  60. package/lib/index.d.ts +30 -30
  61. package/lib/index.js +67 -57
  62. package/lib/interpreter.d.ts +205 -198
  63. package/lib/interpreter.js +1306 -1054
  64. package/lib/invoke.d.ts +10 -10
  65. package/lib/invokeUtils.d.ts +6 -6
  66. package/lib/invokeUtils.js +40 -37
  67. package/lib/json.d.ts +30 -30
  68. package/lib/mapState.d.ts +3 -3
  69. package/lib/mapState.js +31 -32
  70. package/lib/match.d.ts +8 -8
  71. package/lib/match.js +33 -47
  72. package/lib/model.d.ts +4 -38
  73. package/lib/model.js +5 -1
  74. package/lib/model.types.d.ts +37 -0
  75. package/lib/model.types.js +2 -0
  76. package/lib/patterns.d.ts +13 -13
  77. package/lib/registry.d.ts +8 -8
  78. package/lib/registry.js +21 -18
  79. package/lib/scheduler.d.ts +16 -16
  80. package/lib/scheduler.js +79 -70
  81. package/lib/schema.d.ts +1 -1
  82. package/lib/schema.js +6 -4
  83. package/lib/scxml.d.ts +5 -5
  84. package/lib/serviceScope.d.ts +3 -3
  85. package/lib/serviceScope.js +16 -12
  86. package/lib/stateUtils.d.ts +14 -14
  87. package/lib/stateUtils.js +231 -199
  88. package/lib/types.d.ts +928 -907
  89. package/lib/types.js +29 -29
  90. package/lib/utils.d.ts +68 -67
  91. package/lib/utils.js +530 -529
  92. package/package.json +6 -6
package/lib/stateUtils.js CHANGED
@@ -1,237 +1,269 @@
1
- "use strict";
2
- var __values = (this && this.__values) || function(o) {
3
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
- if (m) return m.call(o);
5
- if (o && typeof o.length === "number") return {
6
- next: function () {
7
- if (o && i >= o.length) o = void 0;
8
- return { value: o && o[i++], done: !o };
9
- }
10
- };
11
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
- };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
29
- var __spreadArray = (this && this.__spreadArray) || function (to, from) {
30
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
31
- to[j] = from[i];
32
- return to;
33
- };
34
- Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.getMeta = exports.isInFinalState = exports.nextEvents = exports.has = exports.getValue = exports.getAdjList = exports.getConfiguration = exports.getAllStateNodes = exports.getChildren = exports.isLeafNode = void 0;
36
- var utils_1 = require("./utils");
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('./_virtual/_tslib.js');
6
+ var utils = require('./utils.js');
7
+
37
8
  var isLeafNode = function (stateNode) {
38
- return stateNode.type === 'atomic' || stateNode.type === 'final';
9
+ return stateNode.type === 'atomic' || stateNode.type === 'final';
39
10
  };
40
- exports.isLeafNode = isLeafNode;
41
11
  function getChildren(stateNode) {
42
- return utils_1.keys(stateNode.states).map(function (key) { return stateNode.states[key]; });
12
+ return utils.keys(stateNode.states).map(function (key) {
13
+ return stateNode.states[key];
14
+ });
43
15
  }
44
- exports.getChildren = getChildren;
45
16
  function getAllStateNodes(stateNode) {
46
- var stateNodes = [stateNode];
47
- if (exports.isLeafNode(stateNode)) {
48
- return stateNodes;
49
- }
50
- return stateNodes.concat(utils_1.flatten(getChildren(stateNode).map(getAllStateNodes)));
17
+ var stateNodes = [stateNode];
18
+
19
+ if (isLeafNode(stateNode)) {
20
+ return stateNodes;
21
+ }
22
+
23
+ return stateNodes.concat(utils.flatten(getChildren(stateNode).map(getAllStateNodes)));
51
24
  }
52
- exports.getAllStateNodes = getAllStateNodes;
53
25
  function getConfiguration(prevStateNodes, stateNodes) {
54
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
55
- var prevConfiguration = new Set(prevStateNodes);
56
- var prevAdjList = getAdjList(prevConfiguration);
57
- var configuration = new Set(stateNodes);
26
+ var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
27
+
28
+ var prevConfiguration = new Set(prevStateNodes);
29
+ var prevAdjList = getAdjList(prevConfiguration);
30
+ var configuration = new Set(stateNodes);
31
+
32
+ try {
33
+ // add all ancestors
34
+ for (var configuration_1 = _tslib.__values(configuration), configuration_1_1 = configuration_1.next(); !configuration_1_1.done; configuration_1_1 = configuration_1.next()) {
35
+ var s = configuration_1_1.value;
36
+ var m = s.parent;
37
+
38
+ while (m && !configuration.has(m)) {
39
+ configuration.add(m);
40
+ m = m.parent;
41
+ }
42
+ }
43
+ } catch (e_1_1) {
44
+ e_1 = {
45
+ error: e_1_1
46
+ };
47
+ } finally {
58
48
  try {
59
- // add all ancestors
60
- for (var configuration_1 = __values(configuration), configuration_1_1 = configuration_1.next(); !configuration_1_1.done; configuration_1_1 = configuration_1.next()) {
61
- var s = configuration_1_1.value;
62
- var m = s.parent;
63
- while (m && !configuration.has(m)) {
64
- configuration.add(m);
65
- m = m.parent;
66
- }
67
- }
49
+ if (configuration_1_1 && !configuration_1_1.done && (_a = configuration_1.return)) _a.call(configuration_1);
50
+ } finally {
51
+ if (e_1) throw e_1.error;
68
52
  }
69
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
70
- finally {
71
- try {
72
- if (configuration_1_1 && !configuration_1_1.done && (_a = configuration_1.return)) _a.call(configuration_1);
53
+ }
54
+
55
+ var adjList = getAdjList(configuration);
56
+
57
+ try {
58
+ // add descendants
59
+ for (var configuration_2 = _tslib.__values(configuration), configuration_2_1 = configuration_2.next(); !configuration_2_1.done; configuration_2_1 = configuration_2.next()) {
60
+ var s = configuration_2_1.value; // if previously active, add existing child nodes
61
+
62
+ if (s.type === 'compound' && (!adjList.get(s) || !adjList.get(s).length)) {
63
+ if (prevAdjList.get(s)) {
64
+ prevAdjList.get(s).forEach(function (sn) {
65
+ return configuration.add(sn);
66
+ });
67
+ } else {
68
+ s.initialStateNodes.forEach(function (sn) {
69
+ return configuration.add(sn);
70
+ });
73
71
  }
74
- finally { if (e_1) throw e_1.error; }
75
- }
76
- var adjList = getAdjList(configuration);
77
- try {
78
- // add descendants
79
- for (var configuration_2 = __values(configuration), configuration_2_1 = configuration_2.next(); !configuration_2_1.done; configuration_2_1 = configuration_2.next()) {
80
- var s = configuration_2_1.value;
81
- // if previously active, add existing child nodes
82
- if (s.type === 'compound' && (!adjList.get(s) || !adjList.get(s).length)) {
83
- if (prevAdjList.get(s)) {
84
- prevAdjList.get(s).forEach(function (sn) { return configuration.add(sn); });
85
- }
86
- else {
87
- s.initialStateNodes.forEach(function (sn) { return configuration.add(sn); });
72
+ } else {
73
+ if (s.type === 'parallel') {
74
+ try {
75
+ for (var _e = (e_3 = void 0, _tslib.__values(getChildren(s))), _f = _e.next(); !_f.done; _f = _e.next()) {
76
+ var child = _f.value;
77
+
78
+ if (child.type === 'history') {
79
+ continue;
80
+ }
81
+
82
+ if (!configuration.has(child)) {
83
+ configuration.add(child);
84
+
85
+ if (prevAdjList.get(child)) {
86
+ prevAdjList.get(child).forEach(function (sn) {
87
+ return configuration.add(sn);
88
+ });
89
+ } else {
90
+ child.initialStateNodes.forEach(function (sn) {
91
+ return configuration.add(sn);
92
+ });
88
93
  }
94
+ }
89
95
  }
90
- else {
91
- if (s.type === 'parallel') {
92
- try {
93
- for (var _e = (e_3 = void 0, __values(getChildren(s))), _f = _e.next(); !_f.done; _f = _e.next()) {
94
- var child = _f.value;
95
- if (child.type === 'history') {
96
- continue;
97
- }
98
- if (!configuration.has(child)) {
99
- configuration.add(child);
100
- if (prevAdjList.get(child)) {
101
- prevAdjList.get(child).forEach(function (sn) { return configuration.add(sn); });
102
- }
103
- else {
104
- child.initialStateNodes.forEach(function (sn) { return configuration.add(sn); });
105
- }
106
- }
107
- }
108
- }
109
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
110
- finally {
111
- try {
112
- if (_f && !_f.done && (_c = _e.return)) _c.call(_e);
113
- }
114
- finally { if (e_3) throw e_3.error; }
115
- }
116
- }
96
+ } catch (e_3_1) {
97
+ e_3 = {
98
+ error: e_3_1
99
+ };
100
+ } finally {
101
+ try {
102
+ if (_f && !_f.done && (_c = _e.return)) _c.call(_e);
103
+ } finally {
104
+ if (e_3) throw e_3.error;
117
105
  }
106
+ }
118
107
  }
108
+ }
119
109
  }
120
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
121
- finally {
122
- try {
123
- if (configuration_2_1 && !configuration_2_1.done && (_b = configuration_2.return)) _b.call(configuration_2);
124
- }
125
- finally { if (e_2) throw e_2.error; }
126
- }
110
+ } catch (e_2_1) {
111
+ e_2 = {
112
+ error: e_2_1
113
+ };
114
+ } finally {
127
115
  try {
128
- // add all ancestors
129
- for (var configuration_3 = __values(configuration), configuration_3_1 = configuration_3.next(); !configuration_3_1.done; configuration_3_1 = configuration_3.next()) {
130
- var s = configuration_3_1.value;
131
- var m = s.parent;
132
- while (m && !configuration.has(m)) {
133
- configuration.add(m);
134
- m = m.parent;
135
- }
136
- }
116
+ if (configuration_2_1 && !configuration_2_1.done && (_b = configuration_2.return)) _b.call(configuration_2);
117
+ } finally {
118
+ if (e_2) throw e_2.error;
137
119
  }
138
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
139
- finally {
140
- try {
141
- if (configuration_3_1 && !configuration_3_1.done && (_d = configuration_3.return)) _d.call(configuration_3);
142
- }
143
- finally { if (e_4) throw e_4.error; }
120
+ }
121
+
122
+ try {
123
+ // add all ancestors
124
+ for (var configuration_3 = _tslib.__values(configuration), configuration_3_1 = configuration_3.next(); !configuration_3_1.done; configuration_3_1 = configuration_3.next()) {
125
+ var s = configuration_3_1.value;
126
+ var m = s.parent;
127
+
128
+ while (m && !configuration.has(m)) {
129
+ configuration.add(m);
130
+ m = m.parent;
131
+ }
132
+ }
133
+ } catch (e_4_1) {
134
+ e_4 = {
135
+ error: e_4_1
136
+ };
137
+ } finally {
138
+ try {
139
+ if (configuration_3_1 && !configuration_3_1.done && (_d = configuration_3.return)) _d.call(configuration_3);
140
+ } finally {
141
+ if (e_4) throw e_4.error;
144
142
  }
145
- return configuration;
143
+ }
144
+
145
+ return configuration;
146
146
  }
147
- exports.getConfiguration = getConfiguration;
147
+
148
148
  function getValueFromAdj(baseNode, adjList) {
149
- var childStateNodes = adjList.get(baseNode);
150
- if (!childStateNodes) {
151
- return {}; // todo: fix?
152
- }
153
- if (baseNode.type === 'compound') {
154
- var childStateNode = childStateNodes[0];
155
- if (childStateNode) {
156
- if (exports.isLeafNode(childStateNode)) {
157
- return childStateNode.key;
158
- }
159
- }
160
- else {
161
- return {};
162
- }
149
+ var childStateNodes = adjList.get(baseNode);
150
+
151
+ if (!childStateNodes) {
152
+ return {}; // todo: fix?
153
+ }
154
+
155
+ if (baseNode.type === 'compound') {
156
+ var childStateNode = childStateNodes[0];
157
+
158
+ if (childStateNode) {
159
+ if (isLeafNode(childStateNode)) {
160
+ return childStateNode.key;
161
+ }
162
+ } else {
163
+ return {};
163
164
  }
164
- var stateValue = {};
165
- childStateNodes.forEach(function (csn) {
166
- stateValue[csn.key] = getValueFromAdj(csn, adjList);
167
- });
168
- return stateValue;
165
+ }
166
+
167
+ var stateValue = {};
168
+ childStateNodes.forEach(function (csn) {
169
+ stateValue[csn.key] = getValueFromAdj(csn, adjList);
170
+ });
171
+ return stateValue;
169
172
  }
173
+
170
174
  function getAdjList(configuration) {
171
- var e_5, _a;
172
- var adjList = new Map();
173
- try {
174
- for (var configuration_4 = __values(configuration), configuration_4_1 = configuration_4.next(); !configuration_4_1.done; configuration_4_1 = configuration_4.next()) {
175
- var s = configuration_4_1.value;
176
- if (!adjList.has(s)) {
177
- adjList.set(s, []);
178
- }
179
- if (s.parent) {
180
- if (!adjList.has(s.parent)) {
181
- adjList.set(s.parent, []);
182
- }
183
- adjList.get(s.parent).push(s);
184
- }
175
+ var e_5, _a;
176
+
177
+ var adjList = new Map();
178
+
179
+ try {
180
+ for (var configuration_4 = _tslib.__values(configuration), configuration_4_1 = configuration_4.next(); !configuration_4_1.done; configuration_4_1 = configuration_4.next()) {
181
+ var s = configuration_4_1.value;
182
+
183
+ if (!adjList.has(s)) {
184
+ adjList.set(s, []);
185
+ }
186
+
187
+ if (s.parent) {
188
+ if (!adjList.has(s.parent)) {
189
+ adjList.set(s.parent, []);
185
190
  }
191
+
192
+ adjList.get(s.parent).push(s);
193
+ }
186
194
  }
187
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
188
- finally {
189
- try {
190
- if (configuration_4_1 && !configuration_4_1.done && (_a = configuration_4.return)) _a.call(configuration_4);
191
- }
192
- finally { if (e_5) throw e_5.error; }
195
+ } catch (e_5_1) {
196
+ e_5 = {
197
+ error: e_5_1
198
+ };
199
+ } finally {
200
+ try {
201
+ if (configuration_4_1 && !configuration_4_1.done && (_a = configuration_4.return)) _a.call(configuration_4);
202
+ } finally {
203
+ if (e_5) throw e_5.error;
193
204
  }
194
- return adjList;
205
+ }
206
+
207
+ return adjList;
195
208
  }
196
- exports.getAdjList = getAdjList;
197
209
  function getValue(rootNode, configuration) {
198
- var config = getConfiguration([rootNode], configuration);
199
- return getValueFromAdj(rootNode, getAdjList(config));
210
+ var config = getConfiguration([rootNode], configuration);
211
+ return getValueFromAdj(rootNode, getAdjList(config));
200
212
  }
201
- exports.getValue = getValue;
202
213
  function has(iterable, item) {
203
- if (Array.isArray(iterable)) {
204
- return iterable.some(function (member) { return member === item; });
205
- }
206
- if (iterable instanceof Set) {
207
- return iterable.has(item);
208
- }
209
- return false; // TODO: fix
214
+ if (Array.isArray(iterable)) {
215
+ return iterable.some(function (member) {
216
+ return member === item;
217
+ });
218
+ }
219
+
220
+ if (iterable instanceof Set) {
221
+ return iterable.has(item);
222
+ }
223
+
224
+ return false; // TODO: fix
210
225
  }
211
- exports.has = has;
212
226
  function nextEvents(configuration) {
213
- return __spreadArray([], __read(new Set(utils_1.flatten(__spreadArray([], __read(configuration.map(function (sn) { return sn.ownEvents; })))))));
227
+ return _tslib.__spreadArray([], _tslib.__read(new Set(utils.flatten(_tslib.__spreadArray([], _tslib.__read(configuration.map(function (sn) {
228
+ return sn.ownEvents;
229
+ })))))));
214
230
  }
215
- exports.nextEvents = nextEvents;
216
231
  function isInFinalState(configuration, stateNode) {
217
- if (stateNode.type === 'compound') {
218
- return getChildren(stateNode).some(function (s) { return s.type === 'final' && has(configuration, s); });
219
- }
220
- if (stateNode.type === 'parallel') {
221
- return getChildren(stateNode).every(function (sn) {
222
- return isInFinalState(configuration, sn);
223
- });
224
- }
225
- return false;
232
+ if (stateNode.type === 'compound') {
233
+ return getChildren(stateNode).some(function (s) {
234
+ return s.type === 'final' && has(configuration, s);
235
+ });
236
+ }
237
+
238
+ if (stateNode.type === 'parallel') {
239
+ return getChildren(stateNode).every(function (sn) {
240
+ return isInFinalState(configuration, sn);
241
+ });
242
+ }
243
+
244
+ return false;
226
245
  }
227
- exports.isInFinalState = isInFinalState;
228
246
  function getMeta(configuration) {
229
- if (configuration === void 0) { configuration = []; }
230
- return configuration.reduce(function (acc, stateNode) {
231
- if (stateNode.meta !== undefined) {
232
- acc[stateNode.id] = stateNode.meta;
233
- }
234
- return acc;
235
- }, {});
247
+ if (configuration === void 0) {
248
+ configuration = [];
249
+ }
250
+
251
+ return configuration.reduce(function (acc, stateNode) {
252
+ if (stateNode.meta !== undefined) {
253
+ acc[stateNode.id] = stateNode.meta;
254
+ }
255
+
256
+ return acc;
257
+ }, {});
236
258
  }
259
+
260
+ exports.getAdjList = getAdjList;
261
+ exports.getAllStateNodes = getAllStateNodes;
262
+ exports.getChildren = getChildren;
263
+ exports.getConfiguration = getConfiguration;
237
264
  exports.getMeta = getMeta;
265
+ exports.getValue = getValue;
266
+ exports.has = has;
267
+ exports.isInFinalState = isInFinalState;
268
+ exports.isLeafNode = isLeafNode;
269
+ exports.nextEvents = nextEvents;