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