Events Plus
This module defines a class implementing Node's events.EventEmitter
, but with an extra method: .handle()
.
This allows cascading of events, with each handler choosing to either consume the event or pass it on (possibly asynchronously):
var EventPlus = require('eventplus');
var emitter = new EventPlus();
emitter.handle('myevent', function (foo, bar, next) {
if (foo === 'foo') {
} else {
next();
}
});
emitter.handle('myevent', function (foo, bar, next) {
someAsyncFunction(bar, function (error, result) {
if (error) return next();
});
});
emitter.on('myevent', function (foo, bar) {
console.log('Unhandled myevent:', foo, bar);
});
emitter.emit('myevent', 'foo', 'bar');
This module does not depend on Node's EventEmitter
, but it implements the same API.
Methods
This class implements all the methods of Node's events.EventEmitter
, plus the following:
.handle(event, handler)
Events are a broadcast system - when .emit()
is called, all listeners get notified.
With .handle()
, you have the option to consume the event such that no other handlers or listeners are notified. This is done asynchronously, by appending an additional argument (next()
) to the event arguments for handlers.
myObj.handle('message', function (arg1, next) {
});
myObj.emit('message', 'foo');
If the supplied next()
is called more than once, an error is thrown.
Handlers are always called before listeners. Handlers are called in the order they are registered.
.handlers()
Same as .listeners()
, but for handlers.
.off(?event, ?fn)
The .off()
method can also be used to remove a function as either a listener or a handler. If fn
is not supplied, all handlers/listeners for that event are removed. If event
is not supplied then all handlers/listeners for all events are removed.
For listeners, this makes it equivalent to either .removeListener()
or .removeAllListeners()
, depending on arguments used.
Much like .removeListener()
, if a function is present in both capacities, or is present multiple times, it will only be removed once and it is undefined which entries will be removed first.
Similar to the newListener
and removeListener
events (fired before a listener is added or after it's removed, respectively), two new events are introduced:
newHandler
Called before a new handler is added, with two arguments:
event
- event name (string)handler
- handler function to be added
removeHandler
Called after a handler is removed, with two arguments:
event
- event name (string)handler
- handler function that was removed
Inheriting
Nothing lives in the prototype, so you can include the interface just by calling the constructor. You can pass in the object to enhance as the first argument - otherwise it uses this
:
function MyClass() {
EventPlus(this);
}
MyClass.prototype = {...}
Minimal version
The version in basic.js
contains only the basic methods:
.on()
.handle()
.off()
.emit()
The minified version basic.min.js
is quite small (around 750 bytes) so that you can easily include it in your own project.
License
This package is released as CC-0 - you can re-use any part of it in any way without restriction (including re-licensing under different terms).