xstate 5.0.0-alpha.0 → 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 (83) hide show
  1. package/README.md +23 -13
  2. package/actions/dist/xstate-actions.cjs.dev.js +22 -24
  3. package/actions/dist/xstate-actions.cjs.prod.js +22 -24
  4. package/actions/dist/xstate-actions.esm.js +1 -1
  5. package/actions/dist/xstate-actions.umd.min.js +2 -0
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -0
  7. package/actions/dynamicAction.ts +4 -4
  8. package/actions/package.json +5 -1
  9. package/actors/dist/xstate-actors.cjs.dev.js +12 -13
  10. package/actors/dist/xstate-actors.cjs.prod.js +12 -13
  11. package/actors/dist/xstate-actors.esm.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js +2 -0
  13. package/actors/dist/xstate-actors.umd.min.js.map +1 -0
  14. package/actors/package.json +5 -1
  15. package/dev/dist/xstate-dev.umd.min.js +2 -0
  16. package/dev/dist/xstate-dev.umd.min.js.map +1 -0
  17. package/dev/package.json +5 -1
  18. package/dist/{actions-70094c93.cjs.prod.js → actions-7ea9280f.cjs.prod.js} +3118 -3209
  19. package/dist/{actions-6b86876d.cjs.dev.js → actions-b08d0da8.cjs.dev.js} +3105 -3202
  20. package/dist/{actions-ff512f16.esm.js → actions-b673cb13.esm.js} +3092 -3189
  21. package/dist/declarations/actions/dynamicAction.d.ts +5 -3
  22. package/dist/declarations/src/Machine.d.ts +4 -4
  23. package/dist/declarations/src/Mailbox.d.ts +12 -12
  24. package/dist/declarations/src/SimulatedClock.d.ts +16 -16
  25. package/dist/declarations/src/State.d.ts +97 -104
  26. package/dist/declarations/src/StateMachine.d.ts +120 -112
  27. package/dist/declarations/src/StateNode.d.ts +127 -127
  28. package/dist/declarations/src/actionTypes.d.ts +16 -17
  29. package/dist/declarations/src/actions/assign.d.ts +7 -7
  30. package/dist/declarations/src/actions/cancel.d.ts +10 -10
  31. package/dist/declarations/src/actions/choose.d.ts +3 -3
  32. package/dist/declarations/src/actions/invoke.d.ts +3 -3
  33. package/dist/declarations/src/actions/log.d.ts +11 -11
  34. package/dist/declarations/src/actions/pure.d.ts +3 -3
  35. package/dist/declarations/src/actions/raise.d.ts +9 -9
  36. package/dist/declarations/src/actions/send.d.ts +51 -55
  37. package/dist/declarations/src/actions/stop.d.ts +7 -8
  38. package/dist/declarations/src/actions.d.ts +43 -44
  39. package/dist/declarations/src/actors.d.ts +37 -38
  40. package/dist/declarations/src/constants.d.ts +5 -5
  41. package/dist/declarations/src/dev/index.d.ts +14 -14
  42. package/dist/declarations/src/environment.d.ts +1 -1
  43. package/dist/declarations/src/guards.d.ts +8 -9
  44. package/dist/declarations/src/index.d.ts +44 -47
  45. package/dist/declarations/src/interpreter.d.ts +108 -147
  46. package/dist/declarations/src/mapState.d.ts +3 -3
  47. package/dist/declarations/src/memo.d.ts +2 -1
  48. package/dist/declarations/src/registry.d.ts +8 -8
  49. package/dist/declarations/src/schema.d.ts +2 -2
  50. package/dist/declarations/src/spawn.d.ts +2 -2
  51. package/dist/declarations/src/stateUtils.d.ts +70 -85
  52. package/dist/declarations/src/symbolObservable.d.ts +1 -0
  53. package/dist/declarations/src/typegenTypes.d.ts +132 -132
  54. package/dist/declarations/src/types.d.ts +1001 -1026
  55. package/dist/declarations/src/utils.d.ts +54 -56
  56. package/dist/xstate.cjs.dev.js +856 -80
  57. package/dist/xstate.cjs.prod.js +856 -80
  58. package/dist/xstate.esm.js +815 -35
  59. package/dist/xstate.umd.min.js +2 -0
  60. package/dist/xstate.umd.min.js.map +1 -0
  61. package/guards/dist/xstate-guards.cjs.dev.js +7 -7
  62. package/guards/dist/xstate-guards.cjs.prod.js +7 -7
  63. package/guards/dist/xstate-guards.esm.js +1 -1
  64. package/guards/dist/xstate-guards.umd.min.js +2 -0
  65. package/guards/dist/xstate-guards.umd.min.js.map +1 -0
  66. package/guards/package.json +5 -1
  67. package/package.json +25 -2
  68. package/CHANGELOG.md +0 -1986
  69. package/actions/ExecutableAction.ts +0 -58
  70. package/dist/Machine-9ce74c11.cjs.dev.js +0 -755
  71. package/dist/Machine-dca041dc.cjs.prod.js +0 -749
  72. package/dist/Machine-fb2f47f5.esm.js +0 -751
  73. package/dist/declarations/actions/ExecutableAction.d.ts +0 -23
  74. package/dist/declarations/src/ObservableActorRef.d.ts +0 -25
  75. package/dist/declarations/src/match.d.ts +0 -5
  76. package/dist/declarations/src/model.d.ts +0 -5
  77. package/dist/declarations/src/model.types.d.ts +0 -53
  78. package/model/dist/xstate-model.cjs.d.ts +0 -1
  79. package/model/dist/xstate-model.cjs.dev.js +0 -41
  80. package/model/dist/xstate-model.cjs.js +0 -7
  81. package/model/dist/xstate-model.cjs.prod.js +0 -41
  82. package/model/dist/xstate-model.esm.js +0 -37
  83. package/model/package.json +0 -4
