Skip to content

Commit

Permalink
feat: Add trim() func and size property
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Mar 25, 2024
1 parent 6c89901 commit b2d80fa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cpp/MmkvHostObject.cpp
Expand Up @@ -372,6 +372,25 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
return jsi::Value::undefined();
});
}

if (propName == "trim") {
// MMKV.trim()
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName),
0,
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
instance->trim();

return jsi::Value::undefined();
});
}

if (propName == "size") {
// MMKV.size
size_t size = instance->actualSize();
return jsi::Value(static_cast<int>(size));
}

return jsi::Value::undefined();
}
4 changes: 4 additions & 0 deletions example/src/App.tsx
Expand Up @@ -5,6 +5,10 @@ import { MMKV, useMMKVString } from 'react-native-mmkv';

const storage = new MMKV();

storage.addOnValueChangedListener((key) => {
console.log(`${key} changed! New size: ${storage.size}`);
});

export default function App() {
const [text, setText] = React.useState<string>('');
const [key, setKey] = React.useState<string>('');
Expand Down
29 changes: 29 additions & 0 deletions src/MMKV.ts
@@ -1,3 +1,4 @@
import { AppState } from 'react-native';
import { createMMKV } from './createMMKV';
import { createMockMMKV } from './createMMKV.mock';
import { isJest } from './PlatformChecker';
Expand Down Expand Up @@ -106,6 +107,20 @@ interface MMKVInterface {
* Encryption keys can have a maximum length of 16 bytes.
*/
recrypt: (key: string | undefined) => void;
/**
* Trims the storage space and clears memory cache.
*
* Since MMKV does not resize itself after deleting keys, you can call `trim()`
* after deleting a bunch of keys to manually trim the memory- and
* disk-file to reduce storage and memory usage.
*
* In most applications, this is not needed at all.
*/
trim(): void;
/**
* Get the current total size of the storage, in bytes.
*/
readonly size: number;
/**
* Adds a value changed listener. The Listener will be called whenever any value
* in this storage instance changes (set or delete).
Expand All @@ -129,6 +144,8 @@ export type NativeMMKV = Pick<
| 'getBuffer'
| 'set'
| 'recrypt'
| 'trim'
| 'size'
>;

const onValueChangedListeners = new Map<string, ((key: string) => void)[]>();
Expand All @@ -151,6 +168,11 @@ export class MMKV implements MMKVInterface {
? createMockMMKV()
: createMMKV(configuration);
this.functionCache = {};

const weakSelf = new WeakRef(this);
AppState.addEventListener('memoryWarning', () => {
weakSelf.deref()?.trim();
});
}

private get onValueChangedListeners() {
Expand Down Expand Up @@ -179,6 +201,9 @@ export class MMKV implements MMKVInterface {
}
}

get size(): number {
return this.nativeInstance.size;
}
set(key: string, value: boolean | string | number | ArrayBuffer): void {
const func = this.getFunctionFromCache('set');
func(key, value);
Expand Down Expand Up @@ -227,6 +252,10 @@ export class MMKV implements MMKVInterface {
const func = this.getFunctionFromCache('recrypt');
return func(key);
}
trim(): void {
const func = this.getFunctionFromCache('trim');
func();
}

toString(): string {
return `MMKV (${this.id}): [${this.getAllKeys().join(', ')}]`;
Expand Down

0 comments on commit b2d80fa

Please sign in to comment.