Skip to content

Commit ea2fc44

Browse files
committed
feat: Add MMKV.config getter
1 parent c870593 commit ea2fc44

File tree

8 files changed

+62
-40
lines changed

8 files changed

+62
-40
lines changed

packages/react-native-mmkv/cpp/HybridMMKV.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace margelo::nitro::mmkv {
1515

16-
HybridMMKV::HybridMMKV(const Configuration& config) : HybridObject(TAG) {
16+
HybridMMKV::HybridMMKV(const Configuration& config) : _config(config), HybridObject(TAG) {
1717
std::string path = config.path.has_value() ? config.path.value() : "";
1818
std::string encryptionKey = config.encryptionKey.has_value() ? config.encryptionKey.value() : "";
1919
bool hasEncryptionKey = encryptionKey.size() > 0;
@@ -29,12 +29,12 @@ HybridMMKV::HybridMMKV(const Configuration& config) : HybridObject(TAG) {
2929
}
3030

3131
#ifdef __APPLE__
32-
instance = MMKV::mmkvWithID(config.id, mode, encryptionKeyPtr, pathPtr);
32+
_instance = MMKV::mmkvWithID(config.id, mode, encryptionKeyPtr, pathPtr);
3333
#else
34-
instance = MMKV::mmkvWithID(config.id, DEFAULT_MMAP_SIZE, mode, encryptionKeyPtr, pathPtr);
34+
_instance = MMKV::mmkvWithID(config.id, DEFAULT_MMAP_SIZE, mode, encryptionKeyPtr, pathPtr);
3535
#endif
3636

37-
if (instance == nullptr) [[unlikely]] {
37+
if (_instance == nullptr) [[unlikely]] {
3838
// Check if instanceId is invalid
3939
if (config.id.empty()) [[unlikely]] {
4040
throw std::runtime_error("Failed to create MMKV instance! `id` cannot be empty!");
@@ -55,11 +55,14 @@ HybridMMKV::HybridMMKV(const Configuration& config) : HybridObject(TAG) {
5555
}
5656
}
5757

58+
Configuration HybridMMKV::getConfig() {
59+
return _config;
60+
}
5861
double HybridMMKV::getSize() {
59-
return instance->actualSize();
62+
return _instance->actualSize();
6063
}
6164
bool HybridMMKV::getIsReadOnly() {
62-
return instance->isReadOnly();
65+
return _instance->isReadOnly();
6366
}
6467

6568
// helper: overload pattern matching for lambdas
@@ -78,32 +81,32 @@ void HybridMMKV::set(const std::string& key, const std::variant<bool, std::share
7881
// Pattern-match each potential value in std::variant
7982
bool didSet = std::visit(overloaded{[&](bool b) {
8083
// boolean
81-
return instance->set(b, key);
84+
return _instance->set(b, key);
8285
},
8386
[&](const std::shared_ptr<ArrayBuffer>& buf) {
8487
// ArrayBuffer
8588
MMBuffer buffer(buf->data(), buf->size(), MMBufferCopyFlag::MMBufferNoCopy);
86-
return instance->set(std::move(buffer), key);
89+
return _instance->set(std::move(buffer), key);
8790
},
8891
[&](const std::string& string) {
8992
// string
90-
return instance->set(string, key);
93+
return _instance->set(string, key);
9194
},
9295
[&](double number) {
9396
// number
94-
return instance->set(number, key);
97+
return _instance->set(number, key);
9598
}},
9699
value);
97100
if (!didSet) {
98101
throw std::runtime_error("Failed to set value for key \"" + key + "\"!");
99102
}
100103

101104
// Notify on changed
102-
MMKVValueChangedListenerRegistry::notifyOnValueChanged(instance->mmapID(), key);
105+
MMKVValueChangedListenerRegistry::notifyOnValueChanged(_instance->mmapID(), key);
103106
}
104107
std::optional<bool> HybridMMKV::getBoolean(const std::string& key) {
105108
bool hasValue;
106-
bool result = instance->getBool(key, /* defaultValue */ false, &hasValue);
109+
bool result = _instance->getBool(key, /* defaultValue */ false, &hasValue);
107110
if (hasValue) {
108111
return result;
109112
} else {
@@ -112,7 +115,7 @@ std::optional<bool> HybridMMKV::getBoolean(const std::string& key) {
112115
}
113116
std::optional<std::string> HybridMMKV::getString(const std::string& key) {
114117
std::string result;
115-
bool hasValue = instance->getString(key, result, /* inplaceModification */ true);
118+
bool hasValue = _instance->getString(key, result, /* inplaceModification */ true);
116119
if (hasValue) {
117120
return result;
118121
} else {
@@ -121,7 +124,7 @@ std::optional<std::string> HybridMMKV::getString(const std::string& key) {
121124
}
122125
std::optional<double> HybridMMKV::getNumber(const std::string& key) {
123126
bool hasValue;
124-
double result = instance->getDouble(key, /* defaultValue */ 0.0, &hasValue);
127+
double result = _instance->getDouble(key, /* defaultValue */ 0.0, &hasValue);
125128
if (hasValue) {
126129
return result;
127130
} else {
@@ -132,11 +135,11 @@ std::optional<std::shared_ptr<ArrayBuffer>> HybridMMKV::getBuffer(const std::str
132135
MMBuffer result;
133136
#ifdef __APPLE__
134137
// iOS: Convert std::string to NSString* for MMKVCore pod compatibility
135-
bool hasValue = instance->getBytes(@(key.c_str()), result);
138+
bool hasValue = _instance->getBytes(@(key.c_str()), result);
136139
#else
137140
// Android/other platforms: Use std::string directly (converts to
138141
// std::string_view)
139-
bool hasValue = instance->getBytes(key, result);
142+
bool hasValue = _instance->getBytes(key, result);
140143
#endif
141144
if (hasValue) {
142145
return std::make_shared<ManagedMMBuffer>(std::move(result));
@@ -145,49 +148,49 @@ std::optional<std::shared_ptr<ArrayBuffer>> HybridMMKV::getBuffer(const std::str
145148
}
146149
}
147150
bool HybridMMKV::contains(const std::string& key) {
148-
return instance->containsKey(key);
151+
return _instance->containsKey(key);
149152
}
150153
bool HybridMMKV::remove(const std::string& key) {
151-
bool wasRemoved = instance->removeValueForKey(key);
154+
bool wasRemoved = _instance->removeValueForKey(key);
152155
if (wasRemoved) {
153156
// Notify on changed
154-
MMKVValueChangedListenerRegistry::notifyOnValueChanged(instance->mmapID(), key);
157+
MMKVValueChangedListenerRegistry::notifyOnValueChanged(_instance->mmapID(), key);
155158
}
156159
return wasRemoved;
157160
}
158161
std::vector<std::string> HybridMMKV::getAllKeys() {
159-
return instance->allKeys();
162+
return _instance->allKeys();
160163
}
161164
void HybridMMKV::clearAll() {
162165
auto keysBefore = getAllKeys();
163-
instance->clearAll();
166+
_instance->clearAll();
164167
for (const auto& key : keysBefore) {
165168
// Notify on changed
166-
MMKVValueChangedListenerRegistry::notifyOnValueChanged(instance->mmapID(), key);
169+
MMKVValueChangedListenerRegistry::notifyOnValueChanged(_instance->mmapID(), key);
167170
}
168171
}
169172
void HybridMMKV::recrypt(const std::optional<std::string>& key) {
170173
bool successful = false;
171174
if (key.has_value()) {
172175
// Encrypt with the given key
173-
successful = instance->reKey(key.value());
176+
successful = _instance->reKey(key.value());
174177
} else {
175178
// Remove the encryption key by setting it to a blank string
176-
successful = instance->reKey(std::string());
179+
successful = _instance->reKey(std::string());
177180
}
178181
if (!successful) {
179182
throw std::runtime_error("Failed to recrypt MMKV instance!");
180183
}
181184
}
182185
void HybridMMKV::trim() {
183-
instance->trim();
184-
instance->clearMemoryCache();
186+
_instance->trim();
187+
_instance->clearMemoryCache();
185188
}
186189

187190
Listener HybridMMKV::addOnValueChangedListener(const std::function<void(const std::string& /* key */)>& onValueChanged) {
188191
// Add listener
189-
auto mmkvID = instance->mmapID();
190-
auto listenerID = MMKVValueChangedListenerRegistry::addListener(instance->mmapID(), onValueChanged);
192+
auto mmkvID = _instance->mmapID();
193+
auto listenerID = MMKVValueChangedListenerRegistry::addListener(_instance->mmapID(), onValueChanged);
191194

192195
return Listener([=]() {
193196
// remove()

packages/react-native-mmkv/cpp/HybridMMKV.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class HybridMMKV final : public HybridMMKVSpec {
1919

2020
public:
2121
// Properties
22+
Configuration getConfig() override;
2223
double getSize() override;
2324
bool getIsReadOnly() override;
2425

@@ -41,7 +42,8 @@ class HybridMMKV final : public HybridMMKVSpec {
4142
static MMKVMode getMMKVMode(const Configuration& config);
4243

4344
private:
44-
MMKV* instance;
45+
MMKV* _instance;
46+
Configuration _config;
4547
};
4648

4749
} // namespace margelo::nitro::mmkv

packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace margelo::nitro::mmkv {
1414
HybridObject::loadHybridMethods();
1515
// load custom methods/properties
1616
registerHybrids(this, [](Prototype& prototype) {
17+
prototype.registerHybridGetter("config", &HybridMMKVSpec::getConfig);
1718
prototype.registerHybridGetter("size", &HybridMMKVSpec::getSize);
1819
prototype.registerHybridGetter("isReadOnly", &HybridMMKVSpec::getIsReadOnly);
1920
prototype.registerHybridMethod("set", &HybridMMKVSpec::set);

packages/react-native-mmkv/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
1414
#endif
1515

16+
// Forward declaration of `Configuration` to properly resolve imports.
17+
namespace margelo::nitro::mmkv { struct Configuration; }
1618
// Forward declaration of `Listener` to properly resolve imports.
1719
namespace margelo::nitro::mmkv { struct Listener; }
1820

21+
#include "Configuration.hpp"
1922
#include <string>
2023
#include <NitroModules/ArrayBuffer.hpp>
2124
#include <variant>
@@ -51,6 +54,7 @@ namespace margelo::nitro::mmkv {
5154

5255
public:
5356
// Properties
57+
virtual Configuration getConfig() = 0;
5458
virtual double getSize() = 0;
5559
virtual bool getIsReadOnly() = 0;
5660

packages/react-native-mmkv/src/createMMKV/createMMKV.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let platformContext: MMKVPlatformContext | undefined
1313
export function createMMKV(configuration?: Configuration): MMKV {
1414
if (isTest()) {
1515
// In a test environment, we mock the MMKV instance.
16-
return createMockMMKV()
16+
return createMockMMKV(configuration)
1717
}
1818

1919
if (platformContext == null) {

packages/react-native-mmkv/src/createMMKV/createMMKV.web.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export function createMMKV(
8585
}
8686

8787
return {
88+
config: config,
8889
clearAll: () => {
8990
const keys = Object.keys(storage())
9091
for (const key of keys) {

packages/react-native-mmkv/src/createMMKV/createMockMMKV.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import type { MMKV } from '../specs/MMKV.nitro'
2+
import type { Configuration } from '../specs/MMKVFactory.nitro'
23

34
/**
45
* Mock MMKV instance when used in a Jest/Test environment.
56
*/
6-
export function createMockMMKV(): MMKV {
7+
export function createMockMMKV(
8+
configuration: Configuration = { id: 'mmkv.default' }
9+
): MMKV {
10+
const config = configuration
711
const storage = new Map<string, string | boolean | number | ArrayBuffer>()
812
const listeners = new Set<(key: string) => void>()
913

@@ -14,6 +18,7 @@ export function createMockMMKV(): MMKV {
1418
}
1519

1620
return {
21+
config: config,
1722
clearAll: () => {
1823
const keysBefore = storage.keys()
1924
storage.clear()

packages/react-native-mmkv/src/specs/MMKV.nitro.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import type { HybridObject } from 'react-native-nitro-modules'
2+
import type { Configuration } from './MMKVFactory.nitro'
23

34
export interface Listener {
45
remove: () => void
56
}
67

78
export interface MMKV extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
9+
/**
10+
* The {@linkcode Configuration} that was used to create this {@linkcode MMKV}
11+
* instance.
12+
*/
13+
readonly config: Configuration
14+
/**
15+
* Get the current total size of the storage, in bytes.
16+
*/
17+
readonly size: number
18+
/**
19+
* Returns whether this instance is in read-only mode or not.
20+
* If this is `true`, you can only use "get"-functions.
21+
*/
22+
readonly isReadOnly: boolean
823
/**
924
* Set a {@linkcode value} for the given {@linkcode key}.
1025
*
@@ -75,15 +90,6 @@ export interface MMKV extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
7590
* In most applications, this is not needed at all.
7691
*/
7792
trim(): void
78-
/**
79-
* Get the current total size of the storage, in bytes.
80-
*/
81-
readonly size: number
82-
/**
83-
* Returns whether this instance is in read-only mode or not.
84-
* If this is `true`, you can only use "get"-functions.
85-
*/
86-
readonly isReadOnly: boolean
8793
/**
8894
* Adds a value changed listener. The Listener will be called whenever any value
8995
* in this storage instance changes (set or delete).

0 commit comments

Comments
 (0)