Skip to content

Commit

Permalink
feat: support nullish tags in invalidateTags and selectInvalidatedBy
Browse files Browse the repository at this point in the history
  • Loading branch information
FaberVitale committed Oct 19, 2024
1 parent 7af5345 commit 0b68542
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/toolkit/src/query/core/buildSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
TagTypesFrom,
} from '../endpointDefinitions'
import { expandTagDescription } from '../endpointDefinitions'
import { flatten } from '../utils'
import { flatten, isNotNullish } from '../utils'
import type {
MutationSubState,
QueryCacheKey,
Expand Down Expand Up @@ -205,15 +205,15 @@ export function buildSelectors<

function selectInvalidatedBy(
state: RootState,
tags: ReadonlyArray<TagDescription<string>>,
tags: ReadonlyArray<TagDescription<string> | null | undefined>,
): Array<{
endpointName: string
originalArgs: any
queryCacheKey: QueryCacheKey
}> {
const apiState = state[reducerPath]
const toInvalidate = new Set<QueryCacheKey>()
for (const tag of tags.map(expandTagDescription)) {
for (const tag of tags.filter(isNotNullish).map(expandTagDescription)) {
const provided = apiState.provided[tag.type]
if (!provided) {
continue
Expand Down
12 changes: 8 additions & 4 deletions packages/toolkit/src/query/tests/invalidation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const tagTypes = [
'giraffe',
] as const
type TagTypes = (typeof tagTypes)[number]
type Tags = TagDescription<TagTypes>[]

type ProvidedTags = TagDescription<TagTypes>[]
type InvalidatesTags = (ProvidedTags[number] | null | undefined)[]
/** providesTags, invalidatesTags, shouldInvalidate */
const caseMatrix: [Tags, Tags, boolean][] = [
const caseMatrix: [ProvidedTags, InvalidatesTags, boolean][] = [
// *****************************
// basic invalidation behavior
// *****************************
Expand All @@ -39,7 +39,11 @@ const caseMatrix: [Tags, Tags, boolean][] = [
// type + id invalidates type + id
[[{ type: 'apple', id: 1 }], [{ type: 'apple', id: 1 }], true],
[[{ type: 'apple', id: 1 }], [{ type: 'apple', id: 2 }], false],

// null and undefined
[['apple'], [null], false],
[['apple'], [undefined], false],
[['apple'], [null, 'apple'], true],
[['apple'], [undefined, 'apple'], true],
// *****************************
// test multiple values in array
// *****************************
Expand Down

0 comments on commit 0b68542

Please sign in to comment.