Skip to content

Instantly share code, notes, and snippets.

@guzmonne
Created June 24, 2017 21:55
Show Gist options
  • Select an option

  • Save guzmonne/ad8d44ab7b32b65e1a7b13ac1b77925c to your computer and use it in GitHub Desktop.

Select an option

Save guzmonne/ad8d44ab7b32b65e1a7b13ac1b77925c to your computer and use it in GitHub Desktop.
cognito-auth.EventEmitter.js
(function(win){
var events = {};
function on(event, listener) {
if (typeof events[event] !== 'object') {
events[event] = [];
}
events[event].push(listener);
}
function off(event, listener) {
if (typeof events[event] === 'object') {
var idx = events[event].indexOf(listener)
if (idx > -1) {
events[event].splice(idx, 1)
}
}
}
function emit() {
var args = Array.prototype.slice.call(arguments);
var event = args[0]
if (typeof events[event] === 'object') {
events[event].map(function (listener) {
listener.apply(null, args.slice(1))
})
}
}
function once(event, listener) {
function autoRemoveListener() {
var args = Array.prototype.slice.call(arguments);
off(event, autoRemoveListener)
listener.apply(null, args)
}
on(event, autoRemoveListener)
}
win.EventEmitter = Object.freeze({
on: on,
off: off,
emit: emit,
once: once,
events: events,
})
})(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment