UNPKG

@overextended/ox_lib

Version:
81 lines (80 loc) 3.2 kB
export interface Hook extends HookOptions { resource: string; hookId: string; cb: (payload: any) => boolean; } interface HookOptions { [key: string]: any; } export declare class HookPipeline<T = any> { private hooks; readonly event: string; readonly filter: ((hook: Hook, payload: T) => boolean) | undefined; /** * Creates a hook pipeline for a specific event. * The pipeline manages a collection of registered hooks and controls execution * flow through filtering, rejection, and dispatching. * * It also exposes external resource hooks: * - `registerHook:<event>` adds a hook to the pipeline * - `removeHook:<event>` removes a hook from the pipeline */ constructor(event: string, filter?: (hook: Hook, payload: T) => boolean); /** * Registers a hook into the pipeline for the current event. * @param options Optional metadata attached to the hook. */ registerHook(cb: Hook['cb'] | null, options?: HookOptions): string; /** * Removes hooks from the pipeline. * - If `hookId` is provided, only the matching hook is removed. * - If omitted, all hooks belonging to the invoking resource are removed. */ remove(resource: string, hookId?: string): void; /** * Executes the hook pipeline for the payload. * * Each registered hook is evaluated in order of registration, checking the payload against a provided filter\ * using the hook options and executing the hook callback. * * A hook may block execution by returning `false` from the pipeline filter or its own callback. * * If any hook rejects the execution, dispatch is cancelled and `result.ok` is set to `false`. * * The returned object acts as a finalisation handle and emits results to registered handlers once closed. */ dispatch(payload: T): { ok: boolean; size: number; [Symbol.dispose]: () => void; }; } type PostHookEvent = (ok: boolean, payload: any) => void; declare class EventHook { readonly hookId: string; readonly resource: string; readonly event: string; private handler?; /** Creates a new EventHook instance bound to a specific exported hook. */ constructor(hookId: string, resource: string, event: string); /** * ---Attaches a post-execution event handler for this hook. * The handler is triggered after the hooked event completes and receives: * - `ok` whether the original event execution succeeded * - `payload` the returned or processed event data * * If a handler is already registered, it will be replaced. */ on(handler: PostHookEvent): void; /** Detaches the currently registered post-hook event handler, if one exists. */ off(): void; /** * Fully removes this hook from both the local event system and the external * hook registry provided by the originating resource. * * This invalidates the hook instance; it should not be used afterward. */ remove(): void; } export declare function registerHook(eventName: string, handler?: Hook['cb'] | null, options?: HookOptions): EventHook; export {};