Skip to content

Commit 37f6a32

Browse files
committed
add interface
1 parent 6ec1528 commit 37f6a32

File tree

1 file changed

+15
-4
lines changed
  • src/createAsyncStoragePersisor-experimental

1 file changed

+15
-4
lines changed

src/createAsyncStoragePersisor-experimental/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import AsyncStorage from '@react-native-community/async-storage'
22

3-
export const asyncStoragePersistor = ({ asyncStorageKey, throttleTime }) => {
3+
interface CreateAsyncStoragePersistorOptions {
4+
/** The key to use when storing the cache */
5+
key?: string
6+
/** To avoid spamming,
7+
* pass a time in ms to throttle saving the cache to disk */
8+
throttleTime?: number
9+
}
10+
11+
export const asyncStoragePersistor = ({
12+
key,
13+
throttleTime,
14+
}: CreateAsyncStoragePersistorOptions) => {
415
return {
516
persistClient: throttle(function (persistedClient) {
6-
AsyncStorage.setItem(asyncStorageKey, JSON.stringify(persistedClient))
17+
AsyncStorage.setItem(key, JSON.stringify(persistedClient))
718
}, throttleTime),
819
restoreClient: async function restoreClient() {
9-
const cacheString = await AsyncStorage.getItem(asyncStorageKey)
20+
const cacheString = await AsyncStorage.getItem(key)
1021

1122
if (!cacheString) {
1223
return
@@ -15,7 +26,7 @@ export const asyncStoragePersistor = ({ asyncStorageKey, throttleTime }) => {
1526
return JSON.parse(cacheString)
1627
},
1728
removeClient: function removeClient() {
18-
AsyncStorage.removeItem(asyncStorageKey)
29+
AsyncStorage.removeItem(key)
1930
},
2031
}
2132
}

0 commit comments

Comments
 (0)