Skip to content

Commit

Permalink
Replace array type guard with flatmap
Browse files Browse the repository at this point in the history
This way no extra utils need to be added.
  • Loading branch information
jvuoti committed Nov 17, 2021
1 parent a9bd6f3 commit cc1f1b2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
18 changes: 9 additions & 9 deletions 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'
Expand Down Expand Up @@ -77,17 +77,17 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
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
Expand All @@ -104,7 +104,7 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {

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) {
Expand Down
7 changes: 0 additions & 7 deletions src/core/utils.ts
Expand Up @@ -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<T>(value: T): value is NonNullable<T> {
return value !== null && value !== undefined
}

0 comments on commit cc1f1b2

Please sign in to comment.