xstate 6.0.0-alpha.21 → 6.0.0-alpha.22

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 (40) hide show
  1. package/dist/{StateMachine-550ff7f2.development.cjs.js → StateMachine-1888d034.development.cjs.js} +178 -27
  2. package/dist/{StateMachine-1b155670.cjs.js → StateMachine-610fe929.cjs.js} +178 -27
  3. package/dist/{StateMachine-e25dd61f.development.esm.js → StateMachine-9fad7a2b.development.esm.js} +174 -28
  4. package/dist/{StateMachine-3c17fe3e.esm.js → StateMachine-ddb5d007.esm.js} +174 -28
  5. package/dist/declarations/src/State.d.ts +4 -2
  6. package/dist/declarations/src/StateMachine.d.ts +5 -4
  7. package/dist/declarations/src/actions.d.ts +10 -8
  8. package/dist/declarations/src/actors/logic.d.ts +5 -21
  9. package/dist/declarations/src/createActor.d.ts +3 -1
  10. package/dist/declarations/src/fsm.d.ts +3 -1
  11. package/dist/declarations/src/index.d.ts +2 -2
  12. package/dist/declarations/src/stateUtils.d.ts +1 -1
  13. package/dist/declarations/src/system.d.ts +34 -9
  14. package/dist/declarations/src/transitionActions.d.ts +5 -1
  15. package/dist/declarations/src/types.d.ts +70 -51
  16. package/dist/{index-3f87408c.esm.js → index-2fe11873.esm.js} +782 -389
  17. package/dist/{index-ec7c7a2d.development.cjs.js → index-41c0375c.development.cjs.js} +790 -390
  18. package/dist/{index-e694228c.cjs.js → index-434fac23.cjs.js} +789 -389
  19. package/dist/{index-d39fb138.development.esm.js → index-44d8c2d1.development.esm.js} +783 -390
  20. package/dist/xstate-actors.cjs.js +1 -1
  21. package/dist/xstate-actors.development.cjs.js +1 -1
  22. package/dist/xstate-actors.development.esm.js +1 -1
  23. package/dist/xstate-actors.esm.js +1 -1
  24. package/dist/xstate-actors.umd.min.js +1 -1
  25. package/dist/xstate-actors.umd.min.js.map +1 -1
  26. package/dist/xstate-graph.cjs.js +2 -2
  27. package/dist/xstate-graph.development.cjs.js +2 -2
  28. package/dist/xstate-graph.development.esm.js +2 -2
  29. package/dist/xstate-graph.esm.js +2 -2
  30. package/dist/xstate-graph.umd.min.js +1 -1
  31. package/dist/xstate-graph.umd.min.js.map +1 -1
  32. package/dist/xstate.cjs.js +82 -101
  33. package/dist/xstate.cjs.mjs +1 -0
  34. package/dist/xstate.development.cjs.js +82 -101
  35. package/dist/xstate.development.cjs.mjs +1 -0
  36. package/dist/xstate.development.esm.js +80 -100
  37. package/dist/xstate.esm.js +80 -100
  38. package/dist/xstate.umd.min.js +1 -1
  39. package/dist/xstate.umd.min.js.map +1 -1
  40. package/package.json +1 -1
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var dist_xstateActors = require('./index-e694228c.cjs.js');
6
- var StateMachine = require('./StateMachine-1b155670.cjs.js');
5
+ var dist_xstateActors = require('./index-434fac23.cjs.js');
6
+ var StateMachine = require('./StateMachine-610fe929.cjs.js');
7
7
 
