Skip to content

Commit 8b2ccbb

Browse files
Georg Wicke-Arndtmarkerikson
authored andcommitted
Fix performance concern for hasPendingRequests
1 parent f26dd39 commit 8b2ccbb

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/toolkit/src/query/core/buildMiddleware/invalidationByTags.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ export const buildInvalidationByTagsHandler: InternalHandlerBuilder = ({
7373
}
7474
}
7575

76+
function hasPendingRequests(state: CombinedState<EndpointDefinitions, string, string>) {
77+
for (const key in state.queries) {
78+
if (state.queries[key]?.status === QueryStatus.pending) return true;
79+
}
80+
for (const key in state.mutations) {
81+
if (state.mutations[key]?.status === QueryStatus.pending) return true;
82+
}
83+
return false;
84+
}
85+
7686
function invalidateTags(
7787
newTags: readonly FullTagDescription<string>[],
7888
mwApi: SubMiddlewareApi
@@ -82,13 +92,8 @@ export const buildInvalidationByTagsHandler: InternalHandlerBuilder = ({
8292

8393
pendingTagInvalidations.push(...newTags)
8494

85-
if (state.config.invalidationBehavior === 'delayed') {
86-
const hasPendingRequests = [
87-
...Object.values(state.queries),
88-
...Object.values(state.mutations),
89-
].some((x) => x?.status === QueryStatus.pending)
90-
91-
if (hasPendingRequests) return
95+
if (state.config.invalidationBehavior === 'delayed' && hasPendingRequests(state)) {
96+
return
9297
}
9398

9499
const tags = pendingTagInvalidations

0 commit comments

Comments
 (0)