Open History to see the last commit
1import {
2 MutationObserver,
3 type DefaultError,
4 type MutateOptions,
5 type MutationObserverOptions,
6 type MutationObserverResult,
7} from '@tanstack/query-core'
8import type { QueryClient } from '@tanstack/query-core'
9import type { ReactiveControllerHost } from 'lit'
10import {
11 createValueAccessor,
12 readAccessor,
13 type Accessor,
14 type ValueAccessor,
15} from './accessor.js'
16import { createMissingQueryClientError } from './context.js'
17import { BaseController } from './controllers/BaseController.js'
18β
19/**
20 * Options accepted by `createMutationController`.
21 *
22 * This is the Lit adapter shape for `MutationObserverOptions`. Pass it directly
23 * or through an `Accessor` when the options depend on Lit host state.
24 */
25export type CreateMutationOptions<
26 TData = unknown,
27 TError = DefaultError,
28 TVariables = void,
29 TOnMutateResult = unknown,
30> = MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>
31β
32/**
33 * Accessor returned by `createMutationController`.
34 *
35 * Call the accessor or read its `current` property to get the latest mutation
36 * result. The attached methods delegate to the active mutation observer.
37 */
38export type MutationResultAccessor<TData, TError, TVariables, TOnMutateResult> =
39 ValueAccessor<
40 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>
41 > & {
42 /**
43 * Starts the mutation and swallows the returned promise.
44 *
45 * Throws synchronously if no `QueryClient` can be resolved.
46 */
47 mutate: (
48 variables: TVariables,
49 options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,
50 ) => void
51 /**
52 * Starts the mutation and returns the observer promise.
53 *
54 * Rejects if no `QueryClient` can be resolved.
55 */
56 mutateAsync: MutationObserverResult<
57 TData,
58 TError,
59 TVariables,
60 TOnMutateResult
61 >['mutate']
62 /** Resets the mutation observer to its idle state. */
63 reset: MutationObserverResult<
64 TData,
65 TError,
66 TVariables,
67 TOnMutateResult
68 >['reset']
69 /** Removes the controller from its Lit host and unsubscribes observers. */
70 destroy: () => void
71 }
72β
73function createIdleMutationResult<
74 TData,
75 TError,
76 TVariables,
77 TOnMutateResult,
78>(): MutationObserverResult<TData, TError, TVariables, TOnMutateResult> {
79 return {
80 context: undefined,
81 data: undefined,
82 error: null,
83 failureCount: 0,
84 failureReason: null,
85 isError: false,
86 isIdle: true,
87 isPending: false,
88 isPaused: false,
89 isSuccess: false,
90 status: 'idle',
91 submittedAt: 0,
92 variables: undefined,
93 mutate: (() =>
94 Promise.reject(
95 createMissingQueryClientError(),
96 )) as MutationObserverResult<
97 TData,
98 TError,
99 TVariables,
100 TOnMutateResult
101 >['mutate'],
102 reset: (() => undefined) as MutationObserverResult<
103 TData,
104 TError,
105 TVariables,
106 TOnMutateResult
107 >['reset'],
108 } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>
109}
110β
111class MutationController<
112 TData,
113 TError,
114 TVariables,
115 TOnMutateResult,
116> extends BaseController<
117 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>
118> {
119 private readonly options: Accessor<
120 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>
121 >
122 private observer:
123 | MutationObserver<TData, TError, TVariables, TOnMutateResult>
124 | undefined
125 private unsubscribe: (() => void) | undefined
126 private queryClient: QueryClient | undefined
127β
128 constructor(
129 host: ReactiveControllerHost,
130 options: Accessor<
131 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>
132 >,
133 queryClient?: QueryClient,
134 ) {
135 super(host, createIdleMutationResult(), queryClient)
136 this.options = options
137β
138 if (!queryClient) {
139 return
140 }
141β
142 if (typeof options === 'function') {
143 return
144 }
145β
146 const observer = new MutationObserver(
147 queryClient,
148 this.defaultOptions(queryClient),
149 )
150 this.queryClient = queryClient
151 this.observer = observer
152 this.result = observer.getCurrentResult()
153 }
154β
155 protected onConnected(): void {
156 if (!this.syncClient()) {
157 return
158 }
159β
160 this.refreshOptions()
161 this.subscribe()
162 if (this.observer) {
163 this.setResult(this.observer.getCurrentResult())
164 }
165 }
166β
167 protected onDisconnected(): void {
168 this.unsubscribeObserver()
169 this.syncClient()
170 }
171β
172 protected onHostUpdate(): void {
173 if (typeof this.options !== 'function') {
174 return
175 }
176β
177 this.refreshOptions()
178 }
179β
180 protected onQueryClientChanged(): void {
181 if (!this.syncClient() || !this.connectedState) {
182 return
183 }
184β
185 this.refreshOptions()
186 this.subscribe()
187 if (this.observer) {
188 this.setResult(this.observer.getCurrentResult())
189 }
190 }
191β
192 mutate = (
193 variables: TVariables,
194 mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,
195 ): void => {
196 if (!this.syncClient() || !this.observer) {
197 throw createMissingQueryClientError()
198 }
199β
200 void this.observer.mutate(variables, mutateOptions).catch(() => {
201 // Intentionally swallow in sync mutate path.
202 })
203 }
204β
205 mutateAsync: MutationObserverResult<
206 TData,
207 TError,
208 TVariables,
209 TOnMutateResult
210 >['mutate'] = (...args) => {
211 if (!this.syncClient() || !this.observer) {
212 return Promise.reject(createMissingQueryClientError())
213 }
214β
215 return this.observer.mutate(...args)
216 }
217β
218 reset: MutationObserverResult<
219 TData,
220 TError,
221 TVariables,
222 TOnMutateResult
223 >['reset'] = () => {
224 if (!this.syncClient() || !this.observer) {
225 return
226 }
227β
228 this.observer.reset()
229 this.setResult(this.observer.getCurrentResult())
230 }
231β
232 private subscribe(): void {
233 if (!this.observer) {
234 return
235 }
236β
237 if (this.unsubscribe) {
238 return
239 }
240β
241 this.unsubscribe = this.observer.subscribe((next) => {
242 this.setResult(next)
243 })
244 }
245β
246 private unsubscribeObserver(): void {
247 this.unsubscribe?.()
248 this.unsubscribe = undefined
249 }
250β
251 private syncClient(): boolean {
252 const nextClient = this.tryGetQueryClient()
253 if (!nextClient) {
254 this.unsubscribeObserver()
255 this.queryClient = undefined
256 this.observer = undefined
257 this.setResult(createIdleMutationResult())
258 return false
259 }
260β
261 if (nextClient === this.queryClient) {
262 return true
263 }
264β
265 this.unsubscribeObserver()
266 this.queryClient = nextClient
267 this.observer = new MutationObserver(
268 this.queryClient,
269 this.defaultOptions(this.queryClient),
270 )
271 this.setResult(this.observer.getCurrentResult())
272 return true
273 }
274β
275 private refreshOptions(): boolean {
276 if (!this.syncClient() || !this.observer || !this.queryClient) {
277 return false
278 }
279β
280 this.observer.setOptions(this.defaultOptions())
281 this.setResult(this.observer.getCurrentResult())
282 return true
283 }
284β
285 private defaultOptions(
286 client = this.queryClient,
287 ): MutationObserverOptions<TData, TError, TVariables, TOnMutateResult> {
288 if (!client) {
289 throw createMissingQueryClientError()
290 }
291β
292 return client.defaultMutationOptions(readAccessor(this.options))
293 }
294}
295β
296/**
297 * Creates a Lit reactive controller that subscribes the host to a mutation.
298 *
299 * The returned accessor is callable and also exposes `current`, `mutate`,
300 * `mutateAsync`, `reset`, and `destroy`. When `options` is a function, it is
301 * re-read during host updates so mutation options can follow reactive host
302 * state.
303 *
304 * If `queryClient` is omitted, the controller resolves the client from the
305 * nearest connected `QueryClientProvider`.
306 *
307 * @param host - The Lit reactive controller host that owns the mutation
308 * subscription.
309 * @param options - Mutation observer options, or a getter that returns options.
310 * @param queryClient - Optional explicit query client. Provide this for
311 * controllers that should not resolve a client from Lit context.
312 * @returns An accessor for the latest mutation result with mutation helper
313 * methods.
314 *
315 * @example
316 * ```ts
317 * import { LitElement, html } from 'lit'
318 * import { createMutationController } from '@tanstack/lit-query'
319 *
320 * class AddTodoForm extends LitElement {
321 * private readonly addTodo = createMutationController(this, {
322 * mutationFn: (title: string) =>
323 * fetch('/api/todos', { method: 'POST', body: JSON.stringify({ title }) }),
324 * })
325 *
326 * render() {
327 * const mutation = this.addTodo()
328 *
329 * return html`
330 * <button ?disabled=${mutation.isPending} @click=${() => this.addTodo.mutate('Ship docs')}>
331 * Add todo
332 * </button>
333 * `
334 * }
335 * }
336 * ```
337 */
338export function createMutationController<
339 TData = unknown,
340 TError = DefaultError,
341 TVariables = void,
342 TOnMutateResult = unknown,
343>(
344 host: ReactiveControllerHost,
345 options: Accessor<
346 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>
347 >,
348 queryClient?: QueryClient,
349): MutationResultAccessor<TData, TError, TVariables, TOnMutateResult> {
350 const controller = new MutationController(host, options, queryClient)
351β
352 return Object.assign(
353 createValueAccessor(() => controller.current),
354 {
355 mutate: controller.mutate,
356 mutateAsync: controller.mutateAsync,
357 reset: controller.reset,
358 destroy: () => controller.destroy(),
359 },
360 )
361}