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

Fix memory leaks due to onBroadcast and mutation errors. #7161

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid creating queryManager.mutationStore when unused.
  • Loading branch information
benjamn committed Oct 15, 2020
commit ad4be6e31b24daa14d7cfa55221707af30bb0e54
2 changes: 1 addition & 1 deletion src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class ApolloClient<TCacheShape> implements DataProxy {
action: {},
state: {
queries: this.queryManager.getQueryStore(),
mutations: this.queryManager.mutationStore.getStore(),
mutations: this.queryManager.mutationStore || {},
},
dataWithOptimisticResults: this.cache.extract(true),
});
Expand Down
56 changes: 0 additions & 56 deletions src/core/MutationStore.ts

This file was deleted.

48 changes: 35 additions & 13 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
ConcastSourcesIterable,
} from '../utilities';
import { ApolloError, isApolloError } from '../errors';
import { MutationStore } from './MutationStore';
import {
QueryOptions,
WatchQueryOptions,
Expand All @@ -42,10 +41,16 @@ import { QueryInfo, QueryStoreValue, shouldWriteResult } from './QueryInfo';

const { hasOwnProperty } = Object.prototype;

interface MutationStoreValue {
mutation: DocumentNode;
variables: Record<string, any>;
loading: boolean;
error: Error | null;
}

export class QueryManager<TStore> {
public cache: ApolloCache<TStore>;
public link: ApolloLink;
public mutationStore: MutationStore = new MutationStore();
public readonly assumeImmutableResults: boolean;
public readonly ssrMode: boolean;

Expand All @@ -54,6 +59,9 @@ export class QueryManager<TStore> {
private localState: LocalState<TStore>;

private onBroadcast?: () => void;
public mutationStore?: {
[mutationId: string]: MutationStoreValue;
};

// All the queries that the QueryManager is currently managing (not
// including mutations and subscriptions).
Expand Down Expand Up @@ -85,11 +93,13 @@ export class QueryManager<TStore> {
this.cache = cache;
this.link = link;
this.queryDeduplication = queryDeduplication;
this.onBroadcast = onBroadcast;
this.clientAwareness = clientAwareness;
this.localState = localState || new LocalState({ cache });
this.ssrMode = ssrMode;
this.assumeImmutableResults = !!assumeImmutableResults;
if ((this.onBroadcast = onBroadcast)) {
this.mutationStore = Object.create(null);
}
}

/**
Expand Down Expand Up @@ -142,11 +152,14 @@ export class QueryManager<TStore> {
variables = await this.localState.addExportedVariables(mutation, variables, context);
}

this.mutationStore.initMutation(
mutationId,
mutation,
variables,
);
const mutationStoreValue =
this.mutationStore &&
(this.mutationStore[mutationId] = {
mutation,
variables,
loading: true,
error: null,
} as MutationStoreValue);

if (optimisticResponse) {
this.markMutationOptimistic<T>(optimisticResponse, {
Expand Down Expand Up @@ -184,7 +197,10 @@ export class QueryManager<TStore> {
return;
}

self.mutationStore.markMutationResult(mutationId);
if (mutationStoreValue) {
mutationStoreValue.loading = false;
mutationStoreValue.error = null;
}

if (fetchPolicy !== 'no-cache') {
try {
Expand All @@ -209,7 +225,10 @@ export class QueryManager<TStore> {
},

error(err: Error) {
self.mutationStore.markMutationError(mutationId, self.onBroadcast && err);
if (mutationStoreValue) {
mutationStoreValue.loading = false;
mutationStoreValue.error = err;
}
if (optimisticResponse) {
self.cache.removeOptimistic(mutationId);
}
Expand All @@ -222,8 +241,9 @@ export class QueryManager<TStore> {
},

complete() {
if (error) {
self.mutationStore.markMutationError(mutationId, self.onBroadcast && error);
if (error && mutationStoreValue) {
mutationStoreValue.loading = false;
mutationStoreValue.error = error;
}

if (optimisticResponse) {
Expand Down Expand Up @@ -602,7 +622,9 @@ export class QueryManager<TStore> {
}
});

this.mutationStore.reset();
if (this.mutationStore) {
this.mutationStore = Object.create(null);
}

// begin removing data from the store
return this.cache.reset();
Expand Down