Files
nitro/packages/react-native-nitro-modules/cpp/utils/PropNameIDCache.cpp
T
370b1ebe0e fix: Destroy PropNameIDCache on runtime reload to prevent crash (#1138)
* fix: Destroy PropNameIDCache on runtime reload

* fix: Implicit erase

---------

Co-authored-by: Marc Rousavy <me@mrousavy.com>
2026-01-12 10:35:45 +01:00

43 lines
1.1 KiB
C++

//
// PropNameIDCache.cpp
// react-native-nitro
//
// Created by Marc Rousavy on 17.10.25.
//
#include "PropNameIDCache.hpp"
#include "JSICache.hpp"
namespace margelo::nitro {
using namespace facebook;
std::unordered_map<jsi::Runtime*, PropNameIDCache::CacheMap> PropNameIDCache::_cache;
const jsi::PropNameID& PropNameIDCache::get(jsi::Runtime& runtime, const std::string& value) {
CacheMap& cache = _cache[&runtime];
const auto& cachedName = cache.find(value);
if (cachedName != cache.end()) {
// cache warm!
const BorrowingReference<jsi::PropNameID>& value = cachedName->second;
if (value != nullptr) {
// Reference is still alive - return it.
return *value;
}
// Reference is dead (e.g. runtime was recreated), re-create it.
}
// not cached - create the jsi::PropNameID...
auto propName = jsi::PropNameID::forAscii(runtime, value);
auto jsiCache = JSICache::getOrCreateCache(runtime);
auto sharedPropName = jsiCache.makeShared(std::move(propName));
// store it in cache...
cache.emplace(value, sharedPropName);
// return it!
return *sharedPropName;
}
} // namespace margelo::nitro