8
8
  /**
9
9
  * Asserts that the given event object is of the specified type or types. Throws
@@ -137,6 +137,8 @@ function createSnapshot(value, context, input, machine, stateInput) {
137
137
  context,
138
138
  input,
139
139
  children: {},
140
+ timers: {},
141
+ _nextTimerId: 0,
140
142
  _stateInput: stateInput,
141
143
  machine
142
144
  };
@@ -150,6 +152,8 @@ function cloneSnapshot(snapshot, value, context, stateInput) {
150
152
  context,
151
153
  input: snapshot.input,
152
154
  children: snapshot.children,
155
+ timers: snapshot.timers,
156
+ _nextTimerId: snapshot._nextTimerId,
153
157
  _stateInput: stateInput,
154
158
  machine: snapshot.machine
155
159
  };
@@ -163,6 +167,8 @@ function stopSnapshot(snapshot) {
163
167
  context: snapshot.context,
164
168
  input: undefined,
165
169
  children: snapshot.children,
170
+ timers: snapshot.timers,
171
+ _nextTimerId: snapshot._nextTimerId,
166
172
  _stateInput: snapshot._stateInput,
167
173
  machine: snapshot.machine
168
174
  };
@@ -186,10 +192,14 @@ function resolveSimpleEnqueuedActions(rawActions) {
186
192
  return undefined;
187
193
  }
188
194
  executableActions.push({
195
+ kind: 'action',
189
196
  type: action.action.name || '(anonymous)',
190
197
  params: undefined,
191
198
  args: action.args,
192
- exec: action.args.length ? action.action.bind(null, ...action.args) : action.action
199
+ action: action.action,
200
+ exec() {
201
+ return this.action?.(...this.args);
202
+ }
193
203
  });
194
204
  }
195
205
  return executableActions;
@@ -296,7 +306,32 @@ function createFSM(config) {
296
306
  return [snapshot, []];
297
307
  }
298
308
  if (event.type === dist_xstateActors.XSTATE_STOP) {
299
- return [stopSnapshot(snapshot), emptyExecutableActions];
309
+ const actions = [];
310
+ const enqueue = dist_xstateActors.createTransitionEnqueue(actorScope, actions, []);
311
+ for (const id of Object.keys(snapshot.timers)) {
312
+ enqueue.cancel(id);
313
+ }
314
+ const [withoutTimers, cancelEffects] = dist_xstateActors.resolveActionsWithContext(snapshot, event, actorScope, actions);
315
+ return [stopSnapshot(withoutTimers), cancelEffects];
316
+ }
317
+ if (event.type === dist_xstateActors.XSTATE_TIMER) {
318
+ const timer = snapshot.timers[event.id];
319
+ if (!timer) {
320
+ return [snapshot, emptyExecutableActions];
321
+ }
322
+ const timers = {
323
+ ...snapshot.timers
324
+ };
325
+ delete timers[timer.id];
326
+ const nextSnapshot = {
327
+ ...snapshot,
328
+ timers
329
+ };
330
+ if (timer.type === '@xstate.raise') {
331
+ return transitionCore(nextSnapshot, timer.event, actorScope);
332
+ }
333
+ const target = timer.target === 'self' ? actorScope.self : timer.target;
334
+ return [nextSnapshot, [dist_xstateActors.createSendToEffect(actorScope, target, timer.event)]];
300
335
  }
301
336
  const stateConfig = config.states[snapshot.value];
302
337
  const directTransition = stateConfig?.on?.[event.type];
@@ -392,7 +427,7 @@ function createFSM(config) {
392
427
  };
393
428
  const transition = (...args) => {
394
429
  const [nextSnapshot, effects] = transitionCore(...args);
395
- return [nextSnapshot, [...effects, ...dist_xstateActors.deriveDeferredStarts(effects)]];
430
+ return dist_xstateActors.finalizeTransitionResult(args[2], args[0], [nextSnapshot, [...effects, ...dist_xstateActors.deriveDeferredStarts(effects)]]);
396
431
  };
397
432
  const logic = {
398
433
  id: config.id,
@@ -413,7 +448,7 @@ function createFSM(config) {
413
448
  nextSnapshot = raisedSnapshot;
414
449
  actions.push(...raisedActions);
415
450
  }
416
- return [nextSnapshot, [...actions, ...dist_xstateActors.deriveDeferredStarts(actions)]];
451
+ return dist_xstateActors.finalizeTransitionResult(actorScope, undefined, [nextSnapshot, [...actions, ...dist_xstateActors.deriveDeferredStarts(actions)]]);
417
452
  },
418
453
  getInitialSnapshot: (actorScope, input) => logic.initialTransition(input, actorScope)[0],
419
454
  getPersistedSnapshot: ({
@@ -2182,71 +2217,6 @@ function mergeSetupConfigs(base, extension) {
2182
2217
  };
2183
2218
  }
2184
2219
 
2185
- /** @internal */
2186
- function createInertActorScope(actorLogic) {
2187
- const self = dist_xstateActors.createActor(actorLogic, {
2188
- _inert: true
2189
- });
2190
- const inertActorScope = {
2191
- self: self,
2192
- defer: () => {},
2193
- id: '',
2194
- logger: () => {},
2195
- sessionId: '',
2196
- stopChild: () => {},
2197
- system: self.system,
2198
- emit: () => {},
2199
- actionExecutor: () => {}
2200
- };
2201
- return inertActorScope;
2202
- }
2203
-
2204
- /** @deprecated Use `initialTransition(…)` instead. */
2205
- function getInitialSnapshot(actorLogic, ...[input]) {
2206
- const actorScope = createInertActorScope(actorLogic);
2207
- return actorLogic.initialTransition(input, actorScope)[0];
2208
- }
2209
-
2210
- /**
2211
- * Determines the next snapshot for the given `actorLogic` based on the given
2212
- * `snapshot` and `event`.
2213
- *
2214
- * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
2215
- * used.
2216
- *
2217
- * @deprecated Use `transition(…)` instead.
2218
- * @example
2219
- *
2220
- * ```ts
2221
- * import { getNextSnapshot } from 'xstate';
2222
- * import { trafficLightMachine } from './trafficLightMachine.ts';
2223
- *
2224
- * const nextSnapshot = getNextSnapshot(
2225
- * trafficLightMachine, // actor logic
2226
- * undefined, // snapshot (or initial state if undefined)
2227
- * { type: 'TIMER' }
2228
- * ); // event object
2229
- *
2230
- * console.log(nextSnapshot.value);
2231
- * // => 'yellow'
2232
- *
2233
- * const nextSnapshot2 = getNextSnapshot(
2234
- * trafficLightMachine, // actor logic
2235
- * nextSnapshot, // snapshot
2236
- * { type: 'TIMER' }
2237
- * ); // event object
2238
- *
2239
- * console.log(nextSnapshot2.value);
2240
- * // =>'red'
2241
- * ```
2242
- */
2243
- function getNextSnapshot(actorLogic, snapshot, event) {
2244
- const inertActorScope = createInertActorScope(actorLogic);
2245
- inertActorScope.self._snapshot = snapshot;
2246
- const transitionResult = actorLogic.transition(snapshot, event, inertActorScope);
2247
- return Array.isArray(transitionResult) ? transitionResult[0] : transitionResult;
2248
- }
2249
-
2250
2220
  // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
