Skip to content

Commit 5b3a1a4

Browse files
javachemeta-codesync[bot]
authored andcommitted
Return actual loaded bundle URL from SourceCodeModule (#56698)
Summary: Pull Request resolved: #56698 The C++ `SourceCodeModule` (ReactCxxPlatform) always derived `scriptURL` from `DevServerHelper::getBundleUrl()`, coupling the module to dev infrastructure. This diff replaces that dependency with a simple `std::string` parameter passed at construction time. `ReactHost` and `FoxReactHost` store the source URL in a `shared_ptr<string>` that is updated on each successful dev-server bundle load. When `SourceCodeModule` is instantiated (lazily, after the bundle is loaded), the TurboModule provider dereferences the current value and passes it through as a plain string. For file-based bundle loading the URL is cleared, matching the previous behavior. This decouples `SourceCodeModule` from `DevServerHelper` without introducing lambdas, context container lookups, or shared mutable state in the module itself. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D102669868 fbshipit-source-id: 02aec01d22e6ea429431e079ea29bf63246530ae
1 parent c4f94c7 commit 5b3a1a4

6 files changed

Lines changed: 18 additions & 20 deletions

File tree

packages/react-native/ReactCxxPlatform/react/devsupport/SourceCodeModule.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,10 @@
77

88
#include "SourceCodeModule.h"
99

10-
#include <react/devsupport/DevServerHelper.h>
11-
#include <string>
12-
1310
namespace facebook::react {
1411

1512
SourceCodeConstants SourceCodeModule::getConstants(jsi::Runtime& /*rt*/) {
16-
std::string scriptURL;
17-
if (auto devServerHelper = devServerHelper_.lock()) {
18-
scriptURL = devServerHelper->getBundleUrl();
19-
}
20-
return SourceCodeConstants{.scriptURL = scriptURL};
13+
return SourceCodeConstants{.scriptURL = sourceURL_};
2114
}
2215

2316
} // namespace facebook::react

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,22 @@
1313

1414
namespace facebook::react {
1515

16-
class DevServerHelper;
17-
1816
using SourceCodeConstants = NativeSourceCodeSourceCodeConstants<std::string>;
1917

2018
template <>
2119
struct Bridging<SourceCodeConstants> : NativeSourceCodeSourceCodeConstantsBridging<SourceCodeConstants> {};
2220

2321
class SourceCodeModule : public NativeSourceCodeCxxSpec<SourceCodeModule> {
2422
public:
25-
explicit SourceCodeModule(
26-
std::shared_ptr<CallInvoker> jsInvoker,
27-
std::shared_ptr<DevServerHelper> devServerHelper = nullptr)
28-
: NativeSourceCodeCxxSpec(jsInvoker), devServerHelper_(devServerHelper)
23+
explicit SourceCodeModule(std::shared_ptr<CallInvoker> jsInvoker, std::string sourceURL = "")
24+
: NativeSourceCodeCxxSpec(jsInvoker), sourceURL_(std::move(sourceURL))
2925
{
3026
}
3127

3228
SourceCodeConstants getConstants(jsi::Runtime &rt);
3329

3430
private:
35-
std::weak_ptr<DevServerHelper> devServerHelper_;
31+
std::string sourceURL_;
3632
};
3733

3834
} // namespace facebook::react

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ ReactCxxTurboModuleProvider::ReactCxxTurboModuleProvider(
3838
std::shared_ptr<SurfaceDelegate> logBoxSurfaceDelegate,
3939
HttpClientFactory httpClientFactory,
4040
WebSocketClientFactory webSocketClientFactory,
41-
std::function<void()> liveReloadCallback)
41+
std::function<void()> liveReloadCallback,
42+
std::shared_ptr<std::string> sourceURL)
4243
: turboModuleProviders_(std::move(turboModuleProviders)),
4344
jsInvoker_(std::move(jsInvoker)),
4445
onJsError_(std::move(onJsError)),
@@ -48,7 +49,8 @@ ReactCxxTurboModuleProvider::ReactCxxTurboModuleProvider(
4849
logBoxSurfaceDelegate_(std::move(logBoxSurfaceDelegate)),
4950
httpClientFactory_(std::move(httpClientFactory)),
5051
webSocketClientFactory_(std::move(webSocketClientFactory)),
51-
liveReloadCallback_(std::move(liveReloadCallback)) {}
52+
liveReloadCallback_(std::move(liveReloadCallback)),
53+
sourceURL_(std::move(sourceURL)) {}
5254

5355
std::shared_ptr<TurboModule> ReactCxxTurboModuleProvider::operator()(
5456
const std::string& name) const {
@@ -82,7 +84,8 @@ std::shared_ptr<TurboModule> ReactCxxTurboModuleProvider::operator()(
8284
} else if (name == ImageLoaderModule::kModuleName) {
8385
return std::make_shared<ImageLoaderModule>(jsInvoker_);
8486
} else if (name == SourceCodeModule::kModuleName) {
85-
return std::make_shared<SourceCodeModule>(jsInvoker_, devServerHelper_);
87+
return std::make_shared<SourceCodeModule>(
88+
jsInvoker_, sourceURL_ ? *sourceURL_ : "");
8689
} else if (name == WebSocketModule::kModuleName) {
8790
return std::make_shared<WebSocketModule>(
8891
jsInvoker_, webSocketClientFactory_);

packages/react-native/ReactCxxPlatform/react/runtime/ReactCxxTurboModuleProvider.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class ReactCxxTurboModuleProvider final {
3333
std::shared_ptr<SurfaceDelegate> logBoxSurfaceDelegate = nullptr,
3434
HttpClientFactory httpClientFactory = nullptr,
3535
WebSocketClientFactory webSocketClientFactory = nullptr,
36-
std::function<void()> liveReloadCallback = nullptr);
36+
std::function<void()> liveReloadCallback = nullptr,
37+
std::shared_ptr<std::string> sourceURL = nullptr);
3738

3839
std::shared_ptr<TurboModule> operator()(const std::string &name) const;
3940

@@ -48,6 +49,7 @@ class ReactCxxTurboModuleProvider final {
4849
HttpClientFactory httpClientFactory_;
4950
WebSocketClientFactory webSocketClientFactory_;
5051
std::function<void()> liveReloadCallback_;
52+
std::shared_ptr<std::string> sourceURL_;
5153
};
5254

5355
} // namespace facebook::react

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ void ReactHost::createReactInstance() {
261261
reactInstanceData_->logBoxSurfaceDelegate,
262262
httpClientFactory,
263263
webSocketClientFactory,
264-
std::move(liveReloadCallback));
264+
std::move(liveReloadCallback),
265+
sourceURL_);
265266

