Last active
December 12, 2016 23:42
-
-
Save devsnek/98cab72bfd527690a30c11e59c632384 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
/** | |
* @class EventEmitter | |
* @example | |
* var e = new EventEmitter(); | |
* | |
* e.on('hello', console.log); // 'hi how are you' | |
* | |
* e.emit('hello', 'hi', 'how are you'); | |
*/ | |
class EventEmitter { | |
/** | |
* Event listener | |
* @callback listener~callback | |
* @param {String} e The name of the event | |
* @param {listener~callback} d The callback with the arguments passed to the event from emitter | |
*/ | |
on(name, callback) { | |
window.addEventListener(name, (event) => callback.apply(this, event.detail)); | |
} | |
/** | |
* Emit an event | |
* @param {String} name The name of the event. | |
* @param {...*} Any valid things to pass to the event | |
*/ | |
emit(name) { | |
window.dispatchEvent(new CustomEvent(name, { 'detail': Array.prototype.slice.call(arguments, 1) })); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment