@@ -165,7 +165,8 @@ export type PatchQueryDataThunk<
165165> = < EndpointName extends QueryKeys < Definitions > > (
166166 endpointName : EndpointName ,
167167 args : QueryArgFrom < Definitions [ EndpointName ] > ,
168- patches : readonly Patch [ ]
168+ patches : readonly Patch [ ] ,
169+ updateProvided ?: boolean
169170) => ThunkAction < void , PartialState , any , AnyAction >
170171
171172export type UpdateQueryDataThunk <
@@ -174,7 +175,8 @@ export type UpdateQueryDataThunk<
174175> = < EndpointName extends QueryKeys < Definitions > > (
175176 endpointName : EndpointName ,
176177 args : QueryArgFrom < Definitions [ EndpointName ] > ,
177- updateRecipe : Recipe < ResultTypeFrom < Definitions [ EndpointName ] > >
178+ updateRecipe : Recipe < ResultTypeFrom < Definitions [ EndpointName ] > > ,
179+ updateProvided ?: boolean
178180) => ThunkAction < PatchCollection , PartialState , any , AnyAction >
179181
180182export type UpsertQueryDataThunk <
@@ -211,7 +213,6 @@ export type PatchCollection = {
211213 * A function that will undo the cache update.
212214 */
213215 undo : ( ) => void
214- provided : readonly FullTagDescription < string > [ ]
215216}
216217
217218export function buildThunks <
@@ -236,41 +237,58 @@ export function buildThunks<
236237 type State = RootState < any , string , ReducerPath >
237238
238239 const patchQueryData : PatchQueryDataThunk < EndpointDefinitions , State > =
239- ( endpointName , args , patches ) => ( dispatch ) => {
240+ ( endpointName , args , patches , updateProvided ) => ( dispatch , getState ) => {
240241 const endpointDefinition = endpointDefinitions [ endpointName ]
242+
243+ const queryCacheKey = serializeQueryArgs ( {
244+ queryArgs : args ,
245+ endpointDefinition,
246+ endpointName,
247+ } )
248+
241249 dispatch (
242- api . internalActions . queryResultPatched ( {
243- queryCacheKey : serializeQueryArgs ( {
244- queryArgs : args ,
245- endpointDefinition,
246- endpointName,
247- } ) ,
248- patches,
249- } )
250+ api . internalActions . queryResultPatched ( { queryCacheKey, patches } )
251+ )
252+
253+ if ( ! updateProvided ) {
254+ return
255+ }
256+
257+ const newValue = api . endpoints [ endpointName ] . select ( args ) ( getState ( ) )
258+
259+ const providedTags = calculateProvidedBy (
260+ endpointDefinition . providesTags ,
261+ newValue . data ,
262+ undefined ,
263+ args ,
264+ { } ,
265+ assertTagType
266+ )
267+
268+ dispatch (
269+ api . internalActions . updateProvidedBy ( { queryCacheKey, providedTags } )
250270 )
251271 }
252272
253273 const updateQueryData : UpdateQueryDataThunk < EndpointDefinitions , State > =
254274 ( endpointName , args , updateRecipe , updateProvided = true ) =>
255275 ( dispatch , getState ) => {
256- const currentState = (
257- api . endpoints [ endpointName ] as ApiEndpointQuery < any , any >
258- ) . select ( args ) ( getState ( ) )
276+ const endpointDefinition = api . endpoints [ endpointName ]
277+
278+ const currentState = endpointDefinition . select ( args ) ( getState ( ) )
279+
259280 let ret : PatchCollection = {
260281 patches : [ ] ,
261282 inversePatches : [ ] ,
262- undo : ( ) => {
263- dispatch (
264- api . util . patchQueryData ( endpointName , args , ret . inversePatches )
265- )
266- // this needs to `getState` the current value after patching the `inversePatch`
267- const oldValue = getState ( )
283+ undo : ( ) =>
268284 dispatch (
269- // `updateProvidedBy` needs to be implemented as a new reducer on `invalidationSlice`
270- updateProvidedBy ( endpointName , args , calculateNewProvided ( oldValue ) )
271- )
272- } ,
273- provided : [ ] ,
285+ api . util . patchQueryData (
286+ endpointName ,
287+ args ,
288+ ret . inversePatches ,
289+ updateProvided
290+ )
291+ ) ,
274292 }
275293 if ( currentState . status === QueryStatus . uninitialized ) {
276294 return ret
@@ -296,25 +314,11 @@ export function buildThunks<
296314 }
297315 }
298316
299- if ( updateProvided ) {
300- ret . provided = calculateNewProvided ( newValue )
301- }
302-
303- // `patchQueryData` needs to be added as `extraReducer` on `invalidationSlice`
304- dispatch ( api . util . patchQueryData ( endpointName , args , ret . patches ) )
317+ dispatch (
318+ api . util . patchQueryData ( endpointName , args , ret . patches , updateProvided )
319+ )
305320
306321 return ret
307-
308- function calculateNewProvided ( newValue : unknown ) {
309- return calculateProvidedBy (
310- endpointDefinitions [ endpointName ] . providesTags ,
311- newValue ,
312- undefined ,
313- args ,
314- { } ,
315- assertTagType
316- )
317- }
318322 }
319323
320324 const upsertQueryData : UpsertQueryDataThunk < Definitions , State > =
0 commit comments