Open History to see the last commit
1import { isVue2 } from 'vue-demi'
2import { isServer } from '@tanstack/query-core'
3import type { QueryClientConfig } from '@tanstack/query-core'
4β
5import { QueryClient } from './queryClient'
6import { getClientKey } from './utils'
7import { setupDevtools } from './devtools/devtools'
8import type { MaybeRefDeep } from './types'
9β
10declare global {
11 interface Window {
12 __VUE_QUERY_CONTEXT__?: QueryClient
13 }
14}
15β
16type ClientPersister = (client: QueryClient) => [() => void, Promise<void>]
17β
18interface CommonOptions {
19 queryClientKey?: string
20 contextSharing?: boolean
21 clientPersister?: ClientPersister
22}
23β
24interface ConfigOptions extends CommonOptions {
25 queryClientConfig?: MaybeRefDeep<QueryClientConfig>
26}
27β
28interface ClientOptions extends CommonOptions {
29 queryClient?: QueryClient
30}
31β
32export type VueQueryPluginOptions = ConfigOptions | ClientOptions
33β
34export const VueQueryPlugin = {
35 install: (app: any, options: VueQueryPluginOptions = {}) => {
36 const clientKey = getClientKey(options.queryClientKey)
37 let client: QueryClient
38β
39 if ('queryClient' in options && options.queryClient) {
40 client = options.queryClient
41 } else {
42 if (options.contextSharing && typeof window !== 'undefined') {
43 if (!window.__VUE_QUERY_CONTEXT__) {
44 const clientConfig =
45 'queryClientConfig' in options
46 ? options.queryClientConfig
47 : undefined
48 client = new QueryClient(clientConfig)
49 window.__VUE_QUERY_CONTEXT__ = client
50 } else {
51 client = window.__VUE_QUERY_CONTEXT__
52 }
53 } else {
54 const clientConfig =
55 'queryClientConfig' in options ? options.queryClientConfig : undefined
56 client = new QueryClient(clientConfig)
57 }
58 }
59β
60 if (!isServer) {
61 client.mount()
62 }
63β
64 let persisterUnmount = () => {
65 // noop
66 }
67β
68 if (options.clientPersister) {
69 client.isRestoring.value = true
70 const [unmount, promise] = options.clientPersister(client)
71 persisterUnmount = unmount
72 promise.then(() => {
73 client.isRestoring.value = false
74 })
75 }
76β
77 if (process.env.NODE_ENV !== 'production' && options.contextSharing) {
78 client
79 .getLogger()
80 .error(
81 `The contextSharing option has been deprecated and will be removed in the next major version`,
82 )
83 }
84β
85 const cleanup = () => {
86 client.unmount()
87 persisterUnmount()
88 }
89β
90 if (app.onUnmount) {
91 app.onUnmount(cleanup)
92 } else {
93 const originalUnmount = app.unmount
94 app.unmount = function vueQueryUnmount() {
95 cleanup()
96 originalUnmount()
97 }
98 }
99β
100 /* istanbul ignore next */
101 if (isVue2) {
102 app.mixin({
103 beforeCreate() {
104 // HACK: taken from provide(): https://github.com/vuejs/composition-api/blob/master/src/apis/inject.ts#L30
105 if (!this._provided) {
106 const provideCache = {}
107 Object.defineProperty(this, '_provided', {
108 get: () => provideCache,
109 set: (v) => Object.assign(provideCache, v),
110 })
111 }
112β
113 this._provided[clientKey] = client
114β
115 if (process.env.NODE_ENV === 'development') {
116 if (this === this.$root) {
117 setupDevtools(this, client)
118 }
119 }
120 },
121 })
122 } else {
123 app.provide(clientKey, client)
124β
125 if (process.env.NODE_ENV === 'development') {
126 setupDevtools(app, client)
127 }
128 }
129 },
130}