Skip to content

Commit cbf10a9

Browse files
javachefacebook-github-bot
authored andcommitted
Extract TurboModuleManager from ReactHost (#54651)
Summary: Break up ReactHost and TurboModuleManager, so in the future we can inject this more easily. Changelog: [Internal] Reviewed By: lenaic Differential Revision: D87774276
1 parent 7c603ee commit cbf10a9

File tree

5 files changed

+184
-110
lines changed

5 files changed

+184
-110
lines changed

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class BridgelessNativeModuleProxy;
2121
/**
2222
* Represents the JavaScript binding for the TurboModule system.
2323
*/
24-
class TurboModuleBinding {
24+
class TurboModuleBinding final {
2525
public:
2626
/*
2727
* Installs TurboModuleBinding into JavaScript runtime.
@@ -38,7 +38,7 @@ class TurboModuleBinding {
3838
TurboModuleProviderFunctionType &&moduleProvider,
3939
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection);
4040

41-
virtual ~TurboModuleBinding();
41+
~TurboModuleBinding();
4242

4343
private:
4444
friend BridgelessNativeModuleProxy;

packages/react-native/ReactCxxPlatform/react/devsupport/DevSettingsModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DevSettingsModule : public NativeDevSettingsCxxSpec<DevSettingsModule> {
2121
DevSettingsModule(
2222
std::shared_ptr<CallInvoker> jsInvoker,
2323
std::weak_ptr<DevServerHelper> devServerHelper,
24-
LiveReloadCallback &&liveReloadCallback)
24+
LiveReloadCallback liveReloadCallback)
2525
: NativeDevSettingsCxxSpec(jsInvoker),
2626
devServerHelper_(std::move(devServerHelper)),
2727
liveReloadCallback_(std::move(liveReloadCallback))

packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp

Lines changed: 18 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,15 @@
1313
#include <glog/logging.h>
1414
#include <jserrorhandler/JsErrorHandler.h>
1515
#include <jsinspector-modern/InspectorFlags.h>
16-
#include <react/coremodules/AppStateModule.h>
17-
#include <react/coremodules/DeviceInfoModule.h>
18-
#include <react/coremodules/PlatformConstantsModule.h>
1916
#include <react/debug/react_native_assert.h>
20-
#include <react/devsupport/DevLoadingViewModule.h>
2117
#include <react/devsupport/DevServerHelper.h>
22-
#include <react/devsupport/DevSettingsModule.h>
2318
#include <react/devsupport/IDevUIDelegate.h>
24-
#include <react/devsupport/LogBoxModule.h>
2519
#include <react/devsupport/PackagerConnection.h>
26-
#include <react/devsupport/SourceCodeModule.h>
2720
#include <react/devsupport/inspector/Inspector.h>
2821
#include <react/http/IHttpClient.h>
2922
#include <react/http/IWebSocketClient.h>
30-
#include <react/io/ImageLoaderModule.h>
31-
#include <react/io/NetworkingModule.h>
3223
#include <react/io/ResourceLoader.h>
33-
#include <react/io/WebSocketModule.h>
3424
#include <react/logging/LogOnce.h>
35-
#include <react/logging/NativeExceptionsManager.h>
36-
#include <react/nativemodule/defaults/DefaultTurboModules.h>
37-
#include <react/nativemodule/intersectionobserver/NativeIntersectionObserver.h>
38-
#include <react/nativemodule/mutationobserver/NativeMutationObserver.h>
39-
#include <react/nativemodule/webperformance/NativePerformance.h>
40-
#include <react/renderer/animated/AnimatedModule.h>
4125
#include <react/renderer/componentregistry/native/NativeComponentRegistryBinding.h>
4226
#include <react/renderer/runtimescheduler/RuntimeSchedulerCallInvoker.h>
4327
#include <react/renderer/scheduler/SchedulerDelegate.h>
@@ -50,6 +34,8 @@
5034
#include <react/runtime/hermes/HermesInstance.h>
5135
#include <react/threading/MessageQueueThreadImpl.h>
5236

37+
#include "TurboModuleManager.h"
38+
5339
namespace facebook::react {
5440

5541
struct ReactInstanceData {
@@ -257,6 +243,18 @@ void ReactHost::createReactInstance() {
257243
}
258244

259245
auto liveReloadCallback = [this]() { reloadReactInstance(); };
246+
TurboModuleManager turboModuleManager(
247+
reactInstanceData_->turboModuleProviders,
248+
jsInvoker,
249+
reactInstanceData_->onJsError,
250+
reactInstanceData_->animatedNodesManagerProvider,
251+
reactInstanceConfig_.enableDevMode ? devServerHelper_ : nullptr,
252+
reactInstanceData_->devUIDelegate,
253+
reactInstanceData_->logBoxSurfaceDelegate,
254+
httpClientFactory,
255+
webSocketClientFactory,
256+
std::move(liveReloadCallback));
257+
260258
reactInstance_->initializeRuntime(
261259
{
262260
#if defined(WITH_PERFETTO) || defined(RNCXX_WITH_PROFILING_PROVIDER)
@@ -268,20 +266,9 @@ void ReactHost::createReactInstance() {
268266
[weakMountingManager =
269267
std::weak_ptr<IMountingManager>(reactInstanceData_->mountingManager),
270268
logger = reactInstanceData_->logger,
271-
devUIDelegate = reactInstanceData_->devUIDelegate,
272-
turboModuleProviders = reactInstanceData_->turboModuleProviders,
273-
jsInvoker = std::move(jsInvoker),
274-
logBoxSurfaceDelegate = reactInstanceData_->logBoxSurfaceDelegate,
275-
devServerHelper =
276-
reactInstanceConfig_.enableDevMode ? devServerHelper_ : nullptr,
277-
animatedNodesManagerProvider =
278-
reactInstanceData_->animatedNodesManagerProvider,
279-
onJsError = reactInstanceData_->onJsError,
280269
bindingsInstallFunc = reactInstanceData_->bindingsInstallFunc,
281-
httpClientFactory = std::move(httpClientFactory),
282-
webSocketClientFactory = std::move(webSocketClientFactory),
283-
liveReloadCallback =
284-
std::move(liveReloadCallback)](jsi::Runtime& runtime) {
270+
turboModuleManager =
271+
std::move(turboModuleManager)](jsi::Runtime& runtime) mutable {
285272
if (logger) {
286273
bindNativeLogger(runtime, logger);
287274
}
@@ -295,85 +282,9 @@ void ReactHost::createReactInstance() {
295282
return false;
296283
});
297284

298-
auto turboModuleProvider =
299-
[turboModuleProviders,
300-
jsInvoker,
301-
logBoxSurfaceDelegate,
302-
devServerHelper,
303-
devUIDelegate,
304-
animatedNodesManagerProvider,
305-
onJsError,
306-
httpClientFactory = httpClientFactory,
307-
webSocketClientFactory = webSocketClientFactory,
308-
liveReloadCallback = liveReloadCallback](
309-
const std::string& name) -> std::shared_ptr<TurboModule> {
310-
react_native_assert(
311-
!name.empty() && "TurboModule name must not be empty");
312-
313-
for (const auto& turboModuleProvider : turboModuleProviders) {
314-
if (turboModuleProvider) {
315-
if (auto turboModule = turboModuleProvider(name, jsInvoker)) {
316-
return turboModule;
317-
}
318-
}
319-
}
320-
321-
if (auto turboModule =
322-
DefaultTurboModules::getTurboModule(name, jsInvoker)) {
323-
return turboModule;
324-
}
325-
326-
if (name == AnimatedModule::kModuleName) {
327-
return std::make_shared<AnimatedModule>(
328-
jsInvoker, animatedNodesManagerProvider);
329-
} else if (name == AppStateModule::kModuleName) {
330-
return std::make_shared<AppStateModule>(jsInvoker);
331-
} else if (name == DeviceInfoModule::kModuleName) {
332-
return std::make_shared<DeviceInfoModule>(jsInvoker);
333-
} else if (
334-
devUIDelegate != nullptr &&
335-
name == DevLoadingViewModule::kModuleName) {
336-
return std::make_shared<DevLoadingViewModule>(
337-
jsInvoker, devUIDelegate);
338-
} else if (
339-
devServerHelper && name == DevSettingsModule::kModuleName) {
340-
return std::make_shared<DevSettingsModule>(
341-
jsInvoker, devServerHelper, liveReloadCallback);
342-
} else if (name == PlatformConstantsModule::kModuleName) {
343-
return std::make_shared<PlatformConstantsModule>(jsInvoker);
344-
} else if (name == ImageLoaderModule::kModuleName) {
345-
return std::make_shared<ImageLoaderModule>(jsInvoker);
346-
} else if (name == SourceCodeModule::kModuleName) {
347-
return std::make_shared<SourceCodeModule>(
348-
jsInvoker, devServerHelper);
349-
} else if (name == WebSocketModule::kModuleName) {
350-
return std::make_shared<WebSocketModule>(
351-
jsInvoker, webSocketClientFactory);
352-
} else if (name == NativeExceptionsManager::kModuleName) {
353-
return std::make_shared<NativeExceptionsManager>(
354-
onJsError, jsInvoker);
355-
} else if (name == NativePerformance::kModuleName) {
356-
return std::make_shared<NativePerformance>(jsInvoker);
357-
} else if (name == NativeIntersectionObserver::kModuleName) {
358-
return std::make_shared<NativeIntersectionObserver>(jsInvoker);
359-
} else if (name == NativeMutationObserver::kModuleName) {
360-
return std::make_shared<NativeMutationObserver>(jsInvoker);
361-
} else if (name == NetworkingModule::kModuleName) {
362-
return std::make_shared<NetworkingModule>(
363-
jsInvoker, httpClientFactory);
364-
} else if (name == LogBoxModule::kModuleName) {
365-
if (logBoxSurfaceDelegate) {
366-
return std::make_shared<LogBoxModule>(
367-
jsInvoker, logBoxSurfaceDelegate);
368-
}
369-
}
370-
371-
LOG_WARNING_ONCE << "Failed to load TurboModule: " << name;
372-
return nullptr;
373-
};
374-
375285
// Set up TurboModules
376-
TurboModuleBinding::install(runtime, turboModuleProvider);
286+
TurboModuleBinding::install(runtime, std::move(turboModuleManager));
287+
377288
if (bindingsInstallFunc) {
378289
bindingsInstallFunc(runtime);
379290
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "TurboModuleManager.h"
9+
10+
#include <react/coremodules/AppStateModule.h>
11+
#include <react/coremodules/DeviceInfoModule.h>
12+
#include <react/coremodules/PlatformConstantsModule.h>
13+
#include <react/debug/react_native_assert.h>
14+
#include <react/devsupport/DevLoadingViewModule.h>
15+
#include <react/devsupport/DevSettingsModule.h>
16+
#include <react/devsupport/LogBoxModule.h>
17+
#include <react/devsupport/SourceCodeModule.h>
18+
#include <react/io/ImageLoaderModule.h>
19+
#include <react/io/NetworkingModule.h>
20+
#include <react/io/WebSocketModule.h>
21+
#include <react/logging/NativeExceptionsManager.h>
22+
#include <react/nativemodule/defaults/DefaultTurboModules.h>
23+
#include <react/nativemodule/intersectionobserver/NativeIntersectionObserver.h>
24+
#include <react/nativemodule/mutationobserver/NativeMutationObserver.h>
25+
#include <react/nativemodule/webperformance/NativePerformance.h>
26+
#include <react/renderer/animated/AnimatedModule.h>
27+
28+
using namespace facebook::react;
29+
30+
TurboModuleManager::TurboModuleManager(
31+
TurboModuleProviders turboModuleProviders,
32+
std::shared_ptr<CallInvoker> jsInvoker,
33+
JsErrorHandler::OnJsError onJsError,
34+
std::shared_ptr<NativeAnimatedNodesManagerProvider>
35+
animatedNodesManagerProvider,
36+
std::shared_ptr<DevServerHelper> devServerHelper,
37+
std::shared_ptr<IDevUIDelegate> devUIDelegate,
38+
std::shared_ptr<SurfaceDelegate> logBoxSurfaceDelegate,
39+
HttpClientFactory httpClientFactory,
40+
WebSocketClientFactory webSocketClientFactory,
41+
std::function<void()> liveReloadCallback)
42+
: turboModuleProviders_(std::move(turboModuleProviders)),
43+
jsInvoker_(std::move(jsInvoker)),
44+
onJsError_(std::move(onJsError)),
45+
animatedNodesManagerProvider_(std::move(animatedNodesManagerProvider)),
46+
devServerHelper_(std::move(devServerHelper)),
47+
devUIDelegate_(std::move(devUIDelegate)),
48+
logBoxSurfaceDelegate_(std::move(logBoxSurfaceDelegate)),
49+
httpClientFactory_(std::move(httpClientFactory)),
50+
webSocketClientFactory_(std::move(webSocketClientFactory)),
51+
liveReloadCallback_(std::move(liveReloadCallback)) {}
52+
53+
std::shared_ptr<TurboModule> TurboModuleManager::operator()(
54+
const std::string& name) const {
55+
react_native_assert(!name.empty() && "TurboModule name must not be empty");
56+
57+
for (const auto& turboModuleProvider : turboModuleProviders_) {
58+
if (turboModuleProvider) {
59+
if (auto turboModule = turboModuleProvider(name, jsInvoker_)) {
60+
return turboModule;
61+
}
62+
}
63+
}
64+
65+
if (auto turboModule =
66+
DefaultTurboModules::getTurboModule(name, jsInvoker_)) {
67+
return turboModule;
68+
}
69+
70+
if (name == AnimatedModule::kModuleName) {
71+
return std::make_shared<AnimatedModule>(
72+
jsInvoker_, animatedNodesManagerProvider_);
73+
} else if (name == AppStateModule::kModuleName) {
74+
return std::make_shared<AppStateModule>(jsInvoker_);
75+
} else if (name == DeviceInfoModule::kModuleName) {
76+
return std::make_shared<DeviceInfoModule>(jsInvoker_);
77+
} else if (
78+
devUIDelegate_ != nullptr && name == DevLoadingViewModule::kModuleName) {
79+
return std::make_shared<DevLoadingViewModule>(jsInvoker_, devUIDelegate_);
80+
} else if (devServerHelper_ && name == DevSettingsModule::kModuleName) {
81+
return std::make_shared<DevSettingsModule>(
82+
jsInvoker_, devServerHelper_, liveReloadCallback_);
83+
} else if (name == PlatformConstantsModule::kModuleName) {
84+
return std::make_shared<PlatformConstantsModule>(jsInvoker_);
85+
} else if (name == ImageLoaderModule::kModuleName) {
86+
return std::make_shared<ImageLoaderModule>(jsInvoker_);
87+
} else if (name == SourceCodeModule::kModuleName) {
88+
return std::make_shared<SourceCodeModule>(jsInvoker_, devServerHelper_);
89+
} else if (name == WebSocketModule::kModuleName) {
90+
return std::make_shared<WebSocketModule>(
91+
jsInvoker_, webSocketClientFactory_);
92+
} else if (name == NativeExceptionsManager::kModuleName) {
93+
return std::make_shared<NativeExceptionsManager>(onJsError_, jsInvoker_);
94+
} else if (name == NativePerformance::kModuleName) {
95+
return std::make_shared<NativePerformance>(jsInvoker_);
96+
} else if (name == NativeIntersectionObserver::kModuleName) {
97+
return std::make_shared<NativeIntersectionObserver>(jsInvoker_);
98+
} else if (name == NativeMutationObserver::kModuleName) {
99+
return std::make_shared<NativeMutationObserver>(jsInvoker_);
100+
} else if (name == NetworkingModule::kModuleName) {
101+
return std::make_shared<NetworkingModule>(jsInvoker_, httpClientFactory_);
102+
} else if (name == LogBoxModule::kModuleName) {
103+
if (logBoxSurfaceDelegate_) {
104+
return std::make_shared<LogBoxModule>(jsInvoker_, logBoxSurfaceDelegate_);
105+
}
106+
}
107+
108+
LOG(WARNING) << "Failed to load TurboModule: " << name;
109+
return nullptr;
110+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <jserrorhandler/JsErrorHandler.h>
11+
#include <react/http/IHttpClient.h>
12+
#include <react/http/IWebSocketClient.h>
13+
#include <react/nativemodule/TurboModuleProvider.h>
14+
#include <react/renderer/scheduler/SurfaceDelegate.h>
15+
16+
namespace facebook::react {
17+
18+
class CallInvoker;
19+
class DevServerHelper;
20+
class NativeAnimatedNodesManagerProvider;
21+
class SurfaceDelegate;
22+
struct IDevUIDelegate;
23+
24+
class TurboModuleManager final {
25+
public:
26+
TurboModuleManager(
27+
TurboModuleProviders turboModuleProviders,
28+
std::shared_ptr<CallInvoker> jsInvoker,
29+
JsErrorHandler::OnJsError onJsError,
30+
std::shared_ptr<NativeAnimatedNodesManagerProvider> animatedNodesManagerProvider = nullptr,
31+
std::shared_ptr<DevServerHelper> devServerHelper = nullptr,
32+
std::shared_ptr<IDevUIDelegate> devUIDelegate = nullptr,
33+
std::shared_ptr<SurfaceDelegate> logBoxSurfaceDelegate = nullptr,
34+
HttpClientFactory httpClientFactory = nullptr,
35+
WebSocketClientFactory webSocketClientFactory = nullptr,
36+
std::function<void()> liveReloadCallback = nullptr);
37+
38+
std::shared_ptr<TurboModule> operator()(const std::string &name) const;
39+
40+
private:
41+
TurboModuleProviders turboModuleProviders_;
42+
std::shared_ptr<CallInvoker> jsInvoker_;
43+
JsErrorHandler::OnJsError onJsError_;
44+
std::shared_ptr<NativeAnimatedNodesManagerProvider> animatedNodesManagerProvider_;
45+
std::shared_ptr<DevServerHelper> devServerHelper_;
46+
std::shared_ptr<IDevUIDelegate> devUIDelegate_;
47+
std::shared_ptr<SurfaceDelegate> logBoxSurfaceDelegate_;
48+
HttpClientFactory httpClientFactory_;
49+
WebSocketClientFactory webSocketClientFactory_;
50+
std::function<void()> liveReloadCallback_;
51+
};
52+
53+
} // namespace facebook::react

0 commit comments

Comments
 (0)