JSPM

  • Created
  • Published
  • Downloads 1842571
  • Score
    100M100P100Q195339F
  • License MIT

Awaitable hooks for Node.js

Package Exports

  • hookable

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hookable) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Hookable

CircleCI Codecov npm npm size

Awaitable hooks for Node.js

Install

Using yarn:

yarn add hookable

Using npm:

npm install hookable

Usage

Extend your base class from Hookable:

import Hookable from 'hookable'

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:

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 */ ]
})

Hookable class

constructor(logger)

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)

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 favor of name hook.

deprecateHooks (deprecatedHooks)

Deprecate all hooks from an object (keys are old and values or newer ones).

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

Thanks to Joe Paice for donating hookable package name

License

MIT