Skip to content

Commit

Permalink
Allow silencing broadcast for individual cache writes/evictions.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed May 15, 2020
1 parent ce413b7 commit 9c69f97
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
result: options.data,
query: options.query,
variables: options.variables,
broadcast: options.broadcast,
});
}

Expand All @@ -150,6 +151,7 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
result: options.data,
variables: options.variables,
query: this.getFragmentDoc(options.fragment, options.fragmentName),
broadcast: options.broadcast,
});
}
}
2 changes: 2 additions & 0 deletions src/cache/core/types/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export namespace Cache {
extends DataProxy.Query<TVariables> {
dataId: string;
result: TResult;
broadcast?: boolean;
}

export interface DiffOptions extends ReadOptions {
Expand All @@ -29,6 +30,7 @@ export namespace Cache {
id: string;
fieldName?: string;
args?: Record<string, any>;
broadcast?: boolean;
}

export import DiffResult = DataProxy.DiffResult;
Expand Down
8 changes: 8 additions & 0 deletions src/cache/core/types/DataProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export namespace DataProxy {
* The data you will be writing to the store.
*/
data: TData;
/**
* Whether to notify query watchers (default: true).
*/
broadcast?: boolean;
}

export interface WriteFragmentOptions<TData, TVariables>
Expand All @@ -67,6 +71,10 @@ export namespace DataProxy {
* The data you will be writing to the store.
*/
data: TData;
/**
* Whether to notify query watchers (default: true).
*/
broadcast?: boolean;
}

export type DiffResult<T> = {
Expand Down
9 changes: 7 additions & 2 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
variables: options.variables,
});

this.broadcastWatches();
if (options.broadcast !== false) {
this.broadcastWatches();
}
}

public modify(
Expand Down Expand Up @@ -235,7 +237,10 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
args,
} : idOrOptions,
);
this.broadcastWatches();
if (typeof idOrOptions === "string" ||
idOrOptions.broadcast !== false) {
this.broadcastWatches();
}
return evicted;
}

Expand Down

0 comments on commit 9c69f97

Please sign in to comment.