Skip to content

Commit b30ff98

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Simplify construction of UIManagerBinding
Summary: changelog: [internal] Provide `UIManager` to `UIManagerBinding` in constructor to make the API safer. Reviewed By: philIip Differential Revision: D32668892 fbshipit-source-id: a15cd295196a60c3f46997e59c05c4f90503e18d
1 parent 34efaab commit b30ff98

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

ReactCommon/react/renderer/scheduler/Scheduler.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,11 @@ Scheduler::Scheduler(
8686
uiManager->setDelegate(this);
8787
uiManager->setComponentDescriptorRegistry(componentDescriptorRegistry_);
8888

89-
runtimeExecutor_([uiManager,
90-
runtimeExecutor = runtimeExecutor_](jsi::Runtime &runtime) {
91-
auto uiManagerBinding =
92-
UIManagerBinding::createAndInstallIfNeeded(runtime, runtimeExecutor);
93-
uiManagerBinding->attach(uiManager);
94-
});
89+
runtimeExecutor_(
90+
[uiManager, runtimeExecutor = runtimeExecutor_](jsi::Runtime &runtime) {
91+
UIManagerBinding::createAndInstallIfNeeded(
92+
runtime, runtimeExecutor, uiManager);
93+
});
9594

9695
auto componentDescriptorRegistryKey =
9796
"ComponentDescriptorRegistry_DO_NOT_USE_PRETTY_PLEASE";

ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp

+9-15
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,23 @@ static bool checkGetCallableModuleIsActive(jsi::Runtime &runtime) {
5858
return true;
5959
}
6060

61-
std::shared_ptr<UIManagerBinding> UIManagerBinding::createAndInstallIfNeeded(
61+
void UIManagerBinding::createAndInstallIfNeeded(
6262
jsi::Runtime &runtime,
63-
RuntimeExecutor const &runtimeExecutor) {
63+
RuntimeExecutor const &runtimeExecutor,
64+
std::shared_ptr<UIManager> const &uiManager) {
6465
auto uiManagerModuleName = "nativeFabricUIManager";
6566

6667
auto uiManagerValue =
6768
runtime.global().getProperty(runtime, uiManagerModuleName);
6869
if (uiManagerValue.isUndefined()) {
6970
// The global namespace does not have an instance of the binding;
7071
// we need to create, install and return it.
71-
auto uiManagerBinding = std::make_shared<UIManagerBinding>(runtimeExecutor);
72+
auto uiManagerBinding =
73+
std::make_shared<UIManagerBinding>(uiManager, runtimeExecutor);
7274
auto object = jsi::Object::createFromHostObject(runtime, uiManagerBinding);
7375
runtime.global().setProperty(
7476
runtime, uiManagerModuleName, std::move(object));
75-
return uiManagerBinding;
7677
}
77-
78-
// The global namespace already has an instance of the binding;
79-
// we need to return that.
80-
auto uiManagerObject = uiManagerValue.asObject(runtime);
81-
return uiManagerObject.getHostObject<UIManagerBinding>(runtime);
8278
}
8379

8480
std::shared_ptr<UIManagerBinding> UIManagerBinding::getBinding(
@@ -95,18 +91,16 @@ std::shared_ptr<UIManagerBinding> UIManagerBinding::getBinding(
9591
return uiManagerObject.getHostObject<UIManagerBinding>(runtime);
9692
}
9793

98-
UIManagerBinding::UIManagerBinding(RuntimeExecutor const &runtimeExecutor)
99-
: runtimeExecutor_(runtimeExecutor) {}
94+
UIManagerBinding::UIManagerBinding(
95+
std::shared_ptr<UIManager> const &uiManager,
96+
RuntimeExecutor const &runtimeExecutor)
97+
: uiManager_(uiManager), runtimeExecutor_(runtimeExecutor) {}
10098

10199
UIManagerBinding::~UIManagerBinding() {
102100
LOG(WARNING) << "UIManagerBinding::~UIManagerBinding() was called (address: "
103101
<< this << ").";
104102
}
105103

106-
void UIManagerBinding::attach(std::shared_ptr<UIManager> const &uiManager) {
107-
uiManager_ = uiManager;
108-
}
109-
110104
static jsi::Value callMethodOfModule(
111105
jsi::Runtime &runtime,
112106
std::string const &moduleName,

ReactCommon/react/renderer/uimanager/UIManagerBinding.h

+6-12
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,25 @@ class UIManagerBinding : public jsi::HostObject {
2424
/*
2525
* Installs UIManagerBinding into JavaScript runtime if needed.
2626
* Creates and sets `UIManagerBinding` into the global namespace.
27-
* In case if the global namespace already has a `UIManagerBinding` installed,
28-
* returns that.
2927
* Thread synchronization must be enforced externally.
3028
*/
31-
static std::shared_ptr<UIManagerBinding> createAndInstallIfNeeded(
29+
static void createAndInstallIfNeeded(
3230
jsi::Runtime &runtime,
33-
RuntimeExecutor const &runtimeExecutor);
31+
RuntimeExecutor const &runtimeExecutor,
32+
std::shared_ptr<UIManager> const &uiManager);
3433

3534
/*
3635
* Returns a pointer to UIManagerBinding previously installed into a runtime.
3736
* Thread synchronization must be enforced externally.
3837
*/
3938
static std::shared_ptr<UIManagerBinding> getBinding(jsi::Runtime &runtime);
4039

41-
UIManagerBinding(RuntimeExecutor const &runtimeExecutor);
40+
UIManagerBinding(
41+
std::shared_ptr<UIManager> const &uiManager,
42+
RuntimeExecutor const &runtimeExecutor);
4243

4344
~UIManagerBinding();
4445

45-
/*
46-
* Establish a relationship between `UIManager` and `UIManagerBinding` by
47-
* setting internal pointers to each other.
48-
* Must be called on JavaScript thread or during VM destruction.
49-
*/
50-
void attach(std::shared_ptr<UIManager> const &uiManager);
51-
5246
/*
5347
* Starts React Native Surface with given id, moduleName, and props.
5448
* Thread synchronization must be enforced externally.

0 commit comments

Comments
 (0)