emit-lite
v2.0.0
Published
Super Light weight Event Emitter for Node.js and browser
Downloads
3
Readme
Super Light weight Event Emitter for Node.js and browser.
Installing
Using npm:
$ npm install emit-lite
Using yarn:
$ yarn add emit-lite
Usage
emitter.on(eventType, handler)
emitter.emit(eventType, [arg[, ...]])
import EmitLite from 'emit-lite';
const emitter = new EmitLite();
emitter.on('test:on', (a, b) => {
console.log(a); // => 'a'
console.log(b); // => 'b'
});
emitter.emit('test:on', 'a', 'b');
emitter.once(eventType, handler)
emitter.once('test:once', () => {
console.log('once'); // will only log once.
});
emitter.emit('test:once');
emitter.emit('test:once');
emitter.off(eventType[, handler])
const handler = () => console.log('emitted'); // will never log anything
emitter.on('test:off', handler);
emitter.off('test:off');
emitter.emit('test:off');
emitter.off()
could also be done as
const handler = () => console.log('emitted'); // will never log anything
const off = emitter.on('test:off', handler);
off();
emitter.emit('test:off');
emitter.listenerCount(eventType)
const count = 3;
new Array(count).fill().forEach(() => emitter.on('test:listenerCount'));
console.log(emitter.listenerCount('test:listenerCount')); // => 3;
EmitLite.mixin(targetObject)
import EmitLite from 'emit-lite';
const obj = { a: 'a' };
EmitLite.mixin(obj);
obj.on('test:mixin', (b) => {
console.log(obj.a); // => 'a'
console.log(b); // => 'b'
});
obj.emit('test:mixin', 'b');
ES6 class extends
import EmitLite from 'emit-lite';
class Obj extends EmitLite {
getB() {
return 'b';
}
}
const obj = new Obj();
obj.on('test:extends', function (a) {
console.log(a); // => 'a'
console.log(this.getB()); // => 'b'
});
obj.emit('test:extends', 'a');
License
MIT