xstate 6.0.0-alpha.21 → 6.0.0-alpha.23

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 (41) hide show
  1. package/dist/{StateMachine-3c17fe3e.esm.js → StateMachine-022864fd.esm.js} +183 -64
  2. package/dist/{StateMachine-550ff7f2.development.cjs.js → StateMachine-53114dce.development.cjs.js} +187 -63
  3. package/dist/{StateMachine-1b155670.cjs.js → StateMachine-aad57180.cjs.js} +187 -63
  4. package/dist/{StateMachine-e25dd61f.development.esm.js → StateMachine-d2247245.development.esm.js} +183 -64
  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/spawn.d.ts +3 -3
  13. package/dist/declarations/src/stateUtils.d.ts +1 -1
  14. package/dist/declarations/src/system.d.ts +34 -9
  15. package/dist/declarations/src/transitionActions.d.ts +5 -1
  16. package/dist/declarations/src/types.d.ts +70 -51
  17. package/dist/{index-3f87408c.esm.js → index-3609e096.esm.js} +782 -389
  18. package/dist/{index-ec7c7a2d.development.cjs.js → index-41c0375c.development.cjs.js} +790 -390
  19. package/dist/{index-e694228c.cjs.js → index-434fac23.cjs.js} +789 -389
  20. package/dist/{index-d39fb138.development.esm.js → index-f5407323.development.esm.js} +783 -390
  21. package/dist/xstate-actors.cjs.js +1 -1
  22. package/dist/xstate-actors.development.cjs.js +1 -1
  23. package/dist/xstate-actors.development.esm.js +1 -1
  24. package/dist/xstate-actors.esm.js +1 -1
  25. package/dist/xstate-actors.umd.min.js +1 -1
  26. package/dist/xstate-actors.umd.min.js.map +1 -1
  27. package/dist/xstate-graph.cjs.js +2 -2
  28. package/dist/xstate-graph.development.cjs.js +2 -2
  29. package/dist/xstate-graph.development.esm.js +2 -2
  30. package/dist/xstate-graph.esm.js +2 -2
  31. package/dist/xstate-graph.umd.min.js +1 -1
  32. package/dist/xstate-graph.umd.min.js.map +1 -1
  33. package/dist/xstate.cjs.js +82 -103
  34. package/dist/xstate.cjs.mjs +1 -0
  35. package/dist/xstate.development.cjs.js +82 -103
  36. package/dist/xstate.development.cjs.mjs +1 -0
  37. package/dist/xstate.development.esm.js +80 -102
  38. package/dist/xstate.esm.js +80 -102
  39. package/dist/xstate.umd.min.js +1 -1
  40. package/dist/xstate.umd.min.js.map +1 -1
  41. 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-aad57180.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
 
@@ -2466,8 +2421,6 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2466
2421
 
2467
2422
  /** The concrete actor instance type produced from actor logic. */
2468
2423
 
2469
- // TODO: in v6, this should only accept AnyActorLogic, like ActorRefFromLogic
2470
-
2471
2424
  /**
2472
2425
  * Represents logic which can be used by an actor.
2473
2426
  *
@@ -2485,8 +2438,27 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2485
2438
 
2486
2439
  /** @deprecated */
2487
2440
 
2441
+ /** The terminal result published when an actor completes or errors. */
2442
+
2443
+ /** An executable effect that publishes an actor's terminal result. */
2444
+
2488
2445
  /** Mappers for subscribeTo - maps lifecycle events to machine events */
2489
2446
 
2447
+ function attachMicrostepActorRefs(microsteps, actorScope, inputSnapshot) {
2448
+ if (!microsteps.length) {
2449
+ return microsteps;
2450
+ }
2451
+ const result = microsteps.slice();
2452
+ const finalSnapshot = result.at(-1)[0];
2453
+ StateMachine.setInertActorScopeSnapshot(actorScope, finalSnapshot, false);
2454
+ for (const [snapshot] of result) {
2455
+ if (snapshot !== inputSnapshot) {
2456
+ StateMachine.attachSnapshotActorRef(snapshot.machine, actorScope, snapshot);
2457
+ }
2458
+ }
2459
+ return result;
2460
+ }
2461
+
2490
2462
  /**
2491
2463
  * Given actor `logic`, a `snapshot`, and an `event`, returns a tuple of the
2492
2464
  * `nextSnapshot` and `actions` to execute.
@@ -2494,9 +2466,12 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2494
2466
  * This is a pure function that does not execute `actions`.
2495
2467
  */
