/**
 * @name: emitter
 * @author: kahu4
 * @date: 2024-01-19 18:18
 * @description：事件派发、监听器
 * @update: 2024-01-19 18:18
 * */

class Emitter {
    lister = {}

    constructor() {
        this.lister = {}
    }

    on(name, func) {
        this.lister[name] ? this.lister[name].push(func) : this.lister[name] = [func];
    }

    emit(name, ...args) {
        if (!this.lister[name]) return
        this.lister[name].forEach(func => func(...args))
    }

    clear(name) {
        this.lister[name] = []
    }

    clearAll() {
        this.lister = {}
    }
}


// 只暴露一个实例全局共用
export const emitter = new Emitter()
