"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SafeEventEmitter = exports.EventEmitter = void 0;
class EventEmitter {
    _listeners = new Map();
    on(type, listener) {
        if (!this._listeners.has(type)) {
            this._listeners.set(type, []);
        }
        this._listeners.get(type).push(listener);
    }
    off(type, listener) {
        if (this._listeners.has(type)) {
            this._listeners.set(type, this._listeners.get(type).filter((val) => val !== listener));
        }
    }
    emit(type, value) {
        this._listeners.get(type)?.forEach((listener) => listener(value));
    }
}
exports.EventEmitter = EventEmitter;
class SafeEventEmitter extends EventEmitter {
    onError;
    constructor(options) {
        super();
        this.onError = options?.onError ?? console.error;
    }
    emit(type, value) {
        this._listeners.get(type)?.forEach((listener) => this.safeExec(() => listener(value)));
    }
    safeExec(fn) {
        try {
            fn();
        }
        catch (e) {
            this.onError(e);
        }
    }
}
exports.SafeEventEmitter = SafeEventEmitter;
//# sourceMappingURL=event-emitter.js.map
