From cc1f1b2436368a63c17d484a9b668743e2a3f7cf Mon Sep 17 00:00:00 2001 From: Janne Vuoti Date: Wed, 17 Nov 2021 21:40:46 +0200 Subject: [PATCH] Replace array type guard with flatmap This way no extra utils need to be added. --- src/core/queriesObserver.ts | 18 +++++++++--------- src/core/utils.ts | 7 ------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/core/queriesObserver.ts b/src/core/queriesObserver.ts index c9e1d0d7b1..1a32d51e6e 100644 --- a/src/core/queriesObserver.ts +++ b/src/core/queriesObserver.ts @@ -1,4 +1,4 @@ -import { difference, notNullOrUndefined, replaceAt } from './utils' +import { difference, replaceAt } from './utils' import { notifyManager } from './notifyManager' import type { QueryObserverOptions, QueryObserverResult } from './types' import type { QueryClient } from './queryClient' @@ -77,17 +77,17 @@ export class QueriesObserver extends Subscribable { this.client.defaultQueryObserverOptions(options) ) - const matchingObservers: QueryObserverMatch[] = defaultedQueryOptions - .map(defaultedOptions => { + const matchingObservers: QueryObserverMatch[] = defaultedQueryOptions.flatMap( + defaultedOptions => { const match = prevObservers.find( - observer => observer.options.queryHash === defaultedOptions.queryHash! + observer => observer.options.queryHash === defaultedOptions.queryHash ) if (match != null) { - return { defaultedQueryOptions: defaultedOptions, observer: match } + return [{ defaultedQueryOptions: defaultedOptions, observer: match }] } - return null - }) - .filter(notNullOrUndefined) + return [] + } + ) const matchedQueryHashes = matchingObservers.map( match => match.defaultedQueryOptions.queryHash @@ -104,7 +104,7 @@ export class QueriesObserver extends Subscribable { const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map( (options, index) => { - if (options.keepPreviousData && unmatchedObservers.length > 0) { + if (options.keepPreviousData) { // return previous data from one of the observers that no longer match const previouslyUsedObserver = unmatchedObservers[index] if (previouslyUsedObserver !== undefined) { diff --git a/src/core/utils.ts b/src/core/utils.ts index 41597e6e64..1b01474502 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -453,10 +453,3 @@ export function getAbortController(): AbortController | undefined { return new AbortController() } } - -/** - * Type predicate to filter an array of null or undefined elements - */ -export function notNullOrUndefined(value: T): value is NonNullable { - return value !== null && value !== undefined -}