Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue-query): avoid use sync watchers that cause frequent requests #6043

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/vue-query/src/__tests__/useQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ describe('useQuery', () => {
})

secondKeyRef.value = 'key8'
await flushPromises()

expect(query).toMatchObject({
status: { value: 'loading' },
data: { value: undefined },
Expand All @@ -170,6 +172,9 @@ describe('useQuery', () => {
})

enabled.value = true

await flushPromises()

expect(query).toMatchObject({
fetchStatus: { value: 'fetching' },
data: { value: undefined },
Expand Down
41 changes: 28 additions & 13 deletions packages/vue-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
shouldThrowError,
updateState,
} from './utils'
import type { ToRefs } from 'vue-demi'
import type { ToRef } from 'vue-demi'
import type {
QueryFunction,
QueryKey,
Expand All @@ -31,7 +31,11 @@ export type UseQueryReturnType<
TData,
TError,
Result = QueryObserverResult<TData, TError>,
> = ToRefs<Readonly<Result>> & {
> = {
[K in keyof Result]: Result[K] extends (...args: any[]) => any
? Result[K]
: ToRef<Result[K]>
} & {
suspense: () => Promise<Result>
}

Expand Down Expand Up @@ -106,19 +110,23 @@ export function useBaseQuery<
{ immediate: true },
)

watch(
defaultedOptions,
() => {
observer.setOptions(defaultedOptions.value)
updateState(state, observer.getCurrentResult())
},
{ flush: 'sync' },
)
const updater = () => {
observer.setOptions(defaultedOptions.value)
updateState(state, observer.getCurrentResult())
}

watch(defaultedOptions, updater)

onScopeDispose(() => {
unsubscribe()
})

// fix #5910
const refetch = (...args: Parameters<typeof state['refetch']>) => {
updater()
return state.refetch(...args)
}

const suspense = () => {
return new Promise<QueryObserverResult<TData, TError>>(
(resolve, reject) => {
Expand Down Expand Up @@ -166,10 +174,17 @@ export function useBaseQuery<
},
)

return {
...(toRefs(readonly(state)) as UseQueryReturnType<TData, TError>),
suspense,
const object: any = toRefs(readonly(state))
for (const key in state) {
if (typeof state[key as keyof typeof state] === 'function') {
object[key] = state[key as keyof typeof state]
}
}

object.suspense = suspense
object.refetch = refetch

return object as UseQueryReturnType<TData, TError>
}

export function parseQueryArgs<
Expand Down
9 changes: 2 additions & 7 deletions packages/vue-query/src/useInfiniteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ export function useInfiniteQuery<
arg2,
arg3,
) as InfiniteQueryReturnType<TData, TError>
return {
...result,
fetchNextPage: result.fetchNextPage.value,
fetchPreviousPage: result.fetchPreviousPage.value,
refetch: result.refetch.value,
remove: result.remove.value,
}

return result
}
4 changes: 2 additions & 2 deletions packages/vue-query/src/useIsFetching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
onScopeDispose,
ref,
unref,
watchSyncEffect,
watchEffect,
} from 'vue-demi'
import { useQueryClient } from './useQueryClient'
import { cloneDeepUnref, isQueryKey } from './utils'
Expand Down Expand Up @@ -44,7 +44,7 @@ export function useIsFetching(

const unsubscribe = queryClient.getQueryCache().subscribe(listener)

watchSyncEffect(listener)
watchEffect(listener)

onScopeDispose(() => {
unsubscribe()
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-query/src/useIsMutating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
onScopeDispose,
ref,
unref,
watchSyncEffect,
watchEffect,
} from 'vue-demi'
import { useQueryClient } from './useQueryClient'
import { cloneDeepUnref, isQueryKey } from './utils'
Expand Down Expand Up @@ -44,7 +44,7 @@ export function useIsMutating(

const unsubscribe = queryClient.getMutationCache().subscribe(listener)

watchSyncEffect(listener)
watchEffect(listener)

onScopeDispose(() => {
unsubscribe()
Expand Down
10 changes: 3 additions & 7 deletions packages/vue-query/src/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,9 @@ export function useMutation<
})
}

watch(
options,
() => {
observer.setOptions(queryClient.defaultMutationOptions(options.value))
},
{ flush: 'sync' },
)
watch(options, () => {
observer.setOptions(queryClient.defaultMutationOptions(options.value))
})

onScopeDispose(() => {
unsubscribe()
Expand Down
6 changes: 1 addition & 5 deletions packages/vue-query/src/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,5 @@ export function useQuery<
| UseQueryDefinedReturnType<TData, TError> {
const result = useBaseQuery(QueryObserver, arg1, arg2, arg3)

return {
...result,
refetch: result.refetch.value,
remove: result.remove.value,
}
return result
}