2251
2221
 
2252
2222
  // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
@@ -2403,24 +2373,9 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2403
2373
  return SpecialTargets;
2404
2374
  }({});
2405
2375
 
2406
- /**
2407
- * A pending effect (delayed event/transition that has not fired yet) captured
2408
- * in a persisted snapshot — a serialized built-in action descriptor plus its
2409
- * runtime progress, with enough information to restore it under any strategy.
2410
- */
2376
+ /** A deterministic delayed-delivery declaration owned by a logic snapshot. */
2411
2377
 
2412
- /**
2413
- * How pending effects (timers) from a persisted snapshot are restored on
2414
- * `actor.start()`:
2415
- *
2416
- * - `'resume'` (default) — the clock was "frozen" while persisted: a 5-minute
2417
- * timer persisted with 1 minute elapsed fires after 4 more minutes.
2418
- * - `'restart'` — timers start over with their full delay.
2419
- * - `'absolute'` — timers honor the original wall-clock expiry (`startedAt +
2420
- * delay`); already-expired timers fire immediately.
2421
- * - A custom function receiving the pending effect and returning the remaining
2422
- * delay in ms.
2423
- */
2378
+ /** The logical input delivered to a timer's source when its runtime delay ends. */
2424
2379
 
2425
2380
  /** @deprecated Use `AnyActor` instead. */
2426
2381
 
@@ -2485,8 +2440,27 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2485
2440
 
2486
2441
  /** @deprecated */
2487
2442
 
2443
+ /** The terminal result published when an actor completes or errors. */
2444
+
2445
+ /** An executable effect that publishes an actor's terminal result. */
2446
+
2488
2447
  /** Mappers for subscribeTo - maps lifecycle events to machine events */
2489
2448
 
2449
+ function attachMicrostepActorRefs(microsteps, actorScope, inputSnapshot) {
2450
+ if (!microsteps.length) {
2451
+ return microsteps;
2452
+ }
2453
+ const result = microsteps.slice();
2454
+ const finalSnapshot = result.at(-1)[0];
2455
+ StateMachine.setInertActorScopeSnapshot(actorScope, finalSnapshot, false);
2456
+ for (const [snapshot] of result) {
2457
+ if (snapshot !== inputSnapshot) {
2458
+ StateMachine.attachSnapshotActorRef(snapshot.machine, actorScope, snapshot);
2459
+ }
2460
+ }
2461
+ return result;
2462
+ }
2463
+
2490
2464
  /**
2491
2465
  * Given actor `logic`, a `snapshot`, and an `event`, returns a tuple of the
2492
2466
  * `nextSnapshot` and `actions` to execute.
@@ -2494,9 +2468,12 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2494
2468
  * This is a pure function that does not execute `actions`.
2495
2469
  */
