1.1.0 • Published 2 years ago

ei-tapable v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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.

1.1.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago