export class EventEmitter {
    constructor() {
        this._events = new Map();
    }
    _get_methods(event) {
        const label = String(event);
        let events = this._events.get(label);
        if (events === undefined) {
            events = new Set();
            this._events.set(label, events);
        }
        return events;
    }
    has(topic) {
        const res = this._events.get(topic);
        return (res instanceof Set && res.size > 0);
    }
    on(event, method) {
        this._get_methods(event).add(method);
    }
    once(event, method) {
        const onceFn = (args) => {
            this.remove(event, onceFn);
            void method.apply(this, args);
        };
        this.on(event, onceFn);
    }
    within(event, method, timeout) {
        const withinFn = (args) => {
            void method.apply(this, args);
        };
        setTimeout(() => { this.remove(event, withinFn); }, timeout);
        this.on(event, withinFn);
    }
    emit(event, args) {
        const methods = [];
        this._get_methods(event).forEach(fn => {
            methods.push(fn.apply(this, [args]));
        });
        this._get_methods('*').forEach(fn => {
            methods.push(fn.apply(this, [event, args]));
        });
        void Promise.allSettled(methods);
    }
    remove(event, method) {
        this._get_methods(event).delete(method);
    }
    clear(event) {
        this._events.delete(event);
    }
}
