Skip to content

transitionHook

transitionHook<FC>(config: TransitionHookConfig<FC>): (machine: any) => () => void

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

Registers one or more transition hooks for a FactoryMachine. Each config can specify matching criteria (from, to, type) and any lifecycle hook(s). Returns a setup function for the machine. Usage:

setup(machine)(
transitionHook({
from: "Idle",
type: "start",
to: "Running",
// optional hook functions
transition: transitionFn,
guard: guardFn,
resolveExit: resolveExitFn,
before: beforeFn,
handle: handleFn,
update: updateFn,
leave: leaveFn,
enter: enterFn,
effect: effectFn,
notify: notifyFn,
after: afterFn,
}),
// omit `from`, `to`, `type` to match all transitions
transitionHook({
effect: globalFn,
// more hooks...
}),
Type Parameter
FC extends FactoryMachineContext<KeyedStateFactory>
ParameterType
configTransitionHookConfig<FC>

(machine: any): () => void

ParameterType
machineany

(): void

void

This is the recommended flat API for lifecycle hooks in Matchina. It allows you to declaratively register hooks for specific transitions and phases.

export function transitionHook<FC extends FactoryMachineContext>(
config: TransitionHookConfig<FC>
) {
const { from, to, type, ...hooks } = config;
const filter = { from, to, type } as ChangeEventKeyFilter<
FactoryMachineEvent<FC>
>;
return (machine: any) => {
return createDisposer(
Object.entries(hooks).map(([hookKey, fn]) => {
return hookSetup(hookKey)((ev: FactoryMachineEvent<FC>) => {
if (matchChange(ev, filter)) {
return fn(ev);
}
return ev;
})(machine);
})
);
};
}