xstate 4.35.2 → 5.0.0-alpha.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 (203) hide show
  1. package/README.md +25 -16
  2. package/actions/dist/xstate-actions.cjs.d.ts +1 -0
  3. package/actions/dist/xstate-actions.cjs.dev.js +30 -0
  4. package/actions/dist/xstate-actions.cjs.js +7 -0
  5. package/actions/dist/xstate-actions.cjs.prod.js +30 -0
  6. package/actions/dist/xstate-actions.esm.js +2 -0
  7. package/actions/dist/xstate-actions.umd.min.js +2 -0
  8. package/actions/dist/xstate-actions.umd.min.js.map +1 -0
  9. package/actions/dynamicAction.ts +34 -0
  10. package/actions/package.json +8 -0
  11. package/actors/dist/xstate-actors.cjs.d.ts +1 -0
  12. package/actors/dist/xstate-actors.cjs.dev.js +20 -0
  13. package/actors/dist/xstate-actors.cjs.js +7 -0
  14. package/actors/dist/xstate-actors.cjs.prod.js +20 -0
  15. package/actors/dist/xstate-actors.esm.js +2 -0
  16. package/actors/dist/xstate-actors.umd.min.js +2 -0
  17. package/actors/dist/xstate-actors.umd.min.js.map +1 -0
  18. package/actors/package.json +8 -0
  19. package/dev/dist/xstate-dev.cjs.d.ts +1 -0
  20. package/dev/dist/xstate-dev.cjs.dev.js +11 -0
  21. package/dev/dist/xstate-dev.cjs.js +7 -0
  22. package/{lib/devTools.js → dev/dist/xstate-dev.cjs.prod.js} +17 -10
  23. package/dev/dist/xstate-dev.esm.js +1 -0
  24. package/dev/dist/xstate-dev.umd.min.js +2 -0
  25. package/dev/dist/xstate-dev.umd.min.js.map +1 -0
  26. package/dev/package.json +8 -0
  27. package/dist/actions-7ea9280f.cjs.prod.js +4674 -0
  28. package/dist/actions-b08d0da8.cjs.dev.js +4718 -0
  29. package/dist/actions-b673cb13.esm.js +4660 -0
  30. package/dist/declarations/actions/dynamicAction.d.ts +5 -0
  31. package/dist/declarations/src/Machine.d.ts +4 -0
  32. package/dist/declarations/src/Mailbox.d.ts +12 -0
  33. package/{es → dist/declarations/src}/SimulatedClock.d.ts +16 -17
  34. package/dist/declarations/src/State.d.ts +97 -0
  35. package/dist/declarations/src/StateMachine.d.ts +120 -0
  36. package/dist/declarations/src/StateNode.d.ts +127 -0
  37. package/{es → dist/declarations/src}/actionTypes.d.ts +16 -20
  38. package/dist/declarations/src/actions/assign.d.ts +7 -0
  39. package/dist/declarations/src/actions/cancel.d.ts +10 -0
  40. package/dist/declarations/src/actions/choose.d.ts +3 -0
  41. package/dist/declarations/src/actions/invoke.d.ts +3 -0
  42. package/dist/declarations/src/actions/log.d.ts +11 -0
  43. package/dist/declarations/src/actions/pure.d.ts +3 -0
  44. package/dist/declarations/src/actions/raise.d.ts +9 -0
  45. package/dist/declarations/src/actions/send.d.ts +51 -0
  46. package/dist/declarations/src/actions/stop.d.ts +7 -0
  47. package/dist/declarations/src/actions.d.ts +43 -0
  48. package/dist/declarations/src/actors.d.ts +37 -0
  49. package/dist/declarations/src/constants.d.ts +5 -0
  50. package/{lib/devTools.d.ts → dist/declarations/src/dev/index.d.ts} +14 -14
  51. package/{es → dist/declarations/src}/environment.d.ts +1 -2
  52. package/dist/declarations/src/guards.d.ts +8 -0
  53. package/dist/declarations/src/index.d.ts +44 -0
  54. package/dist/declarations/src/interpreter.d.ts +108 -0
  55. package/{es → dist/declarations/src}/mapState.d.ts +3 -4
  56. package/dist/declarations/src/memo.d.ts +2 -0
  57. package/dist/declarations/src/registry.d.ts +8 -0
  58. package/{es → dist/declarations/src}/schema.d.ts +2 -3
  59. package/dist/declarations/src/spawn.d.ts +2 -0
  60. package/dist/declarations/src/stateUtils.d.ts +70 -0
  61. package/dist/declarations/src/symbolObservable.d.ts +1 -0
  62. package/{lib → dist/declarations/src}/typegenTypes.d.ts +132 -133
  63. package/dist/declarations/src/types.d.ts +1001 -0
  64. package/dist/declarations/src/utils.d.ts +54 -0
  65. package/{es/devTools.js → dist/index-20b364c4.esm.js} +18 -6
  66. package/dist/index-2a564d03.cjs.dev.js +64 -0
  67. package/dist/xstate.cjs.d.ts +1 -0
  68. package/dist/xstate.cjs.dev.js +993 -0
  69. package/dist/xstate.cjs.js +7 -0
  70. package/dist/xstate.cjs.prod.js +993 -0
  71. package/dist/xstate.esm.js +952 -0
  72. package/dist/xstate.umd.min.js +2 -0
  73. package/dist/xstate.umd.min.js.map +1 -0
  74. package/guards/dist/xstate-guards.cjs.d.ts +1 -0
  75. package/guards/dist/xstate-guards.cjs.dev.js +15 -0
  76. package/guards/dist/xstate-guards.cjs.js +7 -0
  77. package/guards/dist/xstate-guards.cjs.prod.js +15 -0
  78. package/guards/dist/xstate-guards.esm.js +2 -0
  79. package/guards/dist/xstate-guards.umd.min.js +2 -0
  80. package/guards/dist/xstate-guards.umd.min.js.map +1 -0
  81. package/guards/package.json +8 -0
  82. package/invoke/package.json +4 -0
  83. package/package.json +44 -30
  84. package/dist/xstate.interpreter.js +0 -15
  85. package/dist/xstate.js +0 -15
  86. package/dist/xstate.web.js +0 -15
  87. package/es/Actor.d.ts +0 -25
  88. package/es/Actor.js +0 -99
  89. package/es/Machine.d.ts +0 -12
  90. package/es/Machine.js +0 -21
  91. package/es/SimulatedClock.js +0 -81
  92. package/es/State.d.ts +0 -123
  93. package/es/State.js +0 -274
  94. package/es/StateNode.d.ts +0 -282
  95. package/es/StateNode.js +0 -1587
  96. package/es/_virtual/_tslib.js +0 -78
  97. package/es/actionTypes.js +0 -22
  98. package/es/actions.d.ts +0 -157
  99. package/es/actions.js +0 -630
  100. package/es/behaviors.d.ts +0 -37
  101. package/es/behaviors.js +0 -131
  102. package/es/constants.d.ts +0 -6
  103. package/es/constants.js +0 -6
  104. package/es/devTools.d.ts +0 -14
  105. package/es/each.d.ts +0 -4
  106. package/es/each.js +0 -13
  107. package/es/environment.js +0 -3
  108. package/es/index.d.ts +0 -22
  109. package/es/index.js +0 -25
  110. package/es/interpreter.d.ts +0 -217
  111. package/es/interpreter.js +0 -1488
  112. package/es/invoke.d.ts +0 -11
  113. package/es/invokeUtils.d.ts +0 -7
  114. package/es/invokeUtils.js +0 -40
  115. package/es/json.d.ts +0 -31
  116. package/es/json.js +0 -86
  117. package/es/mapState.js +0 -32
  118. package/es/match.d.ts +0 -9
  119. package/es/match.js +0 -34
  120. package/es/model.d.ts +0 -8
  121. package/es/model.js +0 -50
  122. package/es/model.types.d.ts +0 -56
  123. package/es/patterns.d.ts +0 -14
  124. package/es/patterns.js +0 -48
  125. package/es/registry.d.ts +0 -9
  126. package/es/registry.js +0 -19
  127. package/es/scheduler.d.ts +0 -17
  128. package/es/scheduler.js +0 -79
  129. package/es/schema.js +0 -6
  130. package/es/scxml.d.ts +0 -6
  131. package/es/serviceScope.d.ts +0 -4
  132. package/es/serviceScope.js +0 -16
  133. package/es/stateUtils.d.ts +0 -18
  134. package/es/stateUtils.js +0 -262
  135. package/es/typegenTypes.d.ts +0 -133
  136. package/es/types.d.ts +0 -1040
  137. package/es/types.js +0 -33
  138. package/es/utils.d.ts +0 -72
  139. package/es/utils.js +0 -622
  140. package/es/waitFor.d.ts +0 -34
  141. package/es/waitFor.js +0 -73
  142. package/lib/Actor.d.ts +0 -25
  143. package/lib/Actor.js +0 -108
  144. package/lib/Machine.d.ts +0 -12
  145. package/lib/Machine.js +0 -26
  146. package/lib/SimulatedClock.d.ts +0 -17
  147. package/lib/SimulatedClock.js +0 -85
  148. package/lib/State.d.ts +0 -123
  149. package/lib/State.js +0 -282
  150. package/lib/StateNode.d.ts +0 -282
  151. package/lib/StateNode.js +0 -1591
  152. package/lib/_virtual/_tslib.js +0 -85
  153. package/lib/actionTypes.d.ts +0 -20
  154. package/lib/actionTypes.js +0 -43
  155. package/lib/actions.d.ts +0 -157
  156. package/lib/actions.js +0 -662
  157. package/lib/behaviors.d.ts +0 -37
  158. package/lib/behaviors.js +0 -137
  159. package/lib/constants.d.ts +0 -6
  160. package/lib/constants.js +0 -13
  161. package/lib/each.d.ts +0 -4
  162. package/lib/each.js +0 -17
  163. package/lib/environment.d.ts +0 -2
  164. package/lib/environment.js +0 -7
  165. package/lib/index.d.ts +0 -22
  166. package/lib/index.js +0 -64
  167. package/lib/interpreter.d.ts +0 -217
  168. package/lib/interpreter.js +0 -1494
  169. package/lib/invoke.d.ts +0 -11
  170. package/lib/invoke.js +0 -20
  171. package/lib/invokeUtils.d.ts +0 -7
  172. package/lib/invokeUtils.js +0 -45
  173. package/lib/json.d.ts +0 -31
  174. package/lib/json.js +0 -94
  175. package/lib/mapState.d.ts +0 -4
  176. package/lib/mapState.js +0 -36
  177. package/lib/match.d.ts +0 -9
  178. package/lib/match.js +0 -38
  179. package/lib/model.d.ts +0 -8
  180. package/lib/model.js +0 -54
  181. package/lib/model.types.d.ts +0 -56
  182. package/lib/model.types.js +0 -2
  183. package/lib/patterns.d.ts +0 -14
  184. package/lib/patterns.js +0 -53
  185. package/lib/registry.d.ts +0 -9
  186. package/lib/registry.js +0 -23
  187. package/lib/scheduler.d.ts +0 -17
  188. package/lib/scheduler.js +0 -83
  189. package/lib/schema.d.ts +0 -3
  190. package/lib/schema.js +0 -11
  191. package/lib/scxml.d.ts +0 -6
  192. package/lib/scxml.js +0 -350
  193. package/lib/serviceScope.d.ts +0 -4
  194. package/lib/serviceScope.js +0 -21
  195. package/lib/stateUtils.d.ts +0 -18
  196. package/lib/stateUtils.js +0 -277
  197. package/lib/typegenTypes.js +0 -2
  198. package/lib/types.d.ts +0 -1040
  199. package/lib/types.js +0 -35
  200. package/lib/utils.d.ts +0 -72
  201. package/lib/utils.js +0 -669
  202. package/lib/waitFor.d.ts +0 -34
  203. package/lib/waitFor.js +0 -77