2496
2468
  function transition(logic, snapshot, event) {
2497
- const actorScope = createInertActorScope(logic);
2498
- const [nextSnapshot, effects] = logic.transition(snapshot, event, actorScope);
2499
- return [nextSnapshot, effects];
2469
+ const actorScope = StateMachine.createInertActorScope(logic, snapshot);
2470
+ StateMachine.setInertActorScopeSnapshot(actorScope, snapshot, false);
2471
+ const [nextSnapshot, effects] = dist_xstateActors.finalizeTransitionResult(actorScope, snapshot, logic.transition(snapshot, event, actorScope));
2472
+ StateMachine.setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
2473
+ const returnedSnapshot = nextSnapshot === snapshot ? nextSnapshot : StateMachine.attachSnapshotActorRef(logic, actorScope, nextSnapshot);
2474
+ return [returnedSnapshot, effects];
2500
2475
  }
2501
2476
 
2502
2477
  /**
@@ -2507,9 +2482,11 @@ function transition(logic, snapshot, event) {
2507
2482
  * This is a pure function that does not execute `actions`.
2508
2483
  */
2509
2484
  function initialTransition(logic, ...[input]) {
2510
- const actorScope = createInertActorScope(logic);
2511
- const [nextSnapshot, executableActions] = logic.initialTransition(input, actorScope);
2512
- return [nextSnapshot, executableActions];
2485
+ const actorScope = StateMachine.createInertActorScope(logic);
2486
+ const [nextSnapshot, executableActions] = dist_xstateActors.finalizeTransitionResult(actorScope, undefined, logic.initialTransition(input, actorScope));
2487
+ StateMachine.setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
2488
+ const returnedSnapshot = StateMachine.attachSnapshotActorRef(logic, actorScope, nextSnapshot);
2489
+ return [returnedSnapshot, executableActions];
2513
2490
  }
2514
2491
 
2515
2492
  /**
@@ -2519,11 +2496,11 @@ function initialTransition(logic, ...[input]) {
2519
2496
  * This is a pure function that does not execute `actions`.
2520
2497
  */
2521
2498
  function getMicrosteps(machine, snapshot, event) {
2522
- const actorScope = createInertActorScope(machine);
2499
+ const actorScope = StateMachine.createInertActorScope(machine, snapshot);
2523
2500
  const {
2524
2501
  microsteps
2525
2502
  } = dist_xstateActors.macrostep(snapshot, event, actorScope, []);
2526
- return microsteps;
2503
+ return attachMicrostepActorRefs(microsteps, actorScope, snapshot);
2527
2504
  }
2528
2505
 
2529
2506
  /**
@@ -2534,15 +2511,16 @@ function getMicrosteps(machine, snapshot, event) {
2534
2511
  * This is a pure function that does not execute `actions`.
2535
2512
  */
2536
2513
  function getInitialMicrosteps(machine, ...[input]) {
2537
- const actorScope = createInertActorScope(machine);
2514
+ const actorScope = StateMachine.createInertActorScope(machine);
2538
2515
  const initEvent = dist_xstateActors.createInitEvent(input);
2539
2516
  const internalQueue = [];
2540
2517
  const preInitialSnapshot = machine._getPreInitialState(actorScope, initEvent);
2518
+ const contextSpawnEffects = Object.values(preInitialSnapshot.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
2541
2519
  const first = dist_xstateActors.initialMicrostep(machine.root, preInitialSnapshot, actorScope, initEvent, internalQueue);
2542
2520
  const {
2543
2521
  microsteps
2544
- } = dist_xstateActors.macrostep(first[0], initEvent, actorScope, internalQueue);
2545
- return [first, ...microsteps];
2522
+ } = dist_xstateActors.macrostep(first[0], initEvent, actorScope, internalQueue, [[first[0], [...contextSpawnEffects, ...first[1]]]]);
2523
+ return attachMicrostepActorRefs(microsteps, actorScope);
2546
2524
  }
2547
2525
 
2548
2526
  /**
@@ -2715,6 +2693,7 @@ exports.createListenerLogic = dist_xstateActors.createListenerLogic;
2715
2693
  exports.createLogic = dist_xstateActors.createLogic;
2716
2694
  exports.createObservableLogic = dist_xstateActors.createObservableLogic;
2717
2695
  exports.createSubscriptionLogic = dist_xstateActors.createSubscriptionLogic;
2696
+ exports.executeEffects = dist_xstateActors.executeEffects;
2718
2697
  exports.getStateNodes = dist_xstateActors.getStateNodes;
2719
2698
  exports.isBuiltInExecutableAction = dist_xstateActors.isBuiltInExecutableAction;
2720
2699
  exports.isMachineSnapshot = dist_xstateActors.isMachineSnapshot;
@@ -2725,6 +2704,8 @@ exports.subscriptionLogic = dist_xstateActors.subscriptionLogic;
2725
2704
  exports.toObserver = dist_xstateActors.toObserver;
2726
2705
  exports.StateMachine = StateMachine.StateMachine;
2727
2706
  exports.StateNode = StateMachine.StateNode;
2707
+ exports.getInitialSnapshot = StateMachine.getInitialSnapshot;
2708
+ exports.getNextSnapshot = StateMachine.getNextSnapshot;
2728
2709
  exports.SimulatedClock = SimulatedClock;
2729
2710
  exports.SpecialTargets = SpecialTargets;
2730
2711
  exports.assertEvent = assertEvent;
@@ -2734,9 +2715,7 @@ exports.createMachineFromConfig = createMachineFromConfig;
2734
2715
  exports.createStateConfig = createStateConfig;
2735
2716
  exports.createSystem = createSystem;
2736
2717
  exports.getInitialMicrosteps = getInitialMicrosteps;
2737
- exports.getInitialSnapshot = getInitialSnapshot;
2738
2718
  exports.getMicrosteps = getMicrosteps;
2739
- exports.getNextSnapshot = getNextSnapshot;
2740
2719
  exports.getNextTransitions = getNextTransitions;
2741
2720
  exports.initialTransition = initialTransition;
2742
2721
  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-53114dce.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
 
@@ -2466,8 +2421,6 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2466
2421
 
2467
2422
  /** The concrete actor instance type produced from actor logic. */
2468
2423
 
2469
- // TODO: in v6, this should only accept AnyActorLogic, like ActorRefFromLogic
2470
-
2471
2424
  /**
2472
2425
  * Represents logic which can be used by an actor.
2473
2426
  *
@@ -2485,8 +2438,27 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2485
2438
 
2486
2439
  /** @deprecated */
2487
2440
 
2441
+ /** The terminal result published when an actor completes or errors. */
2442
+
2443
+ /** An executable effect that publishes an actor's terminal result. */
2444
+
2488
2445
  /** Mappers for subscribeTo - maps lifecycle events to machine events */
2489
2446
 
2447
+ function attachMicrostepActorRefs(microsteps, actorScope, inputSnapshot) {
2448
+ if (!microsteps.length) {
2449
+ return microsteps;
2450
+ }
2451
+ const result = microsteps.slice();
2452
+ const finalSnapshot = result.at(-1)[0];
2453
+ StateMachine.setInertActorScopeSnapshot(actorScope, finalSnapshot, false);
2454
+ for (const [snapshot] of result) {
2455
+ if (snapshot !== inputSnapshot) {
2456
+ StateMachine.attachSnapshotActorRef(snapshot.machine, actorScope, snapshot);
2457
+ }
2458
+ }
2459
+ return result;
2460
+ }
2461
+
2490
2462
  /**
2491
2463
  * Given actor `logic`, a `snapshot`, and an `event`, returns a tuple of the
2492
2464
  * `nextSnapshot` and `actions` to execute.
@@ -2494,9 +2466,12 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
2494
2466
  * This is a pure function that does not execute `actions`.
2495
2467
  */
2496
2468
  function transition(logic, snapshot, event) {
2497
- const actorScope = createInertActorScope(logic);
2498
- const [nextSnapshot, effects] = logic.transition(snapshot, event, actorScope);
2499
- return [nextSnapshot, effects];
2469
+ const actorScope = StateMachine.createInertActorScope(logic, snapshot);
2470
+ StateMachine.setInertActorScopeSnapshot(actorScope, snapshot, false);
2471
+ const [nextSnapshot, effects] = dist_xstateActors.finalizeTransitionResult(actorScope, snapshot, logic.transition(snapshot, event, actorScope));
2472
+ StateMachine.setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
2473
+ const returnedSnapshot = nextSnapshot === snapshot ? nextSnapshot : StateMachine.attachSnapshotActorRef(logic, actorScope, nextSnapshot);
2474
+ return [returnedSnapshot, effects];
2500
2475
  }
2501
2476
 
2502
2477
  /**
@@ -2507,9 +2482,11 @@ function transition(logic, snapshot, event) {
2507
2482
  * This is a pure function that does not execute `actions`.
2508
2483
  */
2509
2484
  function initialTransition(logic, ...[input]) {
2510
- const actorScope = createInertActorScope(logic);
2511
- const [nextSnapshot, executableActions] = logic.initialTransition(input, actorScope);
2512
- return [nextSnapshot, executableActions];
2485
+ const actorScope = StateMachine.createInertActorScope(logic);
2486
+ const [nextSnapshot, executableActions] = dist_xstateActors.finalizeTransitionResult(actorScope, undefined, logic.initialTransition(input, actorScope));
2487
+ StateMachine.setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
2488
+ const returnedSnapshot = StateMachine.attachSnapshotActorRef(logic, actorScope, nextSnapshot);
2489
+ return [returnedSnapshot, executableActions];
2513
2490
  }
