Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stagas/eventstack
Middleware for EventEmitters
https://github.com/stagas/eventstack
Last synced: 13 days ago
JSON representation
Middleware for EventEmitters
- Host: GitHub
- URL: https://github.com/stagas/eventstack
- Owner: stagas
- License: mit
- Created: 2011-10-24T08:07:57.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2012-02-06T12:01:04.000Z (almost 13 years ago)
- Last Synced: 2024-10-10T18:14:21.948Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 101 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
```javascript
ee.use('some event', function (args, next) {
// do stuff here
// ...
// move on
next();
});
```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:
```javascript
var ee = new EventStack();
```Inherit (just like a regular EventEmitter):
```javascript
function Cat () {
EventStack.call(this);
}util.inherits(Cat, EventStack);
Cat.prototype.meow = function () {
this.emit('meow');
}
```Patch existing EventEmitter:
```javascript
var ee = new EventEmitter();
EventStack(ee); // patched!
ee.use(...);
```## Example
```javascript
var EventStack = require('eventstack');var ee = new EventStack;
// add a middleware that waits for `bar` messages on `foo`
// and re-emits `foobar!` to `bar`!
ee.use('foo', function (args, next) {
if (args[1] === 'bar') {
this.emit('bar', 'foobar!');
} else {
next();
}
});ee.on('foo', function (s) {
console.log('listener foo:', s);
});ee.on('bar', function (s) {
console.log('listener bar:', s);
});ee.emit('foo', 'not bar :(');
ee.emit('foo', 'bar');```
## Licence
MIT/X11