Skip to content

Commit f76c860

Browse files
jackmellisJack Ellis
and
Jack Ellis
authored
fix: clear cache timeouts (TanStack#495)
* fix: clear cache timeouts calling queryCache.clear() was empting the queries object but not tearing any of them down this commit adds a clear method to the individual queries, which cancels timeouts and promises queryCache.clear now loops through the queries and clears each one I've also update the return type of queryCache.clear as it doesn't actually return anything * fixup! fix: clear cache timeouts Co-authored-by: Jack Ellis <[email protected]>
1 parent c5b49be commit f76c860

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/queryCache.js

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export function makeQueryCache() {
8484
}
8585

8686
cache.clear = () => {
87+
Object.values(cache.queries).forEach(query => query.clear())
8788
cache.queries = {}
8889
notifyGlobalListeners()
8990
}
@@ -516,6 +517,12 @@ export function makeQueryCache() {
516517
query.scheduleStaleTimeout()
517518
}
518519

520+
query.clear = () => {
521+
clearTimeout(query.staleTimeout)
522+
clearTimeout(query.cacheTimeout)
523+
query.cancel()
524+
}
525+
519526
return query
520527
}
521528

types/index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ export interface CachedQuery<T> {
593593
setData(
594594
dataOrUpdater: unknown | ((oldData: unknown | undefined) => unknown)
595595
): void
596+
clear(): void
596597
}
597598

598599
export interface QueryCache {
@@ -701,7 +702,7 @@ export interface QueryCache {
701702
): void
702703
isFetching: number
703704
subscribe(callback: (queryCache: QueryCache) => void): () => void
704-
clear(): Array<CachedQuery<unknown>>
705+
clear(): void
705706
}
706707

707708
export const queryCache: QueryCache

0 commit comments

Comments
 (0)