266267
reactInstance_->initializeRuntime(
267268
{
@@ -392,6 +393,7 @@ bool ReactHost::loadScriptFromDevServer() {
392393
})
393394
.get();
394395
auto script = std::make_unique<JSBigStdString>(std::move(response));
396+
*sourceURL_ = bundleUrl;
395397
reactInstance_->loadScript(std::move(script), bundleUrl);
396398
devServerHelper_->setupHMRClient();
397399
return true;
@@ -408,6 +410,7 @@ bool ReactHost::loadScriptFromBundlePath(const std::string& bundlePath) {
408410
try {
409411
LOG(INFO) << "Loading JS bundle from bundle path: " << bundlePath;
410412
auto script = ResourceLoader::getFileContents(bundlePath);
413+
*sourceURL_ = "";
411414
reactInstance_->loadScript(std::move(script), bundlePath);
412415
LOG(INFO) << "Loaded JS bundle from bundle path: " << bundlePath;
413416
return true;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class ReactHost {
110110
std::unique_ptr<SurfaceManager> surfaceManager_;
111111

112112
std::shared_ptr<DevServerHelper> devServerHelper_;
113+
std::shared_ptr<std::string> sourceURL_ = std::make_shared<std::string>();
113114
std::shared_ptr<Inspector> inspector_;
114115
std::unique_ptr<PackagerConnection> packagerConnection_;
115116

0 commit comments

Comments
 (0)