Skip to content

Instantly share code, notes, and snippets.

@Sara3
Created July 20, 2017 16:59
Show Gist options
  • Save Sara3/6cca7239911ff956ab366571ec196c01 to your computer and use it in GitHub Desktop.
Save Sara3/6cca7239911ff956ab366571ec196c01 to your computer and use it in GitHub Desktop.
var messageBus = {
subscrib: function(str, callback) {
// make a key in the parent object
if (!this[str]) {
this[str] = [];
this[str].push(callback)
} else {
this[str].push(callback)
}
},
publish: function(str, val) {
console.log(str)
for ( property in this) {
console.log(property)
}
if(str!=='topic') {
return 'no subscriber for that topic';
}
for(let i = 0; i < this[str].length; i++) {
this[str][i](val);
}
}
}
messageBus.subscrib('topic', (arr)=> {
console.log('callback called!', arr)
});
messageBus.subscrib('topic', (arr)=> {
console.log('callback2 called!', arr)
});
messageBus.publish('topic', 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment