Hable
![size](http://img.badgesize.io/https://unpkg.com/hable/dist/hable.cjs.min.js?compression=gzip&style=flat-square)
Awaitable hooks for Node.js
Install
Using yarn:
yarn add hable
Using npm:
npm install hable
Usage
Extend your base class from Hable:
import Hookable from 'hable'
export default class Foo extends Hookable {
constructor() {
super()
}
async someFunction() {
await this.callHook('hook1')
await this.callHookParallel('hook2')
}
}
Inside plugins, register for any hook:
const lib = newFooLib()
lib.hook('hook2', async () => { })
lib.hookObj({
hook1: async () => { },
hook2: [ ]
})
Hookable class
hook (name, fn)
Register a handler for a specific hook. fn
can be a single function or an array.
addHooks(configHooks)
Flatten and register hooks object.
Example:
hookable.addHooks({
test: {
before: () => {},
after: () => {}
}
})
This registers test:before
and test:after
hooks at bulk.
async callHook (name, ...args)
Used by class itself to sequentially call handlers of a specific hook.
deprecateHook (old, name)
Deprecate hook called old
in flavor of name
hook.
clearHook (name)
Clear all hooks for a specific hook.
clearHooks ()
Clear all hooks registered in the class.
flatHooks (hooksObj)
Register many hooks using an object.
Credits
Extracted from Nuxt.js hooks system. Original author: Sébastien Chopin.
License
MIT