Skip to content

Commit 1858966

Browse files
feat(vue-query): support invoke in vue runWithContext() (#5703)
* feat: support invoke in vue `runWithContext()` * chore: run prettier * feat(vue-qury): add a second parameter to inject to avoid vue warning * test: update test * fix: add warning about potential memory leak --------- Co-authored-by: Damian Osipiuk <[email protected]>
1 parent a41f0bf commit 1858966

File tree

7 files changed

+78
-13
lines changed

7 files changed

+78
-13
lines changed

packages/vue-query/src/__tests__/useQueryClient.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ describe('useQueryClient', () => {
1818

1919
expect(queryClient).toStrictEqual(queryClientMock)
2020
expect(injectSpy).toHaveBeenCalledTimes(1)
21-
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
21+
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT, null)
2222
})
2323

2424
test('should throw an error when queryClient does not exist in the context', () => {
2525
injectSpy.mockReturnValueOnce(undefined)
2626

2727
expect(useQueryClient).toThrowError()
2828
expect(injectSpy).toHaveBeenCalledTimes(1)
29-
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
29+
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT, null)
3030
})
3131

3232
test('should throw an error when used outside of setup function', () => {
@@ -44,6 +44,6 @@ describe('useQueryClient', () => {
4444

4545
useQueryClient(queryClientKey)
4646

47-
expect(injectSpy).toHaveBeenCalledWith(expectedKeyParameter)
47+
expect(injectSpy).toHaveBeenCalledWith(expectedKeyParameter, null)
4848
})
4949
})

packages/vue-query/src/useBaseQuery.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
computed,
3+
getCurrentScope,
34
onScopeDispose,
45
reactive,
56
readonly,
@@ -58,6 +59,14 @@ export function useBaseQuery<
5859
| UseQueryOptionsGeneric<TQueryFnData, TError, TData, TQueryKey> = {},
5960
arg3: UseQueryOptionsGeneric<TQueryFnData, TError, TData, TQueryKey> = {},
6061
): UseQueryReturnType<TData, TError> {
62+
if (process.env.NODE_ENV === 'development') {
63+
if (!getCurrentScope()) {
64+
console.warn(
65+
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
66+
)
67+
}
68+
}
69+
6170
const options = computed(() => parseQueryArgs(arg1, arg2, arg3))
6271

6372
const queryClient =

packages/vue-query/src/useIsFetching.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { computed, onScopeDispose, ref, unref, watch } from 'vue-demi'
1+
import {
2+
computed,
3+
getCurrentScope,
4+
onScopeDispose,
5+
ref,
6+
unref,
7+
watch,
8+
} from 'vue-demi'
29
import { useQueryClient } from './useQueryClient'
310
import { cloneDeepUnref, isQueryKey } from './utils'
411
import type { Ref } from 'vue-demi'
@@ -17,6 +24,14 @@ export function useIsFetching(
1724
arg1?: MaybeRef<QueryKey> | QueryFilters,
1825
arg2?: Omit<QueryFilters, 'queryKey'>,
1926
): Ref<number> {
27+
if (process.env.NODE_ENV === 'development') {
28+
if (!getCurrentScope()) {
29+
console.warn(
30+
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
31+
)
32+
}
33+
}
34+
2035
const filters = computed(() => parseFilterArgs(arg1, arg2))
2136
const queryClient =
2237
filters.value.queryClient ?? useQueryClient(filters.value.queryClientKey)

packages/vue-query/src/useIsMutating.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { computed, onScopeDispose, ref, unref, watch } from 'vue-demi'
1+
import {
2+
computed,
3+
getCurrentScope,
4+
onScopeDispose,
5+
ref,
6+
unref,
7+
watch,
8+
} from 'vue-demi'
29
import { useQueryClient } from './useQueryClient'
310
import { cloneDeepUnref, isQueryKey } from './utils'
411
import type { Ref } from 'vue-demi'
@@ -17,6 +24,14 @@ export function useIsMutating(
1724
arg1?: MaybeRef<MutationKey> | MutationFilters,
1825
arg2?: Omit<MutationFilters, 'mutationKey'>,
1926
): Ref<number> {
27+
if (process.env.NODE_ENV === 'development') {
28+
if (!getCurrentScope()) {
29+
console.warn(
30+
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
31+
)
32+
}
33+
}
34+
2035
const filters = computed(() => parseFilterArgs(arg1, arg2))
2136
const queryClient =
2237
filters.value.queryClient ?? useQueryClient(filters.value.queryClientKey)

packages/vue-query/src/useMutation.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
computed,
3+
getCurrentScope,
34
onScopeDispose,
45
reactive,
56
readonly,
@@ -148,6 +149,14 @@ export function useMutation<
148149
VueMutationObserverOptions<TData, TError, TVariables, TContext>
149150
>,
150151
): UseMutationReturnType<TData, TError, TVariables, TContext> {
152+
if (process.env.NODE_ENV === 'development') {
153+
if (!getCurrentScope()) {
154+
console.warn(
155+
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
156+
)
157+
}
158+
}
159+
151160
const options = computed(() => {
152161
return parseMutationArgs(arg1, arg2, arg3)
153162
})

packages/vue-query/src/useQueries.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { QueriesObserver } from '@tanstack/query-core'
3-
import { computed, onScopeDispose, reactive, readonly, watch } from 'vue-demi'
3+
import {
4+
computed,
5+
getCurrentScope,
6+
onScopeDispose,
7+
reactive,
8+
readonly,
9+
watch,
10+
} from 'vue-demi'
411
import { useQueryClient } from './useQueryClient'
512
import { cloneDeepUnref } from './utils'
613
import type { Ref } from 'vue-demi'
@@ -133,6 +140,14 @@ export function useQueries<T extends any[]>({
133140
queries: Ref<UseQueriesOptionsArg<T>> | UseQueriesOptionsArg<T>
134141
queryClient?: QueryClient
135142
}): Readonly<UseQueriesResults<T>> {
143+
if (process.env.NODE_ENV === 'development') {
144+
if (!getCurrentScope()) {
145+
console.warn(
146+
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
147+
)
148+
}
149+
}
150+
136151
const unreffedQueries = computed(
137152
() => cloneDeepUnref(queries) as UseQueriesOptionsArg<T>,
138153
)

packages/vue-query/src/useQueryClient.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import { getClientKey } from './utils'
44
import type { QueryClient } from './queryClient'
55

66
export function useQueryClient(id = ''): QueryClient {
7-
const vm = getCurrentInstance()?.proxy
8-
9-
if (!vm) {
10-
throw new Error('vue-query hooks can only be used inside setup() function.')
11-
}
12-
137
const key = getClientKey(id)
14-
const queryClient = inject<QueryClient>(key)
8+
const queryClient = inject<QueryClient | null>(key, null)
159

1610
if (!queryClient) {
11+
const vm = getCurrentInstance()?.proxy
12+
13+
if (!vm) {
14+
throw new Error(
15+
'vue-query hooks can only be used inside setup() function.',
16+
)
17+
}
18+
1719
throw new Error(
1820
"No 'queryClient' found in Vue context, use 'VueQueryPlugin' to properly initialize the library.",
1921
)

0 commit comments

Comments
 (0)