Skip to content

Instantly share code, notes, and snippets.

@ashwanisindhu1
Forked from h9h/events.js
Created June 11, 2021 12:11
Show Gist options
  • Save ashwanisindhu1/e3fb839b83c6d54eb00cf4edcb1ba1ab to your computer and use it in GitHub Desktop.
Save ashwanisindhu1/e3fb839b83c6d54eb00cf4edcb1ba1ab to your computer and use it in GitHub Desktop.
Minimal Event/Emitter Implementation
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