Skip to content

Commit 525b52a

Browse files
barnabasJmarkerikson
authored andcommitted
dumb down upsertQueryData and add emptyCache boolean to patch result
1 parent ef461bc commit 525b52a

File tree

1 file changed

+24
-54
lines changed

1 file changed

+24
-54
lines changed

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

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,19 @@ export type UpsertQueryDataThunk<
175175
> = <EndpointName extends QueryKeys<Definitions>>(
176176
endpointName: EndpointName,
177177
args: QueryArgFrom<Definitions[EndpointName]>,
178-
upsertRecipe: UpsertRecipe<ResultTypeFrom<Definitions[EndpointName]>>
179-
) => ThunkAction<PatchCollection, PartialState, any, AnyAction>
178+
value: ResultTypeFrom<Definitions[EndpointName]>
179+
) => ThunkAction<void, PartialState, any, AnyAction>
180180

181181
/**
182182
* An object returned from dispatching a `api.util.updateQueryData` call.
183183
*/
184184
export type PatchCollection = {
185+
/**
186+
* A boolean stating if there was already data in the query cache
187+
*
188+
* If there was no data in the cache no update operation is performed
189+
*/
190+
emptyCache: boolean
185191
/**
186192
* An `immer` Patch describing the cache update.
187193
*/
@@ -236,6 +242,7 @@ export function buildThunks<
236242
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
237243
).select(args)(getState())
238244
let ret: PatchCollection = {
245+
emptyCache: true,
239246
patches: [],
240247
inversePatches: [],
241248
undo: () =>
@@ -247,6 +254,7 @@ export function buildThunks<
247254
return ret
248255
}
249256
if ('data' in currentState) {
257+
ret.emptyCache = false
250258
if (isDraftable(currentState.data)) {
251259
const [, patches, inversePatches] = produceWithPatches(
252260
currentState.data,
@@ -271,59 +279,21 @@ export function buildThunks<
271279
}
272280

273281
const upsertQueryData: UpsertQueryDataThunk<EndpointDefinitions, State> =
274-
(endpointName, args, upsertRecipe) => (dispatch, getState) => {
275-
const currentState = (
276-
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
277-
).select(args)(getState())
278-
let ret: PatchCollection = {
279-
patches: [],
280-
inversePatches: [],
281-
undo: () =>
282-
dispatch(
283-
api.util.patchQueryData(endpointName, args, ret.inversePatches)
284-
),
285-
}
286-
if ('data' in currentState) {
287-
if (isDraftable(currentState.data)) {
288-
const [, patches, inversePatches] = produceWithPatches(
289-
currentState.data,
290-
upsertRecipe
291-
)
292-
ret.patches.push(...patches)
293-
ret.inversePatches.push(...inversePatches)
294-
} else {
295-
const value = upsertRecipe(currentState.data)
296-
ret.patches.push({ op: 'replace', path: [], value })
297-
ret.inversePatches.push({
298-
op: 'replace',
299-
path: [],
300-
value: currentState.data,
301-
})
302-
}
303-
dispatch(api.util.patchQueryData(endpointName, args, ret.patches))
304-
} else {
305-
ret.inversePatches.push({
306-
op: 'replace',
307-
path: [],
308-
value: undefined,
282+
(endpointName, args, value) => (dispatch) => {
283+
dispatch(
284+
(
285+
api.endpoints[endpointName] as ApiEndpointQuery<
286+
QueryDefinition<any, any, any, any, any>,
287+
Definitions
288+
>
289+
).initiate(args, {
290+
subscribe: false,
291+
forceRefetch: true,
292+
[forceQueryFnSymbol]: () => ({
293+
data: value,
294+
}),
309295
})
310-
dispatch(
311-
(
312-
api.endpoints[endpointName] as ApiEndpointQuery<
313-
QueryDefinition<any, any, any, any, any>,
314-
Definitions
315-
>
316-
).initiate(args, {
317-
subscribe: false,
318-
forceRefetch: true,
319-
[forceQueryFnSymbol]: () => ({
320-
data: upsertRecipe(undefined),
321-
}),
322-
})
323-
)
324-
}
325-
326-
return ret
296+
)
327297
}
328298

329299
const executeEndpoint: AsyncThunkPayloadCreator<

0 commit comments

Comments
 (0)