Skip to content

Commit

Permalink
RFC: add "merge" functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed May 16, 2021
1 parent a0809da commit 682e4db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/query/core/buildSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import {
QueryThunkArg,
ThunkResult,
} from './buildThunks'
import { AssertTagTypes, EndpointDefinitions } from '../endpointDefinitions'
import {
AssertTagTypes,
EndpointDefinitions,
QueryDefinition,
} from '../endpointDefinitions'
import { applyPatches, Patch } from 'immer'
import { onFocus, onFocusLost, onOffline, onOnline } from './setupListeners'
import {
Expand Down Expand Up @@ -130,10 +134,14 @@ export function buildSlice({
meta.arg.queryCacheKey,
(substate) => {
if (substate.requestId !== meta.requestId) return
const { merge = (x: any) => x } = definitions[
meta.arg.endpointName
] as QueryDefinition<any, any, any, any>

substate.status = QueryStatus.fulfilled
substate.data = copyWithStructuralSharing(
substate.data,
payload.result
merge(payload.result, substate.data)
)
delete substate.error
substate.fulfilledTimeStamp = payload.fulfilledTimeStamp
Expand Down
11 changes: 11 additions & 0 deletions src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ export interface QueryExtraOptions<
* Not to be used. A query should not invalidate tags in the cache.
*/
invalidatesTags?: never
/**
* Can be provided to merge the current cache value into the new cache value.
*
* Useful if you don't want a new request to completely override the current cache value,
* maybe because you have manually updated it from another source and don't want those
* updates to get lost.
*/
merge?(
newCacheValue: ResultType,
currentCacheValue: ResultType | undefined
): ResultType
/** @deprecated please use `onQuery` instead */
onStart?(arg: QueryArg, queryApi: QueryApi<ReducerPath, any>): void
/** @deprecated please use `onQuery` instead */
Expand Down

0 comments on commit 682e4db

Please sign in to comment.