-
Notifications
You must be signed in to change notification settings - Fork 118
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 synchronous version of persistCache #299
Conversation
…chronously when the function is called
@andrewshirk: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/ |
Codecov Report
@@ Coverage Diff @@
## master #299 +/- ##
==========================================
+ Coverage 72.95% 75.11% +2.15%
==========================================
Files 10 11 +1
Lines 196 221 +25
Branches 30 33 +3
==========================================
+ Hits 143 166 +23
- Misses 50 52 +2
Partials 3 3
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome change! Thank you so much for controbution. We are missing following items to get it merged:
- Readme.md chapter on sync api
- Unit test - if can be simply copy paste with changed classes
- exporting file to package level so it can be used
Once again thank you so much for this awesome contribution.
PS: I'm traveling for the rest of the week so I will be able to merge it on next monday after testing it and release the change.
Some minor changes suggested. |
Awesome work. I will sit down next week and make sure that this is merged and new version of package is released. |
Thanks for your support on this! |
Reviewing! |
@andrewshirk I see that unit tests are not passing. I see some issues when playing with them locally. Are you still working on this? I do not want to duplicate work. |
@wtrocki I haven't done any more work on it yet. I ran out of time last week, but will probably have some tonight. Just let me know what'd you like. Thanks again! |
Generally I see that tests using Thank you so much for contribution. |
@wtrocki Great, thanks for the clarification. You suggested copy/pasting earlier, so wasn't sure about that. I will get to it ASAP. |
It was my mistake for not being clear. I should have said to copy paste only the restore test and ignore the rest. |
Very simple test that works for me: import { InMemoryCache } from 'apollo-cache-inmemory';
import { persistCacheSync, SynchronousCachePersistor } from '../';
import MockStorage from '../__mocks__/MockStorage';
import { ApolloLink } from 'apollo-link';
import Observable = require('zen-observable');
import ApolloClient from 'apollo-client';
import gql from 'graphql-tag';
jest.useFakeTimers();
describe('persistCacheSync', () => {
describe('setup', () => {
it('persist cache', async () => {
const operation = gql`
{
hello
}
`;
const result = { data: { hello: 'world' } };
const storage = new MockStorage();
const cache = new InMemoryCache();
const cachePersistor = new SynchronousCachePersistor({ cache, storage });
const link = new ApolloLink(() => Observable.of(result));
const client = new ApolloClient({ cache, link });
expect(cache.extract()).toEqual({});
await client.query({ query: operation });
jest.runTimersToTime(
persistOptions.debounce ? persistOptions.debounce + 1 : 1001
);
const cache2 = new InMemoryCache();
const cachePersistor2 = new SynchronousCachePersistor({ cache: cache2, storage });
cachePersistor2.restoreSync();
const keys = Object.keys(cache2.extract());
expect(keys.length).toEqual(2);
});
});
}); |
Sadly I cannot push changes to your branch, but I have also needed to change: |
Wonderful. I'll have time to work on this tonight. |
@wtrocki I'll investigate the test failures. |
MockStorage is an async API, so we need a MockStorageSync I suppose. |
@wtrocki I gave you push access to my repo, if you want to make any final tweaks. |
Awesome work. I will play with it in sample app and merge changes. Thank you so much |
@andrewshirk Amazing collaboration and great work on the feature. I have spent some time playing with the new API. Merging the change and doing a new release of dev package. |
@wtrocki Super! Thanks again for all your help and support! |
This PR is intended to provide a simple, non-breaking way to start persisting a cache by first restoring the cache synchronously, for apps with small localStorage requirements. This is just a proposal at this point.