Skip to content

Commit f532bda

Browse files
raashish1601TkDodo
andauthored
fix(lit-query): avoid redundant function option updates (#10716)
fix(lit-query): avoid redundant option updates Co-authored-by: Dominik Dorfmeister 🔮 <office@dorfmeister.cc>
1 parent c00ea27 commit f532bda

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/lit-query': patch
3+
---
4+
5+
Avoid scheduling redundant host updates when accessor function options resolve to an unchanged query result.

packages/lit-query/src/createQueryController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class QueryController<
243243

244244
const options = this.defaultOptions(this.queryClient)
245245
this.observer.setOptions(options)
246-
this.setResult(this.observer.getOptimisticResult(options))
246+
this.setResult(this.observer.getCurrentResult())
247247
return true
248248
}
249249

packages/lit-query/src/tests/query-controller.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,48 @@ describe('createQueryController', () => {
344344
expect(seenKeys.includes(2)).toBe(true)
345345
})
346346

347+
it('does not request a host update when function options resolve to an unchanged result', async () => {
348+
const client = new QueryClient({
349+
defaultOptions: {
350+
queries: {
351+
retry: false,
352+
},
353+
},
354+
})
355+
356+
const host = new TestControllerHost()
357+
let callCount = 0
358+
359+
const query = createQueryController(
360+
host,
361+
() => ({
362+
queryKey: ['query-controller', 'stable-function-options'] as const,
363+
staleTime: Infinity,
364+
queryFn: async () => {
365+
callCount += 1
366+
return 'stable'
367+
},
368+
}),
369+
client,
370+
)
371+
372+
host.connect()
373+
host.update()
374+
await waitFor(() => query().isSuccess)
375+
376+
await Promise.resolve()
377+
await Promise.resolve()
378+
const updatesAfterSuccess = host.updatesRequested
379+
380+
host.update()
381+
await Promise.resolve()
382+
await Promise.resolve()
383+
384+
expect(query().data).toBe('stable')
385+
expect(callCount).toBe(1)
386+
expect(host.updatesRequested).toBe(updatesAfterSuccess)
387+
})
388+
347389
it('QSEM-01: refetchOnMount follows stale-vs-fresh policy', async () => {
348390
const client = new QueryClient({
349391
defaultOptions: {

0 commit comments

Comments
 (0)