Skip to content

Commit

Permalink
xplat: pass through config object to fabric UIManager layer
Browse files Browse the repository at this point in the history
Summary: For configuration purpose, pass down config object from the hosting app and use it in UITemplateProcessor.

Reviewed By: sahrens

Differential Revision: D13290322

fbshipit-source-id: 8bb6d7f5a3f977b873e548e15603259876b46dc8
  • Loading branch information
fkgozali authored and facebook-github-bot committed Dec 5, 2018
1 parent 3b6f229 commit 4f9a3bc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
2 changes: 2 additions & 0 deletions ReactCommon/fabric/uimanager/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ rn_xplat_cxx_library(
"xplat//jsi:JSIDynamic",
"xplat//jsi:jsi",
"xplat//third-party/glog:glog",
react_native_xplat_target("config:config"),
react_native_xplat_target("fabric/components/root:root"),
react_native_xplat_target("fabric/components/view:view"),
react_native_xplat_target("fabric/mounting:mounting"),
Expand All @@ -83,6 +84,7 @@ fb_xplat_cxx_test(
"xplat//folly:molly",
"xplat//third-party/gmock:gtest",
":uimanager",
react_native_xplat_target("config:config"),
react_native_xplat_target("fabric/components/activityindicator:activityindicator"),
react_native_xplat_target("fabric/components/image:image"),
react_native_xplat_target("fabric/components/root:root"),
Expand Down
9 changes: 6 additions & 3 deletions ReactCommon/fabric/uimanager/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ Scheduler::Scheduler(const SharedContextContainer &contextContainer)
runtimeExecutor_ =
contextContainer->getInstance<RuntimeExecutor>("runtime-executor");

reactNativeConfig_ =
contextContainer->getInstance<std::shared_ptr<const ReactNativeConfig>>();

auto uiManager = std::make_unique<UIManager>();
auto &uiManagerRef = *uiManager;
uiManagerBinding_ =
std::make_shared<UIManagerBinding>(std::move(uiManager));
uiManagerBinding_ = std::make_shared<UIManagerBinding>(std::move(uiManager));

auto eventPipe = [uiManagerBinding = uiManagerBinding_.get()](
jsi::Runtime &runtime,
Expand Down Expand Up @@ -93,7 +95,8 @@ void Scheduler::renderTemplateToSurface(
surfaceId,
folly::dynamic::object(),
*componentDescriptorRegistry_,
nMR);
nMR,
reactNativeConfig_);

shadowTreeRegistry_.visit(surfaceId, [=](const ShadowTree &shadowTree) {
shadowTree.complete(
Expand Down
2 changes: 2 additions & 0 deletions ReactCommon/fabric/uimanager/Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>
#include <mutex>

#include <react/config/ReactNativeConfig.h>
#include <react/core/ComponentDescriptor.h>
#include <react/core/LayoutConstraints.h>
#include <react/uimanager/ComponentDescriptorRegistry.h>
Expand Down Expand Up @@ -94,6 +95,7 @@ class Scheduler final : public UIManagerDelegate, public ShadowTreeDelegate {
SharedContextContainer contextContainer_;
RuntimeExecutor runtimeExecutor_;
std::shared_ptr<UIManagerBinding> uiManagerBinding_;
std::shared_ptr<const ReactNativeConfig> reactNativeConfig_;
};

} // namespace react
Expand Down
17 changes: 10 additions & 7 deletions ReactCommon/fabric/uimanager/UITemplateProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ SharedShadowNode UITemplateProcessor::runCommand(
std::vector<SharedShadowNode> &nodes,
std::vector<folly::dynamic> &registers,
const ComponentDescriptorRegistry &componentDescriptorRegistry,
const NativeModuleRegistry &nativeModuleRegistry) {
const NativeModuleRegistry &nativeModuleRegistry,
const std::shared_ptr<const ReactNativeConfig> reactNativeConfig) {
const std::string &opcode = command[0].asString();
const int tagOffset = 420000;
// TODO: change to integer codes and a switch statement
Expand All @@ -61,9 +62,8 @@ SharedShadowNode UITemplateProcessor::runCommand(
return nodes[command[1].asInt()];
} else if (opcode == "loadNativeBool") {
int registerNumber = command[1].asInt();
const folly::dynamic &value = nativeModuleRegistry.call(
command[2].asString(), command[3].asString(), command[4]);
registers[registerNumber] = value.asBool();
std::string param = command[4][0].asString();
registers[registerNumber] = reactNativeConfig->getBool(param);
} else if (opcode == "conditional") {
int registerNumber = command[1].asInt();
auto conditionDynamic = registers[registerNumber];
Expand All @@ -89,7 +89,8 @@ SharedShadowNode UITemplateProcessor::runCommand(
nodes,
registers,
componentDescriptorRegistry,
nativeModuleRegistry);
nativeModuleRegistry,
reactNativeConfig);
}
} else {
throw std::runtime_error("Unsupported opcode: " + command[0].asString());
Expand All @@ -102,7 +103,8 @@ SharedShadowNode UITemplateProcessor::buildShadowTree(
Tag rootTag,
const folly::dynamic &params,
const ComponentDescriptorRegistry &componentDescriptorRegistry,
const NativeModuleRegistry &nativeModuleRegistry) {
const NativeModuleRegistry &nativeModuleRegistry,
const std::shared_ptr<const ReactNativeConfig> reactNativeConfig) {
LOG(INFO)
<< "(strt) UITemplateProcessor inject hardcoded 'server rendered' view tree";

Expand All @@ -129,7 +131,8 @@ SharedShadowNode UITemplateProcessor::buildShadowTree(
nodes,
registers,
componentDescriptorRegistry,
nativeModuleRegistry);
nativeModuleRegistry,
reactNativeConfig);
if (ret != nullptr) {
return ret;
}
Expand Down
7 changes: 5 additions & 2 deletions ReactCommon/fabric/uimanager/UITemplateProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <folly/dynamic.h>

#include <react/config/ReactNativeConfig.h>
#include <react/core/ShadowNode.h>
#include <react/uimanager/ComponentDescriptorRegistry.h>
#include <react/uimanager/UIManagerDelegate.h>
Expand Down Expand Up @@ -48,7 +49,8 @@ class UITemplateProcessor {
int rootTag,
const folly::dynamic &params,
const ComponentDescriptorRegistry &componentDescriptorRegistry,
const NativeModuleRegistry &nativeModuleRegistry);
const NativeModuleRegistry &nativeModuleRegistry,
const std::shared_ptr<const ReactNativeConfig> reactNativeConfig);

private:
static SharedShadowNode runCommand(
Expand All @@ -57,7 +59,8 @@ class UITemplateProcessor {
std::vector<SharedShadowNode> &nodes,
std::vector<folly::dynamic> &registers,
const ComponentDescriptorRegistry &componentDescriptorRegistry,
const NativeModuleRegistry &nativeModuleRegistry);
const NativeModuleRegistry &nativeModuleRegistry,
const std::shared_ptr<const ReactNativeConfig> reactNativeConfig);
};
} // namespace react
} // namespace facebook
33 changes: 30 additions & 3 deletions ReactCommon/fabric/uimanager/tests/UITemplateProcessorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using namespace facebook::react;
#include <react/components/text/RawTextComponentDescriptor.h>
#include <react/components/text/TextComponentDescriptor.h>
#include <react/components/view/ViewComponentDescriptor.h>
#include <react/config/ReactNativeConfig.h>
#include <react/uimanager/ComponentDescriptorRegistry.h>

