Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Last active April 12, 2017 08:56
Show Gist options
  • Select an option

  • Save gemmadlou/eb6761f63d659286c4fa53bfcffa98a8 to your computer and use it in GitHub Desktop.

Select an option

Save gemmadlou/eb6761f63d659286c4fa53bfcffa98a8 to your computer and use it in GitHub Desktop.
Simple Event Bus (ES6) < 20 lines
let bus = {
messages: {},
on(message, callback) {
if (!this.messages[message]) {
this.messages[message] = [];
}
if (typeof callback === 'function') {
this.messages[message].push(callback);
}
},
emit(message) {
if (this.messages[message]) {
this.messages[message].forEach((func) => {
func();
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment