npm.io
1.1.0 โ€ข Published 4 years ago

ei-tapable

Licence
MIT
Version
1.1.0
Deps
0
Size
15 kB
Vulns
0
Weekly
0

ei-tapable

Just a little publish-subscribe events library

GitHub

GitHub - ei-tapable

NPM

NPM version Build Status npm

NPM - ei-tapable

install

npm install --save ei-tapable

Usage

const {
  SyncHook
} = require('ei-tapable')

class Foo {
  constructor() {
    this.hooks = {
      tests: new SyncHook(['arg1']),
    }
  }
}

we can now use these hooks:

const foo = new Foo();
foo.hooks.tests.tap('xxxxPlugin', (arg1) => {
  console.log(arg1); // foo
})

need to call them:

foo.hooks.tests.call('foo');
Hook types
  • Sync. A sync hook can only be tapped with synchronous functions (using myhook.tap())

  • Waterfall. A waterfall hook also calls each tapped function in a row. Unlike the basic hook, it passes a return value from each function to the next function.

Interceptor

hook offer an additional interceptor API:

foo.hooks.intercept({
  call: (arg1, arg2) => {
    console.log(arg1, arg2);
  },
  register: ({type, name, cb} /* tapInfo */) => {
    return {
      type,
      name, 
      cb
    } // maybe return a new typeInfo object
  }
})

call: (...args) => void Adding call to your interceptor will trigger when hooks are triggered. You can access to hooks arguments.

tap: (tap: Tap) => void Adding tap to your interceptor will trigger when a plugin taps into a hook. Provided is the Tap object. Tap object can't be changed.

register: (tap: Tap) => Tap | undefined Adding register to your interceptor will trigger for each added Tap and allows to modify it.

Keywords