"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.EventTarget = void 0;

class EventTarget {
    constructor() {
        this.events = {};
    }
    on(type, handler) {
        return this.events[type] || (this.events[type] = []), this.events[type].push(handler), 
        this;
    }
    off(type, handler) {
        if (!this.events[type]) return this;
        if (handler) {
            const idx = this.events[type].indexOf(handler);
            idx >= 0 && this.events[type].splice(idx, 1), 0 === this.events[type].length && delete this.events[type];
        } else delete this.events[type];
        return this;
    }
    fire(type, ...args) {
        if (!this.events[type]) return this;
        const handlers = [ ...this.events[type] ];
        for (const handler of handlers) try {
            handler(...args);
        } catch (e) {}
        return this;
    }
    once(type, handler) {
        const onceHandler = (...args) => {
            this.off(type, onceHandler), handler(...args);
        };
        return this.on(type, onceHandler);
    }
    removeAllListeners() {
        return this.events = {}, this;
    }
    eventNames() {
        return Object.keys(this.events);
    }
    listenerCount(type) {
        var _a;
        return (null === (_a = this.events[type]) || void 0 === _a ? void 0 : _a.length) || 0;
    }
}

exports.EventTarget = EventTarget;
//# sourceMappingURL=event-target.js.map