export default class Notice {

    triggers={};

    on(key,callback,mode='address') {

        if (!callback) return;

        if (this.triggers[mode] === undefined) this.triggers[mode] = {};
        this.triggers[mode][key] = callback;

        return this;

    }

    off(key,mode) {

        if (this.triggers[mode] === undefined) return;

        if (this.triggers[mode][key]) {
            delete this.triggers[mode][key];
        }

        return this;

    }

    trigger(mode,data){
        if (!this.triggers[mode]) return ;
        let keys = Object.keys(this.triggers[mode]);
        return keys.map((key)=>{
            return this.triggerListener(mode,key,data);
        });
    }

    triggerListener(mode,key,data) {

        if (this.triggers[mode][key]) {
            this.triggers[mode][key] && this.triggers[mode][key](data);
            // this.triggers[key].map((item)=> item && item(data));
        }

    }

    destroy(){
        // 清空所有监听事件
        this.triggers = null;
    }

}