Skip to content
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
10 changes: 8 additions & 2 deletions packages/react-native-mmkv/cpp/HybridMMKV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,17 @@ std::vector<std::string> HybridMMKV::getAllKeys() {
return instance->allKeys();
}
void HybridMMKV::clearAll() {
auto mmkvID = instance->mmapID();

if (!MMKVValueChangedListenerRegistry::hasListeners(mmkvID)) {
instance->clearAll();
return;
}

auto keysBefore = getAllKeys();
instance->clearAll();
for (const auto& key : keysBefore) {
// Notify on changed
MMKVValueChangedListenerRegistry::notifyOnValueChanged(instance->mmapID(), key);
MMKVValueChangedListenerRegistry::notifyOnValueChanged(mmkvID, key);
}
}
void HybridMMKV::recrypt(const std::optional<std::string>& key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ void MMKVValueChangedListenerRegistry::removeListener(const std::string& mmkvID,
listeners.end());
}

bool MMKVValueChangedListenerRegistry::hasListeners(const std::string& mmkvID) {
auto entry = _listeners.find(mmkvID);
if (entry == _listeners.end()) {
return false;
}
return !entry->second.empty();
}

void MMKVValueChangedListenerRegistry::notifyOnValueChanged(const std::string& mmkvID, const std::string& key) {
// 1. Get all listeners for the specific MMKV ID
auto entry = _listeners.find(mmkvID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MMKVValueChangedListenerRegistry final {
public:
static ListenerID addListener(const std::string& mmkvID, const std::function<void(const std::string& /* key */)>& callback);
static void removeListener(const std::string& mmkvID, ListenerID id);
static bool hasListeners(const std::string& mmkvID);

public:
static void notifyOnValueChanged(const std::string& mmkvID, const std::string& key);
Expand Down