Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

hable

jsless529.7kMIT3.0.0TypeScript support: included

Awaitable hooks for Node.js

hook, hookable, plugin, tapable, tappable

readme

Hable

CircleCI Codecov npm npm size

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() {
    // 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.

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

3.0.0 (2020-02-25)

Bug Fixes

Features

2.3.0 (2019-09-01)

Features

  • hide deprecate warnings on production builds (0861df3)

2.2.1 (2019-08-21)

2.2.0 (2019-08-21)

Features

2.1.0 (2019-08-21)

Features

  • optional fatal support for logger (7c7355d)

2.0.1 (2019-08-21)

2.0.0 (2019-08-21)

Features

BREAKING CHANGES

  • console is replaced by consola by default

1.0.1 (2019-03-16)

Bug Fixes

1.0.0 (2019-02-11)

Features

BREAKING CHANGES

  • api change

0.0.7 (2018-01-28)

Bug Fixes

  • hook with array or falsy key (7e90de1)

Performance Improvements

0.0.6 (2018-01-26)

Performance Improvements

  • reduce transpiled dist size (df607cf)

0.0.5 (2018-01-26)

Bug Fixes

0.0.4 (2018-01-26)

Performance Improvements

  • handle fn as array faster (ec35edc)

0.0.3 (2018-01-26)

Bug Fixes

  • bind hookObj to this context (6f6f7bc)

Performance Improvements

0.0.2 (2018-01-26)