Skip to content

Commit

Permalink
fix: clear cache timeouts
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jack Ellis committed May 15, 2020
1 parent f671595 commit 8254bd8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/queryCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function makeQueryCache() {
}

cache.clear = () => {
Object.values(cache.queries).forEach(query => query.clear())
cache.queries = {}
notifyGlobalListeners()
}
Expand Down Expand Up @@ -474,6 +475,15 @@ export function makeQueryCache() {
query.scheduleStaleTimeout()
}

query.clear = () => {
clearTimeout(query.staleTimeout)
clearTimeout(query.cacheTimeout)
if (query.cancelQueries) {
query.cancelQueries()
}
query.cancelled = cancelledError
}

return query
}

Expand Down
3 changes: 2 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ export interface CachedQuery<T> {
setData(
dataOrUpdater: unknown | ((oldData: unknown | undefined) => unknown)
): void
clear(): void
}

export interface QueryCache {
Expand Down Expand Up @@ -694,7 +695,7 @@ export interface QueryCache {
getQueries(queryKey: AnyQueryKey): Array<CachedQuery<unknown>>
isFetching: number
subscribe(callback: (queryCache: QueryCache) => void): () => void
clear(): Array<CachedQuery<unknown>>
clear(): void
}

export const queryCache: QueryCache
Expand Down

0 comments on commit 8254bd8

Please sign in to comment.