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
@@ -62,8 +62,9 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
62
62
  * @param snapshot The current snapshot
63
63
  * @param event The received event
64
64
  */
65
- transition(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, actorScope: ActorScope<typeof snapshot, TEvent, AnyActorSystem, TEmitted>): ActorLogicTransitionResult<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, ExecutableActionObjectFromLogic<this>>;
65
+ transition(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, actorScope?: ActorScope<typeof snapshot, TEvent, AnyActorSystem, TEmitted>): ActorLogicTransitionResult<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, ExecutableActionObjectFromLogic<this>>;
66
66
  private _collectEffects;
67
+ private _attachPureActorRef;
67
68
  private _transitionFast;
68
69
  /**
69
70
  * Determines the next state given the current `state` and `event`. Calculates
@@ -79,11 +80,11 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
79
80
  * Returns the initial `State` instance, with reference to `self` as an
80
81
  * `ActorRef`.
81
82
  */
82
- getInitialSnapshot(actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, TEvent, AnyActorSystem, TEmitted>, input?: TInput): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>;
83
- initialTransition(input: TInput | undefined, actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, TEvent, AnyActorSystem, TEmitted>): ActorLogicTransitionResult<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, ExecutableActionObjectFromLogic<this>>;
83
+ getInitialSnapshot(actorScope?: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, TEvent, AnyActorSystem, TEmitted>, input?: TInput): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>;
84
+ initialTransition(input: TInput | undefined, actorScope?: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, TEvent, AnyActorSystem, TEmitted>): ActorLogicTransitionResult<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, ExecutableActionObjectFromLogic<this>>;
84
85
  start(snapshot?: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>): void;
85
86
  getStateNodeById(stateId: string): StateNode<TContext, TEvent>;
86
87
  getPersistedSnapshot(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, options?: unknown): Snapshot<unknown>;
87
- restoreSnapshot(snapshot: Snapshot<unknown>, _actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, TEvent, AnyActorSystem, TEmitted>): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>;
88
+ restoreSnapshot(snapshot: Snapshot<unknown>, actorScope?: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, TEvent, AnyActorSystem, TEmitted>): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>;
88
89
  }
89
90
  export {};
@@ -1,15 +1,17 @@
1
- import { AnyActor, AnyActorScope, EventObject } from "./types.js";
1
+ import { type ActorTermination, AnyActor, AnyActorScope, EventObject } from "./types.js";
2
+ export declare function assertSendToEvent(event: EventObject): void;
2
3
  export declare const builtInActions: {
3
- "@xstate.spawn": (_actor: AnyActor) => void;
4
- "@xstate.start": (actor: AnyActor) => void;
5
- "@xstate.raise": (actorScope: AnyActorScope, event: EventObject, options: {
4
+ "@xstate.spawn": (actor: AnyActor) => any;
5
+ "@xstate.start": (actor: AnyActor) => any;
6
+ "@xstate.raise": (actorScope: AnyActorScope, _event: EventObject, options: {
6
7
  id?: string;
7
8
  delay?: number;
8
- }) => void;
9
+ }) => void | PromiseLike<void>;
9
10
  "@xstate.sendTo": (actorScope: AnyActorScope, actor: AnyActor, event: EventObject, options: {
10
11
  id?: string;
11
12
  delay?: number;
12
- }) => void;
13
- "@xstate.cancel": (actorScope: AnyActorScope, sendId: string) => void;
14
- "@xstate.stop": (actorScope: AnyActorScope, actor: AnyActor) => void;
13
+ }) => void | PromiseLike<void>;
14
+ "@xstate.cancel": (actorScope: AnyActorScope, sendId: string) => void | PromiseLike<void>;
15
+ "@xstate.stop": (actorScope: AnyActorScope, actor: AnyActor) => void | PromiseLike<void>;
16
+ "@xstate.terminate": (actor: AnyActor, termination: ActorTermination) => any;
15
17
  };
@@ -1,6 +1,6 @@
1
1
  import { StandardSchemaV1 } from "../schema.types.js";
2
- import { AnyActorSystem } from "../system.js";
3
- import { ActorLogic, ActorRefFromLogic, ActorScope, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
2
+ import { ActorSystemRuntime, AnyActorSystem } from "../system.js";
3
+ import { ActorLogic, ActorRefFromLogic, ActorScope, EventObject, ExecutableActionObject, NonReducibleUnknown, Snapshot } from "../types.js";
4
4
  export type LogicSnapshot<TContext, TOutput, TInput> = Snapshot<TOutput> & {
5
5
  context: TContext;
6
6
  input: TInput | undefined;
@@ -22,29 +22,14 @@ export interface LogicArgs<TContext, TEvent extends EventObject, TInput> {
22
22
  system: AnyActorSystem;
23
23
  self: LogicActorRef<TContext, unknown, TEvent, TInput>;
24
24
  }
25
- export type LogicEffect<TEvent extends EventObject, TEmitted extends EventObject> = {
26
- type: 'emit';
27
- event: TEmitted;
28
- } | {
29
- type: 'sendBack';
30
- event: EventObject;
31
- } | {
32
- type: 'raise';
33
- event: TEvent;
34
- } | {
35
- type: 'effect';
36
- key?: string;
37
- exec: () => void | (() => void);
38
- } | {
39
- type: 'cleanupEffects';
40
- };
25
+ export type LogicEffect<_TEvent extends EventObject, _TEmitted extends EventObject> = ExecutableActionObject;
41
26
  export interface LogicEnqueue<TEvent extends EventObject, TEmitted extends EventObject> {
42
27
  emit: (emitted: TEmitted) => void;
43
28
  sendBack: (event: EventObject) => void;
44
29
  raise: (event: TEvent) => void;
45
30
  effect: {
46
- (exec: () => void | (() => void)): void;
47
- (key: string, exec: () => void | (() => void)): void;
31
+ (exec: (runtime?: Partial<ActorSystemRuntime>) => void | (() => void)): void;
32
+ (key: string, exec: (runtime?: Partial<ActorSystemRuntime>) => void | (() => void)): void;
48
33
  };
49
34
  }
50
35
  export type LogicPatch<TContext, TOutput, TInput> = Partial<{
@@ -82,7 +67,6 @@ export type LogicActorRef<TContext, TOutput, TEvent extends EventObject, TInput>
82
67
  export declare const XSTATE_LOGIC_EFFECT_RESOLVE = "xstate.logic.effect.resolve";
83
68
  export declare const XSTATE_LOGIC_EFFECT_REJECT = "xstate.logic.effect.reject";
84
69
  export declare const XSTATE_LOGIC_EFFECT_START = "xstate.logic.effect.start";
85
- export declare function executeLogicEffects(effects: readonly unknown[] | undefined, actorScope: ActorScope<any, any, any, any>): void;
86
70
  export declare function createLogic<TContext, const TInputSchema extends StandardSchemaV1, const TOutputSchema extends StandardSchemaV1, TEvent extends EventObject = EventObject, TEmitted extends EventObject = EventObject>(config: LogicConfig<TContext, StandardSchemaV1.InferOutput<TOutputSchema>, TEvent, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted, TInputSchema, TOutputSchema> & {
87
71
  schemas: {
88
72
  input: TInputSchema;
@@ -56,7 +56,9 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorInstanc
56
56
  */
57
57
  constructor(logic: TLogic, options?: ActorOptions<TLogic>);
58
58
  private _deferred;
59
- private _pendingEffects?;
59
+ private _restored;
60
+ /** Associates each live snapshot with this actor for later pure transitions. */
61
+ private _setSnapshot;
60
62
  private _setErrorSnapshot;
61
63
  /** Recover via the logic's error event if possible; otherwise error out. */
62
64
  private _recoverOrError;
@@ -1,10 +1,12 @@
1
1
  import { createTransitionEnqueue } from "./transitionActions.js";
2
- import type { ActorLogic, EventObject, MachineContext, NonReducibleUnknown, Snapshot } from "./types.js";
2
+ import type { ActorLogic, EventObject, MachineContext, LogicalTimer, NonReducibleUnknown, Snapshot } from "./types.js";
3
3
  export type FSMSnapshot<TContext extends MachineContext, TState extends string, TInput = unknown> = Snapshot<undefined> & {
4
4
  value: TState;
5
5
  context: TContext;
6
6
  input: TInput | undefined;
7
7
  children: {};
8
+ timers: Record<string, LogicalTimer>;
9
+ _nextTimerId: number;
8
10
  _stateInput: Record<string, unknown> | undefined;
9
11
  machine: {
10
12
  id: string;
@@ -18,13 +18,13 @@ export { isMachineSnapshot, type MachineSnapshot } from "./State.js";
18
18
  export { StateMachine } from "./StateMachine.js";
19
19
  export { StateNode } from "./StateNode.js";
20
20
  export { getStateNodes } from "./stateUtils.js";
21
- export type { ActorSystem, AnyActorSystem } from "./system.js";
21
+ export type { ActorSystem, ActorSystemRuntime, AnyActorSystem } from "./system.js";
22
22
  export { toPromise } from "./toPromise.js";
23
23
  export * from "./types.js";
24
24
  export type { Next_MachineConfig as MachineConfig, Next_StateNodeConfig as StateNodeConfig, Next_InvokeConfig as InvokeConfig, Next_TransitionConfigOrTarget as TransitionConfigOrTarget, Implementations, InferEvents, WidenLiterals } from "./types.v6.js";
25
25
  export { getAllOwnEventDescriptors as __unsafe_getAllOwnEventDescriptors, matchesState, checkStateIn, pathToStateValue, toObserver } from "./utils.js";
26
26
  export { transition, initialTransition, getMicrosteps, getInitialMicrosteps, getNextTransitions } from "./transition.js";
27
- export { isBuiltInExecutableAction } from "./transitionActions.js";
27
+ export { executeEffects, isBuiltInExecutableAction } from "./transitionActions.js";
28
28
  export { waitFor } from "./waitFor.js";
29
29
  declare global {
30
30
  interface SymbolConstructor {
@@ -50,7 +50,7 @@ export declare function getTransitionResult(transition: Pick<AnyTransitionDefini
50
50
  internalEvents: EventObject[] | undefined;
51
51
  input: Record<string, unknown> | undefined;
52
52
  };
53
- export declare function macrostep(snapshot: AnyMachineSnapshot, event: EventObject, actorScope: AnyActorScope, internalQueue: AnyEventObject[]): {
53
+ export declare function macrostep(snapshot: AnyMachineSnapshot, event: EventObject, actorScope: AnyActorScope, internalQueue: AnyEventObject[], initialMicrosteps?: Microstep[]): {
54
54
  snapshot: typeof snapshot;
55
55
  microsteps: Microstep[];
56
56
  };
@@ -1,29 +1,54 @@
1
1
  import { InspectionEvent } from "./inspection.js";
2
- import { ActorSystemInfo, AnyActor, Observer, EventObject, Subscription, TimersRestoreStrategy } from "./types.js";
3
- interface ScheduledEvent {
2
+ import { AnyEventObject, ActorTermination, ActorSystemInfo, AnyActor, Observer, EventObject, Subscription } from "./types.js";
3
+ interface ScheduledTimer {
4
4
  id: string;
5
- event: EventObject;
6
- startedAt: number;
5
+ scheduledAt: number;
6
+ dueAt: number;
7
7
  delay: number;
8
8
  source: AnyActor;
9
- target: AnyActor;
10
9
  }
11
10
  export interface Clock {
12
11
  setTimeout(fn: (...args: any[]) => void, timeout: number): any;
13
12
  clearTimeout(id: any): void;
14
13
  }
15
14
  interface Scheduler {
16
- schedule(source: AnyActor, target: AnyActor, event: EventObject, delay: number, id: string | undefined): void;
15
+ schedule(source: AnyActor, id: string, delay: number): void;
17
16
  cancel(source: AnyActor, id: string): void;
18
17
  cancelAll(actor: AnyActor): void;
19
18
  }
20
- export interface ActorSystem<T extends ActorSystemInfo> {
19
+ /**
20
+ * Runtime operations used to execute effects.
21
+ *
22
+ * An external interpreter can override these operations while the default actor
23
+ * system provides the local in-memory implementation.
24
+ */
25
+ export interface ActorSystemRuntime {
26
+ /** Publishes a newly created actor to the runtime. */
27
+ spawnActor(source: AnyActor | undefined, actor: AnyActor): void | PromiseLike<void>;
28
+ /** Starts an actor. */
29
+ startActor(actor: AnyActor): void | PromiseLike<void>;
30
+ /** Stops an actor without producing a completion result. */
31
+ stopActor(actor: AnyActor): void | PromiseLike<void>;
32
+ /** Completes or errors an actor and publishes its terminal result. */
33
+ terminateActor(actor: AnyActor, termination: ActorTermination): void | PromiseLike<void>;
34
+ /** Delivers an event between actors. */
35
+ sendEvent(source: AnyActor | undefined, target: AnyActor, event: AnyEventObject): void | PromiseLike<void>;
36
+ /** Publishes an emitted event. */
37
+ emitEvent(source: AnyActor, event: EventObject): void | PromiseLike<void>;
38
+ /** Schedules a logical timer. */
39
+ scheduleTimer(source: AnyActor, id: string, delay: number): void | PromiseLike<void>;
40
+ /** Cancels one logical timer. */
41
+ cancelTimer(source: AnyActor, id: string): void | PromiseLike<void>;
42
+ /** Cancels all logical timers owned by an actor. */
43
+ cancelAllTimers(source: AnyActor): void | PromiseLike<void>;
44
+ }
45
+ export interface ActorSystem<T extends ActorSystemInfo> extends ActorSystemRuntime {
21
46
  get: <K extends keyof T['actors']>(key: K) => T['actors'][K] | undefined;
22
47
  getAll: () => Partial<T['actors']>;
23
48
  inspect: (observer: Observer<InspectionEvent> | ((inspectionEvent: InspectionEvent) => void)) => Subscription;
24
49
  scheduler: Scheduler;
25
50
  getSnapshot: () => {
26
- _scheduledEvents: Record<string, ScheduledEvent>;
51
+ _scheduledTimers: Record<string, ScheduledTimer>;
27
52
  };
28
53
  start: () => void;
29
54
  _clock: Clock;
@@ -34,6 +59,6 @@ export declare function createRuntimeSystem<T extends ActorSystemInfo>(rootActor
34
59
  clock: Clock;
35
60
  logger: (...args: any[]) => void;
36
61
  snapshot?: unknown;
37
- timers?: TimersRestoreStrategy;
62
+ createActorRef: ActorSystem<T>['createActorRef'];
38
63
  }): ActorSystem<T>;
39
64
  export {};
@@ -1,6 +1,8 @@
1
- import type { AnyAction, AnyActorScope, AnyEventObject, AnyMachineSnapshot, EnqueueObject, EventObject, ExecutableActionObject, MachineContext, SpecialExecutableAction, StartExecutableActionObject } from "./types.js";
1
+ import type { ActorSystemRuntime } from "./system.js";
2
+ import type { AnyAction, AnyActor, AnyActorScope, AnyEventObject, AnyMachineSnapshot, EnqueueObject, EventObject, ExecutableActionObject, MachineContext, SpecialExecutableAction, SpawnExecutableActionObject, StartExecutableActionObject } from "./types.js";
2
3
  export declare function mergeContextPatch(context: MachineContext, patch: MachineContext): MachineContext;
3
4
  export declare function createTransitionEnqueue(actorScope: AnyActorScope, actions: any[], internalEvents: EventObject[], actorSubscriptions?: boolean, createActors?: boolean): EnqueueObject<any, any>;
5
+ export declare function createSpawnEffect(actor: AnyActor): SpawnExecutableActionObject;
4
6
  /**
5
7
  * Attached (listener/subscription) starts are ordered before child starts so
6
8
  * that a listener/subscription captures events emitted synchronously as its
@@ -8,5 +10,7 @@ export declare function createTransitionEnqueue(actorScope: AnyActorScope, actio
8
10
  */
9
11
  export declare function deriveDeferredStarts(effects: ReadonlyArray<ExecutableActionObject>): StartExecutableActionObject[];
10
12
  export declare function isBuiltInExecutableAction(action: ExecutableActionObject): action is SpecialExecutableAction;
13
+ /** Executes transition effects sequentially, awaiting each runtime operation. */
14
+ export declare function executeEffects(effects: readonly ExecutableActionObject[], runtime?: Partial<ActorSystemRuntime>): Promise<void>;
11
15
  export declare function resolveActionsWithContext(currentSnapshot: AnyMachineSnapshot, event: AnyEventObject, actorScope: AnyActorScope, actions: AnyAction[]): [AnyMachineSnapshot, ExecutableActionObject[]];
12
16
  export declare function createEnqueueObject(props: Partial<EnqueueObject<any, any>>, action: <T extends (...args: any[]) => any>(fn: T, ...args: Parameters<T>) => void): EnqueueObject<any, any>;
@@ -5,7 +5,7 @@ import { AsyncActorLogic } from "./actors/promise.js";
5
5
  import type { Actor } from "./createActor.js";
6
6
  import { InspectionEvent } from "./inspection.js";
7
7
  import { Spawner } from "./spawn.js";
8
- import { AnyActorSystem, Clock } from "./system.js";
8
+ import type { ActorSystemRuntime, AnyActorSystem, Clock } from "./system.js";
9
9
  import { Implementations, Next_StateNodeConfig } from "./types.v6.js";
10
10
  import { StandardSchemaV1 } from "./schema.types.js";
11
11
  import { builtInActions } from "./actions.js";
@@ -486,50 +486,27 @@ export interface StateConfig<TContext extends MachineContext, TEvent extends Eve
486
486
  context: TContext;
487
487
  historyValue?: HistoryValue;
488
488
  children: Record<string, AnyActorRef | undefined>;
489
+ timers?: Record<string, LogicalTimer>;
489
490
  status: SnapshotStatus;
490
491
  output?: any;
491
492
  error?: unknown;
492
493
  machine?: StateMachine<TContext, TEvent, any, any, any, any, any, any, any, any, any, any, any, any>;
493
494
  }
494
- /**
495
- * A pending effect (delayed event/transition that has not fired yet) captured
496
- * in a persisted snapshot — a serialized built-in action descriptor plus its
497
- * runtime progress, with enough information to restore it under any strategy.
498
- */
499
- export interface PendingEffect {
500
- /** The serialized built-in action type for this effect. */
501
- type: '@xstate.raise';
502
- event: EventObject;
495
+ /** A deterministic delayed-delivery declaration owned by a logic snapshot. */
496
+ export interface LogicalTimer {
503
497
  id: string;
504
- /** The original delay (in ms) the effect was scheduled with. */
505
498
  delay: number;
506
- /** Wall-clock timestamp (ms) when the effect was scheduled. */
507
- startedAt: number;
508
- /** Wall-clock time (ms) that had elapsed when the snapshot was persisted. */
509
- elapsed: number;
499
+ type: '@xstate.raise' | '@xstate.sendTo';
500
+ event: EventObject;
501
+ /** `self` or the logical actor that will receive `event`. */
502
+ target: 'self' | AnyActor;
503
+ }
504
+ /** The logical input delivered to a timer's source when its runtime delay ends. */
505
+ export interface TimerEvent extends EventObject {
506
+ type: 'xstate.timer';
507
+ id: string;
510
508
  }
511
- /**
512
- * How pending effects (timers) from a persisted snapshot are restored on
513
- * `actor.start()`:
514
- *
515
- * - `'resume'` (default) — the clock was "frozen" while persisted: a 5-minute
516
- * timer persisted with 1 minute elapsed fires after 4 more minutes.
517
- * - `'restart'` — timers start over with their full delay.
518
- * - `'absolute'` — timers honor the original wall-clock expiry (`startedAt +
519
- * delay`); already-expired timers fire immediately.
520
- * - A custom function receiving the pending effect and returning the remaining
521
- * delay in ms.
522
- */
523
- export type TimersRestoreStrategy = 'resume' | 'restart' | 'absolute' | ((pendingEffect: PendingEffect) => number);
524
509
  export interface ActorOptions<TLogic extends AnyActorLogic> {
525
- /**
526
- * How pending timers (delayed events/transitions) from a persisted `snapshot`
527
- * are restored when the actor starts. Applies to this actor and (when this is
528
- * the root) all rehydrated child actors.
529
- *
530
- * @default 'resume'
531
- */
532
- timers?: TimersRestoreStrategy;
533
510
  /**
534
511
  * The clock that is responsible for setting and clearing timeouts, such as
535
512
  * delayed events and transitions.
@@ -795,7 +772,7 @@ export interface ActorScope<TSnapshot extends Snapshot<unknown>, TEvent extends
795
772
  sessionId: string;
796
773
  logger: (...args: any[]) => void;
797
774
  defer: (fn: () => void) => void;
798
- emit: (event: TEmitted) => void;
775
+ emit: (event: TEmitted) => void | PromiseLike<void>;
799
776
  system: TSystem;
800
777
  stopChild: (child: AnyActor) => void;
801
778
  actionExecutor: ActionExecutor;
@@ -822,7 +799,7 @@ export type Snapshot<TOutput> = {
822
799
  output: undefined;
823
800
  error: undefined;
824
801
  };
825
- export type ActorLogicTransitionResult<TSnapshot extends Snapshot<unknown>, TEffect = unknown> = [nextSnapshot: TSnapshot, effects: TEffect[]];
802
+ export type ActorLogicTransitionResult<TSnapshot extends Snapshot<unknown>, TEffect = ExecutableActionObject> = [nextSnapshot: TSnapshot, effects: TEffect[]];
826
803
  /**
827
804
  * Represents logic which can be used by an actor.
828
805
  *
@@ -877,8 +854,12 @@ in TInput = NonReducibleUnknown, TSystem extends AnyActorSystem = AnyActorSystem
877
854
  *
878
855
  * @param snapshot - The starting state.
879
856
  * @param actorScope - The actor scope.
857
+ * @param options - Start metadata, including whether the snapshot was
858
+ * restored.
880
859
  */
881
- start?: (snapshot: TSnapshot, actorScope: ActorScope<TSnapshot, TEvent, AnyActorSystem, TEmitted>) => void;
860
+ start?: (snapshot: TSnapshot, actorScope: ActorScope<TSnapshot, TEvent, AnyActorSystem, TEmitted>, options?: {
861
+ restored: boolean;
862
+ }) => void;
882
863
  /**
883
864
  * Obtains the internal state of the actor in a representation which can be be
884
865
  * persisted. The persisted state can be restored by `restoreSnapshot`.
@@ -887,12 +868,6 @@ in TInput = NonReducibleUnknown, TSystem extends AnyActorSystem = AnyActorSystem
887
868
  * @returns The a representation of the internal state to be persisted.
888
869
  */
889
870
  getPersistedSnapshot: (snapshot: TSnapshot, options?: unknown) => Snapshot<unknown>;
890
- /**
891
- * Executes the non-action effects returned from `transition` (e.g. the
892
- * effects produced by `createLogic`-based actors). Actor logic that never
893
- * returns such effects can omit this.
894
- */
895
- executeEffects?: (effects: readonly unknown[], actorScope: ActorScope<TSnapshot, TEvent, TSystem, TEmitted>) => void;
896
871
  /**
897
872
  * Returns an event that the actor should transition with to recover from an
898
873
  * execution error, or `undefined` if the given snapshot cannot handle it.
@@ -906,9 +881,10 @@ export interface AnyActorLogic {
906
881
  initialTransition(input: any, actorScope: any): ActorLogicTransitionResult<any>;
907
882
  getInitialSnapshot(actorScope: any, input: any): any;
908
883
  restoreSnapshot?(persistedState: Snapshot<unknown>, actorScope: any): any;
909
- start?(snapshot: any, actorScope: any): void;
884
+ start?(snapshot: any, actorScope: any, options?: {
885
+ restored: boolean;
886
+ }): void;
910
887
  getPersistedSnapshot(snapshot: any, options?: unknown): Snapshot<unknown>;
911
- executeEffects?(effects: readonly unknown[], actorScope: any): void;
912
888
  getExecutionErrorEvent?(snapshot: any, error: unknown): any;
913
889
  }
914
890
  export type UnknownActorLogic = ActorLogic<any, // snapshot
@@ -1047,13 +1023,35 @@ export type ToStateValue<T extends StateSchema> = T extends {
1047
1023
  export interface BaseExecutableActionObject {
1048
1024
  params: NonReducibleUnknown;
1049
1025
  args: unknown[];
1050
- exec: (() => void) | undefined;
1026
+ exec(runtime?: Partial<ActorSystemRuntime>): void | PromiseLike<void> | undefined;
1051
1027
  }
1028
+ /** The terminal result published when an actor completes or errors. */
1029
+ export type ActorTermination = {
1030
+ status: 'done';
1031
+ output: unknown;
1032
+ error: undefined;
1033
+ } | {
1034
+ status: 'error';
1035
+ output: undefined;
1036
+ error: unknown;
1037
+ };
1052
1038
  export interface CustomExecutableActionObject<TType extends string = string & {}> extends BaseExecutableActionObject {
1039
+ kind: 'action';
1053
1040
  type: TType;
1041
+ action: ((...args: any[]) => void | PromiseLike<void>) | undefined;
1042
+ }
1043
+ export interface EmitExecutableActionObject<TType extends string = string & {}> extends BaseExecutableActionObject {
1044
+ kind: 'emit';
1045
+ type: TType;
1046
+ source: AnyActor;
1047
+ event: EventObject & {
1048
+ type: TType;
1049
+ };
1054
1050
  }
1055
1051
  export interface SpawnExecutableActionObject extends BaseExecutableActionObject {
1052
+ kind: 'builtin';
1056
1053
  type: '@xstate.spawn';
1054
+ source: AnyActor | undefined;
1057
1055
  actor: AnyActor;
1058
1056
  id: string;
1059
1057
  logic: AnyActorLogic;
@@ -1062,20 +1060,26 @@ export interface SpawnExecutableActionObject extends BaseExecutableActionObject
1062
1060
  args: Parameters<(typeof builtInActions)['@xstate.spawn']>;
1063
1061
  }
1064
1062
  export interface StartExecutableActionObject extends BaseExecutableActionObject {
1063
+ kind: 'builtin';
1065
1064
  type: '@xstate.start';
1065
+ source: AnyActor | undefined;
1066
1066
  actor: AnyActor;
1067
1067
  id: string;
1068
1068
  args: Parameters<(typeof builtInActions)['@xstate.start']>;
1069
1069
  }
1070
1070
  export interface RaiseExecutableActionObject extends BaseExecutableActionObject {
1071
+ kind: 'builtin';
1071
1072
  type: '@xstate.raise';
1073
+ source: AnyActor;
1072
1074
  event: EventObject;
1073
1075
  id: string | undefined;
1074
1076
  delay: number | undefined;
1075
1077
  args: Parameters<(typeof builtInActions)['@xstate.raise']>;
1076
1078
  }
1077
1079
  export interface SendToExecutableActionObject extends BaseExecutableActionObject {
1080
+ kind: 'builtin';
1078
1081
  type: '@xstate.sendTo';
1082
+ source: AnyActor;
1079
1083
  target: AnyActor;
1080
1084
  event: EventObject;
1081
1085
  id: string | undefined;
@@ -1083,15 +1087,29 @@ export interface SendToExecutableActionObject extends BaseExecutableActionObject
1083
1087
  args: Parameters<(typeof builtInActions)['@xstate.sendTo']>;
1084
1088
  }
1085
1089
  export interface CancelExecutableActionObject extends BaseExecutableActionObject {
1090
+ kind: 'builtin';
1086
1091
  type: '@xstate.cancel';
1092
+ source: AnyActor;
1087
1093
  id: string;
1088
1094
  args: Parameters<(typeof builtInActions)['@xstate.cancel']>;
1089
1095
  }
1090
1096
  export interface StopExecutableActionObject extends BaseExecutableActionObject {
1097
+ kind: 'builtin';
1091
1098
  type: '@xstate.stop';
1099
+ source: AnyActor;
1092
1100
  actor: AnyActor;
1101
+ id: string;
1093
1102
  args: Parameters<(typeof builtInActions)['@xstate.stop']>;
1094
1103
  }
1104
+ /** An executable effect that publishes an actor's terminal result. */
1105
+ export type TerminateExecutableActionObject = BaseExecutableActionObject & {
1106
+ kind: 'builtin';
1107
+ type: '@xstate.terminate';
1108
+ source: AnyActor;
1109
+ actor: AnyActor;
1110
+ id: string;
1111
+ args: Parameters<(typeof builtInActions)['@xstate.terminate']>;
1112
+ } & ActorTermination;
1095
1113
  export type BuiltInExecutableActionObject = Values<{
1096
1114
  '@xstate.spawn': SpawnExecutableActionObject;
1097
1115
  '@xstate.start': StartExecutableActionObject;
@@ -1099,16 +1117,17 @@ export type BuiltInExecutableActionObject = Values<{
1099
1117
  '@xstate.sendTo': SendToExecutableActionObject;
1100
1118
  '@xstate.cancel': CancelExecutableActionObject;
1101
1119
  '@xstate.stop': StopExecutableActionObject;
1120
+ '@xstate.terminate': TerminateExecutableActionObject;
1102
1121
  }>;
1103
1122
  export type SpecialExecutableAction = BuiltInExecutableActionObject;
1104
- type TransitionExecutableActionObject<TType extends string = never> = BuiltInExecutableActionObject | CustomExecutableActionObject<TType | (string & {})>;
1123
+ type TransitionExecutableActionObject<TType extends string = never> = BuiltInExecutableActionObject | EmitExecutableActionObject | CustomExecutableActionObject<TType | (string & {})>;
1105
1124
  type KnownImplementationKeys<TImplementationMap> = string extends keyof TImplementationMap ? never : Extract<keyof TImplementationMap, string>;
1106
1125
  export type ExecutableActionObjectFromLogic<T extends AnyActorLogic> = T extends StateMachine<any, any, any, any, any, any, any, any, any, any, infer TActionMap, any, any, any> ? TransitionExecutableActionObject<KnownImplementationKeys<TActionMap>> : ExecutableActionObject;
1107
- export type ExecutableActionObject<TType extends string = string & {}> = BuiltInExecutableActionObject | CustomExecutableActionObject<TType>;
1126
+ export type ExecutableActionObject<TType extends string = string & {}> = BuiltInExecutableActionObject | EmitExecutableActionObject | CustomExecutableActionObject<TType>;
1108
1127
  export interface ToExecutableAction<T extends ParameterizedObject> extends CustomExecutableActionObject<T['type']> {
1109
1128
  type: T['type'];
1110
1129
  params: T['params'];
1111
- exec: undefined;
1130
+ action: undefined;
1112
1131
  }
1113
1132
  export type ActionExecutor = (actionToExecute: ExecutableActionObject) => void;
1114
1133
  /** Mappers for subscribeTo - maps lifecycle events to machine events */