@tanstack/vue-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in Vue
114 lines (113 loc) • 3.89 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/useQueries.ts
var useQueries_exports = {};
__export(useQueries_exports, {
useQueries: () => useQueries
});
module.exports = __toCommonJS(useQueries_exports);
var import_query_core = require("@tanstack/query-core");
var import_vue_demi = require("vue-demi");
var import_useQueryClient = require("./useQueryClient.cjs");
var import_utils = require("./utils.cjs");
function useQueries({
queries,
...options
}, queryClient) {
if (process.env.NODE_ENV === "development") {
if (!(0, import_vue_demi.getCurrentScope)()) {
console.warn(
'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.'
);
}
}
const client = queryClient || (0, import_useQueryClient.useQueryClient)();
const defaultedQueries = (0, import_vue_demi.computed)(() => {
const queriesRaw = (0, import_vue_demi.unref)(queries);
return queriesRaw.map((queryOptions) => {
var _a;
const clonedOptions = (0, import_utils.cloneDeepUnref)(queryOptions);
if (typeof clonedOptions.enabled === "function") {
clonedOptions.enabled = queryOptions.enabled();
}
const defaulted = client.defaultQueryOptions(clonedOptions);
defaulted._optimisticResults = ((_a = client.isRestoring) == null ? void 0 : _a.value) ? "isRestoring" : "optimistic";
return defaulted;
});
});
const observer = new import_query_core.QueriesObserver(
client,
defaultedQueries.value,
options
);
const getOptimisticResult = () => {
const [results, getCombinedResult] = observer.getOptimisticResult(
defaultedQueries.value,
options.combine
);
return getCombinedResult(
results.map((result, index) => {
return {
...result,
refetch: async (...args) => {
const [{ [index]: query }] = observer.getOptimisticResult(
defaultedQueries.value,
options.combine
);
return query.refetch(...args);
}
};
})
);
};
const state = (0, import_vue_demi.shallowRef)(getOptimisticResult());
let unsubscribe = () => {
};
if (client.isRestoring) {
(0, import_vue_demi.watch)(
client.isRestoring,
(isRestoring) => {
if (!isRestoring) {
unsubscribe();
unsubscribe = observer.subscribe(() => {
state.value = getOptimisticResult();
});
state.value = getOptimisticResult();
}
},
{ immediate: true }
);
}
(0, import_vue_demi.watch)(defaultedQueries, (queriesValue) => {
observer.setQueries(
queriesValue,
options
);
state.value = getOptimisticResult();
});
(0, import_vue_demi.onScopeDispose)(() => {
unsubscribe();
});
return options.shallow ? (0, import_vue_demi.shallowReadonly)(state) : (0, import_vue_demi.readonly)(state);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useQueries
});
//# sourceMappingURL=useQueries.cjs.map
;