@@ -183,24 +183,68 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
183183 }
184184 const lifecycleMap : Record < string , CacheLifecycle > = { }
185185
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+
186210 const handler : ApiMiddlewareInternalHandler = (
187211 action ,
188212 mwApi ,
189213 stateBefore ,
190214 ) => {
191215 const cacheKey = getCacheKey ( action )
192216
193- if ( queryThunk . pending . match ( action ) ) {
217+ function checkForNewCacheKey (
218+ endpointName : string ,
219+ cacheKey : string ,
220+ requestId : string ,
221+ originalArgs : unknown ,
222+ ) {
194223 const oldState = stateBefore [ reducerPath ] . queries [ cacheKey ]
195224 const state = mwApi . getState ( ) [ reducerPath ] . queries [ cacheKey ]
196225 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 ,
202243 action . meta . requestId ,
244+ originalArgs ,
203245 )
246+
247+ resolveLifecycleEntry ( queryCacheKey , value , { } )
204248 }
205249 } else if ( mutationThunk . pending . match ( action ) ) {
206250 const state = mwApi . getState ( ) [ reducerPath ] . mutations [ cacheKey ]
@@ -214,27 +258,15 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
214258 )
215259 }
216260 } 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 )
225262 } else if (
226263 api . internalActions . removeQueryResult . match ( action ) ||
227264 api . internalActions . removeMutationResult . match ( action )
228265 ) {
229- const lifecycle = lifecycleMap [ cacheKey ]
230- if ( lifecycle ) {
231- delete lifecycleMap [ cacheKey ]
232- lifecycle . cacheEntryRemoved ( )
233- }
266+ removeLifecycleEntry ( cacheKey )
234267 } 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 )
238270 }
239271 }
240272 }
0 commit comments