Last active
March 31, 2016 08:14
-
-
Save MikeDigitize/82650ba378742b320f05 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
var PubSub = (function() { | |
let listeners = {}; | |
return { | |
listeners, | |
subscribe (evt, fn) { | |
if(!this.listeners.hasOwnProperty(evt)) { | |
this.listeners[evt] = []; | |
} | |
this.listeners[evt].push(fn); | |
return () => { | |
if(this.listeners.hasOwnProperty(evt)) { | |
this.listeners[evt].splice(this.listeners[evt].indexOf(fn), 1); | |
if(!this.listeners[evt].length) { | |
delete this.listeners[evt]; | |
} | |
} | |
}; | |
}, | |
publish(evt, data) { | |
if(this.listeners.hasOwnProperty(evt)) { | |
this.listeners[evt].forEach(listener => listener(data)) | |
} | |
} | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment