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