@peacockproject/core
Version:
Type definitions for Peacock's core.
85 lines (84 loc) • 2.44 kB
TypeScript
/**
* A HookMap is a helper class for a Map with Hooks.
*
* @example
* const myHookMap = new HookMap(key => new SyncHook())
*
* @example
* hookMap.for("some-key").tap("MyPlugin", (arg) => { })
*
* @example
* const hook = hookMap.for("some-key")
* hook.call("some value", 123456)
*/
export declare class HookMap<Hook> {
private readonly _createFunc;
private readonly _map;
constructor(_createFunc: (key: string) => Hook);
/**
* Get a hook for the given key.
*
* @param key The hook to get.
* @returns The hook.
*/
for(key: string): Hook;
}
/**
* The options for a hook. Will either be just the name (as a string), or an object containing the additional options.
*/
export type TapOptions = string | {
name: string;
context: boolean;
};
type AsArray<T> = T extends any[] ? T : [T];
/**
* An internal interface containing the properties held by a single taps' container object.
*/
interface Tap<T, R> {
name: string;
func: (...args: AsArray<T>) => R;
enableContext: boolean;
}
/**
* The base for a hook.
*
* @see SyncHook
* @see SyncBailHook
* @see AsyncSeriesHook
*/
export declare abstract class BaseImpl<Params, Return = void> {
protected _taps: Tap<Params, Return>[];
/**
* Tap the hook.
*
* @param nameOrOptions A string containing the tap's name, or an object containing the tap's details.
* @param consumer The function that will be called when the hook is.
* @see TapOptions
*/
tap(nameOrOptions: TapOptions, consumer: (...args: AsArray<Params>) => Return): void;
/**
* Destructively remove all taps from the hook.
* Are you SURE you know what you're doing if you want to use this?
*/
resetTaps(): void;
get allTapNames(): string[];
}
/**
* A hook that runs each tap one-by-one.
*/
export declare class SyncHook<Params> extends BaseImpl<Params> {
call(...params: AsArray<Params>): void;
}
/**
* A hook that runs each tap one-by-one until one returns a result.
*/
export declare class SyncBailHook<Params, Return> extends BaseImpl<Params, Return> {
call(...params: AsArray<Params>): Return | null;
}
/**
* A hook that runs each tap, one-by-one, in an async context (each tap may be an async function).
*/
export declare class AsyncSeriesHook<Params> extends BaseImpl<Params, Promise<void>> {
callAsync(...params: AsArray<Params>): Promise<void>;
}
export {};