Skip to content

resetMachine

resetMachine<T>(machine: T & Partial<{ reset: () => void; }>, state: FactoryKeyedState<ReturnType<T["getState"]>>, type: string): void

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

Resets a state machine to a given state by performing a transition.

Type ParameterDescription
T extends StateMachine<any>Type of StateMachine
ParameterTypeDefault valueDescription
machineT & Partial<{ reset: () => void; }>undefinedThe state machine instance (optionally with a reset method)
stateFactoryKeyedState<ReturnType<T["getState"]>>undefinedThe state to reset to
typestring"reset"The transition type (default: “reset”)

void

This function is useful for programmatically resetting a state machine to a known state, enabling repeatable workflows, error recovery, or test setup. It provides a generic way to trigger a reset transition and is foundational for adding reset capabilities to state machines.

export const resetMachine = <T extends StateMachine<any>>(
machine: T & Partial<{ reset: () => void }>,
state: FactoryKeyedState<ReturnType<T["getState"]>>,
type = "reset"
) => {
const before = machine.getChange();
machine.transition({
from: before.to,
type,
to: state,
} as any);
};