xstate 5.19.3 → 5.20.0
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.
- package/README.md +0 -1
- package/actions/dist/xstate-actions.cjs.d.ts +1 -1
- package/actions/dist/xstate-actions.cjs.js +9 -8
- package/actions/dist/xstate-actions.development.cjs.js +9 -8
- package/actions/dist/xstate-actions.development.esm.js +3 -2
- package/actions/dist/xstate-actions.esm.js +3 -2
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/actors/dist/xstate-actors.cjs.d.ts +1 -1
- package/actors/dist/xstate-actors.cjs.js +1 -1
- package/actors/dist/xstate-actors.development.cjs.js +1 -1
- package/actors/dist/xstate-actors.development.esm.js +1 -1
- package/actors/dist/xstate-actors.esm.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dev/dist/xstate-dev.cjs.d.ts +1 -1
- package/dev/dist/xstate-dev.umd.min.js.map +1 -1
- package/dist/StateMachine-1cda96d3.cjs.js +560 -0
- package/dist/StateMachine-38b5bb3f.development.cjs.js +566 -0
- package/dist/StateMachine-b4e94439.development.esm.js +563 -0
- package/dist/StateMachine-c88ea5dd.esm.js +557 -0
- package/dist/assign-6313ccb3.development.esm.js +133 -0
- package/dist/assign-c3259787.esm.js +127 -0
- package/dist/assign-c84786ab.development.cjs.js +135 -0
- package/dist/assign-e9c344ea.cjs.js +129 -0
- package/dist/declarations/src/StateMachine.d.ts +3 -4
- package/dist/declarations/src/graph/TestModel.d.ts +71 -0
- package/dist/declarations/src/graph/adjacency.d.ts +8 -0
- package/dist/declarations/src/graph/graph.d.ts +14 -0
- package/dist/declarations/src/graph/index.d.ts +9 -0
- package/dist/declarations/src/graph/pathFromEvents.d.ts +3 -0
- package/dist/declarations/src/graph/pathGenerators.d.ts +4 -0
- package/dist/declarations/src/graph/shortestPaths.d.ts +3 -0
- package/dist/declarations/src/graph/simplePaths.d.ts +3 -0
- package/dist/declarations/src/graph/types.d.ts +159 -0
- package/dist/declarations/src/types.d.ts +1 -1
- package/dist/{log-12aa30c9.esm.js → log-1c257a58.esm.js} +5 -126
- package/dist/{log-179b5431.cjs.js → log-215998b6.cjs.js} +5 -127
- package/dist/{log-210b28c2.development.cjs.js → log-2c8d7f98.development.cjs.js} +5 -133
- package/dist/{log-b47a3833.development.esm.js → log-ef959da6.development.esm.js} +5 -132
- package/dist/{raise-cbaedcda.cjs.js → raise-5872b9e8.cjs.js} +4 -2
- package/dist/{raise-527db318.development.esm.js → raise-78b8dcb8.development.esm.js} +5 -3
- package/dist/{raise-32fab6fb.development.cjs.js → raise-7a84f9f0.development.cjs.js} +4 -2
- package/dist/{raise-59f80ebb.esm.js → raise-b0a4e862.esm.js} +5 -3
- package/dist/xstate.cjs.d.ts +1 -1
- package/dist/xstate.cjs.js +8 -563
- package/dist/xstate.development.cjs.js +8 -569
- package/dist/xstate.development.esm.js +7 -568
- package/dist/xstate.esm.js +7 -562
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/graph/dist/xstate-graph.cjs.d.mts +2 -0
- package/graph/dist/xstate-graph.cjs.d.ts +2 -0
- package/graph/dist/xstate-graph.cjs.js +901 -0
- package/graph/dist/xstate-graph.cjs.mjs +15 -0
- package/graph/dist/xstate-graph.development.cjs.js +901 -0
- package/graph/dist/xstate-graph.development.cjs.mjs +15 -0
- package/graph/dist/xstate-graph.development.esm.js +885 -0
- package/graph/dist/xstate-graph.esm.js +885 -0
- package/graph/dist/xstate-graph.umd.min.js +2 -0
- package/graph/dist/xstate-graph.umd.min.js.map +1 -0
- package/graph/package.json +8 -0
- package/guards/dist/xstate-guards.cjs.d.ts +1 -1
- package/guards/dist/xstate-guards.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/guards/dist/xstate-guards.esm.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +22 -6
package/dist/xstate.esm.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.esm.js';
|
|
2
|
-
import { t as toArray,
|
|
3
|
-
export {
|
|
4
|
-
import {
|
|
5
|
-
export { S as
|
|
2
|
+
import { t as toArray, c as createActor } from './raise-b0a4e862.esm.js';
|
|
3
|
+
export { A as Actor, d as __unsafe_getAllOwnEventDescriptors, a as and, f as cancel, c as createActor, g as getStateNodes, i as interpret, b as isMachineSnapshot, m as matchesState, n as not, o as or, p as pathToStateValue, r as raise, h as spawnChild, s as stateIn, j as stop, k as stopChild, e as toObserver } from './raise-b0a4e862.esm.js';
|
|
4
|
+
import { S as StateMachine } from './StateMachine-c88ea5dd.esm.js';
|
|
5
|
+
export { S as StateMachine, a as StateNode } from './StateMachine-c88ea5dd.esm.js';
|
|
6
|
+
export { S as SpecialTargets, e as emit, a as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from './log-1c257a58.esm.js';
|
|
7
|
+
export { a as assign } from './assign-c3259787.esm.js';
|
|
6
8
|
import '../dev/dist/xstate-dev.esm.js';
|
|
7
9
|
|
|
8
10
|
/**
|
|
@@ -36,563 +38,6 @@ function assertEvent(event, type) {
|
|
|
36
38
|
}
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
const cache = new WeakMap();
|
|
40
|
-
function memo(object, key, fn) {
|
|
41
|
-
let memoizedData = cache.get(object);
|
|
42
|
-
if (!memoizedData) {
|
|
43
|
-
memoizedData = {
|
|
44
|
-
[key]: fn()
|
|
45
|
-
};
|
|
46
|
-
cache.set(object, memoizedData);
|
|
47
|
-
} else if (!(key in memoizedData)) {
|
|
48
|
-
memoizedData[key] = fn();
|
|
49
|
-
}
|
|
50
|
-
return memoizedData[key];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const EMPTY_OBJECT = {};
|
|
54
|
-
const toSerializableAction = action => {
|
|
55
|
-
if (typeof action === 'string') {
|
|
56
|
-
return {
|
|
57
|
-
type: action
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
if (typeof action === 'function') {
|
|
61
|
-
if ('resolve' in action) {
|
|
62
|
-
return {
|
|
63
|
-
type: action.type
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
return {
|
|
67
|
-
type: action.name
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
return action;
|
|
71
|
-
};
|
|
72
|
-
class StateNode {
|
|
73
|
-
constructor(/** The raw config used to create the machine. */
|
|
74
|
-
config, options) {
|
|
75
|
-
this.config = config;
|
|
76
|
-
/**
|
|
77
|
-
* The relative key of the state node, which represents its location in the
|
|
78
|
-
* overall state value.
|
|
79
|
-
*/
|
|
80
|
-
this.key = void 0;
|
|
81
|
-
/** The unique ID of the state node. */
|
|
82
|
-
this.id = void 0;
|
|
83
|
-
/**
|
|
84
|
-
* The type of this state node:
|
|
85
|
-
*
|
|
86
|
-
* - `'atomic'` - no child state nodes
|
|
87
|
-
* - `'compound'` - nested child state nodes (XOR)
|
|
88
|
-
* - `'parallel'` - orthogonal nested child state nodes (AND)
|
|
89
|
-
* - `'history'` - history state node
|
|
90
|
-
* - `'final'` - final state node
|
|
91
|
-
*/
|
|
92
|
-
this.type = void 0;
|
|
93
|
-
/** The string path from the root machine node to this node. */
|
|
94
|
-
this.path = void 0;
|
|
95
|
-
/** The child state nodes. */
|
|
96
|
-
this.states = void 0;
|
|
97
|
-
/**
|
|
98
|
-
* The type of history on this state node. Can be:
|
|
99
|
-
*
|
|
100
|
-
* - `'shallow'` - recalls only top-level historical state value
|
|
101
|
-
* - `'deep'` - recalls historical state value at all levels
|
|
102
|
-
*/
|
|
103
|
-
this.history = void 0;
|
|
104
|
-
/** The action(s) to be executed upon entering the state node. */
|
|
105
|
-
this.entry = void 0;
|
|
106
|
-
/** The action(s) to be executed upon exiting the state node. */
|
|
107
|
-
this.exit = void 0;
|
|
108
|
-
/** The parent state node. */
|
|
109
|
-
this.parent = void 0;
|
|
110
|
-
/** The root machine node. */
|
|
111
|
-
this.machine = void 0;
|
|
112
|
-
/**
|
|
113
|
-
* The meta data associated with this state node, which will be returned in
|
|
114
|
-
* State instances.
|
|
115
|
-
*/
|
|
116
|
-
this.meta = void 0;
|
|
117
|
-
/**
|
|
118
|
-
* The output data sent with the "xstate.done.state._id_" event if this is a
|
|
119
|
-
* final state node.
|
|
120
|
-
*/
|
|
121
|
-
this.output = void 0;
|
|
122
|
-
/**
|
|
123
|
-
* The order this state node appears. Corresponds to the implicit document
|
|
124
|
-
* order.
|
|
125
|
-
*/
|
|
126
|
-
this.order = -1;
|
|
127
|
-
this.description = void 0;
|
|
128
|
-
this.tags = [];
|
|
129
|
-
this.transitions = void 0;
|
|
130
|
-
this.always = void 0;
|
|
131
|
-
this.parent = options._parent;
|
|
132
|
-
this.key = options._key;
|
|
133
|
-
this.machine = options._machine;
|
|
134
|
-
this.path = this.parent ? this.parent.path.concat(this.key) : [];
|
|
135
|
-
this.id = this.config.id || [this.machine.id, ...this.path].join(STATE_DELIMITER);
|
|
136
|
-
this.type = this.config.type || (this.config.states && Object.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
|
|
137
|
-
this.description = this.config.description;
|
|
138
|
-
this.order = this.machine.idMap.size;
|
|
139
|
-
this.machine.idMap.set(this.id, this);
|
|
140
|
-
this.states = this.config.states ? mapValues(this.config.states, (stateConfig, key) => {
|
|
141
|
-
const stateNode = new StateNode(stateConfig, {
|
|
142
|
-
_parent: this,
|
|
143
|
-
_key: key,
|
|
144
|
-
_machine: this.machine
|
|
145
|
-
});
|
|
146
|
-
return stateNode;
|
|
147
|
-
}) : EMPTY_OBJECT;
|
|
148
|
-
if (this.type === 'compound' && !this.config.initial) {
|
|
149
|
-
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.`);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// History config
|
|
153
|
-
this.history = this.config.history === true ? 'shallow' : this.config.history || false;
|
|
154
|
-
this.entry = toArray(this.config.entry).slice();
|
|
155
|
-
this.exit = toArray(this.config.exit).slice();
|
|
156
|
-
this.meta = this.config.meta;
|
|
157
|
-
this.output = this.type === 'final' || !this.parent ? this.config.output : undefined;
|
|
158
|
-
this.tags = toArray(config.tags).slice();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** @internal */
|
|
162
|
-
_initialize() {
|
|
163
|
-
this.transitions = formatTransitions(this);
|
|
164
|
-
if (this.config.always) {
|
|
165
|
-
this.always = toTransitionConfigArray(this.config.always).map(t => formatTransition(this, NULL_EVENT, t));
|
|
166
|
-
}
|
|
167
|
-
Object.keys(this.states).forEach(key => {
|
|
168
|
-
this.states[key]._initialize();
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** The well-structured state node definition. */
|
|
173
|
-
get definition() {
|
|
174
|
-
return {
|
|
175
|
-
id: this.id,
|
|
176
|
-
key: this.key,
|
|
177
|
-
version: this.machine.version,
|
|
178
|
-
type: this.type,
|
|
179
|
-
initial: this.initial ? {
|
|
180
|
-
target: this.initial.target,
|
|
181
|
-
source: this,
|
|
182
|
-
actions: this.initial.actions.map(toSerializableAction),
|
|
183
|
-
eventType: null,
|
|
184
|
-
reenter: false,
|
|
185
|
-
toJSON: () => ({
|
|
186
|
-
target: this.initial.target.map(t => `#${t.id}`),
|
|
187
|
-
source: `#${this.id}`,
|
|
188
|
-
actions: this.initial.actions.map(toSerializableAction),
|
|
189
|
-
eventType: null
|
|
190
|
-
})
|
|
191
|
-
} : undefined,
|
|
192
|
-
history: this.history,
|
|
193
|
-
states: mapValues(this.states, state => {
|
|
194
|
-
return state.definition;
|
|
195
|
-
}),
|
|
196
|
-
on: this.on,
|
|
197
|
-
transitions: [...this.transitions.values()].flat().map(t => ({
|
|
198
|
-
...t,
|
|
199
|
-
actions: t.actions.map(toSerializableAction)
|
|
200
|
-
})),
|
|
201
|
-
entry: this.entry.map(toSerializableAction),
|
|
202
|
-
exit: this.exit.map(toSerializableAction),
|
|
203
|
-
meta: this.meta,
|
|
204
|
-
order: this.order || -1,
|
|
205
|
-
output: this.output,
|
|
206
|
-
invoke: this.invoke,
|
|
207
|
-
description: this.description,
|
|
208
|
-
tags: this.tags
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/** @internal */
|
|
213
|
-
toJSON() {
|
|
214
|
-
return this.definition;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/** The logic invoked as actors by this state node. */
|
|
218
|
-
get invoke() {
|
|
219
|
-
return memo(this, 'invoke', () => toArray(this.config.invoke).map((invokeConfig, i) => {
|
|
220
|
-
const {
|
|
221
|
-
src,
|
|
222
|
-
systemId
|
|
223
|
-
} = invokeConfig;
|
|
224
|
-
const resolvedId = invokeConfig.id ?? createInvokeId(this.id, i);
|
|
225
|
-
const sourceName = typeof src === 'string' ? src : `xstate.invoke.${createInvokeId(this.id, i)}`;
|
|
226
|
-
return {
|
|
227
|
-
...invokeConfig,
|
|
228
|
-
src: sourceName,
|
|
229
|
-
id: resolvedId,
|
|
230
|
-
systemId: systemId,
|
|
231
|
-
toJSON() {
|
|
232
|
-
const {
|
|
233
|
-
onDone,
|
|
234
|
-
onError,
|
|
235
|
-
...invokeDefValues
|
|
236
|
-
} = invokeConfig;
|
|
237
|
-
return {
|
|
238
|
-
...invokeDefValues,
|
|
239
|
-
type: 'xstate.invoke',
|
|
240
|
-
src: sourceName,
|
|
241
|
-
id: resolvedId
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
}));
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/** The mapping of events to transitions. */
|
|
249
|
-
get on() {
|
|
250
|
-
return memo(this, 'on', () => {
|
|
251
|
-
const transitions = this.transitions;
|
|
252
|
-
return [...transitions].flatMap(([descriptor, t]) => t.map(t => [descriptor, t])).reduce((map, [descriptor, transition]) => {
|
|
253
|
-
map[descriptor] = map[descriptor] || [];
|
|
254
|
-
map[descriptor].push(transition);
|
|
255
|
-
return map;
|
|
256
|
-
}, {});
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
get after() {
|
|
260
|
-
return memo(this, 'delayedTransitions', () => getDelayedTransitions(this));
|
|
261
|
-
}
|
|
262
|
-
get initial() {
|
|
263
|
-
return memo(this, 'initial', () => formatInitialTransition(this, this.config.initial));
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/** @internal */
|
|
267
|
-
next(snapshot, event) {
|
|
268
|
-
const eventType = event.type;
|
|
269
|
-
const actions = [];
|
|
270
|
-
let selectedTransition;
|
|
271
|
-
const candidates = memo(this, `candidates-${eventType}`, () => getCandidates(this, eventType));
|
|
272
|
-
for (const candidate of candidates) {
|
|
273
|
-
const {
|
|
274
|
-
guard
|
|
275
|
-
} = candidate;
|
|
276
|
-
const resolvedContext = snapshot.context;
|
|
277
|
-
let guardPassed = false;
|
|
278
|
-
try {
|
|
279
|
-
guardPassed = !guard || evaluateGuard(guard, resolvedContext, event, snapshot);
|
|
280
|
-
} catch (err) {
|
|
281
|
-
const guardType = typeof guard === 'string' ? guard : typeof guard === 'object' ? guard.type : undefined;
|
|
282
|
-
throw new Error(`Unable to evaluate guard ${guardType ? `'${guardType}' ` : ''}in transition for event '${eventType}' in state node '${this.id}':\n${err.message}`);
|
|
283
|
-
}
|
|
284
|
-
if (guardPassed) {
|
|
285
|
-
actions.push(...candidate.actions);
|
|
286
|
-
selectedTransition = candidate;
|
|
287
|
-
break;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
return selectedTransition ? [selectedTransition] : undefined;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/** All the event types accepted by this state node and its descendants. */
|
|
294
|
-
get events() {
|
|
295
|
-
return memo(this, 'events', () => {
|
|
296
|
-
const {
|
|
297
|
-
states
|
|
298
|
-
} = this;
|
|
299
|
-
const events = new Set(this.ownEvents);
|
|
300
|
-
if (states) {
|
|
301
|
-
for (const stateId of Object.keys(states)) {
|
|
302
|
-
const state = states[stateId];
|
|
303
|
-
if (state.states) {
|
|
304
|
-
for (const event of state.events) {
|
|
305
|
-
events.add(`${event}`);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
return Array.from(events);
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* All the events that have transitions directly from this state node.
|
|
316
|
-
*
|
|
317
|
-
* Excludes any inert events.
|
|
318
|
-
*/
|
|
319
|
-
get ownEvents() {
|
|
320
|
-
const events = new Set([...this.transitions.keys()].filter(descriptor => {
|
|
321
|
-
return this.transitions.get(descriptor).some(transition => !(!transition.target && !transition.actions.length && !transition.reenter));
|
|
322
|
-
}));
|
|
323
|
-
return Array.from(events);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
const STATE_IDENTIFIER = '#';
|
|
328
|
-
class StateMachine {
|
|
329
|
-
constructor(/** The raw config used to create the machine. */
|
|
330
|
-
config, implementations) {
|
|
331
|
-
this.config = config;
|
|
332
|
-
/** The machine's own version. */
|
|
333
|
-
this.version = void 0;
|
|
334
|
-
this.schemas = void 0;
|
|
335
|
-
this.implementations = void 0;
|
|
336
|
-
/** @internal */
|
|
337
|
-
this.__xstatenode = true;
|
|
338
|
-
/** @internal */
|
|
339
|
-
this.idMap = new Map();
|
|
340
|
-
this.root = void 0;
|
|
341
|
-
this.id = void 0;
|
|
342
|
-
this.states = void 0;
|
|
343
|
-
this.events = void 0;
|
|
344
|
-
this.id = config.id || '(machine)';
|
|
345
|
-
this.implementations = {
|
|
346
|
-
actors: implementations?.actors ?? {},
|
|
347
|
-
actions: implementations?.actions ?? {},
|
|
348
|
-
delays: implementations?.delays ?? {},
|
|
349
|
-
guards: implementations?.guards ?? {}
|
|
350
|
-
};
|
|
351
|
-
this.version = this.config.version;
|
|
352
|
-
this.schemas = this.config.schemas;
|
|
353
|
-
this.transition = this.transition.bind(this);
|
|
354
|
-
this.getInitialSnapshot = this.getInitialSnapshot.bind(this);
|
|
355
|
-
this.getPersistedSnapshot = this.getPersistedSnapshot.bind(this);
|
|
356
|
-
this.restoreSnapshot = this.restoreSnapshot.bind(this);
|
|
357
|
-
this.start = this.start.bind(this);
|
|
358
|
-
this.root = new StateNode(config, {
|
|
359
|
-
_key: this.id,
|
|
360
|
-
_machine: this
|
|
361
|
-
});
|
|
362
|
-
this.root._initialize();
|
|
363
|
-
this.states = this.root.states; // TODO: remove!
|
|
364
|
-
this.events = this.root.events;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Clones this state machine with the provided implementations and merges the
|
|
369
|
-
* `context` (if provided).
|
|
370
|
-
*
|
|
371
|
-
* @param implementations Options (`actions`, `guards`, `actors`, `delays`,
|
|
372
|
-
* `context`) to recursively merge with the existing options.
|
|
373
|
-
* @returns A new `StateMachine` instance with the provided implementations.
|
|
374
|
-
*/
|
|
375
|
-
provide(implementations) {
|
|
376
|
-
const {
|
|
377
|
-
actions,
|
|
378
|
-
guards,
|
|
379
|
-
actors,
|
|
380
|
-
delays
|
|
381
|
-
} = this.implementations;
|
|
382
|
-
return new StateMachine(this.config, {
|
|
383
|
-
actions: {
|
|
384
|
-
...actions,
|
|
385
|
-
...implementations.actions
|
|
386
|
-
},
|
|
387
|
-
guards: {
|
|
388
|
-
...guards,
|
|
389
|
-
...implementations.guards
|
|
390
|
-
},
|
|
391
|
-
actors: {
|
|
392
|
-
...actors,
|
|
393
|
-
...implementations.actors
|
|
394
|
-
},
|
|
395
|
-
delays: {
|
|
396
|
-
...delays,
|
|
397
|
-
...implementations.delays
|
|
398
|
-
}
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
resolveState(config) {
|
|
402
|
-
const resolvedStateValue = resolveStateValue(this.root, config.value);
|
|
403
|
-
const nodeSet = getAllStateNodes(getStateNodes(this.root, resolvedStateValue));
|
|
404
|
-
return createMachineSnapshot({
|
|
405
|
-
_nodes: [...nodeSet],
|
|
406
|
-
context: config.context || {},
|
|
407
|
-
children: {},
|
|
408
|
-
status: isInFinalState(nodeSet, this.root) ? 'done' : config.status || 'active',
|
|
409
|
-
output: config.output,
|
|
410
|
-
error: config.error,
|
|
411
|
-
historyValue: config.historyValue
|
|
412
|
-
}, this);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
* Determines the next snapshot given the current `snapshot` and received
|
|
417
|
-
* `event`. Calculates a full macrostep from all microsteps.
|
|
418
|
-
*
|
|
419
|
-
* @param snapshot The current snapshot
|
|
420
|
-
* @param event The received event
|
|
421
|
-
*/
|
|
422
|
-
transition(snapshot, event, actorScope) {
|
|
423
|
-
return macrostep(snapshot, event, actorScope, []).snapshot;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* Determines the next state given the current `state` and `event`. Calculates
|
|
428
|
-
* a microstep.
|
|
429
|
-
*
|
|
430
|
-
* @param state The current state
|
|
431
|
-
* @param event The received event
|
|
432
|
-
*/
|
|
433
|
-
microstep(snapshot, event, actorScope) {
|
|
434
|
-
return macrostep(snapshot, event, actorScope, []).microstates;
|
|
435
|
-
}
|
|
436
|
-
getTransitionData(snapshot, event) {
|
|
437
|
-
return transitionNode(this.root, snapshot.value, snapshot, event) || [];
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
/**
|
|
441
|
-
* The initial state _before_ evaluating any microsteps. This "pre-initial"
|
|
442
|
-
* state is provided to initial actions executed in the initial state.
|
|
443
|
-
*/
|
|
444
|
-
getPreInitialState(actorScope, initEvent, internalQueue) {
|
|
445
|
-
const {
|
|
446
|
-
context
|
|
447
|
-
} = this.config;
|
|
448
|
-
const preInitial = createMachineSnapshot({
|
|
449
|
-
context: typeof context !== 'function' && context ? context : {},
|
|
450
|
-
_nodes: [this.root],
|
|
451
|
-
children: {},
|
|
452
|
-
status: 'active'
|
|
453
|
-
}, this);
|
|
454
|
-
if (typeof context === 'function') {
|
|
455
|
-
const assignment = ({
|
|
456
|
-
spawn,
|
|
457
|
-
event,
|
|
458
|
-
self
|
|
459
|
-
}) => context({
|
|
460
|
-
spawn,
|
|
461
|
-
input: event.input,
|
|
462
|
-
self
|
|
463
|
-
});
|
|
464
|
-
return resolveActionsAndContext(preInitial, initEvent, actorScope, [assign(assignment)], internalQueue, undefined);
|
|
465
|
-
}
|
|
466
|
-
return preInitial;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
/**
|
|
470
|
-
* Returns the initial `State` instance, with reference to `self` as an
|
|
471
|
-
* `ActorRef`.
|
|
472
|
-
*/
|
|
473
|
-
getInitialSnapshot(actorScope, input) {
|
|
474
|
-
const initEvent = createInitEvent(input); // TODO: fix;
|
|
475
|
-
const internalQueue = [];
|
|
476
|
-
const preInitialState = this.getPreInitialState(actorScope, initEvent, internalQueue);
|
|
477
|
-
const nextState = microstep([{
|
|
478
|
-
target: [...getInitialStateNodes(this.root)],
|
|
479
|
-
source: this.root,
|
|
480
|
-
reenter: true,
|
|
481
|
-
actions: [],
|
|
482
|
-
eventType: null,
|
|
483
|
-
toJSON: null // TODO: fix
|
|
484
|
-
}], preInitialState, actorScope, initEvent, true, internalQueue);
|
|
485
|
-
const {
|
|
486
|
-
snapshot: macroState
|
|
487
|
-
} = macrostep(nextState, initEvent, actorScope, internalQueue);
|
|
488
|
-
return macroState;
|
|
489
|
-
}
|
|
490
|
-
start(snapshot) {
|
|
491
|
-
Object.values(snapshot.children).forEach(child => {
|
|
492
|
-
if (child.getSnapshot().status === 'active') {
|
|
493
|
-
child.start();
|
|
494
|
-
}
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
getStateNodeById(stateId) {
|
|
498
|
-
const fullPath = toStatePath(stateId);
|
|
499
|
-
const relativePath = fullPath.slice(1);
|
|
500
|
-
const resolvedStateId = isStateId(fullPath[0]) ? fullPath[0].slice(STATE_IDENTIFIER.length) : fullPath[0];
|
|
501
|
-
const stateNode = this.idMap.get(resolvedStateId);
|
|
502
|
-
if (!stateNode) {
|
|
503
|
-
throw new Error(`Child state node '#${resolvedStateId}' does not exist on machine '${this.id}'`);
|
|
504
|
-
}
|
|
505
|
-
return getStateNodeByPath(stateNode, relativePath);
|
|
506
|
-
}
|
|
507
|
-
get definition() {
|
|
508
|
-
return this.root.definition;
|
|
509
|
-
}
|
|
510
|
-
toJSON() {
|
|
511
|
-
return this.definition;
|
|
512
|
-
}
|
|
513
|
-
getPersistedSnapshot(snapshot, options) {
|
|
514
|
-
return getPersistedSnapshot(snapshot, options);
|
|
515
|
-
}
|
|
516
|
-
restoreSnapshot(snapshot, _actorScope) {
|
|
517
|
-
const children = {};
|
|
518
|
-
const snapshotChildren = snapshot.children;
|
|
519
|
-
Object.keys(snapshotChildren).forEach(actorId => {
|
|
520
|
-
const actorData = snapshotChildren[actorId];
|
|
521
|
-
const childState = actorData.snapshot;
|
|
522
|
-
const src = actorData.src;
|
|
523
|
-
const logic = typeof src === 'string' ? resolveReferencedActor(this, src) : src;
|
|
524
|
-
if (!logic) {
|
|
525
|
-
return;
|
|
526
|
-
}
|
|
527
|
-
const actorRef = createActor(logic, {
|
|
528
|
-
id: actorId,
|
|
529
|
-
parent: _actorScope.self,
|
|
530
|
-
syncSnapshot: actorData.syncSnapshot,
|
|
531
|
-
snapshot: childState,
|
|
532
|
-
src,
|
|
533
|
-
systemId: actorData.systemId
|
|
534
|
-
});
|
|
535
|
-
children[actorId] = actorRef;
|
|
536
|
-
});
|
|
537
|
-
function resolveStateNodeFromIdOrPath(idOrPath, root) {
|
|
538
|
-
try {
|
|
539
|
-
return root.machine.getStateNodeById(idOrPath);
|
|
540
|
-
} catch {
|
|
541
|
-
try {
|
|
542
|
-
return getStateNodeByPath(root, idOrPath.split('.'));
|
|
543
|
-
} catch {
|
|
544
|
-
return idOrPath;
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
function reviveHistoryValue(historyValue, root) {
|
|
549
|
-
if (!historyValue || typeof historyValue !== 'object') return {};
|
|
550
|
-
const revived = {};
|
|
551
|
-
for (const key in historyValue) {
|
|
552
|
-
const arr = historyValue[key];
|
|
553
|
-
revived[key] = arr.map(item => {
|
|
554
|
-
if (item instanceof StateNode) return item;
|
|
555
|
-
if (typeof item === 'object' && 'id' in item && typeof item.id === 'string') {
|
|
556
|
-
const resolved = resolveStateNodeFromIdOrPath(item.id, root);
|
|
557
|
-
if (resolved instanceof StateNode) return resolved;
|
|
558
|
-
}
|
|
559
|
-
return undefined;
|
|
560
|
-
}).filter(item => item !== undefined);
|
|
561
|
-
if (revived[key].length === 0) {
|
|
562
|
-
delete revived[key];
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
return revived;
|
|
566
|
-
}
|
|
567
|
-
const revivedHistoryValue = reviveHistoryValue(snapshot.historyValue, this.root);
|
|
568
|
-
const restoredSnapshot = createMachineSnapshot({
|
|
569
|
-
...snapshot,
|
|
570
|
-
children,
|
|
571
|
-
_nodes: Array.from(getAllStateNodes(getStateNodes(this.root, snapshot.value))),
|
|
572
|
-
historyValue: revivedHistoryValue
|
|
573
|
-
}, this);
|
|
574
|
-
const seen = new Set();
|
|
575
|
-
function reviveContext(contextPart, children) {
|
|
576
|
-
if (seen.has(contextPart)) {
|
|
577
|
-
return;
|
|
578
|
-
}
|
|
579
|
-
seen.add(contextPart);
|
|
580
|
-
for (const key in contextPart) {
|
|
581
|
-
const value = contextPart[key];
|
|
582
|
-
if (value && typeof value === 'object') {
|
|
583
|
-
if ('xstate$$type' in value && value.xstate$$type === $$ACTOR_TYPE) {
|
|
584
|
-
contextPart[key] = children[value.id];
|
|
585
|
-
continue;
|
|
586
|
-
}
|
|
587
|
-
reviveContext(value, children);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
reviveContext(restoredSnapshot.context, children);
|
|
592
|
-
return restoredSnapshot;
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
|
|
596
41
|
/**
|
|
597
42
|
* Creates a state machine (statechart) with the given configuration.
|
|
598
43
|
*
|
|
@@ -961,4 +406,4 @@ function waitFor(actorRef, predicate, options) {
|
|
|
961
406
|
});
|
|
962
407
|
}
|
|
963
408
|
|
|
964
|
-
export { SimulatedClock,
|
|
409
|
+
export { SimulatedClock, assertEvent, createMachine, getInitialSnapshot, getNextSnapshot, initialTransition, setup, toPromise, transition, waitFor };
|