UNPKG

6.59 kBTypeScriptView Raw
1import type { StateNode } from "./StateNode.js";
2import type { StateMachine } from "./StateMachine.js";
3import type { ProvidedActor, AnyMachineSnapshot, AnyStateMachine, EventObject, HistoryValue, MachineContext, StateConfig, StateValue, AnyActorRef, Snapshot, ParameterizedObject, IsNever, MetaObject, StateSchema, StateId, SnapshotStatus } from "./types.js";
4type ToTestStateValue<TStateValue extends StateValue> = TStateValue extends string ? TStateValue : IsNever<keyof TStateValue> extends true ? never : keyof TStateValue | {
5 [K in keyof TStateValue]?: ToTestStateValue<NonNullable<TStateValue[K]>>;
6};
7export declare function isMachineSnapshot(value: unknown): value is AnyMachineSnapshot;
8interface MachineSnapshotBase<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta, TStateSchema extends StateSchema = StateSchema> {
9 /** The state machine that produced this state snapshot. */
10 machine: StateMachine<TContext, TEvent, TChildren, ProvidedActor, ParameterizedObject, ParameterizedObject, string, TStateValue, TTag, unknown, TOutput, EventObject, // TEmitted
11 any, // TMeta
12 TStateSchema>;
13 /** The tags of the active state nodes that represent the current state value. */
14 tags: Set<string>;
15 /**
16 * The current state value.
17 *
18 * This represents the active state nodes in the state machine.
19 *
20 * - For atomic state nodes, it is a string.
21 * - For compound parent state nodes, it is an object where:
22 *
23 * - The key is the parent state node's key
24 * - The value is the current state value of the active child state node(s)
25 *
26 * @example
27 *
28 * ```ts
29 * // single-level state node
30 * snapshot.value; // => 'yellow'
31 *
32 * // nested state nodes
33 * snapshot.value; // => { red: 'wait' }
34 * ```
35 */
36 value: TStateValue;
37 /** The current status of this snapshot. */
38 status: SnapshotStatus;
39 error: unknown;
40 context: TContext;
41 historyValue: Readonly<HistoryValue<TContext, TEvent>>;
42 /** The enabled state nodes representative of the state value. */
43 _nodes: Array<StateNode<TContext, TEvent>>;
44 /** An object mapping actor names to spawned/invoked actors. */
45 children: TChildren;
46 /**
47 * Whether the current state value is a subset of the given partial state
48 * value.
49 *
50 * @param partialStateValue
51 */
52 matches: (partialStateValue: ToTestStateValue<TStateValue>) => boolean;
53 /**
54 * Whether the current state nodes has a state node with the specified `tag`.
55 *
56 * @param tag
57 */
58 hasTag: (tag: TTag) => boolean;
59 /**
60 * Determines whether sending the `event` will cause a non-forbidden
61 * transition to be selected, even if the transitions have no actions nor
62 * change the state value.
63 *
64 * @param event The event to test
65 * @returns Whether the event will cause a transition
66 */
67 can: (event: TEvent) => boolean;
68 getMeta: () => Record<StateId<TStateSchema> & string, TMeta | undefined>;
69 toJSON: () => unknown;
70}
71interface ActiveMachineSnapshot<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta extends MetaObject, TConfig extends StateSchema> extends MachineSnapshotBase<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig> {
72 status: 'active';
73 output: undefined;
74 error: undefined;
75}
76interface DoneMachineSnapshot<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta extends MetaObject, TConfig extends StateSchema> extends MachineSnapshotBase<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig> {
77 status: 'done';
78 output: TOutput;
79 error: undefined;
80}
81interface ErrorMachineSnapshot<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta extends MetaObject, TConfig extends StateSchema> extends MachineSnapshotBase<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig> {
82 status: 'error';
83 output: undefined;
84 error: unknown;
85}
86interface StoppedMachineSnapshot<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta extends MetaObject, TConfig extends StateSchema> extends MachineSnapshotBase<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig> {
87 status: 'stopped';
88 output: undefined;
89 error: undefined;
90}
91export type MachineSnapshot<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta extends MetaObject, TConfig extends StateSchema> = ActiveMachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig> | DoneMachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig> | ErrorMachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig> | StoppedMachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>;
92export declare function createMachineSnapshot<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TMeta extends MetaObject, TStateSchema extends StateSchema>(config: StateConfig<TContext, TEvent>, machine: AnyStateMachine): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, undefined, TMeta, TStateSchema>;
93export declare function cloneMachineSnapshot<TState extends AnyMachineSnapshot>(snapshot: TState, config?: Partial<StateConfig<any, any>>): TState;
94export declare function getPersistedSnapshot<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta extends MetaObject>(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, any>, options?: unknown): Snapshot<unknown>;
95export {};