Skip to content

guardExecute

guardExecute<F>(guardFn: (…args: Parameters<F>) => boolean): MethodEnhancer<"execute", HasMethod<"execute">>

Defined in: promise-machine-hooks.ts:21

Creates a method enhancer for the execute method of a promise machine, adding guard logic. The returned enhancer wraps the original method, checks the guard function, and throws if the guard fails. When applied, the enhancer returns a disposer function to undo the enhancement.

Type Parameter
F extends (…args: any[]) => any
ParameterTypeDescription
guardFn(…args: Parameters<F>) => booleanFunction that checks whether execution should proceed, based on the arguments.

MethodEnhancer<"execute", HasMethod<"execute">>

A method enhancer for execute, which returns a disposer when applied.

// Enhance the machine's execute method with a guard
const stopGuarding = guardExecute((...args) => args.length > 0)(machine);
// Call stopGuarding() to remove the enhancement

This function is a utility for enhancing promise machines with guard logic, ensuring that certain conditions are met before executing the method. It creates a method enhancer with a a typed guard that can be applied to a promise machine’s

export function guardExecute<F extends (...args: any[]) => any>(
guardFn: (...args: Parameters<F>) => boolean
) {
return createMethodEnhancer("execute")((fn) => (...args) => {
if (!guardFn(...(args as any))) {
throw new Error("Guard condition failed");
}
return fn(...args);
});
}