Skip to content

Commit 8013d4a

Browse files
author
Ben Grynhaus
authored
feat(persistQueryClient): add optional functions to serialize and deserialize from Storage Persistors (#2864)
1 parent 69a5d79 commit 8013d4a

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

docs/src/pages/plugins/createAsyncStoragePersistor.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ interface CreateAsyncStoragePersistorOptions {
6060
/** To avoid localstorage spamming,
6161
* pass a time in ms to throttle saving the cache to disk */
6262
throttleTime?: number
63+
/** How to serialize the data to storage */
64+
serialize?: (client: PersistedClient) => string
65+
/** How to deserialize the data from storage */
66+
deserialize?: (cachedString: string) => PersistedClient
6367
}
6468

6569
interface AsyncStorage {
@@ -75,5 +79,7 @@ The default options are:
7579
{
7680
key = `REACT_QUERY_OFFLINE_CACHE`,
7781
throttleTime = 1000,
82+
serialize = JSON.stringify,
83+
deserialize = JSON.parse,
7884
}
7985
```

docs/src/pages/plugins/createWebStoragePersistor.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ interface CreateWebStoragePersistorOptions {
5757
/** To avoid spamming,
5858
* pass a time in ms to throttle saving the cache to disk */
5959
throttleTime?: number
60+
/** How to serialize the data to storage */
61+
serialize?: (client: PersistedClient) => string
62+
/** How to deserialize the data from storage */
63+
deserialize?: (cachedString: string) => PersistedClient
6064
}
6165
```
6266

@@ -66,5 +70,7 @@ The default options are:
6670
{
6771
key = `REACT_QUERY_OFFLINE_CACHE`,
6872
throttleTime = 1000,
73+
serialize = JSON.stringify,
74+
deserialize = JSON.parse,
6975
}
7076
```

src/createAsyncStoragePersistor-experimental/index.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,28 @@ interface CreateAsyncStoragePersistorOptions {
1414
/** To avoid spamming,
1515
* pass a time in ms to throttle saving the cache to disk */
1616
throttleTime?: number
17+
/**
18+
* How to serialize the data to storage.
19+
* @default `JSON.stringify`
20+
*/
21+
serialize?: (client: PersistedClient) => string
22+
/**
23+
* How to deserialize the data from storage.
24+
* @default `JSON.parse`
25+
*/
26+
deserialize?: (cachedString: string) => PersistedClient
1727
}
1828

1929
export const createAsyncStoragePersistor = ({
2030
storage,
2131
key = `REACT_QUERY_OFFLINE_CACHE`,
2232
throttleTime = 1000,
33+
serialize = JSON.stringify,
34+
deserialize = JSON.parse,
2335
}: CreateAsyncStoragePersistorOptions): Persistor => {
2436
return {
2537
persistClient: asyncThrottle(
26-
persistedClient => storage.setItem(key, JSON.stringify(persistedClient)),
38+
persistedClient => storage.setItem(key, serialize(persistedClient)),
2739
{ interval: throttleTime }
2840
),
2941
restoreClient: async () => {
@@ -33,22 +45,22 @@ export const createAsyncStoragePersistor = ({
3345
return
3446
}
3547

36-
return JSON.parse(cacheString) as PersistedClient
48+
return deserialize(cacheString) as PersistedClient
3749
},
3850
removeClient: () => storage.removeItem(key),
3951
}
4052
}
4153

42-
function asyncThrottle<T>(
43-
func: (...args: ReadonlyArray<unknown>) => Promise<T>,
54+
function asyncThrottle<Args extends readonly unknown[], Result>(
55+
func: (...args: Args) => Promise<Result>,
4456
{ interval = 1000, limit = 1 }: { interval?: number; limit?: number } = {}
4557
) {
4658
if (typeof func !== 'function') throw new Error('argument is not function.')
4759
const running = { current: false }
4860
let lastTime = 0
4961
let timeout: number
50-
const queue: Array<any[]> = []
51-
return (...args: any) =>
62+
const queue: Array<Args> = []
63+
return (...args: Args) =>
5264
(async () => {
5365
if (running.current) {
5466
lastTime = Date.now()

src/createWebStoragePersistor-experimental/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@ interface CreateWebStoragePersistorOptions {
99
/** To avoid spamming,
1010
* pass a time in ms to throttle saving the cache to disk */
1111
throttleTime?: number
12+
/**
13+
* How to serialize the data to storage.
14+
* @default `JSON.stringify`
15+
*/
16+
serialize?: (client: PersistedClient) => string
17+
/**
18+
* How to deserialize the data from storage.
19+
* @default `JSON.parse`
20+
*/
21+
deserialize?: (cachedString: string) => PersistedClient
1222
}
1323

1424
export function createWebStoragePersistor({
1525
storage,
1626
key = `REACT_QUERY_OFFLINE_CACHE`,
1727
throttleTime = 1000,
28+
serialize = JSON.stringify,
29+
deserialize = JSON.parse,
1830
}: CreateWebStoragePersistorOptions): Persistor {
1931
if (typeof storage !== 'undefined') {
2032
return {
2133
persistClient: throttle(persistedClient => {
22-
storage.setItem(key, JSON.stringify(persistedClient))
34+
storage.setItem(key, serialize(persistedClient))
2335
}, throttleTime),
2436
restoreClient: () => {
2537
const cacheString = storage.getItem(key)
@@ -28,7 +40,7 @@ export function createWebStoragePersistor({
2840
return
2941
}
3042

31-
return JSON.parse(cacheString) as PersistedClient
43+
return deserialize(cacheString) as PersistedClient
3244
},
3345
removeClient: () => {
3446
storage.removeItem(key)

0 commit comments

Comments
 (0)