xstate 5.0.0-alpha.0 → 5.0.0-alpha.2

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