diff --git a/packages/react-native-mmkv/cpp/HybridMMKV.cpp b/packages/react-native-mmkv/cpp/HybridMMKV.cpp index 8ff2505d..e4b3dc31 100644 --- a/packages/react-native-mmkv/cpp/HybridMMKV.cpp +++ b/packages/react-native-mmkv/cpp/HybridMMKV.cpp @@ -55,6 +55,9 @@ HybridMMKV::HybridMMKV(const Configuration& config) : HybridObject(TAG) { } } +std::string HybridMMKV::getId() { + return instance->mmapID(); +} double HybridMMKV::getSize() { return instance->actualSize(); } diff --git a/packages/react-native-mmkv/cpp/HybridMMKV.hpp b/packages/react-native-mmkv/cpp/HybridMMKV.hpp index 946be28b..3454326c 100644 --- a/packages/react-native-mmkv/cpp/HybridMMKV.hpp +++ b/packages/react-native-mmkv/cpp/HybridMMKV.hpp @@ -19,6 +19,7 @@ class HybridMMKV final : public HybridMMKVSpec { public: // Properties + std::string getId() override; double getSize() override; bool getIsReadOnly() override; diff --git a/packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp b/packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp index abd7b66d..4b265680 100644 --- a/packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp +++ b/packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp @@ -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); diff --git a/packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp b/packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp index 9952d908..a37e5943 100644 --- a/packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp +++ b/packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp @@ -51,6 +51,7 @@ namespace margelo::nitro::mmkv { public: // Properties + virtual std::string getId() = 0; virtual double getSize() = 0; virtual bool getIsReadOnly() = 0; diff --git a/packages/react-native-mmkv/src/createMMKV/createMMKV.ts b/packages/react-native-mmkv/src/createMMKV/createMMKV.ts index 8a77e953..83c8633c 100644 --- a/packages/react-native-mmkv/src/createMMKV/createMMKV.ts +++ b/packages/react-native-mmkv/src/createMMKV/createMMKV.ts @@ -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) { diff --git a/packages/react-native-mmkv/src/createMMKV/createMMKV.web.ts b/packages/react-native-mmkv/src/createMMKV/createMMKV.web.ts index 790b543b..f99ae585 100644 --- a/packages/react-native-mmkv/src/createMMKV/createMMKV.web.ts +++ b/packages/react-native-mmkv/src/createMMKV/createMMKV.web.ts @@ -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) { @@ -130,8 +133,6 @@ export function createMMKV( recrypt: () => { throw new Error('`recrypt(..)` is not supported on Web!') }, - size: 0, - isReadOnly: false, trim: () => { // no-op }, diff --git a/packages/react-native-mmkv/src/createMMKV/createMockMMKV.ts b/packages/react-native-mmkv/src/createMMKV/createMockMMKV.ts index 66453961..02871993 100644 --- a/packages/react-native-mmkv/src/createMMKV/createMockMMKV.ts +++ b/packages/react-native-mmkv/src/createMMKV/createMockMMKV.ts @@ -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() const listeners = new Set<(key: string) => void>() @@ -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() @@ -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 }, diff --git a/packages/react-native-mmkv/src/specs/MMKV.nitro.ts b/packages/react-native-mmkv/src/specs/MMKV.nitro.ts index 1c922e34..cb5b331e 100644 --- a/packages/react-native-mmkv/src/specs/MMKV.nitro.ts +++ b/packages/react-native-mmkv/src/specs/MMKV.nitro.ts @@ -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}. * @@ -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).