Skip to content

Instantly share code, notes, and snippets.

@devsnek
Last active December 12, 2016 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devsnek/98cab72bfd527690a30c11e59c632384 to your computer and use it in GitHub Desktop.
Save devsnek/98cab72bfd527690a30c11e59c632384 to your computer and use it in GitHub Desktop.
'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