event-intercept
v0.2.2
Published
Intercept event data before it reaches the listeners.
Downloads
9
Maintainers
Readme
event-intercept
Intercept event data before it reaches the listeners.
var intercept = require('event-intercept')
var foo = new EventEmitter()
// Map all data on 'hello' event to uppercase
intercept(foo, 'hello', (args, done) => {
done(null, args.map(data => data.toUpperCase()))
})
foo.on('hello', console.log)
foo.emit('hello', 'world', 'foo', 'bar')
// WORLD FOO BAR
Useful for injecting where you want to view/change data before it reaches listeners. Like parsing, logging, debugging, etc.
Installation
$ npm install --save event-intercept
Usage
intercept(emitter, event, handler)
Intercepts an event and it's data. These are executed in the order they are added.
Parameters
emitter
(EventEmitter
): The emitter you want to add intercepting on.event
(String
): Name of the event you want to intercept.handler
(Function
): A mapping function with the signature(args, done)
.
Handler
The handler has the parameters (args, done)
, and you call done
with parameters (end, args)
. Where end
can be true
(abort safely) or an Error
.
Example
intercept(emitter, 'message', function (args, done) {
// Reverse
args = args.map(x => x.split('').reverse().join(''))
// Callback
done(null, args)
})
License
MIT © Jamen Marz