From 183c81dbf0da2d8bc905cd2881dcaea9213f18f6 Mon Sep 17 00:00:00 2001 From: joe-re Date: Thu, 3 Jan 2019 00:44:05 +0900 Subject: [PATCH] Enable to add listeners for clearStore (#4082) * Enable to add listeners for clearStore * Formatting adjustments * Add `onClearStore` to the Apollo Client API docs * Adjust `onResetStore`/`onClearStore` comment grammar * Bump allowed `apollo-client` bundle size * Changelog update --- CHANGELOG.md | 3 ++ docs/source/api/apollo-client.md | 1 + package.json | 2 +- packages/apollo-client/src/ApolloClient.ts | 28 ++++++++++--- .../apollo-client/src/__tests__/client.ts | 42 +++++++++++++++++++ 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cd507da811..15542e46082 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ - Apollo Client has been updated to use `graphql` 14.x as a dev dependency.
[@hwillson](https://github.com/hwillson) in [#4233](https://github.com/apollographql/apollo-client/pull/4233) +- The `onClearStore` function can now be used to register callbacks that should + be triggered when calling `clearStore`.
+ [@joe-re](https://github.com/joe-re) in [#4082](https://github.com/apollographql/apollo-client/pull/4082) - Documentation updates.
[@lifedup](https://github.com/lifedup) in [#3931](https://github.com/apollographql/apollo-client/pull/3931)
[@Dem0n3D](https://github.com/Dem0n3D) in [#4008](https://github.com/apollographql/apollo-client/pull/4008)
diff --git a/docs/source/api/apollo-client.md b/docs/source/api/apollo-client.md index ea7b4beb755..caa380c7228 100644 --- a/docs/source/api/apollo-client.md +++ b/docs/source/api/apollo-client.md @@ -52,6 +52,7 @@ The `ApolloClient` class is the core API for Apollo, and the one you'll need to {% tsapibox ApolloClient.resetStore %} {% tsapibox ApolloClient.onResetStore %} {% tsapibox ApolloClient.clearStore %} +{% tsapibox ApolloClient.onClearStore %}

ObservableQuery

diff --git a/package.json b/package.json index 190dee801d4..1711afd5895 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ { "name": "apollo-client", "path": "./packages/apollo-client/lib/bundle.min.js", - "maxSize": "10 kB" + "maxSize": "10.25 kB" }, { "name": "apollo-boost", diff --git a/packages/apollo-client/src/ApolloClient.ts b/packages/apollo-client/src/ApolloClient.ts index f99975b1f7e..3a270aab636 100644 --- a/packages/apollo-client/src/ApolloClient.ts +++ b/packages/apollo-client/src/ApolloClient.ts @@ -73,6 +73,7 @@ export default class ApolloClient implements DataProxy { private proxy: ApolloCache | undefined; private ssrMode: boolean; private resetStoreCallbacks: Array<() => Promise> = []; + private clearStoreCallbacks: Array<() => Promise> = []; private clientAwareness: Record = {}; /** @@ -486,16 +487,19 @@ 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), + ); } /** - * Allows callbacks to be registered that are executed with the store is reset. - * onResetStore returns an unsubscribe function for removing your registered callbacks. + * Allows callbacks to be registered that are executed when the store is + * reset. `onResetStore` returns an unsubscribe function that can be used + * to remove registered callbacks. */ - public onResetStore(cb: () => Promise): () => void { this.resetStoreCallbacks.push(cb); return () => { @@ -503,6 +507,18 @@ export default class ApolloClient implements DataProxy { }; } + /** + * Allows callbacks to be registered that are executed when the store is + * cleared. `onClearStore` returns an unsubscribe function that can be used + * to remove 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 623b6002f78..2b92befc798 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(),