Skip to content

Commit fdae2ce

Browse files
scttcpercodexTkDodo
authored
ref(query-core): Reuse refetch interval resolver (#11332)
* perf(query-core): Reuse refetch interval resolver Reuse the generic query option resolver added in #11302 for refetch intervals. Keeps callback and nullish fallback behavior while dropping the duplicate branch. Co-Authored-By: OpenAI Codex <noreply@openai.com> * test(query-core): Remove redundant interval test Drop the cast-heavy timer spy coverage and keep the existing behavioral tests plus the disabled observer timer assertion. Co-authored-by: OpenAI Codex <noreply@openai.com> * ref: another instance for resolveQueryValue --------- Co-authored-by: OpenAI Codex <noreply@openai.com> Co-authored-by: Dominik Dorfmeister 🔮 <office@dorfmeister.cc>
1 parent 87fa4c1 commit fdae2ce

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎.changeset/lazy-cats-poll.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-core': patch
3+
---
4+
5+
Reuse the shared query option resolver for refetch intervals to reduce bundle size.

‎packages/query-core/src/__tests__/queryObserver.test.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ describe('queryObserver', () => {
14061406
expect(result.isStale).toBe(false)
14071407
})
14081408

1409-
it('should not schedule a stale timeout for disabled observers', () => {
1409+
it('should not schedule timers for disabled observers', () => {
14101410
const key = queryKey()
14111411
queryClient.setQueryData(key, 'data', {
14121412
updatedAt: Date.now() - 20,
@@ -1416,12 +1416,15 @@ describe('queryObserver', () => {
14161416
queryKey: key,
14171417
enabled: false,
14181418
staleTime: 10,
1419+
refetchInterval: 10,
14191420
})
14201421
const setTimeoutSpy = vi.spyOn(timeoutManager, 'setTimeout')
1422+
const setIntervalSpy = vi.spyOn(timeoutManager, 'setInterval')
14211423

14221424
const unsubscribe = observer.subscribe(vi.fn())
14231425

14241426
expect(setTimeoutSpy).not.toHaveBeenCalled()
1427+
expect(setIntervalSpy).not.toHaveBeenCalled()
14251428

14261429
unsubscribe()
14271430
})

‎packages/query-core/src/queryObserver.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,8 @@ export class QueryObserver<
402402

403403
#computeRefetchInterval() {
404404
return (
405-
(typeof this.options.refetchInterval === 'function'
406-
? this.options.refetchInterval(this.#currentQuery)
407-
: this.options.refetchInterval) ?? false
405+
resolveQueryValue(this.options.refetchInterval, this.#currentQuery) ??
406+
false
408407
)
409408
}
410409

@@ -757,7 +756,7 @@ function shouldFetchOn(
757756
resolveQueryValue(options.enabled, query) !== false &&
758757
resolveQueryValue(options.staleTime, query) !== 'static'
759758
) {
760-
const value = typeof field === 'function' ? field(query) : field
759+
const value = resolveQueryValue(field, query)
761760

762761
return value === 'always' || (value !== false && isStale(query, options))
763762
}

0 commit comments

Comments
 (0)