Register and unregister hooks
mainHooks can be registered individually using .hook() or in bulk using .addHooks().
To unregister hooks, you can:
- Use the unregister function returned by
.hook()or.addHooks(). - Use
.removeHook(name, fn)to remove a specific handler. - Use
.removeHooks(configHooks)to remove multiple handlers at once. - Use
.removeAllHooks()to clear everything.
const lib = new FooLib();
const hook0 = async () => { /* ... */ };
const hook1 = async () => { /* ... */ };
const hook2 = async () => { /* ... */ };
// The hook() method returns an "unregister" function
const unregisterHook0 = lib.hook("hook0", hook0);
const unregisterHooks1and2 = lib.addHooks({ hook1, hook2 });
/* ... */
unregisterHook0();
unregisterHooks1and2();
// or
lib.removeHooks({ hook0, hook1 });
lib.removeHook("hook2", hook2);