2496
2470
  function transition(logic, snapshot, event) {
2497
- const actorScope = createInertActorScope(logic);
2498
- const [nextSnapshot, effects] = logic.transition(snapshot, event, actorScope);
2499
- return [nextSnapshot, effects];
2471
+ const actorScope = StateMachine.createInertActorScope(logic, snapshot);
2472
+ StateMachine.setInertActorScopeSnapshot(actorScope, snapshot, false);
2473
+ const [nextSnapshot, effects] = dist_xstateActors.finalizeTransitionResult(actorScope, snapshot, logic.transition(snapshot, event, actorScope));
2474
+ StateMachine.setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
2475
+ const returnedSnapshot = nextSnapshot === snapshot ? nextSnapshot : StateMachine.attachSnapshotActorRef(logic, actorScope, nextSnapshot);
2476
+ return [returnedSnapshot, effects];
2500
2477
  }
2501
2478
 
2502
2479
  /**
@@ -2507,9 +2484,11 @@ function transition(logic, snapshot, event) {
2507
2484
  * This is a pure function that does not execute `actions`.
2508
2485
  */
2509
2486
  function initialTransition(logic, ...[input]) {
2510
- const actorScope = createInertActorScope(logic);
2511
- const [nextSnapshot, executableActions] = logic.initialTransition(input, actorScope);
2512
- return [nextSnapshot, executableActions];
2487
+ const actorScope = StateMachine.createInertActorScope(logic);
2488
+ const [nextSnapshot, executableActions] = dist_xstateActors.finalizeTransitionResult(actorScope, undefined, logic.initialTransition(input, actorScope));
2489
+ StateMachine.setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
2490
+ const returnedSnapshot = StateMachine.attachSnapshotActorRef(logic, actorScope, nextSnapshot);
2491
+ return [returnedSnapshot, executableActions];
2513
2492
  }
2514
2493
 
2515
2494
  /**
@@ -2519,11 +2498,11 @@ function initialTransition(logic, ...[input]) {
2519
2498
  * This is a pure function that does not execute `actions`.
2520
2499
  */
2521
2500
  function getMicrosteps(machine, snapshot, event) {
2522
- const actorScope = createInertActorScope(machine);
2501
+ const actorScope = StateMachine.createInertActorScope(machine, snapshot);
2523
2502
  const {
2524
2503
  microsteps
2525
2504
  } = dist_xstateActors.macrostep(snapshot, event, actorScope, []);
2526
- return microsteps;
2505
+ return attachMicrostepActorRefs(microsteps, actorScope, snapshot);
2527
2506
  }
2528
2507
 
2529
2508
  /**
@@ -2534,15 +2513,16 @@ function getMicrosteps(machine, snapshot, event) {
2534
2513
  * This is a pure function that does not execute `actions`.
2535
2514
  */
2536
2515
  function getInitialMicrosteps(machine, ...[input]) {
2537
- const actorScope = createInertActorScope(machine);
2516
+ const actorScope = StateMachine.createInertActorScope(machine);
2538
2517
  const initEvent = dist_xstateActors.createInitEvent(input);
2539
2518
  const internalQueue = [];
2540
2519
  const preInitialSnapshot = machine._getPreInitialState(actorScope, initEvent);
2520
+ const contextSpawnEffects = Object.values(preInitialSnapshot.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
2541
2521
  const first = dist_xstateActors.initialMicrostep(machine.root, preInitialSnapshot, actorScope, initEvent, internalQueue);
2542
2522
  const {
2543
2523
  microsteps
2544
- } = dist_xstateActors.macrostep(first[0], initEvent, actorScope, internalQueue);
2545
- return [first, ...microsteps];
2524
+ } = dist_xstateActors.macrostep(first[0], initEvent, actorScope, internalQueue, [[first[0], [...contextSpawnEffects, ...first[1]]]]);
2525
+ return attachMicrostepActorRefs(microsteps, actorScope);
2546
2526
  }
2547
2527
 
