Skip to content

Cap32/emit-lite

Repository files navigation

Super Light weight Event Emitter for Node.js and browser.

Build Status

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

About

Light weight event-emitter for Node.js and browser

Resources

License

Stars

Watchers

Forks

Packages

No packages published