Skip to content

Commit 1b16bdb

Browse files
authored
feat(MMKVWrapper): Added new storage wrapper
Added storage wrapper for react-native-mmkv. Storage wrapper already existed for another mmvk lib, but they don't have the same interface
1 parent 55d93f4 commit 1b16bdb

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/storageWrappers/MMKVWrapper.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

0 commit comments

Comments
 (0)