Skip to content
Source
Sign inSign up
TanStack/query

try to clean the old persistented data when storage full #2851

MergedNewFuture(GitHub profile, opens in a new tab)merged 5 commits intomaster fromrefs/pull/2851/headmerged by TkDodo View on GitHub

Showing 3 changed files with 214 additions and 2 deletions.

x
0 of 3 viewed, 3 left
175 additions and 0 deletionssrc/createWebStoragePersistor-experimental/tests/storageIsFull.test.tsAdded
View file
@@ -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.`
+ )
+ }
+ }