Skip to content

Instantly share code, notes, and snippets.

@johnny-b-good
Created February 1, 2019 08:53
Show Gist options
  • Save johnny-b-good/049738faaf1fbdf9622072ac4d38fb00 to your computer and use it in GitHub Desktop.
Save johnny-b-good/049738faaf1fbdf9622072ac4d38fb00 to your computer and use it in GitHub Desktop.
Simple JS event bus
function EventBus(props) {
this.bus = document.createElement('fakeelement');
this.addEventListener = function(event, callback) {
this.bus.addEventListener(event, callback);
};
this.removeEventListener = function(event, callback) {
this.bus.removeEventListener(event, callback);
};
this.dispatchEvent = function(event, detail) {
this.bus.dispatchEvent(
new CustomEvent(
event,
{ detail: detail }
)
);
};
}
var bus = new EventBus();
export default bus;
export {
EventBus as EventBus,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment