xstate 5.0.0-alpha.1 → 5.0.0-alpha.2

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