@tanstack/vue-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in Vue
78 lines (77 loc) • 3.07 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/useMutation.ts
var useMutation_exports = {};
__export(useMutation_exports, {
useMutation: () => useMutation
});
module.exports = __toCommonJS(useMutation_exports);
var import_vue_demi = require("vue-demi");
var import_query_core = require("@tanstack/query-core");
var import_utils = require("./utils.cjs");
var import_useQueryClient = require("./useQueryClient.cjs");
function useMutation(mutationOptions, 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 options = (0, import_vue_demi.computed)(() => {
return client.defaultMutationOptions((0, import_utils.cloneDeepUnref)(mutationOptions));
});
const observer = new import_query_core.MutationObserver(client, options.value);
const state = options.value.shallow ? (0, import_vue_demi.shallowReactive)(observer.getCurrentResult()) : (0, import_vue_demi.reactive)(observer.getCurrentResult());
const unsubscribe = observer.subscribe((result) => {
(0, import_utils.updateState)(state, result);
});
const mutate = (variables, mutateOptions) => {
observer.mutate(variables, mutateOptions).catch(() => {
});
};
(0, import_vue_demi.watch)(options, () => {
observer.setOptions(options.value);
});
(0, import_vue_demi.onScopeDispose)(() => {
unsubscribe();
});
const readonlyState = options.value.shallow ? (0, import_vue_demi.shallowReadonly)(state) : (0, import_vue_demi.readonly)(state);
const resultRefs = (0, import_vue_demi.toRefs)(readonlyState);
(0, import_vue_demi.watch)(
() => state.error,
(error) => {
if (error && (0, import_query_core.shouldThrowError)(options.value.throwOnError, [error])) {
throw error;
}
}
);
return {
...resultRefs,
mutate,
mutateAsync: state.mutate,
reset: state.reset
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useMutation
});
//# sourceMappingURL=useMutation.cjs.map
;