Skip to content

Instantly share code, notes, and snippets.

@MichaelDimitras
Created May 10, 2018 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelDimitras/4bfc9566bcae5bcb130895465c737459 to your computer and use it in GitHub Desktop.
Save MichaelDimitras/4bfc9566bcae5bcb130895465c737459 to your computer and use it in GitHub Desktop.
message-buss
class messageBus {
constructor() {
this.subscribers = {};
}
subscribe(topic, cb) {
if (this.subscribers[topic]) {
this.subscribers[topic].push(cb)
} else {
this.subscribers[topic] = [cb];
}
}
publish(topic, payload) {
if(this.subscribers[topic]) {
for(var i = 0; i < this.subscribers[topic].length; i++) {
this.subscribers[topic][i](payload);
}
}
}
}
const mb = new messageBus;
const componentA = () => {
mb.subscribe('new_signup', function(payload) {
console.log(payload);
});
}
const componentB = () => {
mb.publish('new_signup', {'foo': 'bar', 'goo': 'gar'});
}
componentA();
componentB();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment