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

Add complete handler in startGraphQLSubscription #4290

Merged
merged 4 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
[@joe-re](https://github.com/joe-re) in [#4082](https://github.com/apollographql/apollo-client/pull/4082)
- Make `isApolloError` available for external use. <br/>
[@FredyC](https://github.com/FredyC) in [#4223](https://github.com/apollographql/apollo-client/pull/4223)
- The `QueryManager` now calls `complete` on the observables used by
Apollo Client's Subscription handling. This gives finite subscriptions a
chance to handle cleanup. <br/>
[@sujeetsr](https://github.com/sujeetsr) in [#4290](https://github.com/apollographql/apollo-client/pull/4290)
- Documentation updates. <br/>
[@lifedup](https://github.com/lifedup) in [#3931](https://github.com/apollographql/apollo-client/pull/3931) <br />
[@Dem0n3D](https://github.com/Dem0n3D) in [#4008](https://github.com/apollographql/apollo-client/pull/4008) <br />
Expand Down
6 changes: 6 additions & 0 deletions packages/apollo-client/src/__mocks__/mockLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ export class MockSubscriptionLink extends ApolloLink {
}, result.delay || 0);
}

public simulateComplete() {
const { observer } = this;
if (!observer) throw new Error('subscription torn down');
if (observer.complete) observer.complete();
}

public onSetup(listener: any): void {
this.setups = this.setups.concat([listener]);
}
Expand Down
20 changes: 20 additions & 0 deletions packages/apollo-client/src/__tests__/graphqlSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,24 @@ describe('GraphQL Subscriptions', () => {
link.simulateResult(errorResult);
return Promise.all(promises);
});

it('should call complete handler when the subscription completes', done => {
const link = mockObservableLink();
const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),
});
const completeFn = jest.fn();

let count = 0;
const sub = client.subscribe(defaultOptions).subscribe({
complete() {
completeFn();
}
});

link.simulateComplete();
expect(completeFn).toHaveBeenCalled();
done();
});
});
7 changes: 7 additions & 0 deletions packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,13 @@ export class QueryManager<TStore> {
}
});
},
complete: () => {
observers.forEach(obs => {
if (obs.complete) {
obs.complete();
}
});
}
};

// TODO: Should subscriptions also accept a `context` option to pass
Expand Down