Middleware for EventEmitters
npm install eventstack
Sometimes I wished I could take an event and apply some logic to it before it reaches the listener. So I made this thingy.
It is still a regular EventEmitter, with an extra
.use() method where you
use to add middleware to your events.
The API looks like this:
The function is called in the EventEmitter context with 2 arguments,
args is an
Array with the event arguments, including the event name, you
can use to inspect or transform.
next() to move to the next middleware, or emit the event when
it reaches the end of the stack. It is not necessary to call
next() all the
time, depending on your logic, you could use
this.emit(...) to emit
a different event or even do nothing.
var ee = ;
Inherit (just like a regular EventEmitter):
Patch existing EventEmitter:
var ee = ;; // patched!ee;
var EventStack = ;var ee = ;// add a middleware that waits for `bar` messages on `foo`// and re-emits `foobar!` to `bar`!ee;ee;ee;ee;ee;