# @evio/hookable

> some description

Latest version **1.0.8** (published 2023-05-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @evio/hookable
pnpm add @evio/hookable
yarn add @evio/hookable
bun add @evio/hookable
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2023-05-23 |
| First published | 2023-05-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | evio |

## Links

- npm: https://www.npmjs.com/package/@evio/hookable
- npm.io page: https://npm.io/package/@evio/hookable

## Dependencies (1)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Recent versions

- 1.0.8 (latest) — 2023-05-23
- 1.0.7 — 2023-05-10
- 1.0.6 — 2023-05-10
- 1.0.5 — 2023-05-09
- 1.0.4 — 2023-05-09
- 1.0.3 — 2023-05-08
- 1.0.2 — 2023-05-08
- 1.0.1 — 2023-05-06

## README

# `@evio/hookable`

以最简单方式实现 Class hookable 模型。它可以将class作为一个工作流，通过标准的I/O模型产生数据。

## Usage

```ts
import { Hook } from '@pjblog/hookable';

class ABC extends Hook<number, number> {
  public res = 0;
  
  @Hook.Entry
  @Hook.Node
  public async a() {
    this.res += this.req * 2;
    const j = await this.b(4);
    this.res += j;
  }

  @Hook.Node
  async b(a: number) {
    this.res += this.req * 3 + a;
    return 1;
  }
}

// invoke

const obj = new ABC(99);

obj.$hook('b').before('c', o => {
  console.log('c')
  o.res += 1;
})

obj.$hook('b').insertAfter('c', 'd', o => {
  console.log('d')
  o.res += 1;
})

obj.$hook('b').insertBefore('c', 'e', o => {
  console.log('e')
  o.res -= 1;
})

obj.$execute().then(console.log).catch(console.error);

// output：
// e
// c
// d
// 501
```

## Use Container

使用全局注册

```ts
import { Hook } from '@pjblog/hookable';
@Hook.Container
class ABC extends Hook<number, number> {
  public res = 0;
  
  @Hook.Entry
  @Hook.Node
  public async a() {
    this.res += this.req * 2;
    const j = await this.b(4);
    this.res += j;
  }

  @Hook.Node
  async b(a: number) {
    this.res += this.req * 3 + a;
    return 1;
  }
}

const unbind = Hook.use(ABC, abc => {
  abc.$hook('a').after('k', obj => {
    console.log('k')
    obj.res += 1;
  })
})

// invoke

const obj = new ABC(99);

obj.$hook('b').before('c', o => {
  console.log('c')
  o.res += 1;
})

obj.$hook('b').insertAfter('c', 'd', o => {
  console.log('d')
  o.res += 1;
})

obj.$hook('b').insertBefore('c', 'e', o => {
  console.log('e')
  o.res -= 1;
})

obj.$execute().then(console.log).catch(console.error);

setTimeout(() => {
  unbind();
  console.log('-----------')
  const x = new ABC(99);
  x.$execute().then(console.log).catch(console.error);
}, 2000);

// output:
// e
// c
// d
// k
// 502
// -----------
// 500
```

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