EventStack
Middleware for EventEmitters
Installation
npm install eventstack
Description
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:
ee;
The function is called in the EventEmitter context with 2 arguments,
args
and next
.
args
is an Array
with the event arguments, including the event name, you
can use to inspect or transform.
You call 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.
Usage
Standalone:
var ee = ;
Inherit (just like a regular EventEmitter):
{ EventStack;} util; Catprototype { this;}
Patch existing EventEmitter:
var ee = ;; // patched!ee;
Example
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;
Licence
MIT/X11