hookable
JavaScript / TypeScript API reference for the module hookable.
Public API
Class: Hookable
Canonical path: hookable.Hookable
Declared in: src/hookable.ts
Signature
class Hookable <
HooksT = Record<string, HookCallback>,
HookNameT extends HookKeys<HooksT> = HookKeys<HooksT>
> {}
Summary
Hookable is a class for managing and calling lifecycle hooks with support for deprecation and global before/after listeners.
Constructor: constructor
Canonical path: hookable.Hookable.constructor
Declared in: src/hookable.ts
Signature
constructor () {}
Summary
Initializes a new Hookable instance.
Behavior
Initializes internal hook storage, lifecycle listener arrays, and binds core methods to the instance.
Function: createDebugger
Canonical path: hookable.createDebugger
Declared in: src/utils.ts
Signature
function createDebugger (hooks: Hookable<any>, _options: CreateDebuggerOptions = {}) {}
Summary
Start debugging hook names and timing in console
Behavior
Initializes listeners for beforeEach and afterEach hooks to log execution time and parameters to the console based on the provided filter and display options.
Parameters
- hooks: The Hookable instance to be monitored by the debugger.
- _options: Optional settings to control logging behavior such as tags, console grouping, and hook filtering.
Returns
- Return value 1: Returns an object with a close method to stop debugging and remove the attached listeners.
Function: createHooks
Canonical path: hookable.createHooks
Declared in: src/hookable.ts
Signature
function createHooks<T> (): Hookable<T> {}
Summary
Creates a new Hookable instance.
Behavior
Instantiates and returns a new Hookable instance.
Returns
- Return value 1: A new Hookable instance.
Function: flatHooks
Canonical path: hookable.flatHooks
Declared in: src/utils.ts
Signature
function flatHooks<T> (configHooks: NestedHooks<T>, hooks: T = {} as T, parentName?: string): T {}
Summary
Flattens a nested hooks object into a flat object with namespaced keys.
Behavior
Iterates through a nested hooks object and recursively flattens it into a single-level object where keys are joined by colons.
Parameters
- configHooks: The nested hooks configuration to flatten.
- hooks: The target object where flattened hooks are collected; defaults to a new empty object.
- parentName: An optional prefix used for keys during recursive calls to maintain namespace hierarchy.
Returns
- Return value 1: The flattened hooks object.
Function: mergeHooks
Canonical path: hookable.mergeHooks
Declared in: src/utils.ts
Signature
function mergeHooks<T> (...hooks: NestedHooks<T>[]): T {}
Summary
Merges multiple nested hook configurations into a single flat hooks object.
Behavior
Flattens multiple nested hook objects and merges them into one; if multiple functions exist for the same hook key, they are wrapped into a single serial execution function.
Parameters
- hooks: A rest parameter containing multiple nested hook objects to be merged.
Returns
- Return value 1: A single flattened hooks object containing the merged logic from all input hooks.
Function: parallelCaller
Canonical path: hookable.parallelCaller
Declared in: src/utils.ts
Signature
function parallelCaller (hooks: HookCallback[], args?: any[]) {}
Summary
Calls multiple hooks in parallel.
Behavior
Executes all provided hook callbacks simultaneously using Promise.all.
Parameters
- hooks: An array of hook callback functions to execute.
- args: Optional arguments to pass to each hook callback.
Returns
- Return value 1: A promise that resolves when all hook executions have completed.
Function: serial
Canonical path: hookable.serial
Declared in: src/utils.ts
Signature
function serial<T> (tasks: T[], fn: (task: T) => Promise<any> | any) {}
Summary
Executes an array of tasks sequentially using a provided processing function.
Behavior
Reduces an array of tasks into a promise chain where each task is passed to the provided function only after the previous one completes.
Parameters
- tasks: An array of items to process.
- fn: A function that takes a task and returns a value or a promise.
Returns
- Return value 1: A promise that resolves when all tasks have been processed sequentially.
Function: serialCaller
Canonical path: hookable.serialCaller
Declared in: src/utils.ts
Signature
function serialCaller (hooks: HookCallback[], args?: any[]) {}
Summary
Calls multiple hooks sequentially.
Behavior
Uses a promise chain to execute each hook callback one after another in the order they appear in the array.
Parameters
- hooks: An array of hook callback functions to be called sequentially.
- args: Optional arguments to be applied to each hook function.
Returns
- Return value 1: A promise that resolves after the final hook in the sequence has finished.
Interface: CreateDebuggerOptions
Canonical path: hookable.CreateDebuggerOptions
Declared in: src/types.ts
Signature
interface CreateDebuggerOptions {
/** An optional tag to prefix console logs with */
tag?: string
/**
* Show hook params to the console output
*
* Enabled for browsers by default
*/
inspect?: boolean
/**
* Use group/groupEnd wrapper around logs happening during a specific hook
*
* Enabled for browsers by default
*/
group?: boolean
/** Filter which hooks to enable debugger for. Can be a string prefix or fn. */
filter?: string | ((event: string) => boolean)
}
Summary
Configuration options for creating a debugger, including settings for tagging, inspection, grouping, and filtering.
Interface: Hooks
Canonical path: hookable.Hooks
Declared in: src/types.ts
Signature
interface Hooks { [key: string]: HookCallback }
Summary
An interface representing a collection of hook callbacks indexed by string keys.
Method: addHooks
Canonical path: hookable.Hookable.addHooks
Declared in: src/hookable.ts
Signature
addHooks (configHooks: NestedHooks<HooksT>) {}
Summary
Registers multiple hooks from a nested configuration object.
Behavior
Flattens the provided nested hooks and registers each one using the hook method.
Parameters
- configHooks: A configuration object containing nested hooks to be registered.
Returns
- Return value 1: A function that, when called, unregisters all hooks added in this batch.
Method: afterEach
Canonical path: hookable.Hookable.afterEach
Declared in: src/hookable.ts
Signature
afterEach (fn: (event: InferSpyEvent<HooksT>) => void) {}
Summary
Registers a callback to run after every hook call.
Behavior
Adds a callback to the internal list of hooks to be executed after every hook call.
Parameters
- fn: The callback function to execute after each hook.
Returns
- Return value 1: A function to unregister the after-each callback.
Method: beforeEach
Canonical path: hookable.Hookable.beforeEach
Declared in: src/hookable.ts
Signature
beforeEach (fn: (event: InferSpyEvent<HooksT>) => void) {}
Summary
Registers a callback to run before every hook call.
Behavior
Adds a callback to the internal list of hooks to be executed before every hook call.
Parameters
- fn: The callback function to execute before each hook.
Returns
- Return value 1: A function to unregister the before-each callback.
Method: callHook
Canonical path: hookable.Hookable.callHook
Declared in: src/hookable.ts
Signature
callHook<NameT extends HookNameT> (name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): Promise<any> {}
Summary
Calls hooks serially.
Behavior
Invokes the specified hook using a serial caller.
Parameters
- name: The name of the hook to call.
- args: Arguments to pass to the hook callbacks.
Returns
- Return value 1: A promise that resolves with the result of the serial hook execution.
Method: callHookParallel
Canonical path: hookable.Hookable.callHookParallel
Declared in: src/hookable.ts
Signature
callHookParallel<NameT extends HookNameT> (name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): Promise<any[]> {}
Summary
Calls hooks in parallel.
Behavior
Invokes the specified hook using a parallel caller.
Parameters
- name: The name of the hook to call.
- args: Arguments to pass to the hook callbacks.
Returns
- Return value 1: A promise that resolves with an array of results from the parallel hook execution.
Method: callHookWith
Canonical path: hookable.Hookable.callHookWith
Declared in: src/hookable.ts
Signature
callHookWith<NameT extends HookNameT, CallFunction extends (hooks: HookCallback[], args: Parameters<InferCallback<HooksT, NameT>>) => any> (caller: CallFunction, name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): ReturnType<CallFunction> {}
Summary
Internal method to execute hooks with a specific caller and lifecycle events.
Behavior
Executes hooks for a given name using a custom caller function, triggering global before and after listeners if they exist.
Parameters
- caller: A function responsible for executing the registered hook callbacks.
- name: The name of the hook to trigger.
- args: Arguments to be passed to the hook callbacks.
Returns
- Return value 1: The result returned by the caller function.
Method: deprecateHook
Canonical path: hookable.Hookable.deprecateHook
Declared in: src/hookable.ts
Signature
deprecateHook <NameT extends HookNameT> (name: NameT, deprecated: HookKeys<HooksT> | DeprecatedHook<HooksT>) {}
Summary
Deprecates a specific hook.
Behavior
Marks a hook as deprecated, optionally redirecting it to a new hook name, and re-registers existing callbacks under the new name.
Parameters
- name: The name of the hook to deprecate.
- deprecated: The deprecation configuration or the name of the new hook to use instead.
Method: deprecateHooks
Canonical path: hookable.Hookable.deprecateHooks
Declared in: src/hookable.ts
Signature
deprecateHooks (deprecatedHooks: Partial<Record<HookNameT, DeprecatedHook<HooksT>>>) {}
Summary
Deprecates multiple hooks at once.
Behavior
Registers multiple hook deprecations by merging them into the internal deprecation map and processing each one.
Parameters
- deprecatedHooks: An object mapping hook names to their deprecation configurations.
Method: hook
Canonical path: hookable.Hookable.hook
Declared in: src/hookable.ts
Signature
hook<NameT extends HookNameT> (name: NameT, fn: InferCallback<HooksT, NameT>, opts: { allowDeprecated?: boolean } = {}) {}
Summary
Registers a callback for a specific hook.
Behavior
Registers a callback for a hook, handling deprecation redirects and warnings if applicable.
Parameters
- name: The name of the hook.
- fn: The callback function to register.
- opts: Options for registration, such as allowing deprecated hooks without warning.
Returns
- Return value 1: A function to unregister the hook.
Method: hookOnce
Canonical path: hookable.Hookable.hookOnce
Declared in: src/hookable.ts
Signature
hookOnce<NameT extends HookNameT> (name: NameT, fn: InferCallback<HooksT, NameT>) {}
Summary
Registers a hook to be called only once.
Behavior
Registers a hook that unregisters itself immediately after its first execution.
Parameters
- name: The name of the hook to register.
- fn: The callback function to execute once.
Returns
- Return value 1: A function to unregister the hook before it is called.
Method: removeHook
Canonical path: hookable.Hookable.removeHook
Declared in: src/hookable.ts
Signature
removeHook<NameT extends HookNameT> (name: NameT, fn: InferCallback<HooksT, NameT>) {}
Summary
Removes a registered hook callback.
Behavior
Removes a specific callback from the registered hooks for a given name.
Parameters
- name: The name of the hook.
- fn: The callback function to remove.
Method: removeHooks
Canonical path: hookable.Hookable.removeHooks
Declared in: src/hookable.ts
Signature
removeHooks (configHooks: NestedHooks<HooksT>) {}
Summary
Removes multiple hooks defined in a nested configuration object.
Behavior
Flattens the provided nested hooks and removes each one by calling the internal removeHook method for every key-value pair.
Parameters
- configHooks: A nested hooks configuration object to be flattened and removed.
Type Alias: DeprecatedHook
Canonical path: hookable.DeprecatedHook
Declared in: src/types.ts
Signature
type DeprecatedHook<T> = { message?: string, to: HookKeys<T> }
Summary
Represents a hook that has been deprecated, containing an optional message and the new hook key it maps to.
Type Alias: DeprecatedHooks
Canonical path: hookable.DeprecatedHooks
Declared in: src/types.ts
Signature
type DeprecatedHooks<T> = { [name in HookKeys<T>]: DeprecatedHook<T> }
Summary
A map of hook names to their corresponding deprecation definitions.
Type Alias: HookCallback
Canonical path: hookable.HookCallback
Declared in: src/types.ts
Signature
type HookCallback = (...args: any) => Promise<void> | void
Summary
A function signature for hook listeners that can return a Promise or void and accepts any number of arguments.
Type Alias: HookKeys
Canonical path: hookable.HookKeys
Declared in: src/types.ts
Signature
type HookKeys<T> = keyof T & string
Summary
Represents the valid string keys of a given hooks object.
Type Alias: NestedHooks
Canonical path: hookable.NestedHooks
Declared in: src/types.ts
Signature
type NestedHooks<T> =
(Partial<StripGeneric<T>> | Partial<OnlyGeneric<T>>) &
Partial<{ [key in Namespaces<StripGeneric<T>>]: NestedHooks<WithoutNamespace<T, key>> }> &
Partial<{ [key in BareHooks<StripGeneric<T>>]: T[key] }>
Summary
A recursive type definition allowing hooks to be defined in a nested structure, supporting namespaces and generic hook types.