@@ -165,7 +165,8 @@ export type PatchQueryDataThunk<
165
165
> = < EndpointName extends QueryKeys < Definitions > > (
166
166
endpointName : EndpointName ,
167
167
args : QueryArgFrom < Definitions [ EndpointName ] > ,
168
- patches : readonly Patch [ ]
168
+ patches : readonly Patch [ ] ,
169
+ updateProvided ?: boolean
169
170
) => ThunkAction < void , PartialState , any , AnyAction >
170
171
171
172
export type UpdateQueryDataThunk <
@@ -174,7 +175,8 @@ export type UpdateQueryDataThunk<
174
175
> = < EndpointName extends QueryKeys < Definitions > > (
175
176
endpointName : EndpointName ,
176
177
args : QueryArgFrom < Definitions [ EndpointName ] > ,
177
- updateRecipe : Recipe < ResultTypeFrom < Definitions [ EndpointName ] > >
178
+ updateRecipe : Recipe < ResultTypeFrom < Definitions [ EndpointName ] > > ,
179
+ updateProvided ?: boolean
178
180
) => ThunkAction < PatchCollection , PartialState , any , AnyAction >
179
181
180
182
export type UpsertQueryDataThunk <
@@ -211,7 +213,6 @@ export type PatchCollection = {
211
213
* A function that will undo the cache update.
212
214
*/
213
215
undo : ( ) => void
214
- provided : readonly FullTagDescription < string > [ ]
215
216
}
216
217
217
218
export function buildThunks <
@@ -236,41 +237,58 @@ export function buildThunks<
236
237
type State = RootState < any , string , ReducerPath >
237
238
238
239
const patchQueryData : PatchQueryDataThunk < EndpointDefinitions , State > =
239
- ( endpointName , args , patches ) => ( dispatch ) => {
240
+ ( endpointName , args , patches , updateProvided ) => ( dispatch , getState ) => {
240
241
const endpointDefinition = endpointDefinitions [ endpointName ]
242
+
243
+ const queryCacheKey = serializeQueryArgs ( {
244
+ queryArgs : args ,
245
+ endpointDefinition,
246
+ endpointName,
247
+ } )
248
+
241
249
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 } )
250
270
)
251
271
}
252
272
253
273
const updateQueryData : UpdateQueryDataThunk < EndpointDefinitions , State > =
254
274
( endpointName , args , updateRecipe , updateProvided = true ) =>
255
275
( 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
+
259
280
let ret : PatchCollection = {
260
281
patches : [ ] ,
261
282
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 : ( ) =>
268
284
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
+ ) ,
274
292
}
275
293
if ( currentState . status === QueryStatus . uninitialized ) {
276
294
return ret
@@ -296,25 +314,11 @@ export function buildThunks<
296
314
}
297
315
}
298
316
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
+ )
305
320
306
321
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
- }
318
322
}
319
323
320
324
const upsertQueryData : UpsertQueryDataThunk < Definitions , State > =
0 commit comments