events-ex
![license](https://img.shields.io/npm/l/events-ex.svg)
Browser-friendly enhanced events most compatible with standard node.js and coffee-script. It's modified from event-emitter mainly. It can add event-able to your class directly.
Features
- Rewrite of the core architecture for improved performance and more powerful event-able ability
- keep most compatible with node events and event-emitter
- Hookable event system for more control over event handling
- Supports async event emitting
Differences
- Difference with node events
broken change
: The event object supports bubbling
- the event object as listener's "this" object.
- return the result property of event object to the emitter.
- prevent the rest of listener from be executed if the stopped property of the event object is set to true
broken change
: The emit
return the result of listeners's callback function instead of the successful state.broken change
: The this
object of listeners' callback function is the Event
Object instead of the emitter object.
- The emitter object is put into the
target
property of the Event
Object.
- Difference with event-emitter
broken change
: The event object supports bubbling(see above)- Adds the defaultMaxListeners class property to keep compatibility with node events.
- Adds the setMaxListeners method to keep compatible with node events.
- Adds
error
, newListener
and removeListener
events to keep compatibility with node events. - Adds listeners() method to keep compatibility with node events.
- Adds listenerCount() class method to keep compatibility with node events.
Installation
npm install events-ex
Usage
Extends from EventEmitter
:
import {EventEmitter} from 'events-ex';
class MyClass extends EventEmitter {}
Add the event-able feature to your class directly:
import {eventable} from 'events-ex';
class MyClass {}
eventable(MyClass);
Now uses it:
var my = new MyClass;
my.on('event', function() {
console.log('event occur');
});
my.emit('event');
Bubbling event usage:
import {EventEmitter, states} from 'events-ex';
import {isObject} from 'util-ex';
class MyDb extends EventEmitter {
get(key) {
let result = this.emit('getting', key)
if(isObject(result)) {
if (result.state === states.ABORT) return
if (result.state === states.DONE) return result.result
}
return _get(key)
}
}
let db = new MyDb
db.on('getting', function(key){
result = myGet(key);
if (result != null) {
this.result = {
state: states.DONE,
result: result,
}
} else if (result === null) {
this.result = {state: states.ABORT};
}
})
event-emitter usage:
import {wrapEventEmitter as ee} from 'events-ex';
class MyClass { };
ee(MyClass.prototype);
const emitter = new MyClass();
let listener;
emitter.on('test', listener = function (args) {
});
emitter.once('test', function (args) {
});
emitter.emit('test', arg1, arg2);
emitter.emit('test', arg1, arg2);
emitter.off('test', listener);
emitter.emit('test', arg1, arg2);
API
eventable(class[, options]) (events-ex/eventable)
Add the event-able ability to the class directly.
class
: the class to be injected the ability.options
(object): optional options
include
(string[]|string): only these emitter methods will be added to the class
- NOTE: static method should use the prefix '@' with name.
exclude
(string[]|string): theses emitter methods would not be added to the class
- NOTE: static method should use the prefix '@' with name.
methods
(object): hooked methods to the class
- key: the method name to hook.
- value: the new method function
- use
this.super()
to call the original method. this.self
is the original this
object.
classMethods
(object): hooked class methods to the class
import {eventable} from 'events-ex'
class OtherClass {
exec() {console.log "my original exec"}
}
class MyClass {}
eventable(MyClass, include: ['on', 'off', 'emit', 'emitAsync', '@listenerCount'])
eventable(OtherClass, {methods: {
exec() {
console.log("new exec")
this.super()
}}
})
allOff(obj) (events-ex/all-off)
keep compatible only: the removeAllListeners
has already been buildin.
Removes all listeners from given event emitter object
hasListeners(obj[, name]) (events-ex/has-listeners)
Whether object has some listeners attached to the object.
When name
is provided, it checks listeners for specific event name
import {hasListeners, wrapEventEmitter as ee} from 'events-ex/has-listeners';
var emitter = ee();
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]) (events-ex/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) (events-ex/unify)
Unifies event handling for two objects. Events emitted on emitter1 would be also emitter on emitter2, and other way back.
Non reversible.
import {unify as eeUnify, wrapEventEmitter as ee} from 'events-ex';
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');