2514
2491
 
2515
2492
  /**
@@ -2519,11 +2496,11 @@ function initialTransition(logic, ...[input]) {
2519
2496
  * This is a pure function that does not execute `actions`.
2520
2497
  */
2521
2498
  function getMicrosteps(machine, snapshot, event) {
2522
- const actorScope = createInertActorScope(machine);
2499
+ const actorScope = StateMachine.createInertActorScope(machine, snapshot);
2523
2500
  const {
2524
2501
  microsteps
2525
2502
  } = dist_xstateActors.macrostep(snapshot, event, actorScope, []);
2526
- return microsteps;
2503
+ return attachMicrostepActorRefs(microsteps, actorScope, snapshot);
2527
2504
  }
2528
2505
 
2529
2506
  /**
@@ -2534,15 +2511,16 @@ function getMicrosteps(machine, snapshot, event) {
2534
2511
  * This is a pure function that does not execute `actions`.
2535
2512
  */
2536
2513
  function getInitialMicrosteps(machine, ...[input]) {
2537
- const actorScope = createInertActorScope(machine);
2514
+ const actorScope = StateMachine.createInertActorScope(machine);
2538
2515
  const initEvent = dist_xstateActors.createInitEvent(input);
2539
2516
  const internalQueue = [];
2540
2517
  const preInitialSnapshot = machine._getPreInitialState(actorScope, initEvent);
2518
+ const contextSpawnEffects = Object.values(preInitialSnapshot.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
2541
2519
  const first = dist_xstateActors.initialMicrostep(machine.root, preInitialSnapshot, actorScope, initEvent, internalQueue);
2542
2520
  const {
2543
2521
  microsteps
2544
- } = dist_xstateActors.macrostep(first[0], initEvent, actorScope, internalQueue);
2545
- return [first, ...microsteps];
2522
+ } = dist_xstateActors.macrostep(first[0], initEvent, actorScope, internalQueue, [[first[0], [...contextSpawnEffects, ...first[1]]]]);
2523
+ return attachMicrostepActorRefs(microsteps, actorScope);
2546
2524
  }
2547
2525
 
2548
2526
  /**
@@ -2718,6 +2696,7 @@ exports.createListenerLogic = dist_xstateActors.createListenerLogic;
2718
2696
  exports.createLogic = dist_xstateActors.createLogic;
2719
2697
  exports.createObservableLogic = dist_xstateActors.createObservableLogic;
2720
2698
  exports.createSubscriptionLogic = dist_xstateActors.createSubscriptionLogic;
2699
+ exports.executeEffects = dist_xstateActors.executeEffects;
2721
2700
  exports.getStateNodes = dist_xstateActors.getStateNodes;
2722
2701
  exports.isBuiltInExecutableAction = dist_xstateActors.isBuiltInExecutableAction;
2723
2702
  exports.isMachineSnapshot = dist_xstateActors.isMachineSnapshot;
@@ -2728,6 +2707,8 @@ exports.subscriptionLogic = dist_xstateActors.subscriptionLogic;
2728
2707
  exports.toObserver = dist_xstateActors.toObserver;
2729
2708
  exports.StateMachine = StateMachine.StateMachine;
2730
2709
  exports.StateNode = StateMachine.StateNode;
2710
+ exports.getInitialSnapshot = StateMachine.getInitialSnapshot;
2711
+ exports.getNextSnapshot = StateMachine.getNextSnapshot;
2731
2712
  exports.SimulatedClock = SimulatedClock;
2732
2713
  exports.SpecialTargets = SpecialTargets;
2733
2714
  exports.assertEvent = assertEvent;
@@ -2737,9 +2718,7 @@ exports.createMachineFromConfig = createMachineFromConfig;
2737
2718
  exports.createStateConfig = createStateConfig;
2738
2719
  exports.createSystem = createSystem;
2739
2720
  exports.getInitialMicrosteps = getInitialMicrosteps;
2740
- exports.getInitialSnapshot = getInitialSnapshot;
2741
2721
  exports.getMicrosteps = getMicrosteps;
2742
- exports.getNextSnapshot = getNextSnapshot;
2743
2722
  exports.getNextTransitions = getNextTransitions;
2744
2723
  exports.initialTransition = initialTransition;
2745
2724
  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,