class EvenCenter {
constructor() {
this.handlers = {}
}
addEventListener(type, handler) {
if (!this.handlers[type]) {
this.handler[type] = []
}
this.handlers[type].push(handler)
}
dispatchEvent(type, params) {
if (!this.handlers[type]) {
throw new Error('This event is not registered')
}
this.handlers[type].forEach((handler) => {
handler(...params)
})
}
removeEventListener(type, handler) {
if (!this.handlers[type]) {
throw new Error('Invalid event')
}
if (!handler) {
delete this.handlers[type]
}
else {
const index = this.handlers[type].findIndex((el) => el === handler)
if (index === -1) {
throw new Error('No such bound event')
}
this.handlers[type].splice(index, 1)
if (this.handlers[type].length === 0) {
delete this.handlers[type]
}
}
}
}