@@ -1,61 +1,842 @@
1
- import { c as createDynamicAction, p as pure$1, t as toArray, m as matchesState, S as State, _ as _createForOfIteratorHelper, a as _slicedToArray, b as _createClass, d as _toConsumableArray, e as _classCallCheck, f as _defineProperty, r as raise, s as send, g as sendParent, h as sendTo, i as sendUpdate, l as log, j as cancel, k as stop, n as assign, o as after, q as done, u as respond, v as forwardTo, w as escalate, x as choose } from './actions-ff512f16.esm.js';
2
- export { G as ActionTypes, I as Interpreter, z as InterpreterStatus, O as ObservableActorRef, H as SpecialTargets, S as State, n as assign, A as doneInvoke, v as forwardTo, B as getStateNodes, y as interpret, m as matchesState, C as pathToStateValue, s as send, g as sendParent, i as sendUpdate, D as toEventObject, E as toObserver, F as toSCXMLEvent } from './actions-ff512f16.esm.js';
1
+ import { c as createDynamicAction, p as pure$1, t as toArray, _ as _createClass, f as formatTransitions, a as toTransitionConfigArray, N as NULL_EVENT, b as formatTransition, m as mapValues, d as memo, e as _createForOfIteratorHelper, g as evaluateGuard, h as _toConsumableArray, i as flatten, j as _classCallCheck, k as _defineProperty, l as toActionObjects, n as createInvokeId, o as toInvokeConfig, q as _objectSpread2, r as invoke, s as _objectWithoutProperties, u as getDelayedTransitions, v as formatInitialTransition, w as getCandidates, x as toInvokeSource, y as isString, z as createSpawner, A as getConfiguration, B as getStateNodes, C as resolveStateValue, D as isInFinalState, S as State, E as toSCXMLEvent, F as isSCXMLErrorEvent, G as macrostep, H as transitionNode, I as _slicedToArray, J as getInitialConfiguration, K as initEvent, L as resolveActionsAndContext, M as microstep, O as isStateId, P as isStateConfig, Q as isFunction, R as STATE_DELIMITER, T as matchesState, U as raise, V as send, W as sendParent, X as sendTo, Y as log, Z as cancel, $ as stop, a0 as assign, a1 as after, a2 as done, a3 as respond, a4 as forwardTo, a5 as escalate, a6 as choose } from './actions-b673cb13.esm.js';
2
+ export { ad as ActionTypes, a8 as Interpreter, a9 as InterpreterStatus, ae as SpecialTargets, S as State, a0 as assign, aa as doneInvoke, a4 as forwardTo, B as getStateNodes, a7 as interpret, T as matchesState, ab as pathToStateValue, V as send, W as sendParent, ac as toObserver, E as toSCXMLEvent } from './actions-b673cb13.esm.js';
3
3
  import './index-20b364c4.esm.js';
4
- export { a as StateMachine, S as StateNode, c as createMachine } from './Machine-fb2f47f5.esm.js';
5
4
 
