Skip to content

Instantly share code, notes, and snippets.

@harish2704
Created November 23, 2023 08:47
Show Gist options
  • Save harish2704/2cff29440d629a2d21002b3cbb7063bf to your computer and use it in GitHub Desktop.
Save harish2704/2cff29440d629a2d21002b3cbb7063bf to your computer and use it in GitHub Desktop.
Simple event bus in Client side JS
// Source: https://itnext.io/handling-data-with-web-components-9e7e4a452e6e
class EventBus {
constructor() {
this._bus = document.createElement('div');
}
register(event, callback) {
this._bus.addEventListener(event, callback);
}
remove(event, callback) {
this._bus.removeEventListener(event, callback);
}
fire(event, detail = {}) {
this._bus.dispatchEvent(new CustomEvent(event, { detail }));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment