Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useIsFetching.ts

88 lines2.8 KB

1import { getCurrentScope, onScopeDispose, ref, watchEffect } from 'vue-demi'
2import { useQueryClient } from './useQueryClient'
3import { cloneDeepUnref } from './utils'
4import type { Ref } from 'vue-demi'
5import type { QueryFilters } from '@tanstack/query-core'
6import type { MaybeRefDeep } from './types'
7import type { QueryClient } from './queryClient'
8​
9/**
10 * The filters accepted by `useIsFetching`: {@link QueryFilters} as a plain object, a `ref`, or a reactive
11 * getter.
12 */
13export type UseIsFetchingFilters =
14 | MaybeRefDeep<QueryFilters>
15 | (() => MaybeRefDeep<QueryFilters>)
16​
17/**
18 * The `useIsFetching` composable returns a `ref` to the `number` of the queries that your application is
19 * loading or fetching in the background (useful for app-wide loading indicators).
20 *
21 * `fetchingFilters` may be a plain object, `MaybeRefDeep`, or a reactive getter (`() => ({ ... })`) β€” pass a
22 * getter if the filters themselves depend on other reactive state.
23 * @param fetchingFilters - The {@link QueryFilters} to narrow down the matched queries.
24 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
25 * will be used.
26 * @returns A `ref` to the `number` of the queries that your application is currently loading or fetching in
27 * the background.
28 * @example
29 * ```vue
30 * <script setup lang="ts">
31 * import { useIsFetching } from '@tanstack/vue-query'
32 *
33 * // How many queries matching the posts prefix are fetching?
34 * const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
35 * </script>
36 *
37 * <template>
38 * <span v-if="isFetchingPosts">Refreshing posts...</span>
39 * </template>
40 * ```
41 * @example
42 * A global loading indicator for any query fetching in the background, not just the ones on screen:
43 * ```vue
44 * <script setup lang="ts">
45 * import { useIsFetching } from '@tanstack/vue-query'
46 *
47 * const isFetching = useIsFetching()
48 * </script>
49 *
50 * <template>
51 * <div v-if="isFetching">Queries are fetching in the background...</div>
52 * </template>
53 * ```
54 */
55export function useIsFetching(
56 fetchingFilters: UseIsFetchingFilters = {},
57 queryClient?: QueryClient,
58): Ref<number> {
59 if (process.env.NODE_ENV === 'development') {
60 if (!getCurrentScope()) {
61 console.warn(
62 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
63 )
64 }
65 }
66​
67 const client = queryClient || useQueryClient()
68​
69 const isFetching = ref()
70​
71 const listener = () => {
72 const resolvedFilters =
73 typeof fetchingFilters === 'function'
74 ? fetchingFilters()
75 : fetchingFilters
76 isFetching.value = client.isFetching(cloneDeepUnref(resolvedFilters))
77 }
78​
79 const unsubscribe = client.getQueryCache().subscribe(listener)
80​
81 watchEffect(listener)
82​
83 onScopeDispose(() => {
84 unsubscribe()
85 })
86​
87 return isFetching