node-chain-event-emitter
v2.1.2
Published
Chain Event Emitter for NodeJS
Downloads
10
Maintainers
Readme
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 );