diff --git a/.changeset/giant-icons-sit.md b/.changeset/giant-icons-sit.md new file mode 100644 index 00000000000..50f3b54cf7d --- /dev/null +++ b/.changeset/giant-icons-sit.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Added option to prevent broadcasting on query teardown. May increase performance. diff --git a/src/core/ApolloClient.ts b/src/core/ApolloClient.ts index b26da37e579..fae22119ca4 100644 --- a/src/core/ApolloClient.ts +++ b/src/core/ApolloClient.ts @@ -60,6 +60,7 @@ export type ApolloClientOptions = { fragmentMatcher?: FragmentMatcher; name?: string; version?: string; + notifyOnTeardown?: boolean; }; // Though mergeOptions now resides in @apollo/client/utilities, it was @@ -123,6 +124,10 @@ export class ApolloClient implements DataProxy { * version of your client, which you may want to increment on * new builds. This is NOT the version of Apollo Client that * you are using. + * + * @param notifyOnTeardown Defaults to true, but when set to false will + * prevent the client from notifying any active queries. + * Could result in substantial performance gains in some cases. */ constructor(options: ApolloClientOptions) { const { @@ -147,6 +152,7 @@ export class ApolloClient implements DataProxy { fragmentMatcher, name: clientAwarenessName, version: clientAwarenessVersion, + notifyOnTeardown = true } = options; let { link } = options; @@ -253,6 +259,7 @@ export class ApolloClient implements DataProxy { }); } } : void 0, + notifyOnTeardown }); } diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index ecebf55c554..33c04edbb66 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -90,6 +90,7 @@ export class QueryManager { public link: ApolloLink; public defaultOptions: DefaultOptions; + public readonly notifyOnTeardown: boolean; public readonly assumeImmutableResults: boolean; public readonly ssrMode: boolean; @@ -120,6 +121,7 @@ export class QueryManager { clientAwareness = {}, localState, assumeImmutableResults, + notifyOnTeardown = true }: { cache: ApolloCache; link: ApolloLink; @@ -130,6 +132,7 @@ export class QueryManager { clientAwareness?: Record; localState?: LocalState; assumeImmutableResults?: boolean; + notifyOnTeardown?: boolean; }) { this.cache = cache; this.link = link; @@ -139,6 +142,7 @@ export class QueryManager { this.localState = localState || new LocalState({ cache }); this.ssrMode = ssrMode; this.assumeImmutableResults = !!assumeImmutableResults; + this.notifyOnTeardown = !!notifyOnTeardown; if ((this.onBroadcast = onBroadcast)) { this.mutationStore = Object.create(null); } @@ -964,6 +968,7 @@ export class QueryManager { public stopQuery(queryId: string) { this.stopQueryNoBroadcast(queryId); + if(!this.notifyOnTeardown) return; this.broadcastQueries(); }