xstate 5.32.1 → 6.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +7 -8
  2. package/dist/StateMachine-2dcdb1f5.esm.js +720 -0
  3. package/dist/StateMachine-2ea0a7fa.development.esm.js +737 -0
  4. package/dist/StateMachine-64813f46.cjs.js +723 -0
  5. package/dist/StateMachine-8d22a79c.development.cjs.js +740 -0
  6. package/dist/declarations/src/State.d.ts +11 -13
  7. package/dist/declarations/src/StateMachine.d.ts +27 -21
  8. package/dist/declarations/src/StateNode.d.ts +18 -54
  9. package/dist/declarations/src/actions.d.ts +14 -9
  10. package/dist/declarations/src/actors/callback.d.ts +42 -16
  11. package/dist/declarations/src/actors/index.d.ts +9 -6
  12. package/dist/declarations/src/actors/listener.d.ts +18 -0
  13. package/dist/declarations/src/actors/logic.d.ts +105 -0
  14. package/dist/declarations/src/actors/observable.d.ts +72 -27
  15. package/dist/declarations/src/actors/promise.d.ts +133 -48
  16. package/dist/declarations/src/actors/subscription.d.ts +22 -0
  17. package/dist/declarations/src/atom.d.ts +81 -0
  18. package/dist/declarations/src/createActor.d.ts +31 -33
  19. package/dist/declarations/src/createMachine.d.ts +35 -7
  20. package/dist/declarations/src/createMachineFromConfig.d.ts +176 -0
  21. package/dist/declarations/src/graph/graph.d.ts +4 -2
  22. package/dist/declarations/src/index.d.ts +12 -8
  23. package/dist/declarations/src/inspection.d.ts +84 -30
  24. package/dist/declarations/src/mapState.d.ts +4 -2
  25. package/dist/declarations/src/schema.types.d.ts +90 -0
  26. package/dist/declarations/src/serialize.d.ts +44 -0
  27. package/dist/declarations/src/setup.d.ts +251 -135
  28. package/dist/declarations/src/spawn.d.ts +6 -31
  29. package/dist/declarations/src/stateUtils.d.ts +21 -19
  30. package/dist/declarations/src/system.d.ts +8 -7
  31. package/dist/declarations/src/transition.d.ts +5 -5
  32. package/dist/declarations/src/types.d.ts +405 -405
  33. package/dist/declarations/src/types.v6.d.ts +392 -0
  34. package/dist/declarations/src/utils.d.ts +4 -3
  35. package/dist/index-93edd3bd.esm.js +4909 -0
  36. package/dist/index-9cb3b3a0.cjs.js +4968 -0
  37. package/dist/index-e61170ba.development.esm.js +4948 -0
  38. package/dist/index-f6ef20d1.development.cjs.js +5007 -0
  39. package/dist/xstate-actors.cjs.js +12 -838
  40. package/dist/xstate-actors.cjs.mjs +10 -5
  41. package/dist/xstate-actors.development.cjs.js +12 -838
  42. package/dist/xstate-actors.development.cjs.mjs +10 -5
  43. package/dist/xstate-actors.development.esm.js +1 -836
  44. package/dist/xstate-actors.esm.js +1 -836
  45. package/dist/xstate-actors.umd.min.js +1 -1
  46. package/dist/xstate-actors.umd.min.js.map +1 -1
  47. package/dist/xstate-graph.cjs.js +18 -29
  48. package/dist/xstate-graph.development.cjs.js +18 -29
  49. package/dist/xstate-graph.development.esm.js +15 -26
  50. package/dist/xstate-graph.esm.js +15 -26
  51. package/dist/xstate-graph.umd.min.js +1 -1
  52. package/dist/xstate-graph.umd.min.js.map +1 -1
  53. package/dist/xstate.cjs.js +1316 -112
  54. package/dist/xstate.cjs.mjs +22 -22
  55. package/dist/xstate.development.cjs.js +1316 -112
  56. package/dist/xstate.development.cjs.mjs +22 -22
  57. package/dist/xstate.development.esm.js +1278 -76
  58. package/dist/xstate.esm.js +1278 -76
  59. package/dist/xstate.umd.min.js +1 -1
  60. package/dist/xstate.umd.min.js.map +1 -1
  61. package/package.json +3 -47
  62. package/dist/StateMachine-45e79c35.esm.js +0 -560
  63. package/dist/StateMachine-be2e51a3.development.cjs.js +0 -569
  64. package/dist/StateMachine-e0178d48.development.esm.js +0 -566
  65. package/dist/StateMachine-e6732977.cjs.js +0 -563
  66. package/dist/assign-2aa58daa.esm.js +0 -127
  67. package/dist/assign-927227d6.development.cjs.js +0 -135
  68. package/dist/assign-e8f2bd75.cjs.js +0 -129
  69. package/dist/assign-f338cee3.development.esm.js +0 -133
  70. package/dist/declarations/src/actions/assign.d.ts +0 -45
  71. package/dist/declarations/src/actions/cancel.d.ts +0 -39
  72. package/dist/declarations/src/actions/emit.d.ts +0 -44
  73. package/dist/declarations/src/actions/enqueueActions.d.ts +0 -57
  74. package/dist/declarations/src/actions/log.d.ts +0 -16
  75. package/dist/declarations/src/actions/raise.d.ts +0 -21
  76. package/dist/declarations/src/actions/send.d.ts +0 -42
  77. package/dist/declarations/src/actions/spawnChild.d.ts +0 -34
  78. package/dist/declarations/src/actions/stopChild.d.ts +0 -19
  79. package/dist/declarations/src/actors/transition.d.ts +0 -143
  80. package/dist/declarations/src/dev/index.d.ts +0 -14
  81. package/dist/declarations/src/guards.d.ts +0 -128
  82. package/dist/log-410e5e55.development.esm.js +0 -429
  83. package/dist/log-7776fcf2.cjs.js +0 -409
  84. package/dist/log-c2e11c01.development.cjs.js +0 -437
  85. package/dist/log-fa75ec2d.esm.js +0 -401
  86. package/dist/raise-74097812.development.esm.js +0 -2823
  87. package/dist/raise-7c948725.cjs.js +0 -2816
  88. package/dist/raise-964cd4e9.development.cjs.js +0 -2878
  89. package/dist/raise-e974d1c9.esm.js +0 -2761
  90. package/dist/xstate-actions.cjs.d.mts +0 -2
  91. package/dist/xstate-actions.cjs.d.ts +0 -2
  92. package/dist/xstate-actions.cjs.js +0 -23
  93. package/dist/xstate-actions.cjs.mjs +0 -14
  94. package/dist/xstate-actions.development.cjs.js +0 -23
  95. package/dist/xstate-actions.development.cjs.mjs +0 -14
  96. package/dist/xstate-actions.development.esm.js +0 -4
  97. package/dist/xstate-actions.esm.js +0 -4
  98. package/dist/xstate-actions.umd.min.js +0 -2
  99. package/dist/xstate-actions.umd.min.js.map +0 -1
  100. package/dist/xstate-dev.cjs.d.mts +0 -2
  101. package/dist/xstate-dev.cjs.d.ts +0 -2
  102. package/dist/xstate-dev.cjs.js +0 -48
  103. package/dist/xstate-dev.cjs.mjs +0 -5
  104. package/dist/xstate-dev.development.cjs.js +0 -51
  105. package/dist/xstate-dev.development.cjs.mjs +0 -5
  106. package/dist/xstate-dev.development.esm.js +0 -45
  107. package/dist/xstate-dev.esm.js +0 -42
  108. package/dist/xstate-dev.umd.min.js +0 -2
  109. package/dist/xstate-dev.umd.min.js.map +0 -1
  110. package/dist/xstate-guards.cjs.d.mts +0 -2
  111. package/dist/xstate-guards.cjs.d.ts +0 -2
  112. package/dist/xstate-guards.cjs.js +0 -14
  113. package/dist/xstate-guards.cjs.mjs +0 -7
  114. package/dist/xstate-guards.development.cjs.js +0 -14
  115. package/dist/xstate-guards.development.cjs.mjs +0 -7
  116. package/dist/xstate-guards.development.esm.js +0 -2
  117. package/dist/xstate-guards.esm.js +0 -2
  118. package/dist/xstate-guards.umd.min.js +0 -2
  119. package/dist/xstate-guards.umd.min.js.map +0 -1
