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

Reset store returns a promise #1674

Merged
merged 7 commits into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ Andrew E. Rhyne <rhyneandrew@gmail.com>
Miroslav Simulcik <simulcik.miro@gmail.com>
Stephen Potter <me@stevepotter.me>
Michaël De Boey <info@michaeldeboey.be>
Andreas Bergenwall <abergenw@gmail.com>
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change log

### vNEXT
- Make ApolloClient.resetStore() and QueryManager.resetStore() return a promise instead of void [PR #1674](https://github.com/apollographql/apollo-client/pull/1674)

### 1.2.2
- Fix: Remove race condition in queryListenerFromObserver [PR #1670](https://github.com/apollographql/apollo-client/pull/1670)
Expand Down
6 changes: 2 additions & 4 deletions src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,8 @@ export default class ApolloClient implements DataProxy {
* re-execute any queries then you should make sure to stop watching any
* active queries.
*/
public resetStore() {
if (this.queryManager) {
this.queryManager.resetStore();
}
public resetStore(): Promise<ApolloQueryResult<any>[]>|null {
return this.queryManager ? this.queryManager.resetStore() : null;
}

public getInitialState(): { data: Object } {
Expand Down
7 changes: 5 additions & 2 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ export class QueryManager {
}
}

public resetStore(): void {
public resetStore(): Promise<ApolloQueryResult<any>[]> {
// Before we have sent the reset action to the store,
// we can no longer rely on the results returned by in-flight
// requests since these may depend on values that previously existed
Expand All @@ -819,15 +819,18 @@ export class QueryManager {
// watched. If there is an existing query in flight when the store is reset,
// the promise for it will be rejected and its results will not be written to the
// store.
const observableQueryPromises: Promise<ApolloQueryResult<any>>[] = [];
Object.keys(this.observableQueries).forEach((queryId) => {
const storeQuery = this.reduxRootSelector(this.store.getState()).queries[queryId];

const fetchPolicy = this.observableQueries[queryId].observableQuery.options.fetchPolicy;

if (fetchPolicy !== 'cache-only' && fetchPolicy !== 'standby') {
this.observableQueries[queryId].observableQuery.refetch();
observableQueryPromises.push(this.observableQueries[queryId].observableQuery.refetch());
}
});

return Promise.all(observableQueryPromises);
}

public startQuery<T>(queryId: string, options: WatchQueryOptions, listener: QueryListener) {
Expand Down
5 changes: 5 additions & 0 deletions test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2343,6 +2343,11 @@ describe('QueryManager', () => {
});

describe('store resets', () => {
it('returns a promise', done => {
const queryManager = createQueryManager({});
queryManager.resetStore().then(() => done());
Copy link
Contributor

Choose a reason for hiding this comment

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

This should check that all queries have completed refetching before the then is called.

});

it('should change the store state to an empty state', () => {
const queryManager = createQueryManager({});

Expand Down