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

useIsFetching.ts

153 lines4.0 KB

1import { createValueAccessor, readAccessor } from './accessor.js'
2import { BaseController } from './controllers/BaseController.js'
3import type { QueryClient, QueryFilters } from '@tanstack/query-core'
4import type { ReactiveControllerHost } from 'lit'
5import type { Accessor, ValueAccessor } from './accessor.js'
6​
7/**
8 * Accessor returned by `useIsFetching`.
9 *
10 * Call the accessor or read its `current` property to get the number of
11 * currently fetching queries that match the filters.
12 */
13export type IsFetchingAccessor = ValueAccessor<number> & { destroy: () => void }
14​
15class IsFetchingController extends BaseController<number> {
16 private queryClient: QueryClient | undefined
17 private unsubscribe: (() => void) | undefined
18​
19 constructor(
20 host: ReactiveControllerHost,
21 private readonly filters: Accessor<Omit<QueryFilters, 'fetchStatus'>> = {},
22 queryClient?: QueryClient,
23 ) {
24 super(host, 0, queryClient)
25​
26 if (!queryClient) {
27 return
28 }
29​
30 this.queryClient = queryClient
31 this.result = this.computeValue()
32 }
33​
34 protected override onConnected(): void {
35 if (!this.syncClient()) {
36 this.setResult(0)
37 return
38 }
39​
40 this.subscribe()
41 this.setResult(this.computeValue())
42 }
43​
44 protected override onDisconnected(): void {
45 this.unsubscribe?.()
46 this.unsubscribe = undefined
47 this.syncClient()
48 }
49​
50 protected override onHostUpdate(): void {
51 if (typeof this.filters !== 'function') {
52 return
53 }
54​
55 this.setResult(this.syncClient() ? this.computeValue() : 0)
56 }
57​
58 protected override onQueryClientChanged(): void {
59 if (!this.syncClient()) {
60 this.setResult(0)
61 return
62 }
63​
64 if (this.connectedState) {
65 this.subscribe()
66 this.setResult(this.computeValue())
67 }
68 }
69​
70 private syncClient(): boolean {
71 const nextClient = this.tryGetQueryClient()
72 if (!nextClient) {
73 this.unsubscribe?.()
74 this.unsubscribe = undefined
75 this.queryClient = undefined
76 return false
77 }
78​
79 if (nextClient === this.queryClient) {
80 return true
81 }
82​
83 this.unsubscribe?.()
84 this.unsubscribe = undefined
85 this.queryClient = nextClient
86 return true
87 }
88​
89 private subscribe(): void {
90 if (!this.queryClient) {
91 return
92 }
93​
94 if (this.unsubscribe) {
95 return
96 }
97​
98 this.unsubscribe = this.queryClient.getQueryCache().subscribe(() => {
99 this.setResult(this.computeValue())
100 })
101 }
102​
103 private computeValue(): number {
104 if (!this.queryClient) {
105 return 0
106 }
107​
108 return this.queryClient.isFetching(readAccessor(this.filters))
109 }
111​
112/**
113 * Creates a Lit reactive controller that tracks how many matching queries are
114 * currently fetching.
115 *
116 * When `filters` is a function, it is re-read during host updates so the count
117 * can follow reactive host state. If `queryClient` is omitted, the controller
118 * resolves the client from the nearest connected `QueryClientProvider`.
119 * @param host - The Lit reactive controller host that owns the cache
120 * subscription.
121 * @param filters - Query filters, or a getter that returns query filters.
122 * @param queryClient - Optional explicit query client. Provide this for
123 * controllers that should not resolve a client from Lit context.
124 * @returns An accessor for the current number of matching fetching queries.
125 * @example
126 * ```ts
127 * import { LitElement, html } from 'lit'
128 * import { useIsFetching } from '@tanstack/lit-query'
129 *
130 * class TodosStatus extends LitElement {
131 * private readonly todosFetching = useIsFetching(this, {
132 * queryKey: ['todos'],
133 * })
134 *
135 * render() {
136 * return html`<span>${this.todosFetching()} active todo fetches</span>`
137 * }
138 * }
139 * ```
140 */
141export function useIsFetching(
142 host: ReactiveControllerHost,
143 filters: Accessor<Omit<QueryFilters, 'fetchStatus'>> = {},
144 queryClient?: QueryClient,
145): IsFetchingAccessor {
146 const controller = new IsFetchingController(host, filters, queryClient)
147 return Object.assign(
148 createValueAccessor(() => controller.current),
149 {
150 destroy: () => controller.destroy(),
151 },
152 )