2548
2528
  /**
@@ -2715,6 +2695,7 @@ exports.createListenerLogic = dist_xstateActors.createListenerLogic;
2715
2695
  exports.createLogic = dist_xstateActors.createLogic;
2716
2696
  exports.createObservableLogic = dist_xstateActors.createObservableLogic;
2717
2697
  exports.createSubscriptionLogic = dist_xstateActors.createSubscriptionLogic;
2698
+ exports.executeEffects = dist_xstateActors.executeEffects;
2718
2699
  exports.getStateNodes = dist_xstateActors.getStateNodes;
2719
2700
  exports.isBuiltInExecutableAction = dist_xstateActors.isBuiltInExecutableAction;
2720
2701
  exports.isMachineSnapshot = dist_xstateActors.isMachineSnapshot;
@@ -2725,6 +2706,8 @@ exports.subscriptionLogic = dist_xstateActors.subscriptionLogic;
2725
2706
  exports.toObserver = dist_xstateActors.toObserver;
2726
2707
  exports.StateMachine = StateMachine.StateMachine;
2727
2708
  exports.StateNode = StateMachine.StateNode;
2709
+ exports.getInitialSnapshot = StateMachine.getInitialSnapshot;
2710
+ exports.getNextSnapshot = StateMachine.getNextSnapshot;
2728
2711
  exports.SimulatedClock = SimulatedClock;
2729
2712
  exports.SpecialTargets = SpecialTargets;
2730
2713
  exports.assertEvent = assertEvent;
@@ -2734,9 +2717,7 @@ exports.createMachineFromConfig = createMachineFromConfig;
2734
2717
  exports.createStateConfig = createStateConfig;
2735
2718
  exports.createSystem = createSystem;
2736
2719
  exports.getInitialMicrosteps = getInitialMicrosteps;
2737
- exports.getInitialSnapshot = getInitialSnapshot;
2738
2720
  exports.getMicrosteps = getMicrosteps;
2739
- exports.getNextSnapshot = getNextSnapshot;
2740
2721
  exports.getNextTransitions = getNextTransitions;
2741
2722
  exports.initialTransition = initialTransition;
2742
2723
  exports.isTypeSchema = isTypeSchema;
@@ -22,6 +22,7 @@ export {
22
22
  createStateConfig,
23
23
  createSubscriptionLogic,
24
24
  createSystem,
25
+ executeEffects,
25
26
  getInitialMicrosteps,
26
27
  getInitialSnapshot,
27
28
  getMicrosteps,
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var dist_xstateActors = require('./index-ec7c7a2d.development.cjs.js');
6
- var StateMachine = require('./StateMachine-550ff7f2.development.cjs.js');
5
+ var dist_xstateActors = require('./index-41c0375c.development.cjs.js');
6
+ var StateMachine = require('./StateMachine-1888d034.development.cjs.js');
7
7
 
8
8
  /**
9
9
  * Asserts that the given event object is of the specified type or types. Throws
@@ -137,6 +137,8 @@ function createSnapshot(value, context, input, machine, stateInput) {
137
137
  context,
138
138
  input,
139
139
  children: {},
140
+ timers: {},
141
+ _nextTimerId: 0,
140
142
  _stateInput: stateInput,
141
143
  machine
142
144
  };
@@ -150,6 +152,8 @@ function cloneSnapshot(snapshot, value, context, stateInput) {
150
152
  context,
151
153
  input: snapshot.input,
152
154
  children: snapshot.children,
155
+ timers: snapshot.timers,
156
+ _nextTimerId: snapshot._nextTimerId,
153
157
  _stateInput: stateInput,
154
158
  machine: snapshot.machine
155
159
  };
@@ -163,6 +167,8 @@ function stopSnapshot(snapshot) {
163
167
  context: snapshot.context,
164
168
  input: undefined,
165
169
  children: snapshot.children,
170
+ timers: snapshot.timers,
171
+ _nextTimerId: snapshot._nextTimerId,
166
172
  _stateInput: snapshot._stateInput,
167
173
  machine: snapshot.machine
168
174
  };
@@ -186,10 +192,14 @@ function resolveSimpleEnqueuedActions(rawActions) {
186
192
  return undefined;
187
193
  }
188
194
  executableActions.push({
195
+ kind: 'action',
189
196
  type: action.action.name || '(anonymous)',
190
197
  params: undefined,
191
198
  args: action.args,
192
- exec: action.args.length ? action.action.bind(null, ...action.args) : action.action
199
+ action: action.action,
200
+ exec() {
201
+ return this.action?.(...this.args);
202
+ }
193
203
  });
194
204
  }
195
205
  return executableActions;
@@ -296,7 +306,32 @@ function createFSM(config) {
296
306
  return [snapshot, []];
297
307
  }
298
308
  if (event.type === dist_xstateActors.XSTATE_STOP) {
299
- return [stopSnapshot(snapshot), emptyExecutableActions];
309
+ const actions = [];
310
+ const enqueue = dist_xstateActors.createTransitionEnqueue(actorScope, actions, []);
311
+ for (const id of Object.keys(snapshot.timers)) {
312
+ enqueue.cancel(id);
313
+ }
314
+ const [withoutTimers, cancelEffects] = dist_xstateActors.resolveActionsWithContext(snapshot, event, actorScope, actions);
315
+ return [stopSnapshot(withoutTimers), cancelEffects];
316
+ }
317
+ if (event.type === dist_xstateActors.XSTATE_TIMER) {
318
+ const timer = snapshot.timers[event.id];
319
+ if (!timer) {
320
+ return [snapshot, emptyExecutableActions];
321
+ }
322
+ const timers = {
323
+ ...snapshot.timers
324
+ };
325
+ delete timers[timer.id];
326
+ const nextSnapshot = {
327
+ ...snapshot,
328
+ timers
329
+ };
330
+ if (timer.type === '@xstate.raise') {
331
+ return transitionCore(nextSnapshot, timer.event, actorScope);
332
+ }
333
+ const target = timer.target === 'self' ? actorScope.self : timer.target;
334
+ return [nextSnapshot, [dist_xstateActors.createSendToEffect(actorScope, target, timer.event)]];
300
335
  }
301
336
  const stateConfig = config.states[snapshot.value];
302
337
  const directTransition = stateConfig?.on?.[event.type];
@@ -392,7 +427,7 @@ function createFSM(config) {
392
427
  };
393
428
  const transition = (...args) => {
394
429
  const [nextSnapshot, effects] = transitionCore(...args);
395
- return [nextSnapshot, [...effects, ...dist_xstateActors.deriveDeferredStarts(effects)]];
430
+ return dist_xstateActors.finalizeTransitionResult(args[2], args[0], [nextSnapshot, [...effects, ...dist_xstateActors.deriveDeferredStarts(effects)]]);
396
431
  };
397
432
  const logic = {
398
433
  id: config.id,
@@ -413,7 +448,7 @@ function createFSM(config) {
413
448
  nextSnapshot = raisedSnapshot;
414
449
  actions.push(...raisedActions);
415
450
  }
416
- return [nextSnapshot, [...actions, ...dist_xstateActors.deriveDeferredStarts(actions)]];
451
+ return dist_xstateActors.finalizeTransitionResult(actorScope, undefined, [nextSnapshot, [...actions, ...dist_xstateActors.deriveDeferredStarts(actions)]]);
417
452
  },
418
453
  getInitialSnapshot: (actorScope, input) => logic.initialTransition(input, actorScope)[0],
419
454
  getPersistedSnapshot: ({
@@ -2182,71 +2217,6 @@ function mergeSetupConfigs(base, extension) {
2182
2217
  };
2183
2218
  }
2184
2219
 
2185
- /** @internal */
2186
- function createInertActorScope(actorLogic) {
2187
- const self = dist_xstateActors.createActor(actorLogic, {
2188
- _inert: true
2189
- });
2190
- const inertActorScope = {
2191
- self: self,
2192
- defer: () => {},
2193
- id: '',
2194
- logger: () => {},
2195
- sessionId: '',
2196
- stopChild: () => {},
2197
- system: self.system,
2198
- emit: () => {},
2199
- actionExecutor: () => {}
2200
- };
2201
- return inertActorScope;
2202
- }
2203
-
2204
- /** @deprecated Use `initialTransition(…)` instead. */
2205
- function getInitialSnapshot(actorLogic, ...[input]) {
2206
- const actorScope = createInertActorScope(actorLogic);
2207
- return actorLogic.initialTransition(input, actorScope)[0];
2208
- }
2209
-
2210
- /**
2211
- * Determines the next snapshot for the given `actorLogic` based on the given
2212
- * `snapshot` and `event`.
2213
- *
2214
- * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
2215
- * used.
2216
- *
2217
- * @deprecated Use `transition(…)` instead.
2218
- * @example
2219
- *
2220
- * ```ts
2221
- * import { getNextSnapshot } from 'xstate';
2222
- * import { trafficLightMachine } from './trafficLightMachine.ts';
2223
- *
2224
- * const nextSnapshot = getNextSnapshot(
2225
- * trafficLightMachine, // actor logic
2226
- * undefined, // snapshot (or initial state if undefined)
2227
- * { type: 'TIMER' }
2228
- * ); // event object
2229
- *
2230
- * console.log(nextSnapshot.value);
2231
- * // => 'yellow'
2232
- *
2233
- * const nextSnapshot2 = getNextSnapshot(
2234
- * trafficLightMachine, // actor logic
2235
- * nextSnapshot, // snapshot
2236
- * { type: 'TIMER' }
2237
- * ); // event object
2238
- *
2239
- * console.log(nextSnapshot2.value);
2240
- * // =>'red'
2241
- * ```
2242
- */
2243
- function getNextSnapshot(actorLogic, snapshot, event) {
2244
- const inertActorScope = createInertActorScope(actorLogic);
2245
- inertActorScope.self._snapshot = snapshot;
2246
- const transitionResult = actorLogic.transition(snapshot, event, inertActorScope);
2247
- return Array.isArray(transitionResult) ? transitionResult[0] : transitionResult;
2248
- }
2249
-
2250
2220
  // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
2251
2221
 
2252
2222
  // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
@@ -2403,24 +2373,9 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2403
2373
  return SpecialTargets;
2404
2374
  }({});
2405
2375
 
2406
- /**
2407
- * A pending effect (delayed event/transition that has not fired yet) captured
2408
- * in a persisted snapshot — a serialized built-in action descriptor plus its
2409
- * runtime progress, with enough information to restore it under any strategy.
2410
- */
2376
+ /** A deterministic delayed-delivery declaration owned by a logic snapshot. */
2411
2377
 
2412
- /**
2413
- * How pending effects (timers) from a persisted snapshot are restored on
2414
- * `actor.start()`:
2415
- *
2416
- * - `'resume'` (default) — the clock was "frozen" while persisted: a 5-minute
2417
- * timer persisted with 1 minute elapsed fires after 4 more minutes.
2418
- * - `'restart'` — timers start over with their full delay.
2419
- * - `'absolute'` — timers honor the original wall-clock expiry (`startedAt +
2420
- * delay`); already-expired timers fire immediately.
2421
- * - A custom function receiving the pending effect and returning the remaining
2422
- * delay in ms.
2423
- */
2378
+ /** The logical input delivered to a timer's source when its runtime delay ends. */
2424
2379
 
2425
2380
  /** @deprecated Use `AnyActor` instead. */
