Skip to content

Commit

Permalink
feat(MMKVWrapper): Added new storage wrapper
Browse files Browse the repository at this point in the history
Added storage wrapper for react-native-mmkv.
Storage wrapper already existed for another mmvk lib, but they don't have the same interface
  • Loading branch information
Titozzz authored Sep 15, 2021
1 parent 55d93f4 commit 1b16bdb
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/storageWrappers/MMKVWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { PersistentStorage } from '../types';

/**
* Wrapper for react-native-mmkv.
* See [https://github.com/mrousavy/react-native-mmkv](https://github.com/mrousavy/react-native-mmkv) for installation instructions.
*
* @example
* const storage = new MMKV();
* const persistor = new CachePersistor({
* cache,
* storage: new MMKVWrapper(storage),
* });
*
*/
export class MMKVWrapper implements PersistentStorage<string | null> {
private storage;

constructor(storage: MMKVInterface) {
this.storage = storage;
}

getItem(key: string): string | null {
return this.storage.getString(key) || null;
}

removeItem(key: string): void {
return this.storage.delete(key);
}

setItem(key: string, value: string | null): void {
if (value !== null) {
return this.storage.set(key, value);
}
return this.removeItem(key);
}
}

interface MMKVInterface {
set: (key: string, value: boolean | string | number) => void;
getBoolean: (key: string) => boolean;
getString: (key: string) => string | undefined;
getNumber: (key: string) => number;
delete: (key: string) => void;
getAllKeys: () => string[];
clearAll: () => void;
}

0 comments on commit 1b16bdb

Please sign in to comment.