Skip to content

Commit ee0cf74

Browse files
dutziworksmarkerikson
authored andcommittedSep 24, 2023
batch queryResultPatched and updateProvidedBy
1 parent 53ce9e2 commit ee0cf74

File tree

1 file changed

+43
-32
lines changed

1 file changed

+43
-32
lines changed
 

‎packages/toolkit/src/query/core/buildSlice.ts

+43-32
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,22 @@ export function buildSlice({
126126
},
127127
prepare: prepareAutoBatched<QuerySubstateIdentifier>(),
128128
},
129-
queryResultPatched(
130-
draft,
131-
{
132-
payload: { queryCacheKey, patches },
133-
}: PayloadAction<
129+
queryResultPatched: {
130+
reducer(
131+
draft,
132+
{
133+
payload: { queryCacheKey, patches },
134+
}: PayloadAction<
135+
QuerySubstateIdentifier & { patches: readonly Patch[] }
136+
>
137+
) {
138+
updateQuerySubstateIfExists(draft, queryCacheKey, (substate) => {
139+
substate.data = applyPatches(substate.data as any, patches.concat())
140+
})
141+
},
142+
prepare: prepareAutoBatched<
134143
QuerySubstateIdentifier & { patches: readonly Patch[] }
135-
>
136-
) {
137-
updateQuerySubstateIfExists(draft, queryCacheKey, (substate) => {
138-
substate.data = applyPatches(substate.data as any, patches.concat())
139-
})
144+
>(),
140145
},
141146
},
142147
extraReducers(builder) {
@@ -327,33 +332,39 @@ export function buildSlice({
327332
name: `${reducerPath}/invalidation`,
328333
initialState: initialState as InvalidationState<string>,
329334
reducers: {
330-
updateProvidedBy: (
331-
draft,
332-
action: PayloadAction<{
333-
queryCacheKey: QueryCacheKey
334-
providedTags: readonly FullTagDescription<string>[]
335-
}>
336-
) => {
337-
const { queryCacheKey, providedTags } = action.payload
335+
updateProvidedBy: {
336+
reducer(
337+
draft,
338+
action: PayloadAction<{
339+
queryCacheKey: QueryCacheKey
340+
providedTags: readonly FullTagDescription<string>[]
341+
}>
342+
) {
343+
const { queryCacheKey, providedTags } = action.payload
338344

339-
for (const tagTypeSubscriptions of Object.values(draft)) {
340-
for (const idSubscriptions of Object.values(tagTypeSubscriptions)) {
341-
const foundAt = idSubscriptions.indexOf(queryCacheKey)
342-
if (foundAt !== -1) {
343-
idSubscriptions.splice(foundAt, 1)
345+
for (const tagTypeSubscriptions of Object.values(draft)) {
346+
for (const idSubscriptions of Object.values(tagTypeSubscriptions)) {
347+
const foundAt = idSubscriptions.indexOf(queryCacheKey)
348+
if (foundAt !== -1) {
349+
idSubscriptions.splice(foundAt, 1)
350+
}
344351
}
345352
}
346-
}
347353

348-
for (const { type, id } of providedTags) {
349-
const subscribedQueries = ((draft[type] ??= {})[
350-
id || '__internal_without_id'
351-
] ??= [])
352-
const alreadySubscribed = subscribedQueries.includes(queryCacheKey)
353-
if (!alreadySubscribed) {
354-
subscribedQueries.push(queryCacheKey)
354+
for (const { type, id } of providedTags) {
355+
const subscribedQueries = ((draft[type] ??= {})[
356+
id || '__internal_without_id'
357+
] ??= [])
358+
const alreadySubscribed = subscribedQueries.includes(queryCacheKey)
359+
if (!alreadySubscribed) {
360+
subscribedQueries.push(queryCacheKey)
361+
}
355362
}
356-
}
363+
},
364+
prepare: prepareAutoBatched<{
365+
queryCacheKey: QueryCacheKey
366+
providedTags: readonly FullTagDescription<string>[]
367+
}>(),
357368
},
358369
},
359370
extraReducers(builder) {

0 commit comments

Comments
 (0)
Please sign in to comment.