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

Support ApolloClient#stop for safe client disposal. #4336

Merged
merged 5 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
{
"name": "apollo-client",
"path": "./packages/apollo-client/lib/bundle.min.js",
"maxSize": "9.3 kB"
"maxSize": "9.4 kB"
},
{
"name": "apollo-utilities",
Expand Down
11 changes: 11 additions & 0 deletions packages/apollo-client/src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
this.clientAwareness.version = clientAwarenessVersion;
}
}

/**
* Call this method to terminate any active client processes, making it safe
* to dispose of this ApolloClient instance.
*/
public stop() {
if (this.queryManager) {
this.queryManager.stop();
}
}

/**
* This watches the cache store of the query according to the options specified and
* returns an {@link ObservableQuery}. We can subscribe to this {@link ObservableQuery} and
Expand Down
11 changes: 11 additions & 0 deletions packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ export class QueryManager<TStore> {
this.scheduler = new QueryScheduler({ queryManager: this, ssrMode });
}

/**
* Call this method to terminate any active query processes, making it safe
* to dispose of this QueryManager instance.
*/
public stop() {
this.scheduler.stop();
this.fetchQueryRejectFns.forEach(reject => {
reject(new Error('QueryManager stopped while query was in flight'));
});
}

public mutate<T>({
mutation,
variables,
Expand Down
15 changes: 15 additions & 0 deletions packages/apollo-client/src/scheduler/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ export class QueryScheduler<TCacheShape> {
this.ssrMode = ssrMode || false;
}

/**
* Call this method to terminate any active scheduler timers, making it safe
* to dispose of this QueryScheduler instance.
*/
public stop() {
Object.keys(this.registeredQueries).forEach(queryId => {
this.stopPollingQuery(queryId);
});
// After calling this.stopPollingQuery for all registered queries, calling
// fetchQueriesOnInterval will remove the corresponding intervals.
Object.keys(this.intervalQueries).forEach(interval => {
this.fetchQueriesOnInterval(+interval);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjamn Why is there a + before interval?

});
}

public checkInFlight(queryId: string) {
const query = this.queryManager.queryStore.get(queryId);

Expand Down