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
Subscribes to beforeEach and afterEach hooks to log execution time and arguments to the console, optionally using groups and filters.
Parameters
- hooks: The Hookable instance to monitor.
- _options: Optional settings to control logging behavior, such as tags and filters.
Returns
- Return value 1: An object containing a close method to stop debugging and remove the added 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 single-level object with namespaced keys.
Behavior
Iterates through a nested hooks object and populates a flat object where keys are joined by colons for nested structures.
Parameters
- configHooks: The nested hooks configuration to flatten.
- hooks: The target object to populate with flattened hooks.
- parentName: An optional prefix used for keys when recursing into nested objects.
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 objects into one.
Behavior
Flattens multiple hook objects and merges them into a single object, wrapping multiple callbacks for the same key in a serial execution function.
Parameters
- hooks: A rest parameter of nested hook objects to merge.
Returns
- Return value 1: A single object containing the merged 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 functions to execute.
- args: Optional arguments to pass to each hook function.
Returns
- Return value 1: A promise that resolves when all hooks have finished executing.
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 tasks sequentially.
Behavior
Reduces an array of tasks into a promise chain, executing the provided function for each task in order.
Parameters
- tasks: An array of items to process.
- fn: A function to execute for each task, which can return a promise.
Returns
- Return value 1: A promise that resolves when all tasks have been processed.
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 function one after another with the provided arguments.
Parameters
- hooks: An array of hook functions to be called sequentially.
- args: Optional arguments to pass to each hook.
Returns
- Return value 1: A promise that resolves after the final hook in the sequence completes.
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 logging prefixes, inspection toggles, and hook 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 based on a nested configuration object.
Behavior
Flattens the provided nested hooks and removes each one by calling removeHook 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 deprecated hook with an optional migration message and the new hook key.
Type Alias: DeprecatedHooks
Canonical path: hookable.DeprecatedHooks
Declared in: src/types.ts
Signature
type DeprecatedHooks<T> = { [name in HookKeys<T>]: DeprecatedHook<T> }
Summary
A mapping of hook names to their respective deprecation details.
Type Alias: HookCallback
Canonical path: hookable.HookCallback
Declared in: src/types.ts
Signature
type HookCallback = (...args: any) => Promise<void> | void
Summary
A function type that can return a Promise or void, used as a hook listener.
Type Alias: HookKeys
Canonical path: hookable.HookKeys
Declared in: src/types.ts
Signature
type HookKeys<T> = keyof T & string
Summary
Extracts the keys of a type T that are also strings.
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 representing hooks that can be organized into namespaces or provided as a flat object.