Skip to content

Commit 3018d96

Browse files
committed
feat: support nullish tags in invalidateTags and selectInvalidatedBy
1 parent 7af5345 commit 3018d96

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

packages/toolkit/src/query/core/buildSelectors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
TagTypesFrom,
1010
} from '../endpointDefinitions'
1111
import { expandTagDescription } from '../endpointDefinitions'
12-
import { flatten } from '../utils'
12+
import { flatten, isNotNullish } from '../utils'
1313
import type {
1414
MutationSubState,
1515
QueryCacheKey,
@@ -205,15 +205,15 @@ export function buildSelectors<
205205

206206
function selectInvalidatedBy(
207207
state: RootState,
208-
tags: ReadonlyArray<TagDescription<string>>,
208+
tags: ReadonlyArray<TagDescription<string> | null | undefined>,
209209
): Array<{
210210
endpointName: string
211211
originalArgs: any
212212
queryCacheKey: QueryCacheKey
213213
}> {
214214
const apiState = state[reducerPath]
215215
const toInvalidate = new Set<QueryCacheKey>()
216-
for (const tag of tags.map(expandTagDescription)) {
216+
for (const tag of tags.filter(isNotNullish).map(expandTagDescription)) {
217217
const provided = apiState.provided[tag.type]
218218
if (!provided) {
219219
continue

packages/toolkit/src/query/tests/invalidation.test.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const tagTypes = [
1414
'giraffe',
1515
] as const
1616
type TagTypes = (typeof tagTypes)[number]
17-
type Tags = TagDescription<TagTypes>[]
18-
17+
type ProvidedTags = TagDescription<TagTypes>[]
18+
type InvalidatesTags = (ProvidedTags[number] | null | undefined)[]
1919
/** providesTags, invalidatesTags, shouldInvalidate */
20-
const caseMatrix: [Tags, Tags, boolean][] = [
20+
const caseMatrix: [ProvidedTags, InvalidatesTags, boolean][] = [
2121
// *****************************
2222
// basic invalidation behavior
2323
// *****************************
@@ -39,7 +39,11 @@ const caseMatrix: [Tags, Tags, boolean][] = [
3939
// type + id invalidates type + id
4040
[[{ type: 'apple', id: 1 }], [{ type: 'apple', id: 1 }], true],
4141
[[{ type: 'apple', id: 1 }], [{ type: 'apple', id: 2 }], false],
42-
42+
// null and undefined
43+
[['apple'], [null], false],
44+
[['apple'], [undefined], false],
45+
[['apple'], [null, 'apple'], true],
46+
[['apple'], [undefined, 'apple'], true],
4347
// *****************************
4448
// test multiple values in array
4549
// *****************************

0 commit comments

Comments
 (0)