Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement trim() to remove unused space #461

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions android/src/main/cpp/MmkvHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
});
}

if (propName == "trim") {
// MMKV.trim()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdym for reference?

Copy link

@ken0nek ken0nek Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to put the reference links on the MMKKV repo to easily get to the raw implementation. Does not mean feedback or request 👍

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,
Expand Down
16 changes: 16 additions & 0 deletions ios/MmkvHostObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/MMKV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -117,6 +121,7 @@ export type NativeMMKV = Pick<
| 'getNumber'
| 'getString'
| 'set'
| 'trim'
| 'recrypt'
>;

Expand Down Expand Up @@ -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(', ')}]`;
Expand Down
1 change: 1 addition & 0 deletions src/createMMKV.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
},
Expand Down
1 change: 1 addition & 0 deletions src/createMMKV.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
},
Expand Down