Debian Codegraph @

node/colorjs.io/src/hooks.d.ts

raw
1 /**
2 * This is for plugin authors.
3 * If you're not interested in writing plugins for Color.js, you can skip this.
4 *
5 * Hooks afford extensibility far beyond what can be achieved
6 * by overriding properties and methods, or using functions.
7 * Hooks allow plugin authors to change how Color.js’ internal code works,
8 * by adding custom callbacks to predefined points in the execution.
9 *
10 * You can find available hooks by searching the source code for `hooks.run(` in color.js.
11 * If you need a hook that is not present, we typically accept pull requests for new hooks pretty easily!
12 *
13 * The Hooks module exports both a hooks object that is used throughout Color.js (as a default export),
14 * as well as a Hooks class (as a named export) that can be used to create new sets of hooks.
15 */
16 export class Hooks {
17 // Can't find a way to type this more specifically
18 // without conflicting with the types of add and run
19 [name: string]: any;
20
21 /**
22 * Schedule a callback to be executed at a certain point in the source code
23 * @param name The name of the hook to add the callback to
24 * @param callback The code to run at the given hook.
25 * The callback will be callewd with (typically) the same context as the calling code,
26 * and a single object as its only argument (typically called `env`)
27 * with writeable properties for various aspects of the calling environment
28 * @param [first=false] Whether to prepend instead of append this callback to any existing callbacks
29 * on the same hook. Defaults to `false`
30 */
31 add (
32 name: string | string[],
33 callback: (env: Record<string, any>) => void,
34 first?: boolean,
35 ): void;
36 /**
37 * Creates a hook for plugin authors to add code to
38 * @param name The name of the hook to create.
39 * By convention, it's in the form `[class-name]-[function name]-[location in function body]`.
40 * Class name can be omitted if it's in `color.js`.
41 * Location in function body is typically something like `"start"`, `"end'`,
42 * `"before-conversion"`, `"after-init"`, etc.
43 * @param env Object with properties to be passed to the hook's callback.
44 * This will also be used as the function context, unless it has a `context` property,
45 * in which case that is used as the function context
46 */
47 run (name: string, env?: { context?: Record<string, any> } & Record<string, any>): void;
48 }
49
50 declare const hooks: Hooks;
51
52 export default hooks;