Skip to content
Closed
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
Expand Up @@ -7,17 +7,10 @@

#include "SourceCodeModule.h"

#include <react/devsupport/DevServerHelper.h>
#include <string>

namespace facebook::react {

SourceCodeConstants SourceCodeModule::getConstants(jsi::Runtime& /*rt*/) {
std::string scriptURL;
if (auto devServerHelper = devServerHelper_.lock()) {
scriptURL = devServerHelper->getBundleUrl();
}
return SourceCodeConstants{.scriptURL = scriptURL};
return SourceCodeConstants{.scriptURL = sourceURL_};
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,22 @@

namespace facebook::react {

class DevServerHelper;

using SourceCodeConstants = NativeSourceCodeSourceCodeConstants<std::string>;

template <>
struct Bridging<SourceCodeConstants> : NativeSourceCodeSourceCodeConstantsBridging<SourceCodeConstants> {};

class SourceCodeModule : public NativeSourceCodeCxxSpec<SourceCodeModule> {
public:
explicit SourceCodeModule(
std::shared_ptr<CallInvoker> jsInvoker,
std::shared_ptr<DevServerHelper> devServerHelper = nullptr)
: NativeSourceCodeCxxSpec(jsInvoker), devServerHelper_(devServerHelper)
explicit SourceCodeModule(std::shared_ptr<CallInvoker> jsInvoker, std::string sourceURL = "")
: NativeSourceCodeCxxSpec(jsInvoker), sourceURL_(std::move(sourceURL))
{
}

SourceCodeConstants getConstants(jsi::Runtime &rt);

private:
std::weak_ptr<DevServerHelper> devServerHelper_;
std::string sourceURL_;
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ ReactCxxTurboModuleProvider::ReactCxxTurboModuleProvider(
std::shared_ptr<SurfaceDelegate> logBoxSurfaceDelegate,
HttpClientFactory httpClientFactory,
WebSocketClientFactory webSocketClientFactory,
std::function<void()> liveReloadCallback)
std::function<void()> liveReloadCallback,
std::shared_ptr<std::string> sourceURL)
: turboModuleProviders_(std::move(turboModuleProviders)),
jsInvoker_(std::move(jsInvoker)),
onJsError_(std::move(onJsError)),
Expand All @@ -48,7 +49,8 @@ ReactCxxTurboModuleProvider::ReactCxxTurboModuleProvider(
logBoxSurfaceDelegate_(std::move(logBoxSurfaceDelegate)),
httpClientFactory_(std::move(httpClientFactory)),
webSocketClientFactory_(std::move(webSocketClientFactory)),
liveReloadCallback_(std::move(liveReloadCallback)) {}
liveReloadCallback_(std::move(liveReloadCallback)),
sourceURL_(std::move(sourceURL)) {}

std::shared_ptr<TurboModule> ReactCxxTurboModuleProvider::operator()(
const std::string& name) const {
Expand Down Expand Up @@ -82,7 +84,8 @@ std::shared_ptr<TurboModule> ReactCxxTurboModuleProvider::operator()(
} else if (name == ImageLoaderModule::kModuleName) {
return std::make_shared<ImageLoaderModule>(jsInvoker_);
} else if (name == SourceCodeModule::kModuleName) {
return std::make_shared<SourceCodeModule>(jsInvoker_, devServerHelper_);
return std::make_shared<SourceCodeModule>(
jsInvoker_, sourceURL_ ? *sourceURL_ : "");
} else if (name == WebSocketModule::kModuleName) {
return std::make_shared<WebSocketModule>(
jsInvoker_, webSocketClientFactory_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class ReactCxxTurboModuleProvider final {
std::shared_ptr<SurfaceDelegate> logBoxSurfaceDelegate = nullptr,
HttpClientFactory httpClientFactory = nullptr,
WebSocketClientFactory webSocketClientFactory = nullptr,
std::function<void()> liveReloadCallback = nullptr);
std::function<void()> liveReloadCallback = nullptr,
std::shared_ptr<std::string> sourceURL = nullptr);

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

Expand All @@ -48,6 +49,7 @@ class ReactCxxTurboModuleProvider final {
HttpClientFactory httpClientFactory_;
WebSocketClientFactory webSocketClientFactory_;
std::function<void()> liveReloadCallback_;
std::shared_ptr<std::string> sourceURL_;
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ void ReactHost::createReactInstance() {
reactInstanceData_->logBoxSurfaceDelegate,
httpClientFactory,
webSocketClientFactory,
std::move(liveReloadCallback));
std::move(liveReloadCallback),
sourceURL_);

reactInstance_->initializeRuntime(
{
Expand Down Expand Up @@ -392,6 +393,7 @@ bool ReactHost::loadScriptFromDevServer() {
})
.get();
auto script = std::make_unique<JSBigStdString>(std::move(response));
*sourceURL_ = bundleUrl;
reactInstance_->loadScript(std::move(script), bundleUrl);
devServerHelper_->setupHMRClient();
return true;
Expand All @@ -408,6 +410,7 @@ bool ReactHost::loadScriptFromBundlePath(const std::string& bundlePath) {
try {
LOG(INFO) << "Loading JS bundle from bundle path: " << bundlePath;
auto script = ResourceLoader::getFileContents(bundlePath);
*sourceURL_ = "";
reactInstance_->loadScript(std::move(script), bundlePath);
LOG(INFO) << "Loaded JS bundle from bundle path: " << bundlePath;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class ReactHost {
std::unique_ptr<SurfaceManager> surfaceManager_;

std::shared_ptr<DevServerHelper> devServerHelper_;
std::shared_ptr<std::string> sourceURL_ = std::make_shared<std::string>();
std::shared_ptr<Inspector> inspector_;
std::unique_ptr<PackagerConnection> packagerConnection_;

Expand Down
Loading