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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
F extends (…args: any[]) => any |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
guardFn | (…args: Parameters<F>) => boolean | Function that checks whether execution should proceed, based on the arguments. |
Returns
Section titled “Returns”MethodEnhancer<"execute", HasMethod<"execute">>
A method enhancer for execute, which returns a disposer when applied.
Example
Section titled “Example”// Enhance the machine's execute method with a guardconst stopGuarding = guardExecute((...args) => args.length > 0)(machine);// Call stopGuarding() to remove the enhancementSource
Section titled “Source”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); });}