Skip to content

whenEventType

whenEventType<E, K>(type: K, fn: EntryListener<E & { type: K; }>): (ev: E) => void

Defined in: factory-machine-hooks.ts:183

Creates an entry listener that triggers when the event’s type matches the given event type.

Type ParameterDescription
E extends FactoryMachineEvent<any>FactoryMachineEvent type
K extends stringEvent type
ParameterTypeDescription
typeKThe event type to match
fnEntryListener<E & { type: K; }>EntryListener to invoke when matched

An entry listener for the specified event type

(ev: E): void

ParameterType
evE

void

setup(machine)(
whenEventType("activate", handleActivate),
);

Useful for running logic when a specific event type is triggered in a state machine.

export const whenEventType = <
E extends FactoryMachineEvent<any>,
K extends E["type"],
>(
type: K,
fn: EntryListener<E & { type: K }>
) => when<E>((ev) => ev.type === type, fn as any);