-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathAsyncStorageWrapper.ts
40 lines (34 loc) · 1.07 KB
/
AsyncStorageWrapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { PersistentStorage } from '../types';
/**
* Wrapper for react-native's AsyncStorage.
*
* WARNING: AsyncStorage doesn't support values in excess of 2MB on Android.
*
* @example
* const persistor = new CachePersistor({
* cache,
* storage: new AsyncStorageWrapper(AsyncStorage),
* });
*
*/
export class AsyncStorageWrapper implements PersistentStorage<string | null> {
protected storage;
constructor(storage: AsyncStorageInterface) {
this.storage = storage;
}
getItem(key: string): Promise<string | null> {
return this.storage.getItem(key);
}
removeItem(key: string): Promise<void> {
return this.storage.removeItem(key);
}
setItem(key: string, value: string | null): Promise<void> {
return this.storage.setItem(key, value);
}
}
interface AsyncStorageInterface {
// Actual type definition: https://github.com/react-native-async-storage/async-storage/blob/master/types/index.d.ts
getItem(key: string): Promise<string | null>;
setItem(key: string, value: string | null): Promise<void>;
removeItem(key: string): Promise<void>;
}