UNPKG

vue-hooks-plus

Version:
74 lines (73 loc) 2.21 kB
"use strict"; const vue = require("vue"); const useRequest = require("../useRequest"); const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e }; const useRequest__default = /* @__PURE__ */ _interopDefaultLegacy(useRequest); function renderHook(renderFC) { const app = vue.createApp( vue.defineComponent({ setup() { renderFC(); return () => { }; } }) ); app.mount(document.createElement("div")); } const DEFAULT_KEY = "VUE_HOOKS_PLUS_USE_REQUEST_DEFAULT_KEY"; function keyIsStringOrNumber(value) { return typeof value === "string" || typeof value === "number"; } function useFetchs(service, options, self) { const fetchKeyPersist = vue.ref(self == null ? void 0 : self.fetchKey); const fetchs = vue.ref({}); const newFetchs = vue.ref({}); const setFetchs = (fetchs_) => { newFetchs.value = fetchs_; }; const fetchRun = (...args) => { var _a, _b; const newstFetchKey = vue.ref(); const cacheKey = (_b = (_a = fetchKeyPersist.value) == null ? void 0 : _a.call(fetchKeyPersist, ...args)) != null ? _b : DEFAULT_KEY; newstFetchKey.value = cacheKey; renderHook(() => { const { data, run, params, loading } = useRequest__default.default(service, { ...options, cacheKey, manual: true }); vue.watchEffect(() => { fetchs.value[cacheKey] = { key: cacheKey, data: data == null ? void 0 : data.value, params: params.value, loading: loading.value }; setFetchs(fetchs.value); }); run(...args); vue.watch([data, params, loading, newstFetchKey], (curr) => { const [ newData = void 0, newParams = void 0, newLoading = false, key = DEFAULT_KEY ] = curr; const fetchKey = keyIsStringOrNumber(key) ? key : DEFAULT_KEY; fetchs.value[fetchKey] = { key: fetchKey, data: newData, params: newParams, loading: newLoading }; setFetchs(fetchs.value); }); }); }; return { fetchs: newFetchs, fetchRun }; } module.exports = useFetchs;