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