Skip to content

Instantly share code, notes, and snippets.

@sTiLL-iLL
Last active August 29, 2015 14:05
Show Gist options
  • Save sTiLL-iLL/0f7787612b9a60db9647 to your computer and use it in GitHub Desktop.
Save sTiLL-iLL/0f7787612b9a60db9647 to your computer and use it in GitHub Desktop.
var events = (function(){
var topics = {};
return {
subscribe: function(topic, listener) {
if(!topics[topic]) topics[topic] = {
queue: []
};
var index = topics[topic].queue.push(listener) -1;
return {
remove: function() {
delete topics[topic].queue[index];
}
};
},
publish: function(topic, info) {
if(!topics[topic] || !topics[topic].queue.length) {
return;
}
var items = topics[topic].queue;
items.forEach(function(item) {
item(info || {});
});
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment