xstate 5.0.0-alpha.1 → 5.0.0-alpha.3

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