Created
May 10, 2018 16:58
-
-
Save MichaelDimitras/4bfc9566bcae5bcb130895465c737459 to your computer and use it in GitHub Desktop.
message-buss
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
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