Skip to content

withReset

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

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

Adds a reset method to a state machine if not already present.

Type ParameterDescription
T extends StateMachine<any>Type of StateMachine
ParameterTypeDescription
machineT & Partial<{ reset: () => void; }>The state machine instance (optionally with a reset method)
stateFactoryKeyedState<ReturnType<T["getState"]>>The state to reset to

T & { reset: () => void; }

The machine with a reset method

This function is useful for extending state machines with a reset capability, making it easy to restore the machine to a known state. It is foundational for workflows that require repeatable initialization, error recovery, or test setup.

export const withReset = <T extends StateMachine<any>>(
machine: T & Partial<{ reset: () => void }>,
state: FactoryKeyedState<ReturnType<T["getState"]>>
) => {
if (!machine.reset) {
machine.reset = createReset(machine, state);
}
return machine as T & { reset: typeof machine.reset };
};