
// 事件总线第三方库：
import mitt from 'mitt';
const eventBus = mitt();
export default eventBus;


// 封装二代事件总线,实现远程取值   emit(gather) > gather > return > on(setup)
export const dynamicBus = async ( evnent:string, params:any = undefined ) => {

    return new Promise((resolve, resject)=>{
        let timer:any = setInterval(()=>{
            eventBus.emit(evnent, params)
        }, 100)
        eventBus.on(`${evnent}_return`, async (data:any)=>{
            eventBus.off(`${evnent}_return`)
            resolve(data)
        })

        // 接收到  并关闭
        eventBus.on(`${evnent}_close`, async (data:any)=>{
            eventBus.off(`${evnent}_close`)
            clearInterval(timer)
            timer = null
        })

        eventBus.emit(evnent, params)
    })
}