@@ -1,569 +0,0 @@
1
- 'use strict';
2
-
3
- var dist_xstateGuards = require('./raise-964cd4e9.development.cjs.js');
4
- var assign = require('./assign-927227d6.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(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 ? 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 = dist_xstateGuards.toArray(this.config.entry).slice();
122
- this.exit = 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 = dist_xstateGuards.toArray(config.tags).slice();
126
- }
127
-
128
- /** @internal */
129
- _initialize() {
130
- this.transitions = dist_xstateGuards.formatTransitions(this);
131
- if (this.config.always) {
132
- this.always = dist_xstateGuards.toTransitionConfigArray(this.config.always).map(t => dist_xstateGuards.formatTransition(this, 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: 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', () => dist_xstateGuards.toArray(this.config.invoke).map((invokeConfig, i) => {
187
- const {
188
- src,
189
- systemId
190
- } = invokeConfig;
191
- const resolvedId = invokeConfig.id ?? dist_xstateGuards.createInvokeId(this.id, i);
192
- const sourceName = typeof src === 'string' ? src : `xstate.invoke.${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', () => dist_xstateGuards.getDelayedTransitions(this));
228
- }
229
- get initial() {
230
- return memo(this, 'initial', () => 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}`, () => 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 || 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 keys = Object.keys(Object.fromEntries(this.transitions));
288
- const events = new Set(keys.filter(descriptor => {
289
- return this.transitions.get(descriptor).some(transition => !(!transition.target && !transition.actions.length && !transition.reenter));
290
- }));
291
- return Array.from(events);
292
- }
293
- }
294
-
295
- const STATE_IDENTIFIER = '#';
296
- class StateMachine {
297
- constructor(/** The raw config used to create the machine. */
298
- config, implementations) {
299
- this.config = config;
300
- /** The machine's own version. */
301
- this.version = void 0;
302
- this.schemas = void 0;
303
- this.implementations = void 0;
304
- /** Runtime options for machine execution. */
305
- this.options = void 0;
306
- /** @internal */
307
- this.__xstatenode = true;
308
- /** @internal */
309
- this.idMap = new Map();
310
- this.root = void 0;
311
- this.id = void 0;
312
- this.states = void 0;
313
- this.events = void 0;
314
- this.id = config.id || '(machine)';
315
- this.implementations = {
316
- actors: implementations?.actors ?? {},
317
- actions: implementations?.actions ?? {},
318
- delays: implementations?.delays ?? {},
319
- guards: implementations?.guards ?? {}
320
- };
321
- this.version = this.config.version;
322
- this.schemas = this.config.schemas;
323
- this.options = {
324
- maxIterations: Infinity,
325
- ...this.config.options
326
- };
327
- this.transition = this.transition.bind(this);
328
- this.getInitialSnapshot = this.getInitialSnapshot.bind(this);
329
- this.getPersistedSnapshot = this.getPersistedSnapshot.bind(this);
330
- this.restoreSnapshot = this.restoreSnapshot.bind(this);
331
- this.start = this.start.bind(this);
332
- this.root = new StateNode(config, {
333
- _key: this.id,
334
- _machine: this
335
- });
336
- this.root._initialize();
337
- dist_xstateGuards.formatRouteTransitions(this.root);
338
- this.states = this.root.states; // TODO: remove!
339
- this.events = this.root.events;
340
- if (!('output' in this.root) && Object.values(this.states).some(state => state.type === 'final' && 'output' in state)) {
341
- console.warn('Missing `machine.output` declaration (top-level final state with output detected)');
342
- }
343
- }
344
-
345
- /**
346
- * Clones this state machine with the provided implementations.
347
- *
348
- * @param implementations Options (`actions`, `guards`, `actors`, `delays`) to
349
- * recursively merge with the existing options.
350
- * @returns A new `StateMachine` instance with the provided implementations.
351
- */
352
- provide(implementations) {
353
- const {
354
- actions,
355
- guards,
356
- actors,
357
- delays
358
- } = this.implementations;
359
- return new StateMachine(this.config, {
360
- actions: {
361
- ...actions,
362
- ...implementations.actions
363
- },
364
- guards: {
365
- ...guards,
366
- ...implementations.guards
367
- },
368
- actors: {
369
- ...actors,
370
- ...implementations.actors
371
- },
372
- delays: {
373
- ...delays,
374
- ...implementations.delays
375
- }
376
- });
377
- }
378
- resolveState(config) {
379
- const resolvedStateValue = dist_xstateGuards.resolveStateValue(this.root, config.value);
380
- const nodeSet = dist_xstateGuards.getAllStateNodes(dist_xstateGuards.getStateNodes(this.root, resolvedStateValue));
381
- return dist_xstateGuards.createMachineSnapshot({
382
- _nodes: [...nodeSet],
383
- context: config.context || {},
384
- children: {},
385
- status: dist_xstateGuards.isInFinalState(nodeSet, this.root) ? 'done' : config.status || 'active',
386
- output: config.output,
387
- error: config.error,
388
- historyValue: config.historyValue
389
- }, this);
390
- }
391
-
392
- /**
393
- * Determines the next snapshot given the current `snapshot` and received
394
- * `event`. Calculates a full macrostep from all microsteps.
395
- *
396
- * @param snapshot The current snapshot
397
- * @param event The received event
398
- */
399
- transition(snapshot, event, actorScope) {
400
- return dist_xstateGuards.macrostep(snapshot, event, actorScope, []).snapshot;
401
- }
402
-
403
- /**
404
- * Determines the next state given the current `state` and `event`. Calculates
405
- * a microstep.
406
- *
407
- * @param state The current state
408
- * @param event The received event
409
- */
410
- microstep(snapshot, event, actorScope) {
411
- return dist_xstateGuards.macrostep(snapshot, event, actorScope, []).microsteps.map(([s]) => s);
412
- }
413
- getTransitionData(snapshot, event) {
414
- return dist_xstateGuards.transitionNode(this.root, snapshot.value, snapshot, event) || [];
415
- }
416
-
417
- /**
418
- * The initial state _before_ evaluating any microsteps. This "pre-initial"
419
- * state is provided to initial actions executed in the initial state.
420
- *
421
- * @internal
422
- */
423
- _getPreInitialState(actorScope, initEvent, internalQueue) {
424
- const {
425
- context
426
- } = this.config;
427
- const preInitial = dist_xstateGuards.createMachineSnapshot({
428
- context: typeof context !== 'function' && context ? context : {},
429
- _nodes: [this.root],
430
- children: {},
431
- status: 'active'
432
- }, this);
433
- if (typeof context === 'function') {
434
- const assignment = ({
435
- spawn,
436
- event,
437
- self
438
- }) => context({
439
- spawn,
440
- input: event.input,
441
- self
442
- });
443
- return dist_xstateGuards.resolveActionsAndContext(preInitial, initEvent, actorScope, [assign.assign(assignment)], internalQueue, undefined);
444
- }
445
- return preInitial;
446
- }
447
-
448
- /**
449
- * Returns the initial `State` instance, with reference to `self` as an
450
- * `ActorRef`.
451
- */
452
- getInitialSnapshot(actorScope, input) {
453
- const initEvent = dist_xstateGuards.createInitEvent(input); // TODO: fix;
454
- const internalQueue = [];
455
- const preInitialState = this._getPreInitialState(actorScope, initEvent, internalQueue);
456
- const [nextState] = dist_xstateGuards.initialMicrostep(this.root, preInitialState, actorScope, initEvent, internalQueue);
457
- const {
458
- snapshot: macroState
459
- } = dist_xstateGuards.macrostep(nextState, initEvent, actorScope, internalQueue);
460
- return macroState;
461
- }
462
- start(snapshot) {
463
- Object.values(snapshot.children).forEach(child => {
464
- if (child.getSnapshot().status === 'active') {
465
- child.start();
466
- }
467
- });
468
- }
469
- getStateNodeById(stateId) {
470
- const fullPath = dist_xstateGuards.toStatePath(stateId);
471
- const relativePath = fullPath.slice(1);
472
- const resolvedStateId = dist_xstateGuards.isStateId(fullPath[0]) ? fullPath[0].slice(STATE_IDENTIFIER.length) : fullPath[0];
473
- const stateNode = this.idMap.get(resolvedStateId);
474
- if (!stateNode) {
475
- throw new Error(`Child state node '#${resolvedStateId}' does not exist on machine '${this.id}'`);
476
- }
477
- return dist_xstateGuards.getStateNodeByPath(stateNode, relativePath);
478
- }
479
- get definition() {
480
- return this.root.definition;
481
- }
482
- toJSON() {
483
- return this.definition;
484
- }
485
- getPersistedSnapshot(snapshot, options) {
486
- return dist_xstateGuards.getPersistedSnapshot(snapshot, options);
487
- }
488
- restoreSnapshot(snapshot, _actorScope) {
489
- const children = {};
490
- const snapshotChildren = snapshot.children;
491
- Object.keys(snapshotChildren).forEach(actorId => {
492
- const actorData = snapshotChildren[actorId];
493
- const childState = actorData.snapshot;
494
- const src = actorData.src;
495
- const logic = typeof src === 'string' ? dist_xstateGuards.resolveReferencedActor(this, src) : src;
496
- if (!logic) {
497
- return;
498
- }
499
- const actorRef = dist_xstateGuards.createActor(logic, {
500
- id: actorId,
501
- parent: _actorScope.self,
502
- syncSnapshot: actorData.syncSnapshot,
503
- snapshot: childState,
504
- src,
505
- systemId: actorData.systemId
506
- });
507
- children[actorId] = actorRef;
508
- });
509
- function resolveHistoryReferencedState(root, referenced) {
510
- if (referenced instanceof StateNode) {
511
- return referenced;
512
- }
513
- try {
514
- return root.machine.getStateNodeById(referenced.id);
515
- } catch {
516
- {
517
- console.warn(`Could not resolve StateNode for id: ${referenced.id}`);
518
- }
519
- }
520
- }
521
- function reviveHistoryValue(root, historyValue) {
522
- if (!historyValue || typeof historyValue !== 'object') {
523
- return {};
524
- }
525
- const revived = {};
526
- for (const key in historyValue) {
527
- const arr = historyValue[key];
528
- for (const item of arr) {
529
- const resolved = resolveHistoryReferencedState(root, item);
530
- if (!resolved) {
531
- continue;
532
- }
533
- revived[key] ??= [];
534
- revived[key].push(resolved);
535
- }
536
- }
537
- return revived;
538
- }
539
- const revivedHistoryValue = reviveHistoryValue(this.root, snapshot.historyValue);
540
- const restoredSnapshot = dist_xstateGuards.createMachineSnapshot({
541
- ...snapshot,
542
- children,
543
- _nodes: Array.from(dist_xstateGuards.getAllStateNodes(dist_xstateGuards.getStateNodes(this.root, snapshot.value))),
544
- historyValue: revivedHistoryValue
545
- }, this);
546
- const seen = new Set();
547
- function reviveContext(contextPart, children) {
548
- if (seen.has(contextPart)) {
549
- return;
550
- }
551
- seen.add(contextPart);
552
- for (const key in contextPart) {
553
- const value = contextPart[key];
554
- if (value && typeof value === 'object') {
555
- if ('xstate$$type' in value && value.xstate$$type === dist_xstateGuards.$$ACTOR_TYPE) {
556
- contextPart[key] = children[value.id];
557
- continue;
558
- }
559
- reviveContext(value, children);
560
- }
561
- }
562
- }
563
- reviveContext(restoredSnapshot.context, children);
564
- return restoredSnapshot;
565
- }
566
- }
567
-
568
- exports.StateMachine = StateMachine;
569
- exports.StateNode = StateNode;