import { ref } from 'vue';
import { createEventHook } from '@vueuse/core';

const useLoading = (fn) => {
  const isLoading = ref(false);
  const data = ref();
  const getDataHook = createEventHook();
  const getDataFinally = async () => {
    if (!isLoading.value) {
      return;
    }
    return new Promise((resolve) => {
      getDataHook.on(resolve);
    });
  };
  const exec = async (...args) => {
    isLoading.value = true;
    try {
      const result = await fn(...args);
      data.value = result;
      return result;
    } finally {
      isLoading.value = false;
      getDataHook.trigger();
    }
  };
  return {
    isLoading,
    exec,
    getDataFinally
    // 获取数据完成
  };
};

export { useLoading };
//# sourceMappingURL=index.mjs.map
