File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { PersistentStorage } from '../types' ;
2
+
3
+ /**
4
+ * Wrapper for react-native-mmkv.
5
+ * See [https://github.com/mrousavy/react-native-mmkv](https://github.com/mrousavy/react-native-mmkv) for installation instructions.
6
+ *
7
+ * @example
8
+ * const storage = new MMKV();
9
+ * const persistor = new CachePersistor({
10
+ * cache,
11
+ * storage: new MMKVWrapper(storage),
12
+ * });
13
+ *
14
+ */
15
+ export class MMKVWrapper implements PersistentStorage < string | null > {
16
+ private storage ;
17
+
18
+ constructor ( storage : MMKVInterface ) {
19
+ this . storage = storage ;
20
+ }
21
+
22
+ getItem ( key : string ) : string | null {
23
+ return this . storage . getString ( key ) || null ;
24
+ }
25
+
26
+ removeItem ( key : string ) : void {
27
+ return this . storage . delete ( key ) ;
28
+ }
29
+
30
+ setItem ( key : string , value : string | null ) : void {
31
+ if ( value !== null ) {
32
+ return this . storage . set ( key , value ) ;
33
+ }
34
+ return this . removeItem ( key ) ;
35
+ }
36
+ }
37
+
38
+ interface MMKVInterface {
39
+ set : ( key : string , value : boolean | string | number ) => void ;
40
+ getBoolean : ( key : string ) => boolean ;
41
+ getString : ( key : string ) => string | undefined ;
42
+ getNumber : ( key : string ) => number ;
43
+ delete : ( key : string ) => void ;
44
+ getAllKeys : ( ) => string [ ] ;
45
+ clearAll : ( ) => void ;
46
+ }
You can’t perform that action at this time.
0 commit comments