Skip to content

Commit 65512c4

Browse files
authored
Merge pull request #447 from Titozzz/patch-1
feat(MMKVWrapper): Added new storage wrapper
2 parents 44db77d + e3dbe4c commit 65512c4

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

docs/storage-providers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
| [`localForage`](https://github.com/localForage/localForage) | web | `LocalForageWrapper` |
1818
| [`Ionic storage`](https://ionicframework.com/docs/building/storage) | web and mobile | `IonicStorageWrapper` |
1919
| [`MMKV Storage`](https://github.com/ammarahm-ed/react-native-mmkv-storage) | React Native | `MMKVStorageWrapper` |
20+
| [`MMKV`](https://github.com/mrousavy/react-native-mmkv) | React Native | `MMKVWrapper` |
2021

2122

2223
## Redux Persist Providers

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+
}

src/storageWrappers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from './IonicStorageWrapper';
33
export * from './LocalForageWrapper';
44
export * from './LocalStorageWrapper';
55
export * from './MMKVStorageWrapper';
6+
export * from './MMKVWrapper';
67
export * from './SessionStorageWrapper';

0 commit comments

Comments
 (0)