Skip to content

Commit 6274ec8

Browse files
phryneasmarkerikson
authored andcommittedSep 24, 2023
outline of work to be done
1 parent 9f4fdea commit 6274ec8

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed
 

‎packages/toolkit/src/query/core/buildThunks.ts

+37-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
QueryArgFrom,
2121
QueryDefinition,
2222
ResultTypeFrom,
23+
FullTagDescription,
2324
} from '../endpointDefinitions'
2425
import { isQueryDefinition } from '../endpointDefinitions'
2526
import { calculateProvidedBy } from '../endpointDefinitions'
@@ -210,6 +211,7 @@ export type PatchCollection = {
210211
* A function that will undo the cache update.
211212
*/
212213
undo: () => void
214+
provided: readonly FullTagDescription<string>[]
213215
}
214216

215217
export function buildThunks<
@@ -222,12 +224,14 @@ export function buildThunks<
222224
context: { endpointDefinitions },
223225
serializeQueryArgs,
224226
api,
227+
assertTagType,
225228
}: {
226229
baseQuery: BaseQuery
227230
reducerPath: ReducerPath
228231
context: ApiContext<Definitions>
229232
serializeQueryArgs: InternalSerializeQueryArgs
230233
api: Api<BaseQuery, Definitions, ReducerPath, any>
234+
assertTagType: AssertTagTypes
231235
}) {
232236
type State = RootState<any, string, ReducerPath>
233237

@@ -247,32 +251,43 @@ export function buildThunks<
247251
}
248252

249253
const updateQueryData: UpdateQueryDataThunk<EndpointDefinitions, State> =
250-
(endpointName, args, updateRecipe) => (dispatch, getState) => {
254+
(endpointName, args, updateRecipe, updateProvided = true) =>
255+
(dispatch, getState) => {
251256
const currentState = (
252257
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
253258
).select(args)(getState())
254259
let ret: PatchCollection = {
255260
patches: [],
256261
inversePatches: [],
257-
undo: () =>
262+
undo: () => {
258263
dispatch(
259264
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: [],
261274
}
262275
if (currentState.status === QueryStatus.uninitialized) {
263276
return ret
264277
}
278+
let newValue
265279
if ('data' in currentState) {
266280
if (isDraftable(currentState.data)) {
267-
const [, patches, inversePatches] = produceWithPatches(
281+
const [value, patches, inversePatches] = produceWithPatches(
268282
currentState.data,
269283
updateRecipe
270284
)
271285
ret.patches.push(...patches)
272286
ret.inversePatches.push(...inversePatches)
287+
newValue = value
273288
} 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 })
276291
ret.inversePatches.push({
277292
op: 'replace',
278293
path: [],
@@ -281,9 +296,25 @@ export function buildThunks<
281296
}
282297
}
283298

299+
if (updateProvided) {
300+
ret.provided = calculateNewProvided(newValue)
301+
}
302+
303+
// `patchQueryData` needs to be added as `extraReducer` on `invalidationSlice`
284304
dispatch(api.util.patchQueryData(endpointName, args, ret.patches))
285305

286306
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+
}
287318
}
288319

289320
const upsertQueryData: UpsertQueryDataThunk<Definitions, State> =

0 commit comments

Comments
 (0)
Please sign in to comment.