2426
2381
 
@@ -2485,8 +2440,27 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2485
2440
 
2486
2441
  /** @deprecated */
2487
2442
 
2443
+ /** The terminal result published when an actor completes or errors. */
2444
+
2445
+ /** An executable effect that publishes an actor's terminal result. */
2446
+
2488
2447
  /** Mappers for subscribeTo - maps lifecycle events to machine events */
2489
2448
 
2449
+ function attachMicrostepActorRefs(microsteps, actorScope, inputSnapshot) {
2450
+ if (!microsteps.length) {
2451
+ return microsteps;
2452
+ }
2453
+ const result = microsteps.slice();
2454
+ const finalSnapshot = result.at(-1)[0];
2455
+ StateMachine.setInertActorScopeSnapshot(actorScope, finalSnapshot, false);
2456
+ for (const [snapshot] of result) {
2457
+ if (snapshot !== inputSnapshot) {
2458
+ StateMachine.attachSnapshotActorRef(snapshot.machine, actorScope, snapshot);
2459
+ }
2460
+ }
2461
+ return result;
2462
+ }
2463
+
2490
2464
  /**
2491
2465
  * Given actor `logic`, a `snapshot`, and an `event`, returns a tuple of the
2492
2466
  * `nextSnapshot` and `actions` to execute.
@@ -2494,9 +2468,12 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2494
2468
  * This is a pure function that does not execute `actions`.
2495
2469
  */
2496
2470
  function transition(logic, snapshot, event) {
2497
- const actorScope = createInertActorScope(logic);
2498
- const [nextSnapshot, effects] = logic.transition(snapshot, event, actorScope);
2499
- return [nextSnapshot, effects];
2471
+ const actorScope = StateMachine.createInertActorScope(logic, snapshot);
2472
+ StateMachine.setInertActorScopeSnapshot(actorScope, snapshot, false);
2473
+ const [nextSnapshot, effects] = dist_xstateActors.finalizeTransitionResult(actorScope, snapshot, logic.transition(snapshot, event, actorScope));
2474
+ StateMachine.setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
2475
+ const returnedSnapshot = nextSnapshot === snapshot ? nextSnapshot : StateMachine.attachSnapshotActorRef(logic, actorScope, nextSnapshot);
2476
+ return [returnedSnapshot, effects];
2500
2477
  }
2501
2478
 
2502
2479
  /**
@@ -2507,9 +2484,11 @@ function transition(logic, snapshot, event) {
2507
2484
  * This is a pure function that does not execute `actions`.
2508
2485
  */
2509
2486
  function initialTransition(logic, ...[input]) {
2510
- const actorScope = createInertActorScope(logic);
2511
- const [nextSnapshot, executableActions] = logic.initialTransition(input, actorScope);
2512
- return [nextSnapshot, executableActions];
2487
+ const actorScope = StateMachine.createInertActorScope(logic);
2488
+ const [nextSnapshot, executableActions] = dist_xstateActors.finalizeTransitionResult(actorScope, undefined, logic.initialTransition(input, actorScope));
2489
+ StateMachine.setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
2490
+ const returnedSnapshot = StateMachine.attachSnapshotActorRef(logic, actorScope, nextSnapshot);
2491
+ return [returnedSnapshot, executableActions];
2513
2492
  }
