node-chain-event-emitter

2.1.2 • Public • Published

node-chain-event-emitter

Chain Event Emitter for Node JS

npm i -s node-chain-event-emitter

const EventEmitter = require('node-chain-event-emitter');

let emitter = new EventEmitter();


emitter.on( 'test', ( data, next ) => {

  console.log({ firstHandler: data });
  
  if ( ! data.works ) {
    next();
  }
  
});


emitter.on( '*', ( data, next ) => {

  console.log({ beforeAllEvents: data});
  next();
  
});


emitter.on('test', (data, next) => {

  console.log( { secondHandler: data } );
  next();
  
});

emitter.emit('test', { works: true } );

//  { beforeAllEvents: { works: true } }
//  { firstHandler: { works: true } }

If you want to change passed data while handling event, call next function with changed data as an argument:

emitter.on('test', (data, next) => {

  // Do something with data
  next(data);
  
});

If you want to change context in the each handler you should pass it to the ChainEventEmitter constructor in the options object:

const EventEmitter = require('node-chain-event-emitter');
const ctx = {};

let emitter = new EventEmitter({ ctx });

Also You can do it on-the-fly:

const context = {};

emitter.emit('test', {}, ctx );

Package Sidebar

Install

npm i node-chain-event-emitter

Weekly Downloads

0

Version

2.1.2

License

MIT

Unpacked Size

28.5 kB

Total Files

10

Last publish

Collaborators

  • pashaman