Skip to content

Instantly share code, notes, and snippets.

@rashidtvmr
Forked from mvneerven/events.js
Created June 9, 2022 05:23
Show Gist options
  • Select an option

  • Save rashidtvmr/d2d6680aa7eafb5a5f26b79c0d195bf6 to your computer and use it in GitHub Desktop.

Select an option

Save rashidtvmr/d2d6680aa7eafb5a5f26b79c0d195bf6 to your computer and use it in GitHub Desktop.
/**
* Simplest event system for any class to implement
*/
export class Events {
constructor(host) {
this.proxy = document.createDocumentFragment();
this.proxy.host = host;
["addEventListener", "dispatchEvent", "removeEventListener"].forEach(
this.delegate,
this
);
host.on = (eventName, func) => {
host.addEventListener(eventName, func);
return host;
};
host.emit = (eventName, options) => {
host.dispatchEvent(new CustomEvent(eventName, options));
}
}
delegate(method) {
this.proxy.host[method] = this.proxy[method].bind(this.proxy);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment