class EventEmitter {
    event
    constructor(){
        //键值对的事件对象
        this.event = new Map()
    }
 
    subscribe(eventName,callback){
        if(!this.event.has(eventName)){
            this.event.set(eventName,[callback])
        }else{
            this.event.get(eventName).push(callback)
        }
    }
 
    publish(eventName,...args){
        if(!this.event.has(eventName)) return
 
        const callbacks= this.event.get(eventName)
        callbacks.forEach(c => {
            c(...args)
        });
    }
 
    unsubscribe(eventName, callback) {
        if (!this.event.has(eventName)) return;
        
        if (!callback) {
            // 如果没传具体的 callback,则删除整个事件的所有监听
            this.event.delete(eventName);
        } else {
            // 过滤掉匹配的那一个
            const callbacks = this.event.get(eventName);
            this.event.set(eventName, callbacks.filter(c => c !== callback));
        }
    }
 
    once(eventName,callback){
        const fuc = function(...args){
            callback(...args)
            this.unsubscribe(eventName)
        }
        this.event.subscribe(eventName,fuc)
    }
}
 
const bus = new EventEmitter()
 
const call = (name)=>{
    console.log(`我是${name},记得叫我`)
}
 
bus.subscribe('起床',call)
bus.subscribe('吃饭',call)
 
bus.publish('起床','mio')
bus.publish('吃饭','mio')