class EventBus {
    constructor() {
        this.events = {};
    }
    /**
     * @param {any} type 事件名称
     * @param {any} handler 事件处理器 暂时支持一个事件只有一个处理器
     * @param {any} isKeep 是否保持常驻不被消费
     * @returns 
     */
    on(type, handler, isKeep) {
        this.events[type] = handler;
        if (isKeep) this.events[type].isKeep = true;
        return this;
    }

    //取消订阅事件
    off(type) {
        if (!type) {
            return this;
        }
        delete this.events[type];
        
        return this;
    }

    //触发事件
    emit(type, data, callback) {
        const fn = this.events[type];
        if (fn) {
            fn.call(this, data, callback);
            if (!this.events[type].isKeep) {
                this.events[type] = null;
            }
        }
        return this;
    }
}

const eventBus = new EventBus();

export default eventBus;