@@ -20,6 +20,7 @@ import type {
20
20
QueryArgFrom ,
21
21
QueryDefinition ,
22
22
ResultTypeFrom ,
23
+ FullTagDescription ,
23
24
} from '../endpointDefinitions'
24
25
import { isQueryDefinition } from '../endpointDefinitions'
25
26
import { calculateProvidedBy } from '../endpointDefinitions'
@@ -210,6 +211,7 @@ export type PatchCollection = {
210
211
* A function that will undo the cache update.
211
212
*/
212
213
undo : ( ) => void
214
+ provided : readonly FullTagDescription < string > [ ]
213
215
}
214
216
215
217
export function buildThunks <
@@ -222,12 +224,14 @@ export function buildThunks<
222
224
context : { endpointDefinitions } ,
223
225
serializeQueryArgs,
224
226
api,
227
+ assertTagType,
225
228
} : {
226
229
baseQuery : BaseQuery
227
230
reducerPath : ReducerPath
228
231
context : ApiContext < Definitions >
229
232
serializeQueryArgs : InternalSerializeQueryArgs
230
233
api : Api < BaseQuery , Definitions , ReducerPath , any >
234
+ assertTagType : AssertTagTypes
231
235
} ) {
232
236
type State = RootState < any , string , ReducerPath >
233
237
@@ -247,32 +251,43 @@ export function buildThunks<
247
251
}
248
252
249
253
const updateQueryData : UpdateQueryDataThunk < EndpointDefinitions , State > =
250
- ( endpointName , args , updateRecipe ) => ( dispatch , getState ) => {
254
+ ( endpointName , args , updateRecipe , updateProvided = true ) =>
255
+ ( dispatch , getState ) => {
251
256
const currentState = (
252
257
api . endpoints [ endpointName ] as ApiEndpointQuery < any , any >
253
258
) . select ( args ) ( getState ( ) )
254
259
let ret : PatchCollection = {
255
260
patches : [ ] ,
256
261
inversePatches : [ ] ,
257
- undo : ( ) =>
262
+ undo : ( ) => {
258
263
dispatch (
259
264
api . util . patchQueryData ( endpointName , args , ret . inversePatches )
260
- ) ,
265
+ )
266
+ // this needs to `getState` the current value after patching the `inversePatch`
267
+ const oldValue = getState ( )
268
+ dispatch (
269
+ // `updateProvidedBy` needs to be implemented as a new reducer on `invalidationSlice`
270
+ updateProvidedBy ( endpointName , args , calculateNewProvided ( oldValue ) )
271
+ )
272
+ } ,
273
+ provided : [ ] ,
261
274
}
262
275
if ( currentState . status === QueryStatus . uninitialized ) {
263
276
return ret
264
277
}
278
+ let newValue
265
279
if ( 'data' in currentState ) {
266
280
if ( isDraftable ( currentState . data ) ) {
267
- const [ , patches , inversePatches ] = produceWithPatches (
281
+ const [ value , patches , inversePatches ] = produceWithPatches (
268
282
currentState . data ,
269
283
updateRecipe
270
284
)
271
285
ret . patches . push ( ...patches )
272
286
ret . inversePatches . push ( ...inversePatches )
287
+ newValue = value
273
288
} else {
274
- const value = updateRecipe ( currentState . data )
275
- ret . patches . push ( { op : 'replace' , path : [ ] , value } )
289
+ newValue = updateRecipe ( currentState . data )
290
+ ret . patches . push ( { op : 'replace' , path : [ ] , value : newValue } )
276
291
ret . inversePatches . push ( {
277
292
op : 'replace' ,
278
293
path : [ ] ,
@@ -281,9 +296,25 @@ export function buildThunks<
281
296
}
282
297
}
283
298
299
+ if ( updateProvided ) {
300
+ ret . provided = calculateNewProvided ( newValue )
301
+ }
302
+
303
+ // `patchQueryData` needs to be added as `extraReducer` on `invalidationSlice`
284
304
dispatch ( api . util . patchQueryData ( endpointName , args , ret . patches ) )
285
305
286
306
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
+ }
287
318
}
288
319
289
320
const upsertQueryData : UpsertQueryDataThunk < Definitions , State > =
0 commit comments