Skip to content

Commit

Permalink
feat: add prevent broadcast option in QueryManager
Browse files Browse the repository at this point in the history
  • Loading branch information
alekangelov committed Dec 14, 2022
1 parent 52d5af2 commit 6c8e4c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
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 @@ -85,6 +85,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 @@ -115,6 +116,7 @@ export class QueryManager<TStore> {
clientAwareness = {},
localState,
assumeImmutableResults,
notifyOnTeardown = true
}: {
cache: ApolloCache<TStore>;
link: ApolloLink;
Expand All @@ -125,6 +127,7 @@ export class QueryManager<TStore> {
clientAwareness?: Record<string, string>;
localState?: LocalState<TStore>;
assumeImmutableResults?: boolean;
notifyOnTeardown?: boolean;
}) {
this.cache = cache;
this.link = link;
Expand All @@ -134,6 +137,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 @@ -915,6 +919,7 @@ export class QueryManager<TStore> {

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

Expand Down

0 comments on commit 6c8e4c8

Please sign in to comment.