UNPKG

@tanstack/vue-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in Vue

76 lines (75 loc) 2.99 kB
"use strict"; 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/useMutationState.ts var useMutationState_exports = {}; __export(useMutationState_exports, { useIsMutating: () => useIsMutating, useMutationState: () => useMutationState }); module.exports = __toCommonJS(useMutationState_exports); var import_vue_demi = require("vue-demi"); var import_useQueryClient = require("./useQueryClient.cjs"); var import_utils = require("./utils.cjs"); function useIsMutating(filters = {}, 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 mutationState = useMutationState( { filters: (0, import_vue_demi.computed)(() => ({ ...(0, import_utils.cloneDeepUnref)(filters), status: "pending" })) }, client ); const length = (0, import_vue_demi.computed)(() => mutationState.value.length); return length; } function getResult(mutationCache, options) { return mutationCache.findAll(options.filters).map( (mutation) => options.select ? options.select(mutation) : mutation.state ); } function useMutationState(options = {}, queryClient) { const filters = (0, import_vue_demi.computed)(() => (0, import_utils.cloneDeepUnref)(options.filters)); const mutationCache = (queryClient || (0, import_useQueryClient.useQueryClient)()).getMutationCache(); const state = (0, import_vue_demi.shallowRef)(getResult(mutationCache, options)); const unsubscribe = mutationCache.subscribe(() => { state.value = getResult(mutationCache, options); }); (0, import_vue_demi.watch)(filters, () => { state.value = getResult(mutationCache, options); }); (0, import_vue_demi.onScopeDispose)(() => { unsubscribe(); }); return (0, import_vue_demi.shallowReadonly)(state); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useIsMutating, useMutationState }); //# sourceMappingURL=useMutationState.cjs.map