Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass RuntimeSchedulerCallInvoker to C++ TurboModule initialization #12826

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Pass RuntimeSchedulerCallInvoker to C++ TurboModule initialization",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch"
}
26 changes: 23 additions & 3 deletions vnext/Microsoft.ReactNative.Cxx/TurboModuleProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

namespace winrt::Microsoft::ReactNative {

ReactPropertyId<ReactNonAbiValue<std::weak_ptr<facebook::react::CallInvoker>>> CallInvokerProperty() noexcept {
ReactPropertyId<ReactNonAbiValue<std::weak_ptr<facebook::react::CallInvoker>>> propId{
L"ReactNative.JSI", L"CallInvoker"};
return propId;
}

// CallInvoker implementation based on JSDispatcher.
struct AbiCallInvoker final : facebook::react::CallInvoker {
AbiCallInvoker(IReactDispatcher const &jsDispatcher) : m_jsDispatcher(jsDispatcher) {}
Expand All @@ -24,9 +30,23 @@ struct AbiCallInvoker final : facebook::react::CallInvoker {
IReactDispatcher m_jsDispatcher{nullptr};
};

// Creates CallInvoker based on JSDispatcher.
std::shared_ptr<facebook::react::CallInvoker> MakeAbiCallInvoker(IReactDispatcher const &jsDispatcher) noexcept {
return std::make_shared<AbiCallInvoker>(jsDispatcher);
std::shared_ptr<facebook::react::CallInvoker> GetCallInvokerFromContext(IReactContext const &context) noexcept {
// Try to get the CallInvoker instance from Context
const auto callInvokerPropertyValue = ReactPropertyBag{context.Properties()}.Get(CallInvokerProperty());
const auto weakCallInvoker =
callInvokerPropertyValue ? callInvokerPropertyValue.Value() : std::weak_ptr<facebook::react::CallInvoker>{};
if (const auto callInvoker = weakCallInvoker.lock()) {
return callInvoker;
}

// Fallback to creating CallInvoker based on JSDispatcher.
return std::make_shared<AbiCallInvoker>(context.JSDispatcher());
}

void SetCallInvoker(
IReactPropertyBag const &properties,
std::weak_ptr<facebook::react::CallInvoker> const &value) noexcept {
ReactPropertyBag{properties}.Set(CallInvokerProperty(), value);
}

} // namespace winrt::Microsoft::ReactNative
8 changes: 5 additions & 3 deletions vnext/Microsoft.ReactNative.Cxx/TurboModuleProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

namespace winrt::Microsoft::ReactNative {

// Creates CallInvoker based on JSDispatcher.
std::shared_ptr<facebook::react::CallInvoker> MakeAbiCallInvoker(IReactDispatcher const &jsDispatcher) noexcept;
void SetCallInvoker(
IReactPropertyBag const &properties,
std::weak_ptr<facebook::react::CallInvoker> const &callInvoker) noexcept;
std::shared_ptr<facebook::react::CallInvoker> GetCallInvokerFromContext(IReactContext const &context) noexcept;

template <
typename TTurboModule,
Expand All @@ -23,7 +25,7 @@ void AddTurboModuleProvider(IReactPackageBuilder const &packageBuilder, std::wst
// We expect the initializer to be called immediately for TurboModules
moduleBuilder.AddInitializer([&abiTurboModule](IReactContext const &context) mutable {
TryGetOrCreateContextRuntime(ReactContext{context}); // Ensure the JSI runtime is created.
auto callInvoker = MakeAbiCallInvoker(context.JSDispatcher());
auto callInvoker = GetCallInvokerFromContext(context);
auto turboModule = std::make_shared<TTurboModule>(callInvoker);
abiTurboModule = winrt::make<JsiHostObjectWrapper>(std::move(turboModule));
});
Expand Down
12 changes: 8 additions & 4 deletions vnext/Shared/OInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <ReactPropertyBag.h>
#include <SchedulerSettings.h>
#include <Shlwapi.h>
#include <TurboModuleProvider.h>
#include <WebSocketJSExecutorFactory.h>
#include <safeint.h>
#include "PackagerConnection.h"
Expand Down Expand Up @@ -513,10 +514,13 @@ InstanceImpl::InstanceImpl(
if (runtimeScheduler) {
RuntimeSchedulerBinding::createAndInstallIfNeeded(*runtimeHolder->getRuntime(), runtimeScheduler);
}
auto turboModuleManager = std::make_shared<TurboModuleManager>(
turboModuleRegistry,
runtimeScheduler ? std::make_shared<RuntimeSchedulerCallInvoker>(runtimeScheduler)
: innerInstance->getJSCallInvoker());

const auto callInvoker = runtimeScheduler ? std::make_shared<RuntimeSchedulerCallInvoker>(runtimeScheduler)
: innerInstance->getJSCallInvoker();

auto turboModuleManager = std::make_shared<TurboModuleManager>(turboModuleRegistry, callInvoker);

SetCallInvoker(propertyBag, callInvoker);

// TODO: The binding here should also add the proxys that convert cxxmodules into turbomodules
// [@vmoroz] Note, that we must not use the RN TurboCxxModule.h code because it uses global
Expand Down
Loading