Skip to content

Commit

Permalink
fix: clear cache timeouts (#495)
Browse files Browse the repository at this point in the history
* 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 <jack.ellis@godaddy.com>
  • Loading branch information
jackmellis and Jack Ellis authored May 17, 2020
1 parent c5b49be commit f76c860
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/queryCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function makeQueryCache() {
}

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

query.clear = () => {
clearTimeout(query.staleTimeout)
clearTimeout(query.cacheTimeout)
query.cancel()
}

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 @@ -701,7 +702,7 @@ export interface QueryCache {
): void
isFetching: number
subscribe(callback: (queryCache: QueryCache) => void): () => void
clear(): Array<CachedQuery<unknown>>
clear(): void
}

export const queryCache: QueryCache
Expand Down

0 comments on commit f76c860

Please sign in to comment.