Skip to content

Instantly share code, notes, and snippets.

@ashwanisindhu-2
Forked from ashwanisindhu1/events.js
Created June 11, 2021 12:12
Show Gist options
  • Save ashwanisindhu-2/444bafdcb897f4f133341bd70ae620f0 to your computer and use it in GitHub Desktop.
Save ashwanisindhu-2/444bafdcb897f4f133341bd70ae620f0 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