@@ -183,24 +183,68 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
183
183
}
184
184
const lifecycleMap : Record < string , CacheLifecycle > = { }
185
185
186
+ function resolveLifecycleEntry (
187
+ cacheKey : string ,
188
+ data : unknown ,
189
+ meta : unknown ,
190
+ ) {
191
+ const lifecycle = lifecycleMap [ cacheKey ]
192
+
193
+ if ( lifecycle ?. valueResolved ) {
194
+ lifecycle . valueResolved ( {
195
+ data,
196
+ meta,
197
+ } )
198
+ delete lifecycle . valueResolved
199
+ }
200
+ }
201
+
202
+ function removeLifecycleEntry ( cacheKey : string ) {
203
+ const lifecycle = lifecycleMap [ cacheKey ]
204
+ if ( lifecycle ) {
205
+ delete lifecycleMap [ cacheKey ]
206
+ lifecycle . cacheEntryRemoved ( )
207
+ }
208
+ }
209
+
186
210
const handler : ApiMiddlewareInternalHandler = (
187
211
action ,
188
212
mwApi ,
189
213
stateBefore ,
190
214
) => {
191
215
const cacheKey = getCacheKey ( action )
192
216
193
- if ( queryThunk . pending . match ( action ) ) {
217
+ function checkForNewCacheKey (
218
+ endpointName : string ,
219
+ cacheKey : string ,
220
+ requestId : string ,
221
+ originalArgs : unknown ,
222
+ ) {
194
223
const oldState = stateBefore [ reducerPath ] . queries [ cacheKey ]
195
224
const state = mwApi . getState ( ) [ reducerPath ] . queries [ cacheKey ]
196
225
if ( ! oldState && state ) {
197
- handleNewKey (
198
- action . meta . arg . endpointName ,
199
- action . meta . arg . originalArgs ,
200
- cacheKey ,
201
- mwApi ,
226
+ handleNewKey ( endpointName , originalArgs , cacheKey , mwApi , requestId )
227
+ }
228
+ }
229
+
230
+ if ( queryThunk . pending . match ( action ) ) {
231
+ checkForNewCacheKey (
232
+ action . meta . arg . endpointName ,
233
+ cacheKey ,
234
+ action . meta . requestId ,
235
+ action . meta . arg . originalArgs ,
236
+ )
237
+ } else if ( api . internalActions . cacheEntriesUpserted . match ( action ) ) {
238
+ for ( const { queryDescription, value } of action . payload ) {
239
+ const { endpointName, originalArgs, queryCacheKey } = queryDescription
240
+ checkForNewCacheKey (
241
+ endpointName ,
242
+ queryCacheKey ,
202
243
action . meta . requestId ,
244
+ originalArgs ,
203
245
)
246
+
247
+ resolveLifecycleEntry ( queryCacheKey , value , { } )
204
248
}
205
249
} else if ( mutationThunk . pending . match ( action ) ) {
206
250
const state = mwApi . getState ( ) [ reducerPath ] . mutations [ cacheKey ]
@@ -214,27 +258,15 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
214
258
)
215
259
}
216
260
} else if ( isFulfilledThunk ( action ) ) {
217
- const lifecycle = lifecycleMap [ cacheKey ]
218
- if ( lifecycle ?. valueResolved ) {
219
- lifecycle . valueResolved ( {
220
- data : action . payload ,
221
- meta : action . meta . baseQueryMeta ,
222
- } )
223
- delete lifecycle . valueResolved
224
- }
261
+ resolveLifecycleEntry ( cacheKey , action . payload , action . meta . baseQueryMeta )
225
262
} else if (
226
263
api . internalActions . removeQueryResult . match ( action ) ||
227
264
api . internalActions . removeMutationResult . match ( action )
228
265
) {
229
- const lifecycle = lifecycleMap [ cacheKey ]
230
- if ( lifecycle ) {
231
- delete lifecycleMap [ cacheKey ]
232
- lifecycle . cacheEntryRemoved ( )
233
- }
266
+ removeLifecycleEntry ( cacheKey )
234
267
} else if ( api . util . resetApiState . match ( action ) ) {
235
- for ( const [ cacheKey , lifecycle ] of Object . entries ( lifecycleMap ) ) {
236
- delete lifecycleMap [ cacheKey ]
237
- lifecycle . cacheEntryRemoved ( )
268
+ for ( const cacheKey of Object . keys ( lifecycleMap ) ) {
269
+ removeLifecycleEntry ( cacheKey )
238
270
}
239
271
}
240
272
}
0 commit comments