Skip to content

Commit

Permalink
Call clearStore callbacks after clearing store (#4695)
Browse files Browse the repository at this point in the history
Fixes #4694.
  • Loading branch information
ds8k authored and benjamn committed Apr 16, 2019
1 parent fc69f18 commit dc8f5a4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
- Remove temporary `queryId` after `fetchMore` completes. <br/>
[@doomsower](https://github.com/doomsower) in [#4440](https://github.com/apollographql/apollo-client/pull/4440)

- Call `clearStore` callbacks after clearing store. <br/>
[@ds8k](https://github.com/ds8k) in [#4695](https://github.com/apollographql/apollo-client/pull/4695)

### Apollo Cache In-Memory

- Support `new InMemoryCache({ freezeResults: true })` to help enforce immutability. <br/>
Expand Down
30 changes: 7 additions & 23 deletions packages/apollo-client/src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,7 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
* to dispose of this `ApolloClient` instance.
*/
public stop() {
if (this.queryManager) {
this.queryManager.stop();
}
this.queryManager.stop();
}

/**
Expand Down Expand Up @@ -505,31 +503,19 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
*/
public resetStore(): Promise<ApolloQueryResult<any>[] | null> {
return Promise.resolve()
.then(() => {
return this.queryManager
? this.queryManager.clearStore()
: Promise.resolve(null);
})
.then(() => this.queryManager.clearStore())
.then(() => Promise.all(this.resetStoreCallbacks.map(fn => fn())))
.then(() => {
return this.queryManager && this.queryManager.reFetchObservableQueries
? this.queryManager.reFetchObservableQueries()
: Promise.resolve(null);
});
.then(() => this.queryManager.reFetchObservableQueries());
}

/**
* Remove all data from the store. Unlike `resetStore`, `clearStore` will
* not refetch any active queries.
*/
public clearStore(): Promise<void | null> {
const { queryManager } = this;
public clearStore(): Promise<any[]> {
return Promise.resolve()
.then(() => Promise.all(this.clearStoreCallbacks.map(fn => fn())))
.then(
() =>
queryManager ? queryManager.clearStore() : Promise.resolve(null),
);
.then(() => this.queryManager.clearStore())
.then(() => Promise.all(this.clearStoreCallbacks.map(fn => fn())));
}

/**
Expand Down Expand Up @@ -571,9 +557,7 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
public reFetchObservableQueries(
includeStandby?: boolean,
): Promise<ApolloQueryResult<any>[]> | Promise<null> {
return this.queryManager
? this.queryManager.reFetchObservableQueries(includeStandby)
: Promise.resolve(null);
return this.queryManager.reFetchObservableQueries(includeStandby);
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-client/src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2296,6 +2296,7 @@ describe('client', () => {
cache: new InMemoryCache(),
});
client.queryManager = {
reFetchObservableQueries() {},
clearStore: () => {
done();
},
Expand Down

0 comments on commit dc8f5a4

Please sign in to comment.