Skip to content

Commit 0fa161e

Browse files
committed
refactor: cacheTime-zero
schedule a new garbage collection after each new fetch; this won't do anything when you still have observers, but it fixes an edge case where prefetching took longer than the cacheTime, in which case the query was again never removed test needed adaption because we don't instantly remove, but deferred by a tick
1 parent aba22c0 commit 0fa161e

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/core/query.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,8 @@ export class Query<
451451
// Notify cache callback
452452
this.cache.config.onSuccess?.(data, this as Query<any, any, any, any>)
453453

454-
// Remove query after fetching if cache time is 0
455-
if (this.cacheTime === 0) {
456-
this.optionalRemove()
457-
}
454+
// Remove query after fetching
455+
this.scheduleGc()
458456
},
459457
onError: (error: TError | { silent?: boolean }) => {
460458
// Optimistically update state if needed
@@ -473,10 +471,8 @@ export class Query<
473471
getLogger().error(error)
474472
}
475473

476-
// Remove query after fetching if cache time is 0
477-
if (this.cacheTime === 0) {
478-
this.optionalRemove()
479-
}
474+
// Remove query after fetching
475+
this.scheduleGc()
480476
},
481477
onFail: () => {
482478
this.dispatch({ type: 'failed' })

src/core/tests/queryClient.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,10 @@ describe('queryClient', () => {
416416
},
417417
{ cacheTime: 0 }
418418
)
419-
const result2 = queryClient.getQueryData(key1)
420419
expect(result).toEqual(1)
421-
expect(result2).toEqual(undefined)
420+
await waitFor(() =>
421+
expect(queryClient.getQueryData(key1)).toEqual(undefined)
422+
)
422423
})
423424

424425
test('should keep a query in cache if cache time is Infinity', async () => {

0 commit comments

Comments
 (0)