-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
try to clean the old persistented data when storage full #2851
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91cb162
try to clean the old persistented data when storage full
NewFuture 6b0be09
fix typos and save data without mutating original cache
NewFuture 1214bb1
add tests
NewFuture a10573e
createWebStoragePersistor:change mutations and queries cleaning order
NewFuture ddab6d4
Merge branch 'master' into fix-storage-full
NewFuture File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
src/createWebStoragePersistor-experimental/tests/storageIsFull.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import { dehydrate, MutationCache, QueryCache, QueryClient } from '../../core' | ||
| import { sleep } from '../../react/tests/utils' | ||
| import { createWebStoragePersistor } from '../index' | ||
|
|
||
| function getMockStorage(limitSize?: number) { | ||
| const dataSet = new Map<string, string>() | ||
| return ({ | ||
| getItem(key: string): string | null { | ||
| const value = dataSet.get(key) | ||
| return value === undefined ? null : value | ||
| }, | ||
|
|
||
| setItem(key: string, value: string) { | ||
| if (limitSize) { | ||
| const currentSize = Array.from(dataSet.entries()).reduce( | ||
| (n, d) => d[0].length + d[1].length + n, | ||
| 0 | ||
| ) | ||
| if ( | ||
| currentSize - (dataSet.get(key)?.length || 0) + value.length > | ||
| limitSize | ||
| ) { | ||
| throw Error( | ||
| ` Failed to execute 'setItem' on 'Storage': Setting the value of '${key}' exceeded the quota.` | ||
| ) | ||
| } | ||
| } | ||
| return dataSet.set(key, value) | ||
| }, | ||
| removeItem(key: string) { | ||
| return dataSet.delete(key) | ||
| }, | ||
| } as any) as Storage | ||
| } | ||
|
|
||
| describe('createWebStoragePersistor ', () => { | ||
| test('basic store and recover', async () => { | ||
| const queryCache = new QueryCache() | ||
| const mutationCache = new MutationCache() | ||
| const queryClient = new QueryClient({ queryCache, mutationCache }) | ||
|
|
||
| const storage = getMockStorage() | ||
| const webStoragePersistor = createWebStoragePersistor({ | ||
| throttleTime: 0, | ||
| storage, | ||
| }) | ||
|
|
||
| await queryClient.prefetchQuery('string', () => Promise.resolve('string')) | ||
| await queryClient.prefetchQuery('number', () => Promise.resolve(1)) | ||
| await queryClient.prefetchQuery('boolean', () => Promise.resolve(true)) | ||
| await queryClient.prefetchQuery('null', () => Promise.resolve(null)) | ||
| await queryClient.prefetchQuery('array', () => | ||
| Promise.resolve(['string', 0]) | ||
| ) | ||
|
|
||
| const persistClient = { | ||
| buster: 'test-buster', | ||
| timestamp: Date.now(), | ||
| clientState: dehydrate(queryClient), | ||
| } | ||
| webStoragePersistor.persistClient(persistClient) | ||
| await sleep(1) | ||
| const restoredClient = await webStoragePersistor.restoreClient() | ||
| expect(restoredClient).toEqual(persistClient) | ||
| }) | ||
|
|
||
| test('should clean the old queries when storage full', async () => { | ||
| const queryCache = new QueryCache() | ||
| const mutationCache = new MutationCache() | ||
| const queryClient = new QueryClient({ queryCache, mutationCache }) | ||
|
|
||
| const N = 2000 | ||
| const storage = getMockStorage(N * 5) // can save 4 items; | ||
| const webStoragePersistor = createWebStoragePersistor({ | ||
| throttleTime: 0, | ||
| storage, | ||
| }) | ||
|
|
||
| await queryClient.prefetchQuery('A', () => Promise.resolve('A'.repeat(N))) | ||
| await sleep(1) | ||
| await queryClient.prefetchQuery('B', () => Promise.resolve('B'.repeat(N))) | ||
| await sleep(1) | ||
| await queryClient.prefetchQuery('C', () => Promise.resolve('C'.repeat(N))) | ||
| await sleep(1) | ||
| await queryClient.prefetchQuery('D', () => Promise.resolve('D'.repeat(N))) | ||
|
|
||
| await sleep(1) | ||
| await queryClient.prefetchQuery('E', () => Promise.resolve('E'.repeat(N))) | ||
|
|
||
| const persistClient = { | ||
| buster: 'test-limit', | ||
| timestamp: Date.now(), | ||
| clientState: dehydrate(queryClient), | ||
| } | ||
| webStoragePersistor.persistClient(persistClient) | ||
| await sleep(10) | ||
| const restoredClient = await webStoragePersistor.restoreClient() | ||
| expect(restoredClient?.clientState.queries.length).toEqual(4) | ||
| expect( | ||
| restoredClient?.clientState.queries.find(q => q.queryKey === 'A') | ||
| ).toBeUndefined() | ||
| expect( | ||
| restoredClient?.clientState.queries.find(q => q.queryKey === 'B') | ||
| ).not.toBeUndefined() | ||
|
|
||
| // update query Data | ||
| await queryClient.prefetchQuery('A', () => Promise.resolve('a'.repeat(N))) | ||
| const updatedPersistClient = { | ||
| buster: 'test-limit', | ||
| timestamp: Date.now(), | ||
| clientState: dehydrate(queryClient), | ||
| } | ||
| webStoragePersistor.persistClient(updatedPersistClient) | ||
| await sleep(10) | ||
| const restoredClient2 = await webStoragePersistor.restoreClient() | ||
| expect(restoredClient2?.clientState.queries.length).toEqual(4) | ||
| expect( | ||
| restoredClient2?.clientState.queries.find(q => q.queryKey === 'A') | ||
| ).toHaveProperty('state.data', 'a'.repeat(N)) | ||
| expect( | ||
| restoredClient2?.clientState.queries.find(q => q.queryKey === 'B') | ||
| ).toBeUndefined() | ||
| }) | ||
|
|
||
| test('should clean queries before mutations when storage full', async () => { | ||
| const queryCache = new QueryCache() | ||
| const mutationCache = new MutationCache() | ||
| const queryClient = new QueryClient({ queryCache, mutationCache }) | ||
|
|
||
| const N = 2000 | ||
| const storage = getMockStorage(N * 5) // can save 4 items; | ||
| const webStoragePersistor = createWebStoragePersistor({ | ||
| throttleTime: 0, | ||
| storage, | ||
| }) | ||
|
|
||
| mutationCache.build( | ||
| queryClient, | ||
| { | ||
| mutationKey: 'MUTATIONS', | ||
| mutationFn: () => Promise.resolve('M'.repeat(N)), | ||
| }, | ||
| { | ||
| error: null, | ||
| context: '', | ||
| failureCount: 1, | ||
| isPaused: true, | ||
| status: 'success', | ||
| variables: '', | ||
| data: 'M'.repeat(N), | ||
| } | ||
| ) | ||
| await sleep(1) | ||
| await queryClient.prefetchQuery('A', () => Promise.resolve('A'.repeat(N))) | ||
| await sleep(1) | ||
| await queryClient.prefetchQuery('B', () => Promise.resolve('B'.repeat(N))) | ||
| await queryClient.prefetchQuery('C', () => Promise.resolve('C'.repeat(N))) | ||
| await sleep(1) | ||
| await queryClient.prefetchQuery('D', () => Promise.resolve('D'.repeat(N))) | ||
|
|
||
| const persistClient = { | ||
| buster: 'test-limit-mutations', | ||
| timestamp: Date.now(), | ||
| clientState: dehydrate(queryClient), | ||
| } | ||
| webStoragePersistor.persistClient(persistClient) | ||
| await sleep(10) | ||
| const restoredClient = await webStoragePersistor.restoreClient() | ||
| expect(restoredClient?.clientState.mutations.length).toEqual(1) | ||
| expect(restoredClient?.clientState.queries.length).toEqual(3) | ||
| expect( | ||
| restoredClient?.clientState.queries.find(q => q.queryKey === 'A') | ||
| ).toBeUndefined() | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.