Skip to content

Instantly share code, notes, and snippets.

@fdrobidoux
Forked from jyotiarora2610/Pub sub pattern
Last active August 29, 2019 18:15
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 fdrobidoux/d71e6776e0e81391e11cdfbda7e52aa9 to your computer and use it in GitHub Desktop.
Save fdrobidoux/d71e6776e0e81391e11cdfbda7e52aa9 to your computer and use it in GitHub Desktop.
Simple publish/subscribe pattern in javascript.
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