xstate 5.0.0-beta.9 → 5.0.1

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