UNPKG

@tanstack/vue-query

Version:

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

80 lines 2.36 kB
// src/vueQueryPlugin.ts import { isVue2 } from "vue-demi"; import { isServer } from "@tanstack/query-core"; import { QueryClient } from "./queryClient.js"; import { getClientKey } from "./utils.js"; import { setupDevtools } from "./devtools/devtools.js"; var VueQueryPlugin = { install: (app, options = {}) => { const clientKey = getClientKey(options.queryClientKey); let client; if ("queryClient" in options && options.queryClient) { client = options.queryClient; } else { const clientConfig = "queryClientConfig" in options ? options.queryClientConfig : void 0; client = new QueryClient(clientConfig); } if (!isServer) { client.mount(); } let persisterUnmount = () => { }; if (options.clientPersister) { if (client.isRestoring) { client.isRestoring.value = true; } const [unmount, promise] = options.clientPersister(client); persisterUnmount = unmount; promise.then(() => { var _a; if (client.isRestoring) { client.isRestoring.value = false; } (_a = options.clientPersisterOnSuccess) == null ? void 0 : _a.call(options, client); }); } const cleanup = () => { client.unmount(); persisterUnmount(); }; if (app.onUnmount) { app.onUnmount(cleanup); } else { const originalUnmount = app.unmount; app.unmount = function vueQueryUnmount() { cleanup(); originalUnmount(); }; } if (isVue2) { app.mixin({ beforeCreate() { if (!this._provided) { const provideCache = {}; Object.defineProperty(this, "_provided", { get: () => provideCache, set: (v) => Object.assign(provideCache, v) }); } this._provided[clientKey] = client; if (process.env.NODE_ENV === "development") { if (this === this.$root && options.enableDevtoolsV6Plugin) { setupDevtools(this, client); } } } }); } else { app.provide(clientKey, client); if (process.env.NODE_ENV === "development") { if (options.enableDevtoolsV6Plugin) { setupDevtools(app, client); } } } } }; export { VueQueryPlugin }; //# sourceMappingURL=vueQueryPlugin.js.map