Skip to content

Instantly share code, notes, and snippets.

@DylanCodeCabin
Created November 24, 2022 07:12
Show Gist options
  • Save DylanCodeCabin/7dc532ece8f440b434849ab445988728 to your computer and use it in GitHub Desktop.
Save DylanCodeCabin/7dc532ece8f440b434849ab445988728 to your computer and use it in GitHub Desktop.
class CustomEventSystem {
constructor(target) {
this.target = target;
}
trigger(event, data){
const relay = new CustomEvent(event, { detail: (data ? data : false) });
this.target.dispatchEvent(relay);
}
listen(event, listener){
this.target.addEventListener(event, listener);
}
}
/* Now we use it */
evSystem = new CustomEventSystem(document.querySelector('.my-element'));
evSystem.listen("hello", () => {
console.log("Hey there");
});
evSystem.trigger("hello");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment