xstate 5.0.0-alpha.6 → 5.0.0-beta.11

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