whenFromState
whenFromState<
E,K>(stateKey:K,fn:EntryListener<E& {from:FactoryKeyedState<E["machine"]["states"],K>; }>): (ev:E) =>void
Defined in: factory-machine-hooks.ts:139
Creates an entry listener that triggers when the event’s from.key matches the given state key.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
E extends FactoryMachineEvent<any> | FactoryMachineEvent type |
K extends any | State key type |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
stateKey | K | The state key to match against from.key |
fn | EntryListener<E & { from: FactoryKeyedState<E["machine"]["states"], K>; }> | EntryListener to invoke when matched |
Returns
Section titled “Returns”An entry listener for the specified state key
(
ev:E):void
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
ev | E |
Returns
Section titled “Returns”void
Example
Section titled “Example”setup(machine)( effect(whenFromState("Idle", handleLeaveIdle)),);Source
Section titled “Source”Useful for running logic when leaving a specific state in a state machine.
export const whenFromState = < E extends FactoryMachineEvent<any>, K extends E["from"]["key"],>( stateKey: K, fn: EntryListener<E & { from: FactoryKeyedState<E["machine"]["states"], K> }>) => when<E>((ev) => ev.from.key === stateKey, fn);