Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Last active June 17, 2018 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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