/** * Defines a simple state machine consisting of states, transitions and callbacks when entering and existing states. * * See also {@link useStateMachine}. */ export type StateMachineDefinition = { /** The set of states keyed by state name */ [index: string]: { /** Optional label that can be used for display. */ label?: string; /** Called when entering a state. Can optionally return a new state to transition to (by name, or name plus user-defined payload). */ enter?(): void | string | { state: string; data: unknown; } | Promise; /** * Called when exiting a state. This function is called synchronously and cannot return a value or a promise */ exit?(): undefined; /** * Possible transitions out of this state. The keys of the object are the target * state names. The values are either a boolean, or a function returning boolean. * A transition is triggered when its value evaluates as true. */ transitions?: { [index: string]: (() => boolean) | boolean; }; /** * Transitions that are possible from the `enter` function. This is optional and does * not affect the state machine behaviour, but can be used to document and visualise the state machine. */ implicitTransitions?: string[]; }; }; export declare class StateMachine { currentState: string | undefined; userData: string | undefined; private readonly initialState; private previousState; private readonly onChangeHandler; /** * Construct a new state machine. * * @param initialState The state to enter when the state machine is first instantiated * @param onChange Function that will be called when state changes. Used to trigger re-render by the {@link useStateMachine} hook. */ constructor(initialState: string, onChange?: (state: { currentState: string; previousState?: string; userData: unknown; }) => void); /** * Perform an eval on the given state machine. * * Checks the possibly transitions of the currently active state and transitions the first new state where the eval function returns `true`. * @param machine */ eval(machine: StateMachineDefinition): void; private triggerChangeEffect; private transition; } type StateMachineState = { currentState: string; previousState?: string; userData: unknown; definition: StateMachineDefinition; }; /** * Evaluates the state machine given and returns current state, previous state and any user defined data associated with the latest transition. * * The state machine definition can contain dynamic values that change (for example due to React state changes). The re-evaluation * of the state machine (that is, any state transitions) will occur when values in the dependency array changes. You can think of the * return from this function being the memoised state of the state machine based on the provided dependencies. * * See {@link StateMachineDefinition} for the structure of the state machine definition and refer to the [GBR state machine](/docs/gbr/state_machine) section * of the GBR documentation for a full worked example. * * @param definition The state machine definition * @param initialState The initial state to enter on creation * @param dependencies The values that can trigger state changes */ export declare const useStateMachine: (definition: StateMachineDefinition, initialState: string, dependencies: any) => StateMachineState; export {};