events-ex
Browser-friendly enhanced events most compatible with standard node.js and coffee-script. It's modified from event-emitter mainly.
Difference with event-emitter and events
- domain is not supported yet(TODO)
broken change
: The event object bubbling Supports
- the event object as listener's "this" object.
- return the result property of event object to emitter.
- prevent the rest of listener from be excuted if set the stopped property of event object to true
broken change
: the emit
return the result of listeners' callback instead of the successful state.broken change
: the listeners' callback function this object is Event
Object instead of the emitter object.
- the emitter object is the this.target property now.
- add the defaultMaxListeners class property to keep compatible.
- add the setMaxListeners method to keep compatible.
- add
error
, newListener
and removeListener
events to keep compatible. - add listeners() method to keep compatible.
- add listenerCount() class method to keep compatible.
Installation
$ npm install event-ex
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
Usage
Node JS events Usage:
EventEmitter = require('event-ex')
inherits = require('inherits-ex')
# Demo the event object bubbling usage:
class MyDb
inherits MyDb, EventEmitter
get: (key)->
result = @emit 'getting', key
if isObject result
return if result.state is ABORT
return result.result if result.state is DONE
_get(key)
event-emitter usage:
var ee = require('event-ex/event-emitter');
var emitter = ee({}), listener;
emitter.on('test', listener = function (args) {
});
emitter.once('test', function (args) {
this.result = 18;
});
var result = emitter.emit('test', arg1, arg2);
emitter.emit('test', arg1, arg2);
emitter.off('test', listener);
emitter.emit('test', arg1, arg2);
Additional utilities
allOff(obj) (event-emitter/all-off)
Removes all listeners from given event emitter object
hasListeners(obj[, name]) (event-emitter/has-listeners)
Whether object has some listeners attached to the object.
When name
is provided, it checks listeners for specific event name
var emitter = ee();
var hasListeners = require('event-emitter/has-listeners');
var listener = function () {};
hasListeners(emitter);
emitter.on('foo', listener);
hasListeners(emitter);
hasListeners(emitter, 'foo');
hasListeners(emitter, 'bar');
emitter.off('foo', listener);
hasListeners(emitter, 'foo');
pipe(source, target[, emitMethodName]) (event-emitter/pipe)
Pipes all events from source emitter onto target emitter (all events from source emitter will be emitted also on target emitter, but not other way).
Returns pipe object which exposes pipe.close
function. Invoke it to close configured pipe.
It works internally by redefinition of emit
method, if in your interface this method is referenced differently, provide its name (or symbol) with third argument.
unify(emitter1, emitter2) (event-emitter/unify)
Unifies event handling for two objects. Events emitted on emitter1 would be also emitter on emitter2, and other way back.
Non reversible.
var eeUnify = require('event-emitter/unify');
var emitter1 = ee(), listener1, listener3;
var emitter2 = ee(), listener2, listener4;
emitter1.on('test', listener1 = function () { });
emitter2.on('test', listener2 = function () { });
emitter1.emit('test');
emitter2.emit('test');
var unify = eeUnify(emitter1, emitter2);
emitter1.emit('test');
emitter2.emit('test');
emitter1.on('test', listener3 = function () { });
emitter2.on('test', listener4 = function () { });
emitter1.emit('test');
emitter2.emit('test');