package/lib/scxml.js DELETED
@@ -1,350 +0,0 @@
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 __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, pack) {
30
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
- if (ar || !(i in from)) {
32
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
- ar[i] = from[i];
34
- }
35
- }
36
- return to.concat(ar || Array.prototype.slice.call(from));
37
- };
38
- var __values = (this && this.__values) || function(o) {
39
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
40
- if (m) return m.call(o);
41
- if (o && typeof o.length === "number") return {
42
- next: function () {
43
- if (o && i >= o.length) o = void 0;
44
- return { value: o && o[i++], done: !o };
45
- }
46
- };
47
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
48
- };
49
- Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.toMachine = void 0;
51
- var xml_js_1 = require("xml-js");
52
- var index_1 = require("./index");
53
- var utils_1 = require("./utils");
54
- var actions = require("./actions");
55
- function getAttribute(element, attribute) {
56
- return element.attributes ? element.attributes[attribute] : undefined;
57
- }
58
- function indexedRecord(items, identifier) {
59
- var record = {};
60
- var identifierFn = (0, utils_1.isString)(identifier)
61
- ? function (item) { return item[identifier]; }
62
- : identifier;
63
- items.forEach(function (item) {
64
- var key = identifierFn(item);
65
- record[key] = item;
66
- });
67
- return record;
68
- }
69
- function executableContent(elements) {
70
- var transition = {
71
- actions: mapActions(elements)
72
- };
73
- return transition;
74
- }
75
- function getTargets(targetAttr) {
76
- // return targetAttr ? [`#${targetAttr}`] : undefined;
77
- return targetAttr
78
- ? "".concat(targetAttr).split(/\s+/).map(function (target) { return "#".concat(target); })
79
- : undefined;
80
- }
81
- function delayToMs(delay) {
82
- if (!delay) {
83
- return undefined;
84
- }
85
- if (typeof delay === 'number') {
86
- return delay;
87
- }
88
- var millisecondsMatch = delay.match(/(\d+)ms/);
89
- if (millisecondsMatch) {
90
- return parseInt(millisecondsMatch[1], 10);
91
- }
92
- var secondsMatch = delay.match(/(\d*)(\.?)(\d+)s/);
93
- if (secondsMatch) {
94
- var hasDecimal = !!secondsMatch[2];
95
- if (!hasDecimal) {
96
- return parseInt(secondsMatch[3], 10) * 1000;
97
- }
98
- var secondsPart = !!secondsMatch[1]
99
- ? parseInt(secondsMatch[1], 10) * 1000
100
- : 0;
101
- var millisecondsPart = parseInt(secondsMatch[3].padEnd(3, '0'), 10);
102
- if (millisecondsPart >= 1000) {
103
- throw new Error("Can't parse \"".concat(delay, " delay.\""));
104
- }
105
- return secondsPart + millisecondsPart;
106
- }
107
- throw new Error("Can't parse \"".concat(delay, " delay.\""));
108
- }
109
- var evaluateExecutableContent = function (context, _ev, meta, body) {
110
- var datamodel = context
111
- ? Object.keys(context)
112
- .map(function (key) { return "const ".concat(key, " = context['").concat(key, "'];"); })
113
- .join('\n')
114
- : '';
115
- var scope = ['const _sessionid = "NOT_IMPLEMENTED";', datamodel]
116
- .filter(Boolean)
117
- .join('\n');
118
- var args = ['context', '_event'];
119
- var fnBody = "\n ".concat(scope, "\n ").concat(body, "\n ");
120
- var fn = new (Function.bind.apply(Function, __spreadArray(__spreadArray([void 0], __read(args), false), [fnBody], false)))();
121
- return fn(context, meta._event);
122
- };
123
- function createCond(cond) {
124
- return function (context, _event, meta) {
125
- return evaluateExecutableContent(context, _event, meta, "return ".concat(cond, ";"));
126
- };
127
- }
128
- function mapAction(element) {
129
- var e_1, _a;
130
- switch (element.name) {
131
- case 'raise': {
132
- return actions.raise(element.attributes.event);
133
- }
134
- case 'assign': {
135
- return actions.assign(function (context, e, meta) {
136
- var fnBody = "\n return {'".concat(element.attributes.location, "': ").concat(element.attributes.expr, "};\n ");
137
- return evaluateExecutableContent(context, e, meta, fnBody);
138
- });
139
- }
140
- case 'send': {
141
- var _b = element.attributes, event_1 = _b.event, eventexpr_1 = _b.eventexpr, target = _b.target;
142
- var convertedEvent = void 0;
143
- var convertedDelay = void 0;
144
- var params_1 = element.elements &&
145
- element.elements.reduce(function (acc, child) {
146
- if (child.name === 'content') {
147
- throw new Error('Conversion of <content/> inside <send/> not implemented.');
148
- }
149
- return "".concat(acc).concat(child.attributes.name, ":").concat(child.attributes.expr, ",\n");
150
- }, '');
151
- if (event_1 && !params_1) {
152
- convertedEvent = event_1;
153
- }
154
- else {
155
- convertedEvent = function (context, _ev, meta) {
156
- var fnBody = "\n return { type: ".concat(event_1 ? "\"".concat(event_1, "\"") : eventexpr_1, ", ").concat(params_1 ? params_1 : '', " }\n ");
157
- return evaluateExecutableContent(context, _ev, meta, fnBody);
158
- };
159
- }
160
- if ('delay' in element.attributes) {
161
- convertedDelay = delayToMs(element.attributes.delay);
162
- }
163
- else if (element.attributes.delayexpr) {
164
- convertedDelay = function (context, _ev, meta) {
165
- var fnBody = "\n return (".concat(delayToMs, ")(").concat(element.attributes.delayexpr, ");\n ");
166
- return evaluateExecutableContent(context, _ev, meta, fnBody);
167
- };
168
- }
169
- return actions.send(convertedEvent, {
170
- delay: convertedDelay,
171
- to: target
172
- });
173
- }
174
- case 'log': {
175
- var label = element.attributes.label;
176
- return actions.log(function (context, e, meta) {
177
- var fnBody = "\n return ".concat(element.attributes.expr, ";\n ");
178
- return evaluateExecutableContent(context, e, meta, fnBody);
179
- }, label !== undefined ? String(label) : undefined);
180
- }
181
- case 'if': {
182
- var conds = [];
183
- var current = {
184
- cond: createCond(element.attributes.cond),
185
- actions: []
186
- };
187
- try {
188
- for (var _c = __values(element.elements), _d = _c.next(); !_d.done; _d = _c.next()) {
189
- var el = _d.value;
190
- if (el.type === 'comment') {
191
- continue;
192
- }
193
- switch (el.name) {
194
- case 'elseif':
195
- conds.push(current);
196
- current = {
197
- cond: createCond(el.attributes.cond),
198
- actions: []
199
- };
200
- break;
201
- case 'else':
202
- conds.push(current);
203
- current = { actions: [] };
204
- break;
205
- default:
206
- current.actions.push(mapAction(el));
207
- break;
208
- }
209
- }
210
- }
211
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
212
- finally {
213
- try {
214
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
215
- }
216
- finally { if (e_1) throw e_1.error; }
217
- }
218
- conds.push(current);
219
- return actions.choose(conds);
220
- }
221
- default:
222
- throw new Error("Conversion of \"".concat(element.name, "\" elements is not implemented yet."));
223
- }
224
- }
225
- function mapActions(elements) {
226
- var e_2, _a;
227
- var mapped = [];
228
- try {
229
- for (var elements_1 = __values(elements), elements_1_1 = elements_1.next(); !elements_1_1.done; elements_1_1 = elements_1.next()) {
230
- var element = elements_1_1.value;
231
- if (element.type === 'comment') {
232
- continue;
233
- }
234
- mapped.push(mapAction(element));
235
- }
236
- }
237
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
238
- finally {
239
- try {
240
- if (elements_1_1 && !elements_1_1.done && (_a = elements_1.return)) _a.call(elements_1);
241
- }
242
- finally { if (e_2) throw e_2.error; }
243
- }
244
- return mapped;
245
- }
246
- function toConfig(nodeJson, id, options) {
247
- var parallel = nodeJson.name === 'parallel';
248
- var initial = parallel ? undefined : nodeJson.attributes.initial;
249
- var elements = nodeJson.elements;
250
- switch (nodeJson.name) {
251
- case 'history': {
252
- if (!elements) {
253
- return {
254
- id: id,
255
- history: nodeJson.attributes.type || 'shallow'
256
- };
257
- }
258
- var _a = __read(elements.filter(function (element) { return element.name === 'transition'; }), 1), transitionElement = _a[0];
259
- var target = getAttribute(transitionElement, 'target');
260
- var history_1 = getAttribute(nodeJson, 'type') || 'shallow';
261
- return {
262
- id: id,
263
- history: history_1,
264
- target: target ? "#".concat(target) : undefined
265
- };
266
- }
267
- case 'final': {
268
- return __assign(__assign({}, nodeJson.attributes), { type: 'final' });
269
- }
270
- default:
271
- break;
272
- }
273
- if (nodeJson.elements) {
274
- var stateElements = nodeJson.elements.filter(function (element) {
275
- return element.name === 'state' ||
276
- element.name === 'parallel' ||
277
- element.name === 'final' ||
278
- element.name === 'history';
279
- });
280
- var transitionElements = nodeJson.elements.filter(function (element) { return element.name === 'transition'; });
281
- var invokeElements = nodeJson.elements.filter(function (element) { return element.name === 'invoke'; });
282
- var onEntryElement = nodeJson.elements.find(function (element) { return element.name === 'onentry'; });
283
- var onExitElement = nodeJson.elements.find(function (element) { return element.name === 'onexit'; });
284
- var states = indexedRecord(stateElements, function (item) { return "".concat(item.attributes.id); });
285
- var initialElement = !initial
286
- ? nodeJson.elements.find(function (element) { return element.name === 'initial'; })
287
- : undefined;
288
- if (initialElement && initialElement.elements.length) {
289
- initial = initialElement.elements.find(function (element) { return element.name === 'transition'; }).attributes.target;
290
- }
291
- else if (!initialElement && stateElements.length) {
292
- initial = stateElements[0].attributes.id;
293
- }
294
- var on = transitionElements.map(function (value) {
295
- var event = getAttribute(value, 'event') || '';
296
- var targets = getAttribute(value, 'target');
297
- var internal = getAttribute(value, 'type') === 'internal';
298
- return __assign(__assign(__assign({ event: event, target: getTargets(targets) }, (value.elements ? executableContent(value.elements) : undefined)), (value.attributes && value.attributes.cond
299
- ? {
300
- cond: createCond(value.attributes.cond)
301
- }
302
- : undefined)), { internal: internal });
303
- });
304
- var onEntry = onEntryElement
305
- ? mapActions(onEntryElement.elements)
306
- : undefined;
307
- var onExit = onExitElement
308
- ? mapActions(onExitElement.elements)
309
- : undefined;
310
- var invoke = invokeElements.map(function (element) {
311
- if (!['scxml', 'http://www.w3.org/TR/scxml/'].includes(element.attributes.type)) {
312
- throw new Error('Currently only converting invoke elements of type SCXML is supported.');
313
- }
314
- var content = element.elements.find(function (el) { return el.name === 'content'; });
315
- return scxmlToMachine(content, options);
316
- });
317
- return __assign(__assign(__assign(__assign(__assign(__assign(__assign({ id: id }, (initial ? { initial: initial } : undefined)), (parallel ? { type: 'parallel' } : undefined)), (stateElements.length
318
- ? {
319
- states: (0, utils_1.mapValues)(states, function (state, key) {
320
- return toConfig(state, key, options);
321
- })
322
- }
323
- : undefined)), (transitionElements.length ? { on: on } : undefined)), (onEntry ? { onEntry: onEntry } : undefined)), (onExit ? { onExit: onExit } : undefined)), (invoke.length ? { invoke: invoke } : undefined));
324
- }
325
- return { id: id };
326
- }
327
- function scxmlToMachine(scxmlJson, options) {
328
- var machineElement = scxmlJson.elements.find(function (element) { return element.name === 'scxml'; });
329
- var dataModelEl = machineElement.elements.filter(function (element) { return element.name === 'datamodel'; })[0];
330
- var extState = dataModelEl
331
- ? dataModelEl
332
- .elements.filter(function (element) { return element.name === 'data'; })
333
- .reduce(function (acc, element) {
334
- if (element.attributes.src) {
335
- throw new Error("Conversion of `src` attribute on datamodel's <data> elements is not supported.");
336
- }
337
- acc[element.attributes.id] = element.attributes.expr
338
- ? // tslint:disable-next-line:no-eval
339
- eval("(".concat(element.attributes.expr, ")"))
340
- : undefined;
341
- return acc;
342
- }, {})
343
- : undefined;
344
- return (0, index_1.Machine)(__assign(__assign({}, toConfig(machineElement, '(machine)', options)), { context: extState, delimiter: options.delimiter }));
345
- }
346
- function toMachine(xml, options) {
347
- var json = (0, xml_js_1.xml2js)(xml);
348
- return scxmlToMachine(json, options);
349
- }
350
- exports.toMachine = toMachine;
@@ -1,4 +0,0 @@
1
- import { AnyInterpreter } from './types';
2
- export declare const provide: <T, TService extends AnyInterpreter>(service: TService | undefined, fn: (service: TService | undefined) => T) => T;
3
- export declare const consume: <T, TService extends AnyInterpreter>(fn: (service: TService) => T) => T;
4
- //# sourceMappingURL=serviceScope.d.ts.map
@@ -1,21 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- /**
6
- * Maintains a stack of the current service in scope.
7
- * This is used to provide the correct service to spawn().
8
- */
9
- var serviceStack = [];
10
- var provide = function (service, fn) {
11
- serviceStack.push(service);
12
- var result = fn(service);
13
- serviceStack.pop();
14
- return result;
15
- };
16
- var consume = function (fn) {
17
- return fn(serviceStack[serviceStack.length - 1]);
18
- };
19
-
20
- exports.consume = consume;
21
- exports.provide = provide;
@@ -1,18 +0,0 @@
1
- import { EventObject, StateValue } from './types';
2
- import { StateNode } from './StateNode';
3
- declare type Configuration<TC, TE extends EventObject> = Iterable<StateNode<TC, any, TE>>;
4
- declare type AdjList<TC, TE extends EventObject> = Map<StateNode<TC, any, TE>, Array<StateNode<TC, any, TE>>>;
5
- export declare const isLeafNode: (stateNode: StateNode<any, any, any, any, any, any>) => boolean;
6
- export declare function getAllChildren<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE>): Array<StateNode<TC, any, TE>>;
7
- export declare function getChildren<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE>): Array<StateNode<TC, any, TE>>;
8
- export declare function getAllStateNodes<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE, any, any, any>): Array<StateNode<TC, any, TE, any, any, any>>;
9
- export declare function getConfiguration<TC, TE extends EventObject>(prevStateNodes: Iterable<StateNode<TC, any, TE, any, any, any>>, stateNodes: Iterable<StateNode<TC, any, TE, any, any, any>>): Set<StateNode<TC, any, TE, any, any, any>>;
10
- export declare function getAdjList<TC, TE extends EventObject>(configuration: Configuration<TC, TE>): AdjList<TC, TE>;
11
- export declare function getValue<TC, TE extends EventObject>(rootNode: StateNode<TC, any, TE, any>, configuration: Configuration<TC, TE>): StateValue;
12
- export declare function has<T>(iterable: Iterable<T>, item: T): boolean;
13
- export declare function nextEvents<TC, TE extends EventObject>(configuration: Array<StateNode<TC, any, TE>>): Array<TE['type']>;
14
- export declare function isInFinalState<TC, TE extends EventObject>(configuration: Array<StateNode<TC, any, TE, any, any, any>>, stateNode: StateNode<TC, any, TE, any, any, any>): boolean;
15
- export declare function getMeta(configuration?: StateNode[]): Record<string, any>;
16
- export declare function getTagsFromConfiguration(configuration: StateNode<any, any, any, any>[]): Set<string>;
17
- export {};
18
- //# sourceMappingURL=stateUtils.d.ts.map
package/lib/stateUtils.js DELETED
@@ -1,277 +0,0 @@
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
-
8
- var isLeafNode = function (stateNode) {
9
- return stateNode.type === 'atomic' || stateNode.type === 'final';
10
- };
11
- function getAllChildren(stateNode) {
12
- return Object.keys(stateNode.states).map(function (key) {
13
- return stateNode.states[key];
14
- });
15
- }
16
- function getChildren(stateNode) {
17
- return getAllChildren(stateNode).filter(function (sn) {
18
- return sn.type !== 'history';
19
- });
20
- }
21
- function getAllStateNodes(stateNode) {
22
- var stateNodes = [stateNode];
23
-
24
- if (isLeafNode(stateNode)) {
25
- return stateNodes;
26
- }
27
-
28
- return stateNodes.concat(utils.flatten(getChildren(stateNode).map(getAllStateNodes)));
29
- }
30
- function getConfiguration(prevStateNodes, stateNodes) {
31
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
32
-
33
- var prevConfiguration = new Set(prevStateNodes);
34
- var prevAdjList = getAdjList(prevConfiguration);
35
- var configuration = new Set(stateNodes);
36
-
37
- try {
38
- // add all ancestors
39
- for (var configuration_1 = _tslib.__values(configuration), configuration_1_1 = configuration_1.next(); !configuration_1_1.done; configuration_1_1 = configuration_1.next()) {
40
- var s = configuration_1_1.value;
41
- var m = s.parent;
42
-
43
- while (m && !configuration.has(m)) {
44
- configuration.add(m);
45
- m = m.parent;
46
- }
47
- }
48
- } catch (e_1_1) {
49
- e_1 = {
50
- error: e_1_1
51
- };
52
- } finally {
53
- try {
54
- if (configuration_1_1 && !configuration_1_1.done && (_a = configuration_1.return)) _a.call(configuration_1);
55
- } finally {
56
- if (e_1) throw e_1.error;
57
- }
58
- }
59
-
60
- var adjList = getAdjList(configuration);
61
-
62
- try {
63
- // add descendants
64
- for (var configuration_2 = _tslib.__values(configuration), configuration_2_1 = configuration_2.next(); !configuration_2_1.done; configuration_2_1 = configuration_2.next()) {
65
- var s = configuration_2_1.value; // if previously active, add existing child nodes
66
-
67
- if (s.type === 'compound' && (!adjList.get(s) || !adjList.get(s).length)) {
68
- if (prevAdjList.get(s)) {
69
- prevAdjList.get(s).forEach(function (sn) {
70
- return configuration.add(sn);
71
- });
72
- } else {
73
- s.initialStateNodes.forEach(function (sn) {
74
- return configuration.add(sn);
75
- });
76
- }
77
- } else {
78
- if (s.type === 'parallel') {
79
- try {
80
- for (var _e = (e_3 = void 0, _tslib.__values(getChildren(s))), _f = _e.next(); !_f.done; _f = _e.next()) {
81
- var child = _f.value;
82
-
83
- if (!configuration.has(child)) {
84
- configuration.add(child);
85
-
86
- if (prevAdjList.get(child)) {
87
- prevAdjList.get(child).forEach(function (sn) {
88
- return configuration.add(sn);
89
- });
90
- } else {
91
- child.initialStateNodes.forEach(function (sn) {
92
- return configuration.add(sn);
93
- });
94
- }
95
- }
96
- }
97
- } catch (e_3_1) {
98
- e_3 = {
99
- error: e_3_1
100
- };
101
- } finally {
102
- try {
103
- if (_f && !_f.done && (_c = _e.return)) _c.call(_e);
104
- } finally {
105
- if (e_3) throw e_3.error;
106
- }
107
- }
108
- }
109
- }
110
- }
111
- } catch (e_2_1) {
112
- e_2 = {
113
- error: e_2_1
114
- };
115
- } finally {
116
- try {
117
- if (configuration_2_1 && !configuration_2_1.done && (_b = configuration_2.return)) _b.call(configuration_2);
118
- } finally {
119
- if (e_2) throw e_2.error;
120
- }
121
- }
122
-
123
- try {
124
- // add all ancestors
125
- for (var configuration_3 = _tslib.__values(configuration), configuration_3_1 = configuration_3.next(); !configuration_3_1.done; configuration_3_1 = configuration_3.next()) {
126
- var s = configuration_3_1.value;
127
- var m = s.parent;
128
-
129
- while (m && !configuration.has(m)) {
130
- configuration.add(m);
131
- m = m.parent;
132
- }
133
- }
134
- } catch (e_4_1) {
135
- e_4 = {
136
- error: e_4_1
137
- };
138
- } finally {
139
- try {
140
- if (configuration_3_1 && !configuration_3_1.done && (_d = configuration_3.return)) _d.call(configuration_3);
141
- } finally {
142
- if (e_4) throw e_4.error;
143
- }
144
- }
145
-
146
- return configuration;
147
- }
148
-
149
- function getValueFromAdj(baseNode, adjList) {
150
- var childStateNodes = adjList.get(baseNode);
151
-
152
- if (!childStateNodes) {
153
- return {}; // todo: fix?
154
- }
155
-
156
- if (baseNode.type === 'compound') {
157
- var childStateNode = childStateNodes[0];
158
-
159
- if (childStateNode) {
160
- if (isLeafNode(childStateNode)) {
161
- return childStateNode.key;
162
- }
163
- } else {
164
- return {};
165
- }
166
- }
167
-
168
- var stateValue = {};
169
- childStateNodes.forEach(function (csn) {
170
- stateValue[csn.key] = getValueFromAdj(csn, adjList);
171
- });
172
- return stateValue;
173
- }
174
-
175
- function getAdjList(configuration) {
176
- var e_5, _a;
177
-
178
- var adjList = new Map();
179
-
180
- try {
181
- for (var configuration_4 = _tslib.__values(configuration), configuration_4_1 = configuration_4.next(); !configuration_4_1.done; configuration_4_1 = configuration_4.next()) {
182
- var s = configuration_4_1.value;
183
-
184
- if (!adjList.has(s)) {
185
- adjList.set(s, []);
186
- }
187
-
188
- if (s.parent) {
189
- if (!adjList.has(s.parent)) {
190
- adjList.set(s.parent, []);
191
- }
192
-
193
- adjList.get(s.parent).push(s);
194
- }
195
- }
196
- } catch (e_5_1) {
197
- e_5 = {
198
- error: e_5_1
199
- };
200
- } finally {
201
- try {
202
- if (configuration_4_1 && !configuration_4_1.done && (_a = configuration_4.return)) _a.call(configuration_4);
203
- } finally {
204
- if (e_5) throw e_5.error;
205
- }
206
- }
207
-
208
- return adjList;
209
- }
210
- function getValue(rootNode, configuration) {
211
- var config = getConfiguration([rootNode], configuration);
212
- return getValueFromAdj(rootNode, getAdjList(config));
213
- }
214
- function has(iterable, item) {
215
- if (Array.isArray(iterable)) {
216
- return iterable.some(function (member) {
217
- return member === item;
218
- });
219
- }
220
-
221
- if (iterable instanceof Set) {
222
- return iterable.has(item);
223
- }
224
-
225
- return false; // TODO: fix
226
- }
227
- function nextEvents(configuration) {
228
- return _tslib.__spreadArray([], _tslib.__read(new Set(utils.flatten(_tslib.__spreadArray([], _tslib.__read(configuration.map(function (sn) {
229
- return sn.ownEvents;
230
- })), false)))), false);
231
- }
232
- function isInFinalState(configuration, stateNode) {
233
- if (stateNode.type === 'compound') {
234
- return getChildren(stateNode).some(function (s) {
235
- return s.type === 'final' && has(configuration, s);
236
- });
237
- }
238
-
239
- if (stateNode.type === 'parallel') {
240
- return getChildren(stateNode).every(function (sn) {
241
- return isInFinalState(configuration, sn);
242
- });
243
- }
244
-
245
- return false;
246
- }
247
- function getMeta(configuration) {
248
- if (configuration === void 0) {
249
- configuration = [];
250
- }
251
-
252
- return configuration.reduce(function (acc, stateNode) {
253
- if (stateNode.meta !== undefined) {
254
- acc[stateNode.id] = stateNode.meta;
255
- }
256
-
257
- return acc;
258
- }, {});
259
- }
260
- function getTagsFromConfiguration(configuration) {
261
- return new Set(utils.flatten(configuration.map(function (sn) {
262
- return sn.tags;
263
- })));
264
- }
265
-
266
- exports.getAdjList = getAdjList;
267
- exports.getAllChildren = getAllChildren;
268
- exports.getAllStateNodes = getAllStateNodes;
269
- exports.getChildren = getChildren;
270
- exports.getConfiguration = getConfiguration;
271
- exports.getMeta = getMeta;
272
- exports.getTagsFromConfiguration = getTagsFromConfiguration;
273
- exports.getValue = getValue;
274
- exports.has = has;
275
- exports.isInFinalState = isInFinalState;
276
- exports.isLeafNode = isLeafNode;
277
- exports.nextEvents = nextEvents;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });