import { onLoad } from "@dcloudio/uni-app";

export function registerOnceEvent(callback, prefix = 'once_') {
  // 生成一个唯一事件名
  const eventName = `${prefix}${Date.now()}_${Math.floor(Math.random() * 10000)}`;
  uni.$once(eventName, callback);
  return eventName;
}

// 触发事件
export function triggerOnceEvent() {
  let eventName = '';
  onLoad((e) => {
    eventName = e.eventName;
  })
  function trigger(data) {
    if (eventName && typeof eventName === 'string') {
      uni.$emit(eventName, data);
    }
  }
  return trigger;
}