xstate 5.32.2 → 6.0.0-alpha.10

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