1- import AsyncStorage from '@react-native-community/async-storage'
1+ interface AsyncStorage {
2+ getItem : ( key : string ) => Promise < string >
3+ setItem : ( key : string , value : string ) => Promise < unknown >
4+ removeItem : ( key : string ) => Promise < unknown >
5+ }
26
37interface CreateAsyncStoragePersistorOptions {
8+ /** The storage client used for setting an retrieving items from cache */
9+ storage : AsyncStorage
410 /** The key to use when storing the cache */
511 key ?: string
612 /** To avoid spamming,
@@ -9,25 +15,25 @@ interface CreateAsyncStoragePersistorOptions {
915}
1016
1117export const asyncStoragePersistor = ( {
12- key,
18+ storage,
19+ key = `REACT_QUERY_OFFLINE_CACHE` ,
1320 throttleTime,
1421} : CreateAsyncStoragePersistorOptions ) => {
1522 return {
16- persistClient : throttle ( function ( persistedClient ) {
17- AsyncStorage . setItem ( key , JSON . stringify ( persistedClient ) )
18- } , throttleTime ) ,
19- restoreClient : async function restoreClient ( ) {
20- const cacheString = await AsyncStorage . getItem ( key )
23+ persistClient : throttle (
24+ persistedClient => storage . setItem ( key , JSON . stringify ( persistedClient ) ) ,
25+ throttleTime
26+ ) ,
27+ restoreClient : async ( ) => {
28+ const cacheString = await storage . getItem ( key )
2129
2230 if ( ! cacheString ) {
2331 return
2432 }
2533
2634 return JSON . parse ( cacheString )
2735 } ,
28- removeClient : function removeClient ( ) {
29- AsyncStorage . removeItem ( key )
30- } ,
36+ removeClient : ( ) => storage . removeItem ( key ) ,
3137 }
3238}
3339
0 commit comments