import Taro from '@tarojs/taro';
import createEvent from "@zjxpcyc/js_event";

function evtBus() {
  const bus = {};

  const currentPath = () => {
    const currentPage = Taro.getCurrentPages().slice().pop();
    return currentPage.route;
  }

  // 没有则新建一个
  const current = () => {
    const path = currentPath();
    const target = bus[path];
    
    if (target) {
      return target;
    }

    const evt = createEvent();
    bus[path] = evt;
    return evt;
  }

  const remove = () => {
    const path = currentPath();
    delete bus[path];
  }

  return {
    remove,
    current,
  }
}

const bus = evtBus();
export default bus;
