From 9c69f97c11f0ea74df472edffcfa27d80807d80c Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 11 May 2020 14:52:40 -0400 Subject: [PATCH] Allow silencing broadcast for individual cache writes/evictions. https://github.com/apollographql/apollo-client/issues/6262#issuecomment-626864804 --- src/cache/core/cache.ts | 2 ++ src/cache/core/types/Cache.ts | 2 ++ src/cache/core/types/DataProxy.ts | 8 ++++++++ src/cache/inmemory/inMemoryCache.ts | 9 +++++++-- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cache/core/cache.ts b/src/cache/core/cache.ts index 5ae6f8c466b..ff0415d9b73 100644 --- a/src/cache/core/cache.ts +++ b/src/cache/core/cache.ts @@ -139,6 +139,7 @@ export abstract class ApolloCache implements DataProxy { result: options.data, query: options.query, variables: options.variables, + broadcast: options.broadcast, }); } @@ -150,6 +151,7 @@ export abstract class ApolloCache implements DataProxy { result: options.data, variables: options.variables, query: this.getFragmentDoc(options.fragment, options.fragmentName), + broadcast: options.broadcast, }); } } diff --git a/src/cache/core/types/Cache.ts b/src/cache/core/types/Cache.ts index 234fa05282d..1fda8d1f150 100644 --- a/src/cache/core/types/Cache.ts +++ b/src/cache/core/types/Cache.ts @@ -14,6 +14,7 @@ export namespace Cache { extends DataProxy.Query { dataId: string; result: TResult; + broadcast?: boolean; } export interface DiffOptions extends ReadOptions { @@ -29,6 +30,7 @@ export namespace Cache { id: string; fieldName?: string; args?: Record; + broadcast?: boolean; } export import DiffResult = DataProxy.DiffResult; diff --git a/src/cache/core/types/DataProxy.ts b/src/cache/core/types/DataProxy.ts index b20038e4bcd..d9da863b13c 100644 --- a/src/cache/core/types/DataProxy.ts +++ b/src/cache/core/types/DataProxy.ts @@ -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 @@ -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 = { diff --git a/src/cache/inmemory/inMemoryCache.ts b/src/cache/inmemory/inMemoryCache.ts index c6cc2873619..22d797c924c 100644 --- a/src/cache/inmemory/inMemoryCache.ts +++ b/src/cache/inmemory/inMemoryCache.ts @@ -146,7 +146,9 @@ export class InMemoryCache extends ApolloCache { variables: options.variables, }); - this.broadcastWatches(); + if (options.broadcast !== false) { + this.broadcastWatches(); + } } public modify( @@ -235,7 +237,10 @@ export class InMemoryCache extends ApolloCache { args, } : idOrOptions, ); - this.broadcastWatches(); + if (typeof idOrOptions === "string" || + idOrOptions.broadcast !== false) { + this.broadcastWatches(); + } return evicted; }