Open History to see the last commit
1import { createValueAccessor, readAccessor } from './accessor.js'
2import { BaseController } from './controllers/BaseController.js'
3import type { MutationFilters, QueryClient } from '@tanstack/query-core'
4import type { ReactiveControllerHost } from 'lit'
5import type { Accessor, ValueAccessor } from './accessor.js'
6β
7/**
8 * Accessor returned by `useIsMutating`.
9 *
10 * Call the accessor or read its `current` property to get the number of
11 * currently pending mutations that match the filters.
12 */
13export type IsMutatingAccessor = ValueAccessor<number> & { destroy: () => void }
14β
15class IsMutatingController 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<MutationFilters, 'status'>> = {},
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.getMutationCache().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.isMutating(readAccessor(this.filters))
109 }
110}
111β
112/**
113 * Creates a Lit reactive controller that tracks how many matching mutations are
114 * currently pending.
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 - Mutation filters, or a getter that returns mutation 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 pending mutations.
125 * @example
126 * ```ts
127 * import { LitElement, html } from 'lit'
128 * import { useIsMutating } from '@tanstack/lit-query'
129 *
130 * class MutationStatus extends LitElement {
131 * private readonly savesPending = useIsMutating(this, {
132 * mutationKey: ['save-project'],
133 * })
134 *
135 * render() {
136 * return html`<span>${this.savesPending()} saves pending</span>`
137 * }
138 * }
139 * ```
140 */
141export function useIsMutating(
142 host: ReactiveControllerHost,
143 filters: Accessor<Omit<MutationFilters, 'status'>> = {},
144 queryClient?: QueryClient,
145): IsMutatingAccessor {
146 const controller = new IsMutatingController(host, filters, queryClient)
147 return Object.assign(
148 createValueAccessor(() => controller.current),
149 {
150 destroy: () => controller.destroy(),
151 },
152 )
153}