@@ -5,9 +5,12 @@ import {
5
5
isRejectedWithValue ,
6
6
} from '../rtkImports'
7
7
8
- import type { FullTagDescription } from '../../endpointDefinitions'
8
+ import type {
9
+ EndpointDefinitions ,
10
+ FullTagDescription ,
11
+ } from '../../endpointDefinitions'
9
12
import { calculateProvidedBy } from '../../endpointDefinitions'
10
- import type { QueryCacheKey } from '../apiState'
13
+ import type { CombinedState , QueryCacheKey } from '../apiState'
11
14
import { QueryStatus } from '../apiState'
12
15
import { calculateProvidedByThunk } from '../buildThunks'
13
16
import type {
@@ -29,11 +32,7 @@ export const buildInvalidationByTagsHandler: InternalHandlerBuilder = ({
29
32
refetchQuery,
30
33
internalState,
31
34
} ) => {
32
- const {
33
- removeQueryResult,
34
- addPendingTagInvalidations,
35
- clearPendingTagInvalidations,
36
- } = api . internalActions
35
+ const { removeQueryResult } = api . internalActions
37
36
const isThunkActionWithTags = isAnyOf (
38
37
isFulfilled ( mutationThunk ) ,
39
38
isRejectedWithValue ( mutationThunk )
@@ -46,22 +45,23 @@ export const buildInvalidationByTagsHandler: InternalHandlerBuilder = ({
46
45
isRejected ( queryThunk )
47
46
)
48
47
48
+ let pendingTagInvalidations : FullTagDescription < string > [ ] = [ ]
49
+
49
50
const handler : ApiMiddlewareInternalHandler = ( action , mwApi ) => {
50
51
if ( isThunkActionWithTags ( action ) ) {
51
- processPendingTagInvalidations (
52
+ invalidateTags (
52
53
calculateProvidedByThunk (
53
54
action ,
54
55
'invalidatesTags' ,
55
56
endpointDefinitions ,
56
57
assertTagType
57
58
) ,
58
- mwApi ,
59
- internalState
59
+ mwApi
60
60
)
61
61
} else if ( isQueryEnd ( action ) ) {
62
- processPendingTagInvalidations ( [ ] , mwApi , internalState )
62
+ invalidateTags ( [ ] , mwApi )
63
63
} else if ( api . util . invalidateTags . match ( action ) ) {
64
- processPendingTagInvalidations (
64
+ invalidateTags (
65
65
calculateProvidedBy (
66
66
action . payload ,
67
67
undefined ,
@@ -70,57 +70,40 @@ export const buildInvalidationByTagsHandler: InternalHandlerBuilder = ({
70
70
undefined ,
71
71
assertTagType
72
72
) ,
73
- mwApi ,
74
- internalState
73
+ mwApi
75
74
)
76
75
}
77
76
}
78
77
79
- function processPendingTagInvalidations (
80
- tags : readonly FullTagDescription < string > [ ] ,
81
- mwApi : SubMiddlewareApi ,
82
- internalState : InternalMiddlewareState
78
+ function hasPendingRequests (
79
+ state : CombinedState < EndpointDefinitions , string , string >
83
80
) {
84
- const rootState = mwApi . getState ( )
85
-
86
- const state = rootState [ reducerPath ]
87
-
88
- if ( state . config . invalidateImmediately ) {
89
- handleInvalidatedTags ( tags , mwApi )
90
- return
91
- }
92
-
93
- const hasPendingQueries = Object . values ( state . queries ) . some (
94
- ( x ) => x ?. status === QueryStatus . pending
95
- )
96
- const hasPendingMutations = Object . values ( state . mutations ) . some (
97
- ( x ) => x ?. status === QueryStatus . pending
98
- )
99
-
100
- if ( hasPendingQueries || hasPendingMutations ) {
101
- if ( tags && tags . length > 0 )
102
- mwApi . dispatch ( addPendingTagInvalidations ( tags ) )
103
- } else {
104
- handleInvalidatedTags ( tags , mwApi )
105
- }
81
+ return Object . values ( {
82
+ ...state . queries ,
83
+ ...state . mutations ,
84
+ } ) . some ( ( x ) => x ?. status === QueryStatus . pending )
106
85
}
107
86
108
- function handleInvalidatedTags (
87
+ function invalidateTags (
109
88
newTags : readonly FullTagDescription < string > [ ] ,
110
89
mwApi : SubMiddlewareApi
111
90
) {
112
91
const rootState = mwApi . getState ( )
113
92
const state = rootState [ reducerPath ]
114
93
115
- const tags = [ ...state . pendingTagInvalidations , ...newTags ]
94
+ pendingTagInvalidations . push ( ...newTags )
95
+
96
+ if ( ! state . config . invalidateImmediately && hasPendingRequests ( state ) ) {
97
+ return
98
+ }
99
+
100
+ const tags = pendingTagInvalidations
101
+ pendingTagInvalidations = [ ]
116
102
if ( tags . length === 0 ) return
117
103
118
104
const toInvalidate = api . util . selectInvalidatedBy ( rootState , tags )
119
105
120
106
context . batch ( ( ) => {
121
- if ( state . pendingTagInvalidations . length > 0 )
122
- mwApi . dispatch ( clearPendingTagInvalidations ( ) )
123
-
124
107
const valuesArray = Array . from ( toInvalidate . values ( ) )
125
108
for ( const { queryCacheKey } of valuesArray ) {
126
109
const querySubState = state . queries [ queryCacheKey ]
0 commit comments