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