Skip to content

Commit

Permalink
Pass previous diff to watch.callback, onWatchUpdated, and onQueryUpda…
Browse files Browse the repository at this point in the history
…ted.
  • Loading branch information
benjamn committed May 5, 2021
1 parent 6845604 commit 8336923
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export type BatchOptions<C extends ApolloCache<any>> = {
onWatchUpdated?: (
this: C,
watch: Cache.WatchOptions,
diff: Cache.DiffResult<any>,
newDiff: Cache.DiffResult<any>,
oldDiff?: Cache.DiffResult<any>,
) => any;
};

Expand Down
5 changes: 4 additions & 1 deletion src/cache/core/types/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { DataProxy } from './DataProxy';
import { Modifier, Modifiers } from './common';

export namespace Cache {
export type WatchCallback = (diff: Cache.DiffResult<any>) => void;
export type WatchCallback = (
newDiff: Cache.DiffResult<any>,
oldDiff?: Cache.DiffResult<any>,
) => void;

export interface ReadOptions<TVariables = any, TData = any>
extends DataProxy.Query<TVariables, TData> {
Expand Down
7 changes: 4 additions & 3 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
c: Cache.WatchOptions,
options?: BroadcastOptions,
) {
const { lastDiff } = c;
const diff = this.diff<any>({
query: c.query,
variables: c.variables,
Expand All @@ -491,15 +492,15 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
}

if (options.onWatchUpdated &&
options.onWatchUpdated.call(this, c, diff) === false) {
options.onWatchUpdated.call(this, c, diff, lastDiff) === false) {
// Returning false from the onWatchUpdated callback will prevent
// calling c.callback(diff) for this watcher.
return;
}
}

if (!c.lastDiff || c.lastDiff.result !== diff.result) {
c.callback(c.lastDiff = diff);
if (!lastDiff || lastDiff.result !== diff.result) {
c.callback(c.lastDiff = diff, lastDiff);
}
}
}
4 changes: 2 additions & 2 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,14 +1137,14 @@ export class QueryManager<TStore> {
// temporary optimistic layer, in case that ever matters.
removeOptimistic,

onWatchUpdated: onQueryUpdated && function (watch, diff) {
onWatchUpdated: onQueryUpdated && function (watch, newDiff, oldDiff) {
const oq =
watch.watcher instanceof QueryInfo &&
watch.watcher.observableQuery;

if (oq) {
includedQueriesById.delete(oq.queryId);
return maybeAddResult(oq, onQueryUpdated(oq, diff));
return maybeAddResult(oq, onQueryUpdated(oq, newDiff, oldDiff));
}
},
});
Expand Down
3 changes: 2 additions & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export type QueryListener = (queryInfo: QueryInfo) => void;

export type OnQueryUpdated<TData> = (
observableQuery: ObservableQuery,
diff: Cache.DiffResult<TData>,
newDiff: Cache.DiffResult<TData>,
oldDiff?: Cache.DiffResult<TData>,
) => boolean | Promise<ApolloQueryResult<TData>>;

export type OperationVariables = Record<string, any>;
Expand Down

0 comments on commit 8336923

Please sign in to comment.