From 8254bd880616939b0f74ca9445adb09c0ac7f0f5 Mon Sep 17 00:00:00 2001 From: Jack Ellis Date: Fri, 15 May 2020 11:36:53 +0100 Subject: [PATCH] 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 --- src/queryCache.js | 10 ++++++++++ types/index.d.ts | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/queryCache.js b/src/queryCache.js index 677795cc3aa..3df8ef0a38d 100644 --- a/src/queryCache.js +++ b/src/queryCache.js @@ -47,6 +47,7 @@ export function makeQueryCache() { } cache.clear = () => { + Object.values(cache.queries).forEach(query => query.clear()) cache.queries = {} notifyGlobalListeners() } @@ -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 } diff --git a/types/index.d.ts b/types/index.d.ts index cb23e40c445..4547a6858ee 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 { @@ -694,7 +695,7 @@ export interface QueryCache { getQueries(queryKey: AnyQueryKey): Array> isFetching: number subscribe(callback: (queryCache: QueryCache) => void): () => void - clear(): Array> + clear(): void } export const queryCache: QueryCache