2017-07-06 06:49:33 +00:00
|
|
|
export default class EventEmitter {
|
|
|
|
listeners = {}
|
|
|
|
|
|
|
|
on (event, cb) {
|
|
|
|
if (!this.listeners[event]) {
|
|
|
|
this.listeners[event] = new Set()
|
|
|
|
}
|
|
|
|
|
2017-07-09 03:46:53 +00:00
|
|
|
if (this.listeners[event].has(cb)) {
|
|
|
|
throw new Error(`The listener already exising in event: ${event}`)
|
|
|
|
}
|
|
|
|
|
2017-07-06 06:49:33 +00:00
|
|
|
this.listeners[event].add(cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
emit (event, ...data) {
|
|
|
|
if (!this.listeners[event]) return
|
2018-03-27 18:11:03 +00:00
|
|
|
this.listeners[event].forEach(cb => cb(...data)) // eslint-disable-line standard/no-callback-literal
|
2017-07-06 06:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
off (event, cb) {
|
|
|
|
this.listeners[event].delete(cb)
|
|
|
|
}
|
|
|
|
}
|