From f76c8601a3c30ab279082a94ec1b51ed4920b52c Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 17 May 2020 01:48:55 +0100 Subject: [PATCH] fix: clear cache timeouts (#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 --- src/queryCache.js | 7 +++++++ types/index.d.ts | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/queryCache.js b/src/queryCache.js index 79a7467957..4dc47f72cf 100644 --- a/src/queryCache.js +++ b/src/queryCache.js @@ -84,6 +84,7 @@ export function makeQueryCache() { } cache.clear = () => { + Object.values(cache.queries).forEach(query => query.clear()) cache.queries = {} notifyGlobalListeners() } @@ -516,6 +517,12 @@ export function makeQueryCache() { query.scheduleStaleTimeout() } + query.clear = () => { + clearTimeout(query.staleTimeout) + clearTimeout(query.cacheTimeout) + query.cancel() + } + return query } diff --git a/types/index.d.ts b/types/index.d.ts index 09a8d43992..0edecdb7fc 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -593,6 +593,7 @@ export interface CachedQuery { setData( dataOrUpdater: unknown | ((oldData: unknown | undefined) => unknown) ): void + clear(): void } export interface QueryCache { @@ -701,7 +702,7 @@ export interface QueryCache { ): void isFetching: number subscribe(callback: (queryCache: QueryCache) => void): () => void - clear(): Array> + clear(): void } export const queryCache: QueryCache