Open History to see the last commit
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
11 * object, a `ref`, or a reactive 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
19 * that your application is loading or fetching in the background (useful for
20 * app-wide loading indicators).
21 *
22 * `fetchingFilters` may be a plain object, `MaybeRefDeep`, or a reactive getter
23 * (`() => ({ ... })`) β pass a getter if the filters themselves depend on other
24 * reactive state.
25 *
26 * @example
27 * ;```vue
28 * <script setup lang="ts">
29 * import { useIsFetching } from '@tanstack/vue-query'
30 *
31 * // How many queries matching the posts prefix are fetching?
32 * const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
33 * </script>
34 *
35 * <template>
36 * <span v-if="isFetchingPosts">Refreshing posts...</span>
37 * </template>
38 * ```
39 *
40 * @example
41 * A global loading indicator for any query fetching in the background, not just the ones on screen:
42 * ```vue
43 * <script setup lang="ts">
44 * import { useIsFetching } from '@tanstack/vue-query'
45 *
46 * const isFetching = useIsFetching()
47 * </script>
48 *
49 * <template>
50 * <div v-if="isFetching">Queries are fetching in the background...</div>
51 * </template>
52 * ```
53 *
54 * @param fetchingFilters - The {@link QueryFilters} to narrow down the matched
55 * queries.
56 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the
57 * one provided by `VueQueryPlugin` will be used.
58 * @returns A `ref` to the `number` of the queries that your application is
59 * currently loading or fetching in the background.
60 */
61export function useIsFetching(
62 fetchingFilters: UseIsFetchingFilters = {},
63 queryClient?: QueryClient,
64): Ref<number> {
65 if (process.env.NODE_ENV === 'development') {
66 if (!getCurrentScope()) {
67 console.warn(
68 '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.',
69 )
70 }
71 }
72β
73 const client = queryClient || useQueryClient()
74β
75 const isFetching = ref()
76β
77 const listener = () => {
78 const resolvedFilters =
79 typeof fetchingFilters === 'function'
80 ? fetchingFilters()
81 : fetchingFilters
82 isFetching.value = client.isFetching(cloneDeepUnref(resolvedFilters))
83 }
84β
85 const unsubscribe = client.getQueryCache().subscribe(listener)
86β
87 watchEffect(listener)
88β
89 onScopeDispose(() => {
90 unsubscribe()
91 })
92β
93 return isFetching
94}