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