# hable

> Awaitable hooks for Node.js

Latest version **3.0.0** (published 2020-02-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install hable
pnpm add hable
yarn add hable
bun add hable
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2020-02-25 |
| First published | 2018-01-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 27.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 966 |
| Maintainers | pi0 |
| Keywords | hook, hookable, plugin, tapable, tappable |

## Links

- npm: https://www.npmjs.com/package/hable
- Repository: https://github.com/jsless/hable
- Homepage: https://github.com/jsless/hable#readme
- Issues: https://github.com/jsless/hable/issues
- npm.io page: https://npm.io/package/hable

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2020-02-25
- 3.0.0-0 (beta) — 2019-09-01
- 2.3.2 — 2019-09-02
- 2.3.1 — 2019-09-02
- 2.3.0 — 2019-09-01
- 2.2.1 — 2019-08-21
- 2.2.0 — 2019-08-21
- 2.1.0 — 2019-08-21
- 2.0.1 — 2019-08-21
- 2.0.0 — 2019-08-21
- 1.0.1 — 2019-03-16
- 1.0.0 — 2019-02-11
- 0.0.7 — 2018-01-28
- 0.0.6 — 2018-01-26
- 0.0.5 — 2018-01-26
- … 3 more at https://npm.io/package/hable/versions

## README

# Hable

[![CircleCI](https://img.shields.io/circleci/project/github/jsless/hable.svg?style=flat-square)](https://circleci.com/gh/jsless/hable)
[![Codecov](https://img.shields.io/codecov/c/github/jsless/hable.svg?style=flat-square)](https://codecov.io/gh/jsless/hable)
[![npm](https://img.shields.io/npm/v/hable.svg?style=flat-square)](https://www.npmjs.com/package/hable)
[![npm](https://img.shields.io/npm/dt/hable.svg?style=flat-square)](https://www.npmjs.com/package/hable)
[![size](http://img.badgesize.io/https://unpkg.com/hable/dist/hable.cjs.min.js?compression=gzip&style=flat-square)](https://unpkg.com/hable)

> Awaitable hooks for Node.js

## Install

Using yarn:

```bash
yarn add hable
```

Using npm:

```bash
npm install hable
```

## Usage

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

## Hookable class

### `constructor(logger)`

Custom logger. Default logger is `console` but you can use your own or [consola](https://github.com/nuxt/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:

```js
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](github.com/nuxt/nuxt.js) hooks system. Original author: [Sébastien Chopin](https://github.com/Atinux).

## License

MIT

---
_Source: https://npm.io/package/hable · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
