Files
nitro/packages/react-native-nitro-modules/cpp/threading/Dispatcher.cpp
T
Marc Rousavy e6f84bee16 fix: Fix JS callbacks not using Dispatcher!!!!!
lol this broke a lot I cant believe I didnt catch this
2024-07-30 12:17:22 +02:00

57 lines
2.4 KiB
C++

#include "Dispatcher.hpp"
#include "NitroLogger.hpp"
#include "GetRuntimeID.h"
namespace margelo::nitro {
using namespace facebook;
static constexpr auto GLOBAL_DISPATCHER_HOLDER_NAME = "__nitroDispatcher";
std::unordered_map<jsi::Runtime*, std::weak_ptr<Dispatcher>> Dispatcher::_globalCache;
void Dispatcher::installRuntimeGlobalDispatcher(jsi::Runtime& runtime, std::shared_ptr<Dispatcher> dispatcher) {
Logger::log(TAG, "Installing global Dispatcher Holder into Runtime \"%s\"...", getRuntimeId(runtime));
// Store a weak reference in global cache
_globalCache[&runtime] = std::weak_ptr(dispatcher);
// Inject the dispatcher into Runtime global (runtime will hold a strong reference)
jsi::Object dispatcherHolder(runtime);
dispatcherHolder.setNativeState(runtime, dispatcher);
runtime.global().setProperty(runtime, GLOBAL_DISPATCHER_HOLDER_NAME, dispatcherHolder);
}
std::shared_ptr<Dispatcher> Dispatcher::getRuntimeGlobalDispatcher(jsi::Runtime& runtime) {
if (_globalCache.contains(&runtime)) [[likely]] {
// the runtime is known - we have something in cache
std::weak_ptr<Dispatcher> weakDispatcher = _globalCache[&runtime];
std::shared_ptr<Dispatcher> strongDispatcher = weakDispatcher.lock();
if (strongDispatcher) {
// the weak reference we cached is still valid - return it!
return strongDispatcher;
}
}
Logger::log(TAG, "Unknown Runtime (%s), looking for Dispatcher through JSI global lookup...", getRuntimeId(runtime));
jsi::Value dispatcherHolderValue = getRuntimeGlobalDispatcherHolder(runtime);
jsi::Object dispatcherHolder = dispatcherHolderValue.getObject(runtime);
return dispatcherHolder.getNativeState<Dispatcher>(runtime);
}
jsi::Value Dispatcher::getRuntimeGlobalDispatcherHolder(jsi::Runtime& runtime) {
#if DEBUG
if (!runtime.global().hasProperty(runtime, GLOBAL_DISPATCHER_HOLDER_NAME)) {
throw std::runtime_error("Failed to get current Dispatcher - the global Dispatcher "
"holder (global." +
std::string(GLOBAL_DISPATCHER_HOLDER_NAME) +
") "
"does not exist! Was Dispatcher::installDispatcherIntoRuntime() called "
"for this jsi::Runtime?");
}
#endif
return runtime.global().getProperty(runtime, GLOBAL_DISPATCHER_HOLDER_NAME);
}
} // namespace margelo::nitro