6
5
  function pure(getActions) {
7
- return createDynamicAction(pure$1, {
8
- get: getActions
9
- }, function (_ref, ctx, _event) {
6
+ return createDynamicAction({
7
+ type: pure$1,
8
+ params: {
9
+ get: getActions
10
+ }
11
+ }, function (_event, _ref) {
10
12
  var _toArray;
11
13
 
12
- var params = _ref.params;
13
- return {
14
+ var state = _ref.state;
15
+ return [state, {
14
16
  type: pure$1,
15
17
  params: {
16
- actions: (_toArray = toArray(params.get(ctx, _event.data))) !== null && _toArray !== void 0 ? _toArray : []
18
+ actions: (_toArray = toArray(getActions(state.context, _event.data))) !== null && _toArray !== void 0 ? _toArray : []
17
19
  }
18
- };
20
+ }];
19
21
  });
20
22
  }
21
23
 
22
- function mapState(stateMap, stateId) {
23
- var foundStateId;
24
+ var _excluded = ["onDone", "onError"];
25
+ var EMPTY_OBJECT = {};
26
+ var StateNode = /*#__PURE__*/function () {
27
+ /**
28
+ * The relative key of the state node, which represents its location in the overall state value.
29
+ */
24
30
 
25
- for (var _i = 0, _Object$keys = Object.keys(stateMap); _i < _Object$keys.length; _i++) {
26
- var mappedStateId = _Object$keys[_i];
31
+ /**
32
+ * The unique ID of the state node.
33
+ */
27
34
 
28
- if (matchesState(mappedStateId, stateId) && (!foundStateId || stateId.length > foundStateId.length)) {
29
- foundStateId = mappedStateId;
35
+ /**
36
+ * The type of this state node:
37
+ *
38
+ * - `'atomic'` - no child state nodes
39
+ * - `'compound'` - nested child state nodes (XOR)
40
+ * - `'parallel'` - orthogonal nested child state nodes (AND)
41
+ * - `'history'` - history state node
42
+ * - `'final'` - final state node
43
+ */
44
+
45
+ /**
46
+ * The string path from the root machine node to this node.
47
+ */
48
+
49
+ /**
50
+ * The child state nodes.
51
+ */
52
+
53
+ /**
54
+ * The type of history on this state node. Can be:
55
+ *
56
+ * - `'shallow'` - recalls only top-level historical state value
57
+ * - `'deep'` - recalls historical state value at all levels
58
+ */
59
+
60
+ /**
61
+ * The action(s) to be executed upon entering the state node.
62
+ */
63
+
64
+ /**
65
+ * The action(s) to be executed upon exiting the state node.
66
+ */
67
+
68
+ /**
69
+ * The parent state node.
70
+ */
71
+
72
+ /**
73
+ * The root machine node.
74
+ */
75
+
76
+ /**
77
+ * The meta data associated with this state node, which will be returned in State instances.
78
+ */
79
+
80
+ /**
81
+ * The data sent with the "done.state._id_" event if this is a final state node.
82
+ */
83
+
84
+ /**
85
+ * The order this state node appears. Corresponds to the implicit SCXML document order.
86
+ */
87
+ function StateNode(
88
+ /**
89
+ * The raw config used to create the machine.
90
+ */
91
+ config, options) {
92
+ var _this = this;
93
+
94
+ _classCallCheck(this, StateNode);
95
+
96
+ this.config = config;
97
+
98
+ _defineProperty(this, "key", void 0);
99
+
100
+ _defineProperty(this, "id", void 0);
101
+
102
+ _defineProperty(this, "type", void 0);
103
+
104
+ _defineProperty(this, "path", void 0);
105
+
106
+ _defineProperty(this, "states", void 0);
107
+
108
+ _defineProperty(this, "history", void 0);
109
+
110
+ _defineProperty(this, "entry", void 0);
111
+
112
+ _defineProperty(this, "exit", void 0);
113
+
114
+ _defineProperty(this, "parent", void 0);
115
+
116
+ _defineProperty(this, "machine", void 0);
117
+
118
+ _defineProperty(this, "meta", void 0);
119
+
120
+ _defineProperty(this, "doneData", void 0);
121
+
122
+ _defineProperty(this, "order", -1);
123
+
124
+ _defineProperty(this, "description", void 0);
125
+
126
+ _defineProperty(this, "tags", []);
127
+
128
+ _defineProperty(this, "transitions", void 0);
129
+
130
+ _defineProperty(this, "always", void 0);
131
+
132
+ this.parent = options._parent;
133
+ this.key = options._key;
134
+ this.machine = options._machine;
135
+ this.path = this.parent ? this.parent.path.concat(this.key) : [];
136
+ this.id = this.config.id || [this.machine.id].concat(_toConsumableArray(this.path)).join(this.machine.delimiter);
137
+ this.type = this.config.type || (this.config.states && Object.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
138
+ this.description = this.config.description;
139
+ this.order = this.machine.idMap.size;
140
+ this.machine.idMap.set(this.id, this);
141
+ this.states = this.config.states ? mapValues(this.config.states, function (stateConfig, key) {
142
+ var stateNode = new StateNode(stateConfig, {
143
+ _parent: _this,
144
+ _key: key,
145
+ _machine: _this.machine
146
+ });
147
+ return stateNode;
148
+ }) : EMPTY_OBJECT;
149
+
150
+ if (this.type === 'compound' && !this.config.initial) {
151
+ throw new Error("No initial state specified for compound state node \"#".concat(this.id, "\". Try adding { initial: \"").concat(Object.keys(this.states)[0], "\" } to the state config."));
152
+ } // History config
153
+
154
+
155
+ this.history = this.config.history === true ? 'shallow' : this.config.history || false;
156
+ this.entry = toActionObjects(this.config.entry);
157
+ this.exit = toActionObjects(this.config.exit);
158
+ this.meta = this.config.meta;
159
+ this.doneData = this.type === 'final' ? this.config.data : undefined;
160
+ this.tags = toArray(config.tags);
161
+ }
162
+
163
+ _createClass(StateNode, [{
164
+ key: "_initialize",
165
+ value: function _initialize() {
166
+ var _this2 = this;
167
+
168
+ this.transitions = formatTransitions(this);
169
+
170
+ if (this.config.always) {
171
+ this.always = toTransitionConfigArray(NULL_EVENT, this.config.always).map(function (t) {
172
+ return formatTransition(_this2, t);
173
+ });
174
+ }
175
+
176
+ Object.keys(this.states).forEach(function (key) {
177
+ _this2.states[key]._initialize();
178
+ });
179
+ }
180
+ /**
181
+ * The well-structured state node definition.
182
+ */
183
+
184
+ }, {
185
+ key: "definition",
186
+ get: function get() {
187
+ var _this3 = this;
188
+
189
+ return {
190
+ id: this.id,
191
+ key: this.key,
192
+ version: this.machine.version,
193
+ type: this.type,
194
+ initial: this.initial ? {
195
+ target: this.initial.target,
196
+ source: this,
197
+ actions: this.initial.actions,
198
+ eventType: null,
199
+ toJSON: function toJSON() {
200
+ return {
201
+ target: _this3.initial.target.map(function (t) {
202
+ return "#".concat(t.id);
203
+ }),
204
+ source: "#".concat(_this3.id),
205
+ actions: _this3.initial.actions,
206
+ eventType: null
207
+ };
208
+ }
209
+ } : undefined,
210
+ history: this.history,
211
+ states: mapValues(this.states, function (state) {
212
+ return state.definition;
213
+ }),
214
+ on: this.on,
215
+ transitions: this.transitions,
216
+ entry: this.entry,
217
+ exit: this.exit,
218
+ meta: this.meta,
219
+ order: this.order || -1,
220
+ data: this.doneData,
221
+ invoke: this.invoke,
222
+ description: this.description,
223
+ tags: this.tags
224
+ };
30
225
  }
226
+ }, {
227
+ key: "toJSON",
228
+ value: function toJSON() {
229
+ return this.definition;
230
+ }
231
+ /**
232
+ * The behaviors invoked as actors by this state node.
233
+ */
234
+
235
+ }, {
236
+ key: "invoke",
237
+ get: function get() {
238
+ var _this4 = this;
239
+
240
+ return memo(this, 'invoke', function () {
241
+ return toArray(_this4.config.invoke).map(function (invocable, i) {
242
+ var generatedId = createInvokeId(_this4.id, i);
243
+ var invokeConfig = toInvokeConfig(invocable, generatedId);
244
+ var resolvedId = invokeConfig.id || generatedId;
245
+ var src = invokeConfig.src;
246
+ var resolvedSrc = toInvokeSource(isString(src) ? src : !('type' in src) ? resolvedId : src);
247
+
248
+ if (!_this4.machine.options.actors[resolvedId] && typeof src !== 'string' && !('type' in src)) {
249
+ _this4.machine.options.actors = _objectSpread2(_objectSpread2({}, _this4.machine.options.actors), {}, _defineProperty({}, resolvedId, typeof src === 'function' ? src : function () {
250
+ return src;
251
+ }));
252
+ }
253
+
254
+ return _objectSpread2(_objectSpread2({
255
+ type: invoke
256
+ }, invokeConfig), {}, {
257
+ src: resolvedSrc,
258
+ id: resolvedId,
259
+ toJSON: function toJSON() {
260
+ var onDone = invokeConfig.onDone,
261
+ onError = invokeConfig.onError,
262
+ invokeDefValues = _objectWithoutProperties(invokeConfig, _excluded);
263
+
264
+ return _objectSpread2(_objectSpread2({}, invokeDefValues), {}, {
265
+ type: invoke,
266
+ src: resolvedSrc,
267
+ id: resolvedId
268
+ });
269
+ }
270
+ });
271
+ });
272
+ });
273
+ }
274
+ /**
275
+ * The mapping of events to transitions.
276
+ */
277
+
278
+ }, {
279
+ key: "on",
280
+ get: function get() {
281
+ var _this5 = this;
282
+
283
+ return memo(this, 'on', function () {
284
+ var transitions = _this5.transitions;
285
+ return transitions.reduce(function (map, transition) {
286
+ map[transition.eventType] = map[transition.eventType] || [];
287
+ map[transition.eventType].push(transition);
288
+ return map;
289
+ }, {});
290
+ });
291
+ }
292
+ }, {
293
+ key: "after",
294
+ get: function get() {
295
+ var _this6 = this;
296
+
297
+ return memo(this, 'delayedTransitions', function () {
298
+ return getDelayedTransitions(_this6);
299
+ });
300
+ }
301
+ }, {
302
+ key: "initial",
303
+ get: function get() {
304
+ var _this7 = this;
305
+
306
+ return memo(this, 'initial', function () {
307
+ return formatInitialTransition(_this7, _this7.config.initial || []);
308
+ });
309
+ }
310
+ /**
311
+ * Returns `true` if this state node explicitly handles the given event.
312
+ *
313
+ * @param event The event in question
314
+ */
315
+
316
+ }, {
317
+ key: "handles",
318
+ value: function handles(event) {
319
+ return this.events.includes(event.type);
320
+ }
321
+ }, {
322
+ key: "next",
323
+ value: function next(state, _event) {
324
+ var _this8 = this;
325
+
326
+ var eventName = _event.name;
327
+ var actions = [];
328
+ var selectedTransition;
329
+ var candidates = memo(this, "candidates-".concat(eventName.toString()), function () {
330
+ return getCandidates(_this8, eventName, _this8.machine.config.scxml // Whether token matching should be used
331
+ );
332
+ });
333
+
334
+ var _iterator = _createForOfIteratorHelper(candidates),
335
+ _step;
336
+
337
+ try {
338
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
339
+ var candidate = _step.value;
340
+ var guard = candidate.guard;
341
+ var resolvedContext = state.context;
342
+ var guardPassed = false;
343
+
344
+ try {
345
+ guardPassed = !guard || evaluateGuard(guard, resolvedContext, _event, state);
346
+ } catch (err) {
347
+ throw new Error("Unable to evaluate guard '".concat(guard.type, "' in transition for event '").concat(eventName, "' in state node '").concat(this.id, "':\n").concat(err.message));
348
+ }
349
+
350
+ if (guardPassed) {
351
+ actions.push.apply(actions, _toConsumableArray(candidate.actions));
352
+ selectedTransition = candidate;
353
+ break;
354
+ }
355
+ }
356
+ } catch (err) {
357
+ _iterator.e(err);
358
+ } finally {
359
+ _iterator.f();
360
+ }
361
+
362
+ return selectedTransition ? [selectedTransition] : undefined;
363
+ }
364
+ /**
365
+ * The target state value of the history state node, if it exists. This represents the
366
+ * default state value to transition to if no history value exists yet.
367
+ */
368
+
369
+ }, {
370
+ key: "target",
371
+ get: function get() {
372
+ if (this.type === 'history') {
373
+ var historyConfig = this.config;
374
+ return historyConfig.target;
375
+ }
376
+
377
+ return undefined;
378
+ }
379
+ /**
380
+ * All the state node IDs of this state node and its descendant state nodes.
381
+ */
382
+
383
+ }, {
384
+ key: "stateIds",
385
+ get: function get() {
386
+ var _this9 = this;
387
+
388
+ var childStateIds = flatten(Object.keys(this.states).map(function (stateKey) {
389
+ return _this9.states[stateKey].stateIds;
390
+ }));
391
+ return [this.id].concat(childStateIds);
392
+ }
393
+ /**
394
+ * All the event types accepted by this state node and its descendants.
395
+ */
396
+
397
+ }, {
398
+ key: "events",
399
+ get: function get() {
400
+ var _this10 = this;
401
+
402
+ return memo(this, 'events', function () {
403
+ var states = _this10.states;
404
+ var events = new Set(_this10.ownEvents);
405
+
406
+ if (states) {
407
+ for (var _i = 0, _Object$keys = Object.keys(states); _i < _Object$keys.length; _i++) {
408
+ var stateId = _Object$keys[_i];
409
+ var state = states[stateId];
410
+
411
+ if (state.states) {
412
+ var _iterator2 = _createForOfIteratorHelper(state.events),
413
+ _step2;
414
+
415
+ try {
416
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
417
+ var event = _step2.value;
418
+ events.add("".concat(event));
419
+ }
420
+ } catch (err) {
421
+ _iterator2.e(err);
422
+ } finally {
423
+ _iterator2.f();
424
+ }
425
+ }
426
+ }
427
+ }
428
+
429
+ return Array.from(events);
430
+ });
431
+ }
432
+ /**
433
+ * All the events that have transitions directly from this state node.
434
+ *
435
+ * Excludes any inert events.
436
+ */
437
+
438
+ }, {
439
+ key: "ownEvents",
440
+ get: function get() {
441
+ var events = new Set(this.transitions.filter(function (transition) {
442
+ return !(!transition.target && !transition.actions.length && transition.internal);
443
+ }).map(function (transition) {
444
+ return transition.eventType;
445
+ }));
446
+ return Array.from(events);
447
+ }
448
+ }]);
449
+
450
+ return StateNode;
451
+ }();
452
+
453
+ var STATE_IDENTIFIER = '#';
454
+
455
+ function createDefaultOptions() {
456
+ return {
457
+ actions: {},
458
+ actors: {},
459
+ delays: {},
460
+ guards: {},
461
+ context: {}
462
+ };
463
+ }
464
+
465
+ function resolveContext(context, partialContext) {
466
+ if (isFunction(partialContext)) {
467
+ return _objectSpread2(_objectSpread2({}, context), partialContext);
31
468
  }
32
469
 
33
- return stateMap[foundStateId];
470
+ return _objectSpread2(_objectSpread2({}, context), partialContext);
34
471
  }
35
472
 
36
- function matchState(state, patterns, defaultValue) {
37
- var resolvedState = State.from(state, state instanceof State ? state.context : undefined);
473
+ var StateMachine = /*#__PURE__*/function () {
474
+ function StateMachine(
475
+ /**
476
+ * The raw config used to create the machine.
477
+ */
478
+ config, options) {
479
+ var _this$config$schema;
480
+
481
+ _classCallCheck(this, StateMachine);
482
+
483
+ this.config = config;
484
+
485
+ _defineProperty(this, "_contextFactory", void 0);
486
+
487
+ _defineProperty(this, "version", void 0);
488
+
489
+ _defineProperty(this, "delimiter", void 0);
490
+
491
+ _defineProperty(this, "options", void 0);
492
+
493
+ _defineProperty(this, "schema", void 0);
494
+
495
+ _defineProperty(this, "__xstatenode", true);
496
+
497
+ _defineProperty(this, "idMap", new Map());
498
+
499
+ _defineProperty(this, "root", void 0);
500
+
501
+ _defineProperty(this, "id", void 0);
502
+
503
+ _defineProperty(this, "states", void 0);
504
+
505
+ _defineProperty(this, "events", void 0);
38
506
 
39
- var _iterator = _createForOfIteratorHelper(patterns),
40
- _step;
507
+ _defineProperty(this, "__TContext", void 0);
41
508
 
42
- try {
43
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
44
- var _step$value = _slicedToArray(_step.value, 2),
45
- stateValue = _step$value[0],
46
- getValue = _step$value[1];
509
+ _defineProperty(this, "__TEvent", void 0);
47
510
 
48
- if (resolvedState.matches(stateValue)) {
49
- return getValue(resolvedState);
511
+ _defineProperty(this, "__TAction", void 0);
512
+
513
+ _defineProperty(this, "__TActorMap", void 0);
514
+
515
+ _defineProperty(this, "__TResolvedTypesMeta", void 0);
516
+
517
+ this.id = config.id || '(machine)';
518
+ this.options = Object.assign(createDefaultOptions(), options);
519
+ this._contextFactory = isFunction(config.context) ? config.context : function (stuff) {
520
+ var partialContext = typeof (options === null || options === void 0 ? void 0 : options.context) === 'function' ? options.context(stuff) : options === null || options === void 0 ? void 0 : options.context;
521
+ return resolveContext(config.context, partialContext);
522
+ }; // TODO: fix types
523
+ // this.context = resolveContext(config.context, options?.context);
524
+
525
+ this.delimiter = this.config.delimiter || STATE_DELIMITER;
526
+ this.version = this.config.version;
527
+ this.schema = (_this$config$schema = this.config.schema) !== null && _this$config$schema !== void 0 ? _this$config$schema : {};
528
+ this.transition = this.transition.bind(this);
529
+ this.root = new StateNode(config, {
530
+ _key: this.id,
531
+ _machine: this
532
+ });
533
+
534
+ this.root._initialize();
535
+
536
+ this.states = this.root.states; // TODO: remove!
537
+
538
+ this.events = this.root.events;
539
+ }
540
+ /**
541
+ * Clones this state machine with the provided implementations
542
+ * and merges the `context` (if provided).
543
+ *
544
+ * @param implementations Options (`actions`, `guards`, `actors`, `delays`, `context`)
545
+ * to recursively merge with the existing options.
546
+ *
547
+ * @returns A new `StateMachine` instance with the provided implementations.
548
+ */
549
+
550
+
551
+ _createClass(StateMachine, [{
552
+ key: "context",
553
+ get: function get() {
554
+ return this.getContextAndActions()[0];
555
+ }
556
+ }, {
557
+ key: "getContextAndActions",
558
+ value: function getContextAndActions() {
559
+ var actions = []; // TODO: merge with this.options.context
560
+
561
+ var context = this._contextFactory({
562
+ spawn: createSpawner(this, null, null, actions) // TODO: fix types
563
+
564
+ });
565
+
566
+ return [context, actions];
567
+ }
568
+ /**
569
+ * The machine's own version.
570
+ */
571
+
572
+ }, {
573
+ key: "provide",
574
+ value: function provide(implementations) {
575
+ var _this$options = this.options,
576
+ actions = _this$options.actions,
577
+ guards = _this$options.guards,
578
+ actors = _this$options.actors,
579
+ delays = _this$options.delays;
580
+ return new StateMachine(this.config, {
581
+ actions: _objectSpread2(_objectSpread2({}, actions), implementations.actions),
582
+ guards: _objectSpread2(_objectSpread2({}, guards), implementations.guards),
583
+ actors: _objectSpread2(_objectSpread2({}, actors), implementations.actors),
584
+ delays: _objectSpread2(_objectSpread2({}, delays), implementations.delays),
585
+ context: implementations.context
586
+ });
587
+ }
588
+ /**
589
+ * Clones this state machine with custom `context`.
590
+ *
591
+ * The `context` provided can be partial `context`, which will be combined with the original `context`.
592
+ *
593
+ * @param context Custom context (will override predefined context, not recursive)
594
+ */
595
+
596
+ }, {
597
+ key: "withContext",
598
+ value: function withContext(context) {
599
+ return this.provide({
600
+ context: context
601
+ });
602
+ }
603
+ /**
604
+ * Resolves the given `state` to a new `State` instance relative to this machine.
605
+ *
606
+ * This ensures that `.nextEvents` represent the correct values.
607
+ *
608
+ * @param state The state to resolve
609
+ */
610
+
611
+ }, {
612
+ key: "resolveState",
613
+ value: function resolveState(state) {
614
+ var configurationSet = getConfiguration(getStateNodes(this.root, state.value));
615
+ var configuration = Array.from(configurationSet);
616
+ return this.createState(_objectSpread2(_objectSpread2({}, state), {}, {
617
+ value: resolveStateValue(this.root, state.value),
618
+ configuration: configuration,
619
+ done: isInFinalState(configuration)
620
+ }));
621
+ }
622
+ }, {
623
+ key: "resolveStateValue",
624
+ value: function resolveStateValue$1(stateValue) {
625
+ var resolvedStateValue = resolveStateValue(this.root, stateValue);
626
+
627
+ var resolvedContext = this.context;
628
+ return this.resolveState(State.from(resolvedStateValue, resolvedContext, this));
629
+ }
630
+ /**
631
+ * Determines the next state given the current `state` and received `event`.
632
+ * Calculates a full macrostep from all microsteps.
633
+ *
634
+ * @param state The current State instance or state value
635
+ * @param event The received event
636
+ */
637
+
638
+ }, {
639
+ key: "transition",
640
+ value: function transition() {
641
+ var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.initialState;
642
+ var event = arguments.length > 1 ? arguments[1] : undefined;
643
+ var actorCtx = arguments.length > 2 ? arguments[2] : undefined;
644
+ var currentState = state instanceof State ? state : this.resolveStateValue(state); // TODO: handle error events in a better way
645
+
646
+ var scxmlEvent = toSCXMLEvent(event);
647
+
648
+ if (isSCXMLErrorEvent(scxmlEvent) && !currentState.nextEvents.some(function (nextEvent) {
649
+ return nextEvent === scxmlEvent.name;
650
+ })) {
651
+ throw scxmlEvent.data.data;
50
652
  }
653
+
654
+ var _macrostep = macrostep(currentState, scxmlEvent, actorCtx),
655
+ nextState = _macrostep.state;
656
+
657
+ return nextState;
658
+ }
659
+ /**
660
+ * Determines the next state given the current `state` and `event`.
661
+ * Calculates a microstep.
662
+ *
663
+ * @param state The current state
664
+ * @param event The received event
665
+ */
666
+
667
+ }, {
668
+ key: "microstep",
669
+ value: function microstep() {
670
+ var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.initialState;
671
+ var event = arguments.length > 1 ? arguments[1] : undefined;
672
+ var actorCtx = arguments.length > 2 ? arguments[2] : undefined;
673
+ var scxmlEvent = toSCXMLEvent(event);
674
+
675
+ var _macrostep2 = macrostep(state, scxmlEvent, actorCtx),
676
+ microstates = _macrostep2.microstates;
677
+
678
+ return microstates;
679
+ }
680
+ }, {
681
+ key: "getTransitionData",
682
+ value: function getTransitionData(state, _event) {
683
+ return transitionNode(this.root, state.value, state, _event) || [];
684
+ }
685
+ /**
686
+ * The initial state _before_ evaluating any microsteps.
687
+ * This "pre-initial" state is provided to initial actions executed in the initial state.
688
+ */
689
+
690
+ }, {
691
+ key: "getPreInitialState",
692
+ value: function getPreInitialState(actorCtx) {
693
+ var _actorCtx$sessionId, _preInitial$actions;
694
+
695
+ var _this$getContextAndAc = this.getContextAndActions(),
696
+ _this$getContextAndAc2 = _slicedToArray(_this$getContextAndAc, 2),
697
+ context = _this$getContextAndAc2[0],
698
+ actions = _this$getContextAndAc2[1];
699
+
700
+ var config = getInitialConfiguration(this.root);
701
+ var preInitial = this.resolveState(this.createState({
702
+ value: {},
703
+ // TODO: this is computed in state constructor
704
+ context: context,
705
+ _event: initEvent,
706
+ _sessionid: (_actorCtx$sessionId = actorCtx === null || actorCtx === void 0 ? void 0 : actorCtx.sessionId) !== null && _actorCtx$sessionId !== void 0 ? _actorCtx$sessionId : undefined,
707
+ actions: [],
708
+ meta: undefined,
709
+ configuration: config,
710
+ transitions: [],
711
+ children: {}
712
+ }));
713
+ preInitial._initial = true;
714
+
715
+ (_preInitial$actions = preInitial.actions).unshift.apply(_preInitial$actions, _toConsumableArray(actions));
716
+
717
+ if (actorCtx) {
718
+ var _resolveActionsAndCon = resolveActionsAndContext(actions, initEvent, preInitial, actorCtx),
719
+ nextState = _resolveActionsAndCon.nextState;
720
+
721
+ preInitial.children = nextState.children;
722
+ preInitial.actions = nextState.actions;
723
+ }
724
+
725
+ return preInitial;
726
+ }
727
+ /**
728
+ * The initial State instance, which includes all actions to be executed from
729
+ * entering the initial state.
730
+ */
731
+
732
+ }, {
733
+ key: "initialState",
734
+ get: function get() {
735
+ return this.getInitialState();
736
+ }
737
+ /**
738
+ * Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
739
+ */
740
+
741
+ }, {
742
+ key: "getInitialState",
743
+ value: function getInitialState(actorCtx) {
744
+ var _nextState$actions;
745
+
746
+ var preInitialState = this.getPreInitialState(actorCtx);
747
+ var nextState = microstep([], preInitialState, actorCtx, initEvent);
748
+
749
+ (_nextState$actions = nextState.actions).unshift.apply(_nextState$actions, _toConsumableArray(preInitialState.actions));
750
+
751
+ var _macrostep3 = macrostep(nextState, initEvent, actorCtx),
752
+ macroState = _macrostep3.state;
753
+
754
+ return macroState;
755
+ }
756
+ }, {
757
+ key: "start",
758
+ value: function start(state, actorCtx) {
759
+ // When starting from a restored state, execute the actions
760
+ state.actions.forEach(function (action) {
761
+ var _action$execute;
762
+
763
+ (_action$execute = action.execute) === null || _action$execute === void 0 ? void 0 : _action$execute.call(action, actorCtx);
764
+ });
765
+ return state;
766
+ }
767
+ }, {
768
+ key: "getStateNodeById",
769
+ value: function getStateNodeById(stateId) {
770
+ var resolvedStateId = isStateId(stateId) ? stateId.slice(STATE_IDENTIFIER.length) : stateId;
771
+ var stateNode = this.idMap.get(resolvedStateId);
772
+
773
+ if (!stateNode) {
774
+ throw new Error("Child state node '#".concat(resolvedStateId, "' does not exist on machine '").concat(this.id, "'"));
775
+ }
776
+
777
+ return stateNode;
778
+ }
779
+ }, {
780
+ key: "definition",
781
+ get: function get() {
782
+ return _objectSpread2({
783
+ context: this.context
784
+ }, this.root.definition);
785
+ }
786
+ }, {
787
+ key: "toJSON",
788
+ value: function toJSON() {
789
+ return this.definition;
790
+ }
791
+ }, {
792
+ key: "createState",
793
+ value: function createState(stateConfig) {
794
+ var state = stateConfig instanceof State ? stateConfig : new State(stateConfig, this);
795
+
796
+ var _resolveActionsAndCon2 = resolveActionsAndContext(state.actions, state._event, state, undefined),
797
+ resolvedState = _resolveActionsAndCon2.nextState;
798
+
799
+ return resolvedState;
800
+ }
801
+ }, {
802
+ key: "getStatus",
803
+ value: function getStatus(state) {
804
+ return state.done ? {
805
+ status: 'done',
806
+ data: state.output
807
+ } : {
808
+ status: 'active'
809
+ };
810
+ }
811
+ }, {
812
+ key: "restoreState",
813
+ value: function restoreState(state, _actorCtx) {
814
+ var restoredState = isStateConfig(state) ? this.resolveState(state) : this.resolveState(State.from(state, this.context, this));
815
+ return restoredState;
816
+ }
817
+ /**@deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
818
+
819
+ }]);
820
+
821
+ return StateMachine;
822
+ }();
823
+
824
+ function createMachine(config, implementations) {
825
+ return new StateMachine(config, implementations);
826
+ }
827
+
828
+ function mapState(stateMap, stateId) {
829
+ var foundStateId;
830
+
831
+ for (var _i = 0, _Object$keys = Object.keys(stateMap); _i < _Object$keys.length; _i++) {
832
+ var mappedStateId = _Object$keys[_i];
833
+
834
+ if (matchesState(mappedStateId, stateId) && (!foundStateId || stateId.length > foundStateId.length)) {
835
+ foundStateId = mappedStateId;
51
836
  }
52
- } catch (err) {
53
- _iterator.e(err);
54
- } finally {
55
- _iterator.f();
56
837
  }
57
838
 
58
- return defaultValue(resolvedState);
839
+ return stateMap[foundStateId];
59
840
  }
60
841
 
61
842
  function createSchema(schema) {
@@ -155,7 +936,6 @@ var actions = {
155
936
  send: send,
156
937
  sendParent: sendParent,
157
938
  sendTo: sendTo,
158
- sendUpdate: sendUpdate,
159
939
  log: log,
160
940
  cancel: cancel,
161
941
  stop: stop,
@@ -169,4 +949,4 @@ var actions = {
169
949
  pure: pure
170
950
  };
171
951
 
172
- export { SimulatedClock, actions, createSchema, mapState, matchState, t };
952
+ export { SimulatedClock, StateMachine, StateNode, actions, createMachine, createSchema, mapState, t };