Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option to prevent broadcast on query teardown (10x perf increase) #10363

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/giant-icons-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Added option to prevent broadcasting on query teardown. May increase performance.
7 changes: 7 additions & 0 deletions src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type ApolloClientOptions<TCacheShape> = {
fragmentMatcher?: FragmentMatcher;
name?: string;
version?: string;
notifyOnTeardown?: boolean;
};

// Though mergeOptions now resides in @apollo/client/utilities, it was
Expand Down Expand Up @@ -123,6 +124,10 @@ export class ApolloClient<TCacheShape> 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<TCacheShape>) {
const {
Expand All @@ -147,6 +152,7 @@ export class ApolloClient<TCacheShape> implements DataProxy {
fragmentMatcher,
name: clientAwarenessName,
version: clientAwarenessVersion,
notifyOnTeardown = true
} = options;

let { link } = options;
Expand Down Expand Up @@ -253,6 +259,7 @@ export class ApolloClient<TCacheShape> implements DataProxy {
});
}
} : void 0,
notifyOnTeardown
});
}

Expand Down
5 changes: 5 additions & 0 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class QueryManager<TStore> {
public link: ApolloLink;
public defaultOptions: DefaultOptions;

public readonly notifyOnTeardown: boolean;
public readonly assumeImmutableResults: boolean;
public readonly ssrMode: boolean;

Expand Down Expand Up @@ -120,6 +121,7 @@ export class QueryManager<TStore> {
clientAwareness = {},
localState,
assumeImmutableResults,
notifyOnTeardown = true
}: {
cache: ApolloCache<TStore>;
link: ApolloLink;
Expand All @@ -130,6 +132,7 @@ export class QueryManager<TStore> {
clientAwareness?: Record<string, string>;
localState?: LocalState<TStore>;
assumeImmutableResults?: boolean;
notifyOnTeardown?: boolean;
}) {
this.cache = cache;
this.link = link;
Expand All @@ -139,6 +142,7 @@ export class QueryManager<TStore> {
this.localState = localState || new LocalState({ cache });
this.ssrMode = ssrMode;
this.assumeImmutableResults = !!assumeImmutableResults;
this.notifyOnTeardown = !!notifyOnTeardown;
if ((this.onBroadcast = onBroadcast)) {
this.mutationStore = Object.create(null);
}
Expand Down Expand Up @@ -964,6 +968,7 @@ export class QueryManager<TStore> {

public stopQuery(queryId: string) {
this.stopQueryNoBroadcast(queryId);
if(!this.notifyOnTeardown) return;
this.broadcastQueries();
}

Expand Down