Skip to content

Commit 2b2c22f

Browse files
author
Ben Grynhaus
committed
feat: add optional functions to serialize and deserialize from Storage Persistors
1 parent 69a5d79 commit 2b2c22f

File tree

2 files changed

+26
-4
lines changed
  • src
    • createAsyncStoragePersistor-experimental
    • createWebStoragePersistor-experimental

2 files changed

+26
-4
lines changed

src/createAsyncStoragePersistor-experimental/index.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@ interface CreateAsyncStoragePersistorOptions {
1414
/** To avoid spamming,
1515
* pass a time in ms to throttle saving the cache to disk */
1616
throttleTime?: number
17+
/**
18+
* @defualt `JSON.stringify`
19+
*/
20+
serialize?: (client: PersistedClient) => string;
21+
/**
22+
* @defualt `JSON.parse`
23+
*/
24+
deserialize?: (cachedString: string) => PersistedClient;
1725
}
1826

1927
export const createAsyncStoragePersistor = ({
2028
storage,
2129
key = `REACT_QUERY_OFFLINE_CACHE`,
2230
throttleTime = 1000,
31+
serialize = JSON.stringify,
32+
deserialize = JSON.parse,
2333
}: CreateAsyncStoragePersistorOptions): Persistor => {
2434
return {
2535
persistClient: asyncThrottle(
26-
persistedClient => storage.setItem(key, JSON.stringify(persistedClient)),
36+
persistedClient => storage.setItem(key, serialize(persistedClient)),
2737
{ interval: throttleTime }
2838
),
2939
restoreClient: async () => {
@@ -33,7 +43,7 @@ export const createAsyncStoragePersistor = ({
3343
return
3444
}
3545

36-
return JSON.parse(cacheString) as PersistedClient
46+
return deserialize(cacheString) as PersistedClient
3747
},
3848
removeClient: () => storage.removeItem(key),
3949
}

src/createWebStoragePersistor-experimental/index.ts

+14-2
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+
* @defualt `JSON.stringify`
15+
*/
16+
serialize?: (client: PersistedClient) => string;
17+
/**
18+
* How to deserialize the data from storage.
19+
* @defualt `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)