Skip to content

Instantly share code, notes, and snippets.

@daartv
Created April 13, 2017 16:51
Show Gist options
  • Save daartv/d4ff2b5ea7eec15917fd467893aa5ae9 to your computer and use it in GitHub Desktop.
Save daartv/d4ff2b5ea7eec15917fd467893aa5ae9 to your computer and use it in GitHub Desktop.
Implement a Message Bus
let messageBus = {
topics: {},
subscribe: (topic, listener) => {
if (!this.topics[topic]) {
this.topics[topic] = []
}
this.topics[topic].push(listener)
},
publish: (topic, payload) => {
if (!this.topics[topic] || this.topics[topic].length < 1) {
return
}
this.topics[topic].forEach((listener) => {
listener(data || {})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment