1.0.8 • Published 12 months ago

@evio/hookable v1.0.8

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

@evio/hookable

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

Usage

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

使用全局注册

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
1.0.8

12 months ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago