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 Parameters
Section titled “Type Parameters”Type Parameter | Description |
---|---|
T extends StateMachine <any > | Type of StateMachine |
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
machine | T & Partial <{ reset : () => void ; }> | The state machine instance (optionally with a reset method) |
state | FactoryKeyedState <ReturnType <T ["getState" ]>> | The state to reset to |
Returns
Section titled “Returns”T
& { reset
: () => void
; }
The machine with a reset method
Source
Section titled “Source”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 };};