mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
1770efad63
Tiny typo fix 🙂
33 lines
704 B
JavaScript
33 lines
704 B
JavaScript
export default class EventEmitter {
|
|
listeners = {}
|
|
|
|
on (event, cb) {
|
|
if (!this.listeners[event]) {
|
|
this.listeners[event] = new Set()
|
|
}
|
|
|
|
if (this.listeners[event].has(cb)) {
|
|
throw new Error(`Listener already exists for router event: \`${event}\``)
|
|
}
|
|
|
|
this.listeners[event].add(cb)
|
|
return this
|
|
}
|
|
|
|
emit (event, ...data) {
|
|
const listeners = this.listeners[event]
|
|
const hasListeners = listeners && listeners.size
|
|
if (!hasListeners) {
|
|
return false
|
|
}
|
|
|
|
listeners.forEach(cb => cb(...data)) // eslint-disable-line standard/no-callback-literal
|
|
return true
|
|
}
|
|
|
|
off (event, cb) {
|
|
this.listeners[event].delete(cb)
|
|
return this
|
|
}
|
|
}
|