2514
2493
 
2515
2494
  /**
@@ -2519,11 +2498,11 @@ function initialTransition(logic, ...[input]) {
2519
2498
  * This is a pure function that does not execute `actions`.
2520
2499
  */
2521
2500
  function getMicrosteps(machine, snapshot, event) {
2522
- const actorScope = createInertActorScope(machine);
2501
+ const actorScope = StateMachine.createInertActorScope(machine, snapshot);
2523
2502
  const {
2524
2503
  microsteps
2525
2504
  } = dist_xstateActors.macrostep(snapshot, event, actorScope, []);
2526
- return microsteps;
2505
+ return attachMicrostepActorRefs(microsteps, actorScope, snapshot);
2527
2506
  }
2528
2507
 
2529
2508
  /**
@@ -2534,15 +2513,16 @@ function getMicrosteps(machine, snapshot, event) {
2534
2513
  * This is a pure function that does not execute `actions`.
2535
2514
  */
2536
2515
  function getInitialMicrosteps(machine, ...[input]) {
2537
- const actorScope = createInertActorScope(machine);
2516
+ const actorScope = StateMachine.createInertActorScope(machine);
2538
2517
  const initEvent = dist_xstateActors.createInitEvent(input);
2539
2518
  const internalQueue = [];
2540
2519
  const preInitialSnapshot = machine._getPreInitialState(actorScope, initEvent);
2520
+ const contextSpawnEffects = Object.values(preInitialSnapshot.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
2541
2521
  const first = dist_xstateActors.initialMicrostep(machine.root, preInitialSnapshot, actorScope, initEvent, internalQueue);
2542
2522
  const {
2543
2523
  microsteps
2544
- } = dist_xstateActors.macrostep(first[0], initEvent, actorScope, internalQueue);
2545
- return [first, ...microsteps];
2524
+ } = dist_xstateActors.macrostep(first[0], initEvent, actorScope, internalQueue, [[first[0], [...contextSpawnEffects, ...first[1]]]]);
2525
+ return attachMicrostepActorRefs(microsteps, actorScope);
2546
2526
  }
2547
2527
 
2548
2528
  /**
@@ -2718,6 +2698,7 @@ exports.createListenerLogic = dist_xstateActors.createListenerLogic;
2718
2698
  exports.createLogic = dist_xstateActors.createLogic;
2719
2699
  exports.createObservableLogic = dist_xstateActors.createObservableLogic;
2720
2700
  exports.createSubscriptionLogic = dist_xstateActors.createSubscriptionLogic;
2701
+ exports.executeEffects = dist_xstateActors.executeEffects;
2721
2702
  exports.getStateNodes = dist_xstateActors.getStateNodes;
2722
2703
  exports.isBuiltInExecutableAction = dist_xstateActors.isBuiltInExecutableAction;
2723
2704
  exports.isMachineSnapshot = dist_xstateActors.isMachineSnapshot;
@@ -2728,6 +2709,8 @@ exports.subscriptionLogic = dist_xstateActors.subscriptionLogic;
2728
2709
  exports.toObserver = dist_xstateActors.toObserver;
2729
2710
  exports.StateMachine = StateMachine.StateMachine;
2730
2711
  exports.StateNode = StateMachine.StateNode;
2712
+ exports.getInitialSnapshot = StateMachine.getInitialSnapshot;
2713
+ exports.getNextSnapshot = StateMachine.getNextSnapshot;
2731
2714
  exports.SimulatedClock = SimulatedClock;
2732
2715
  exports.SpecialTargets = SpecialTargets;
2733
2716
  exports.assertEvent = assertEvent;
@@ -2737,9 +2720,7 @@ exports.createMachineFromConfig = createMachineFromConfig;
2737
2720
  exports.createStateConfig = createStateConfig;
2738
2721
  exports.createSystem = createSystem;
2739
2722
  exports.getInitialMicrosteps = getInitialMicrosteps;
2740
- exports.getInitialSnapshot = getInitialSnapshot;
2741
2723
  exports.getMicrosteps = getMicrosteps;
2742
- exports.getNextSnapshot = getNextSnapshot;
2743
2724
  exports.getNextTransitions = getNextTransitions;
2744
2725
  exports.initialTransition = initialTransition;
2745
2726
  exports.isTypeSchema = isTypeSchema;
@@ -22,6 +22,7 @@ export {
22
22
  createStateConfig,
23
23
  createSubscriptionLogic,
24
24
  createSystem,
25
+ executeEffects,
25
26
  getInitialMicrosteps,
26
27
  getInitialSnapshot,
27
28
  getMicrosteps,