Skip to content

createReset

createReset<T>(machine: T, state: FactoryKeyedState<ReturnType<T["getState"]>>): () => void

Defined in: extras/with-reset.ts:44

Creates a reset function for a state machine, resetting it to a given state. Usage:

const reset = createReset(machine, "Idle");
reset(); // Resets the machine to the "Idle" state
Type ParameterDescription
T extends StateMachine<any>Type of StateMachine
ParameterTypeDescription
machineTThe state machine instance
stateFactoryKeyedState<ReturnType<T["getState"]>>The state to reset to

A function that resets the machine to the specified state

(): void

void

This function is useful for generating a reusable reset function for a state machine, allowing you to reset the machine to a specific state on demand. It is commonly used in scenarios where you want to expose a reset API or automate state restoration.

export const createReset =
<T extends StateMachine<any>>(
machine: T,
state: FactoryKeyedState<ReturnType<T["getState"]>>
) =>
() => {
resetMachine(machine, state);
};