Created
June 24, 2017 21:55
-
-
Save guzmonne/ad8d44ab7b32b65e1a7b13ac1b77925c to your computer and use it in GitHub Desktop.
cognito-auth.EventEmitter.js
This file contains hidden or 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
| (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