-
-
Save fdrobidoux/d71e6776e0e81391e11cdfbda7e52aa9 to your computer and use it in GitHub Desktop.
Simple publish/subscribe pattern in javascript.
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
let subscribers = {}; | |
module.exports = { | |
publish(event, data) { | |
if (!subscribers[event]) return; | |
subscribers.forEach(callback => { | |
callback(data); | |
}) | |
}, | |
subscribe(event, callback) { | |
if (!subscribers[event]) { | |
subscribers[event] = []; | |
} | |
subscribers[event].push(callback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment