class EventEmitter {
#listeners = Object.create(null);
#maxListener = 10;
constructor() {
}
on(evt, cb) {
const cbs = this.#listeners[evt] || (this.#listeners[evt] = []);
cbs.length < this.#maxListener && (typeof cb == 'function' || cb.hasOwnProperty('handleEvent')) && cbs.push(cb);
}
once(evt, cb) {
const fn = (...args) => {
cb.call(null, ...args);
this.off(evt, fn);
};
this.on(evt, fn);
}
off(evt) {
(evt && this.#listeners[evt]) ? this.#listeners[evt] = [] : this.#listeners = Object.create(null);
}
emit(evt, ...args) {
if (!this.#listeners[evt]) return
this.#listeners[evt].forEach(cb => {
if (cb.hasOwnProperty('handleEvent')) {
cb['handleEvent'].call(cb, ...args);
} else {
cb.call(null, ...args);
}
});
}
}