diff --git a/packages/apollo-client/src/ApolloClient.ts b/packages/apollo-client/src/ApolloClient.ts index 5f6d5cfb1ec..9225042f03d 100644 --- a/packages/apollo-client/src/ApolloClient.ts +++ b/packages/apollo-client/src/ApolloClient.ts @@ -71,6 +71,7 @@ export default class ApolloClient implements DataProxy { private proxy: ApolloCache | undefined; private ssrMode: boolean; private resetStoreCallbacks: Array<() => Promise> = []; + private clearStoreCallbacks: Array<() => Promise> = []; /** * Constructs an instance of {@link ApolloClient}. @@ -114,7 +115,7 @@ export default class ApolloClient implements DataProxy { const supportedDirectives = new ApolloLink( (operation: Operation, forward: NextLink) => { let result = supportedCache.get(operation.query); - if (! result) { + if (!result) { result = removeConnectionDirectiveFromDocument(operation.query); supportedCache.set(operation.query, result); supportedCache.set(result, result); @@ -460,9 +461,12 @@ export default class ApolloClient implements DataProxy { */ public clearStore(): Promise { const { queryManager } = this; - return Promise.resolve().then( - () => (queryManager ? queryManager.clearStore() : Promise.resolve(null)), - ); + return Promise.resolve() + .then(() => Promise.all(this.clearStoreCallbacks.map(fn => fn()))) + .then( + () => + queryManager ? queryManager.clearStore() : Promise.resolve(null), + ); } /** @@ -477,6 +481,18 @@ export default class ApolloClient implements DataProxy { }; } + /** + * Allows callbacks to be registered that are executed with the store is cleared. + * onClearStore returns an unsubscribe function for removing your registered callbacks. + */ + + public onClearStore(cb: () => Promise): () => void { + this.clearStoreCallbacks.push(cb); + return () => { + this.clearStoreCallbacks = this.clearStoreCallbacks.filter(c => c !== cb); + }; + } + /** * Refetches all of your active queries. * diff --git a/packages/apollo-client/src/__tests__/client.ts b/packages/apollo-client/src/__tests__/client.ts index 96d17a99baf..57b931d1992 100644 --- a/packages/apollo-client/src/__tests__/client.ts +++ b/packages/apollo-client/src/__tests__/client.ts @@ -2200,6 +2200,48 @@ describe('client', () => { }); }); + it('has a clearStore method which calls QueryManager', done => { + const client = new ApolloClient({ + link: ApolloLink.empty(), + cache: new InMemoryCache(), + }); + client.queryManager = { + clearStore: () => { + done(); + }, + } as QueryManager; + client.clearStore(); + }); + + it('has an onClearStore method which takes a callback to be called after clearStore', async () => { + const client = new ApolloClient({ + link: ApolloLink.empty(), + cache: new InMemoryCache(), + }); + + const onClearStore = jest.fn(); + client.onClearStore(onClearStore); + + await client.clearStore(); + + expect(onClearStore).toHaveBeenCalled(); + }); + + it('onClearStore returns a method that unsubscribes the callback', async () => { + const client = new ApolloClient({ + link: ApolloLink.empty(), + cache: new InMemoryCache(), + }); + + const onClearStore = jest.fn(); + const unsubscribe = client.onClearStore(onClearStore); + + unsubscribe(); + + await client.clearStore(); + expect(onClearStore).not.toHaveBeenCalled(); + }); + it('has a resetStore method which calls QueryManager', done => { const client = new ApolloClient({ link: ApolloLink.empty(),