File tree Expand file tree Collapse file tree 2 files changed +26
-4
lines changed
createAsyncStoragePersistor-experimental
createWebStoragePersistor-experimental Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Original file line number Diff line number Diff 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
1927export 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 }
Original file line number Diff line number Diff 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
1424export 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 )
You can’t perform that action at this time.
0 commit comments