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

useIsFetching.ts

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