-
-
Save ashwanisindhu1/e3fb839b83c6d54eb00cf4edcb1ba1ab to your computer and use it in GitHub Desktop.
Minimal Event/Emitter Implementation
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
| module.exports = () => ({ | |
| events: { }, | |
| emit(event, ...args) { | |
| (this.events[event] || []).forEach(i => i(...args)) | |
| }, | |
| on(event, cb) { | |
| (this.events[event] = this.events[event] || []).push(cb) | |
| return () => ( | |
| this.events[event] = this.events[event].filter(i => i !== cb) | |
| ) | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment