An event utils system written in ES6.
An event utils system written in ES6.
npm install event-utils
import EventEmitter from "event-utils";
const emitter = new EventEmitter();
// foo on
function callback1(arg) {
console.log("callback1 called", arg);
}
function callback2() {
console.log("callback2 called");
}
function callbackOnce() {
console.log("callback called once");
}
emitter.on("foo", callback1);
emitter.on("foo", callback2);
emitter.once("foo", callbackOnce);
// bar on
function callbackBar() {
console.log("callbackBar called");
}
emitter.on("bar", callbackBar);
// Emit foo and bar
emitter.emit("foo", "arg");
emitter.emit("bar");
emitter.emit("foo"); // To test once
// Off
emitter.off("foo");
// Emit bar
emitter.emit("bar");
// off all = off bar
emitter.off("bar");
// Emit with no callbacks
emitter.emit("bar");
emitter.emit("foo");
An event utils system written in ES6.
Kind: global class
Creates an instance of EventEmitter.
EventEmitter
Add callback to event
Kind: instance method of EventEmitter
Param | Type |
---|---|
event | string |
cb | function |
EventEmitter
Add callback to event and remove on first call
Kind: instance method of EventEmitter
Param | Type |
---|---|
event | string |
cb | function |
EventEmitter
Remove callback from event
Kind: instance method of EventEmitter
Param | Type |
---|---|
event | string |
cb | function |
EventEmitter
Emit an event with arguments
Kind: instance method of EventEmitter
Param | Type |
---|---|
event | string |
…cbs | args |
Array.
Return all callbacks attached to an event
Kind: instance method of EventEmitter
Param | Type |
---|---|
event | string |
boolean
Return a boolean if the event has listeners
Kind: instance method of EventEmitter
Param | Type |
---|---|
event | string |
MIT. See license file.