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