Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Last active May 11, 2025 14:24
Show Gist options
  • Save AutoSponge/7a8df970d769a5792d01d9ab74d55fa0 to your computer and use it in GitHub Desktop.
Save AutoSponge/7a8df970d769a5792d01d9ab74d55fa0 to your computer and use it in GitHub Desktop.
event emitter
class Emitter {
constructor (data) {
this.data = data
this.events = new Map()
}
on (name, fn) {
const fns = this.events.get(name) || new Set()
fns.add(fn)
this.events.set(name, fns)
return this
}
off (name, fn) {
return this.events.has(name) && this.events.get(name).delete(fn)
}
emit (name, ...args) {
const fns = this.events.get(name) || []
fns.forEach(fn => fn(...args))
return this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment