diff --git a/android/src/main/cpp/MmkvHostObject.cpp b/android/src/main/cpp/MmkvHostObject.cpp index 756624e9..6af66d9d 100644 --- a/android/src/main/cpp/MmkvHostObject.cpp +++ b/android/src/main/cpp/MmkvHostObject.cpp @@ -221,6 +221,20 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro }); } + 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 == "recrypt") { // MMKV.recrypt(encryptionKey) return jsi::Function::createFromHostFunction(runtime, diff --git a/ios/MmkvHostObject.mm b/ios/MmkvHostObject.mm index e0a8b92f..e9481f07 100644 --- a/ios/MmkvHostObject.mm +++ b/ios/MmkvHostObject.mm @@ -227,6 +227,22 @@ }); } + 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 { + NSLog(@"Before: %zu / %zu", [instance actualSize], [instance totalSize]); + [instance trim]; + NSLog(@"After: %zu / %zu", [instance actualSize], [instance totalSize]); + return jsi::Value::undefined(); + }); + } + if (propName == "recrypt") { // MMKV.recrypt(encryptionKey) return jsi::Function::createFromHostFunction(runtime, diff --git a/src/MMKV.ts b/src/MMKV.ts index 2e3e7cd7..24c429f6 100644 --- a/src/MMKV.ts +++ b/src/MMKV.ts @@ -88,6 +88,10 @@ interface MMKVInterface { * Delete all keys. */ clearAll: () => void; + /** + * Trim unused space on the storage file + */ + trim: () => void; /** * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with. * @@ -117,6 +121,7 @@ export type NativeMMKV = Pick< | 'getNumber' | 'getString' | 'set' + | 'trim' | 'recrypt' >; @@ -212,6 +217,10 @@ export class MMKV implements MMKVInterface { const func = this.getFunctionFromCache('recrypt'); return func(key); } + trim(): void { + const func = this.getFunctionFromCache('trim'); + return func(); + } toString(): string { return `MMKV (${this.id}): [${this.getAllKeys().join(', ')}]`; diff --git a/src/createMMKV.mock.ts b/src/createMMKV.mock.ts index 8012fe06..e88d3cc9 100644 --- a/src/createMMKV.mock.ts +++ b/src/createMMKV.mock.ts @@ -22,6 +22,7 @@ export const createMockMMKV = (): NativeMMKV => { }, getAllKeys: () => Array.from(storage.keys()), contains: (key) => storage.has(key), + trim: () => {}, recrypt: () => { console.warn('Encryption is not supported in mocked MMKV instances!'); }, diff --git a/src/createMMKV.web.ts b/src/createMMKV.web.ts index 893b19c4..ecb3493d 100644 --- a/src/createMMKV.web.ts +++ b/src/createMMKV.web.ts @@ -46,6 +46,7 @@ export const createMMKV = (config: MMKVConfiguration): NativeMMKV => { }, getAllKeys: () => Object.keys(storage()), contains: (key) => storage().getItem(key) != null, + trim: () => {}, recrypt: () => { throw new Error('`recrypt(..)` is not supported on Web!'); },