Created
February 28, 2018 11:07
-
-
Save Johannestegner/139b542f628f28c04544392959d4b99c to your computer and use it in GitHub Desktop.
Simple event emitter class to inherit from.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Event emitter class. | |
* Allows adding of event listeners (with on/off/once) which will be fired on emit call. | |
*/ | |
export default class EventEmitter { | |
constructor () { | |
this.listeners = {}; | |
} | |
/** | |
* Attach a listener to a given event. | |
* @param {string} type Event type. | |
* @param {function} handler Callback to fire. | |
* @return {EventEmitter} this | |
*/ | |
on (type, handler) { | |
if (!this.listeners[type]) { | |
this.listeners[type] = []; | |
} | |
this.listeners[type].push(handler); | |
return this; | |
} | |
/** | |
* Attach a listener to a given event. The listener will be removed when the event is fired. | |
* @param {string} type Event type | |
* @param {function} handler Callback to fire. | |
* @return {EventEmitter} this | |
*/ | |
once (type, handler) { | |
let inner = (...args) => { | |
handler(...args); | |
this.off(type, inner); | |
}; | |
this.on(type, inner.bind(this)); | |
} | |
/** | |
* Remove a given handler from a event. | |
* @param {string} type Event type. | |
* @param {function} handler Handler to remove. | |
* @return {EventEmitter} this | |
*/ | |
off (type, handler) { | |
if (!this.listeners[type]) { | |
throw new Error(`No listener exists on the ${type} event.`); | |
} | |
let index = this.listeners[type].indexOf(handler); | |
if (index < 0) { | |
throw new Error(`The provided handler for the ${type} event did not exist.`); | |
} | |
this.listeners.splice(index, 1); | |
return this; | |
} | |
/** | |
* Emit a given event. | |
* @param {string} type Event type. | |
* @param {...} args Arguments. | |
* @return {EventEmitter} | |
*/ | |
emit (type, ...args) { | |
if (this.listeners[type].length > 0) { | |
this.listeners[type].forEach((listener) => { | |
listener(...args); | |
}); | |
} | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment