Files
nitro/packages/react-native-nitro-modules/cpp/threading/Dispatcher.cpp
T
Marc RousavyandGitHub 4d07b2a1d5 feat: Create Sync<T> to pass fully synchronous JS callbacks (#564)
* feat: Create `Sync<T>`

* Return

* Make it return number

* feat: Add `isSync` check...

* check

* Impl

* feat: Add `JSCallback`

* perf: Store `Dispatcher` in cache if really missed

* I think it builds now

* fix memory

* fix: Use `isAsync`

* Rename

* fix: Impl `callHybridFunction` again

* fix: Avoid forward decl

* Docs

* feat: Cache `regex` in `NitroTypeInfo`

* Update NitroTypeInfo.cpp

* perf: Make `Dispatcher` a `weak_ptr`

* Some formating

* fix: Fix styling in Benchmarks

* feat: Test if callbackSync returns same number

* fix: Fix warning in `toString()`

* Update Sync.ts

* perf: Allow `std::forward<...>`
2025-02-23 15:59:01 +01:00

68 lines
2.7 KiB
C++

//
// Dispatcher.cpp
// react-native-nitro
//
// Created by Marc Rousavy on 22.07.24.
//
#include "Dispatcher.hpp"
#include "JSIHelpers.hpp"
#include "NitroDefines.hpp"
#include "NitroLogger.hpp"
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(LogLevel::Info, TAG, "Installing global Dispatcher Holder into Runtime \"%s\"...", getRuntimeId(runtime).c_str());
// 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(LogLevel::Warning, TAG, "Unknown Runtime (%s), looking for Dispatcher through JSI global lookup...",
getRuntimeId(runtime).c_str());
jsi::Value dispatcherHolderValue = getRuntimeGlobalDispatcherHolder(runtime);
jsi::Object dispatcherHolder = dispatcherHolderValue.getObject(runtime);
std::shared_ptr<Dispatcher> dispatcher = dispatcherHolder.getNativeState<Dispatcher>(runtime);
_globalCache[&runtime] = dispatcher;
return dispatcher;
}
jsi::Value Dispatcher::getRuntimeGlobalDispatcherHolder(jsi::Runtime& runtime) {
#ifdef NITRO_DEBUG
if (!runtime.global().hasProperty(runtime, GLOBAL_DISPATCHER_HOLDER_NAME)) [[unlikely]] {
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