1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/lib/EventEmitter.js
Tim Neutkens ebf0c47c25
Upgrade standard.js (#4064)
* Upgrade standard.js

# Conflicts:
#	yarn.lock

* Upgrade babel-eslint
2018-03-27 20:11:03 +02:00

25 lines
552 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(`The listener already exising in event: ${event}`)
}
this.listeners[event].add(cb)
}
emit (event, ...data) {
if (!this.listeners[event]) return
this.listeners[event].forEach(cb => cb(...data)) // eslint-disable-line standard/no-callback-literal
}
off (event, cb) {
this.listeners[event].delete(cb)
}
}