-
-
Save rashidtvmr/d2d6680aa7eafb5a5f26b79c0d195bf6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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