tiny-emit
v0.2.0
Published
Another event emitter, only tiny.
Downloads
2
Readme
tiny-emit
Another event emitter, only tiny.
Install
Yarn
yarn add tiny-emit
NPM
npm install --save-dev tiny-emit
Usage
import tiny from 'tiny-emit';
const emitter = tiny();
emitter.on('foo', (bar) => {
console.log(bar); // bar
});
emitter.emit('foo', 'bar');
Once
let i = 0;
emitter.once('incr', () => (i += 1));
emitter.emit('incr');
emitter.emit('incr');
console.log(i); // 1
Off
There is many ways to switch off a listener for an event. Call the listeners off function returned whenever the listener was added. This works for both .on()
and .once()
methods.
const off = emitter.on('foo', () => {});
off();
Or use the .off()
method on the emitter.
emitter.off('foo', listener); // specific event and listener
emitter.off('foo'); // all listeners for given event
emitter.off(); // every listener for every event
API
#tiny()
Returns an emitter object.
Emitter Object
.on(<event>, <fn>)
event
String The event name.fn
Function The listener function.
Returns an off Function.
.once(<event>, <fn>)
event
String The event name.fn
Function The listener function.
Returns an off Function.
.off([event, [fn]])
event
String The event name.fn
Function The listener function.
Returns void.
.emit(<event>, [...args])
event
String The event name....args
Any Optional arguments to pass.
Returns void.
License
Copyright (c) 2017 Christopher Turner