Created
April 13, 2017 16:51
-
-
Save daartv/d4ff2b5ea7eec15917fd467893aa5ae9 to your computer and use it in GitHub Desktop.
Implement a Message Bus
This file contains hidden or 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
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