UNPKG

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