Awaitable hooks for Node.js
npm install hable




> Awaitable hooks for Node.js
Using yarn:
``bash`
yarn add hable
Using npm:
`bash`
npm install hable
Extend your base class from Hable:
`js
import Hookable from 'hable'
export default class Foo extends Hookable {
constructor() {
// Call to parent to initialize
super()
// Initialize Hookable with custom logger
// super(consola)
}
async someFunction() {
// Call and wait for hook1 hooks (if any) sequential
await this.callHook('hook1')
// Call and wait for hook2 hooks (if any) in parallel`
await this.callHookParallel('hook2')
}
}
Inside plugins, register for any hook:
`js
const lib = newFooLib()
// Register a handler for hook2
lib.hook('hook2', async () => { / ... / })
// Register multiply handlers at once
lib.hookObj({
hook1: async () => { / ... / },
hook2: [ / can be also an array / ]
})
`
Custom logger. Default logger is console but you can use your own or consola.
It should be an object implementing following functions:
- warn
- error
- fatal (optional)
Register a handler for a specific hook. fn can be a single function or an array.
Flatten and register hooks object.
Example:
`js
hookable.addHooks({
test: {
before: () => {},
after: () => {}
}
})
`
This registers test:before and test:after hooks at bulk.
Used by class itself to sequentially call handlers of a specific hook.
Deprecate hook called old in favor of name` hook.
Deprecate all hooks from an object (keys are old and values or newer ones).
Clear all hooks for a specific hook.
Clear all hooks registered in the class.
Register many hooks using an object.
Extracted from Nuxt.js hooks system. Original author: Sébastien Chopin.
MIT