Skip to content
Merged
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
3 changes: 3 additions & 0 deletions packages/react-native-mmkv/cpp/HybridMMKV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ HybridMMKV::HybridMMKV(const Configuration& config) : HybridObject(TAG) {
}
}

std::string HybridMMKV::getId() {
return instance->mmapID();
}
double HybridMMKV::getSize() {
return instance->actualSize();
}
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-mmkv/cpp/HybridMMKV.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class HybridMMKV final : public HybridMMKVSpec {

public:
// Properties
std::string getId() override;
double getSize() override;
bool getIsReadOnly() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace margelo::nitro::mmkv {
HybridObject::loadHybridMethods();
// load custom methods/properties
registerHybrids(this, [](Prototype& prototype) {
prototype.registerHybridGetter("id", &HybridMMKVSpec::getId);
prototype.registerHybridGetter("size", &HybridMMKVSpec::getSize);
prototype.registerHybridGetter("isReadOnly", &HybridMMKVSpec::getIsReadOnly);
prototype.registerHybridMethod("set", &HybridMMKVSpec::set);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace margelo::nitro::mmkv {

public:
// Properties
virtual std::string getId() = 0;
virtual double getSize() = 0;
virtual bool getIsReadOnly() = 0;

Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-mmkv/src/createMMKV/createMMKV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let platformContext: MMKVPlatformContext | undefined
export function createMMKV(configuration?: Configuration): MMKV {
if (isTest()) {
// In a test environment, we mock the MMKV instance.
return createMockMMKV()
return createMockMMKV(configuration)
}

if (platformContext == null) {
Expand Down
5 changes: 3 additions & 2 deletions packages/react-native-mmkv/src/createMMKV/createMMKV.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export function createMMKV(
}

return {
id: config.id,
size: 0,
isReadOnly: false,
clearAll: () => {
const keys = Object.keys(storage())
for (const key of keys) {
Expand Down Expand Up @@ -130,8 +133,6 @@ export function createMMKV(
recrypt: () => {
throw new Error('`recrypt(..)` is not supported on Web!')
},
size: 0,
isReadOnly: false,
trim: () => {
// no-op
},
Expand Down
14 changes: 9 additions & 5 deletions packages/react-native-mmkv/src/createMMKV/createMockMMKV.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { MMKV } from '../specs/MMKV.nitro'
import type { Configuration } from '../specs/MMKVFactory.nitro'

/**
* Mock MMKV instance when used in a Jest/Test environment.
*/
export function createMockMMKV(): MMKV {
export function createMockMMKV(
config: Configuration = { id: 'mmkv.default' }
): MMKV {
const storage = new Map<string, string | boolean | number | ArrayBuffer>()
const listeners = new Set<(key: string) => void>()

Expand All @@ -14,6 +17,11 @@ export function createMockMMKV(): MMKV {
}

return {
id: config.id,
get size(): number {
return storage.size
},
isReadOnly: false,
clearAll: () => {
const keysBefore = storage.keys()
storage.clear()
Expand Down Expand Up @@ -55,10 +63,6 @@ export function createMockMMKV(): MMKV {
recrypt: () => {
console.warn('Encryption is not supported in mocked MMKV instances!')
},
get size(): number {
return storage.size
},
isReadOnly: false,
trim: () => {
// no-op
},
Expand Down
22 changes: 13 additions & 9 deletions packages/react-native-mmkv/src/specs/MMKV.nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ export interface Listener {
}

export interface MMKV extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
/**
* Get the ID of this {@linkcode MMKV} instance.
*/
readonly id: string
/**
* Get the current total size of the storage, in bytes.
*/
readonly size: number
/**
* Returns whether this instance is in read-only mode or not.
* If this is `true`, you can only use "get"-functions.
*/
readonly isReadOnly: boolean
/**
* Set a {@linkcode value} for the given {@linkcode key}.
*
Expand Down Expand Up @@ -75,15 +88,6 @@ export interface MMKV extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
* In most applications, this is not needed at all.
*/
trim(): void
/**
* Get the current total size of the storage, in bytes.
*/
readonly size: number
/**
* Returns whether this instance is in read-only mode or not.
* If this is `true`, you can only use "get"-functions.
*/
readonly isReadOnly: boolean
/**
* Adds a value changed listener. The Listener will be called whenever any value
* in this storage instance changes (set or delete).
Expand Down