namespace facebook {
Expand Down Expand Up @@ -63,6 +64,29 @@ NativeModuleRegistry buildNativeModuleRegistry() {
return nMR;
}

class MockReactNativeConfig : public ReactNativeConfig {
public:
MockReactNativeConfig() {}
bool getBool(const std::string &param) const override {
return mockSimpleTestValue_;
}

std::string getString(const std::string &param) const override {
return "";
}

int64_t getInt64(const std::string &param) const override {
return 0;
}

double getDouble(const std::string &param) const override {
return 0.0;
}
};

std::shared_ptr<const ReactNativeConfig> mockReactNativeConfig_ =
std::make_shared<const MockReactNativeConfig>();

} // namespace react
} // namespace facebook

Expand All @@ -85,7 +109,8 @@ TEST(UITemplateProcessorTest, testSimpleBytecode) {
surfaceId,
folly::dynamic::object(),
*componentDescriptorRegistry,
nativeModuleRegistry);
nativeModuleRegistry,
mockReactNativeConfig_);
LOG(INFO) << std::endl << root1->getDebugDescription();
auto props1 = std::dynamic_pointer_cast<const ViewProps>(root1->getProps());
ASSERT_NEAR(props1->opacity, 0.5, 0.001);
Expand Down Expand Up @@ -120,7 +145,8 @@ TEST(UITemplateProcessorTest, testConditionalBytecode) {
surfaceId,
folly::dynamic::object(),
*componentDescriptorRegistry,
nativeModuleRegistry);
nativeModuleRegistry,
mockReactNativeConfig_);
LOG(INFO) << std::endl << root1->getDebugDescription();
auto props1 = std::dynamic_pointer_cast<const ViewProps>(root1->getProps());
ASSERT_STREQ(props1->testId.c_str(), "root");
Expand All @@ -137,7 +163,8 @@ TEST(UITemplateProcessorTest, testConditionalBytecode) {
surfaceId,
folly::dynamic::object(),
*componentDescriptorRegistry,
nativeModuleRegistry);
nativeModuleRegistry,
mockReactNativeConfig_);
auto child_props2 = std::dynamic_pointer_cast<const ViewProps>(
root2->getChildren().at(0)->getProps());
ASSERT_STREQ(child_props2->testId.c_str(), "cond_false");
Expand Down

0 comments on commit 4f9a3bc

Please sign in to comment.