Skip to content

Commit

Permalink
Simplify construction of UIManagerBinding
Browse files Browse the repository at this point in the history
Summary:
changelog: [internal]

Provide `UIManager` to `UIManagerBinding` in constructor to make the API safer.

Reviewed By: philIip

Differential Revision: D32668892

fbshipit-source-id: a15cd295196a60c3f46997e59c05c4f90503e18d
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Dec 13, 2021
1 parent 34efaab commit b30ff98
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
11 changes: 5 additions & 6 deletions ReactCommon/react/renderer/scheduler/Scheduler.cpp
Expand Up @@ -86,12 +86,11 @@ Scheduler::Scheduler(
uiManager->setDelegate(this);
uiManager->setComponentDescriptorRegistry(componentDescriptorRegistry_);

runtimeExecutor_([uiManager,
runtimeExecutor = runtimeExecutor_](jsi::Runtime &runtime) {
auto uiManagerBinding =
UIManagerBinding::createAndInstallIfNeeded(runtime, runtimeExecutor);
uiManagerBinding->attach(uiManager);
});
runtimeExecutor_(
[uiManager, runtimeExecutor = runtimeExecutor_](jsi::Runtime &runtime) {
UIManagerBinding::createAndInstallIfNeeded(
runtime, runtimeExecutor, uiManager);
});

auto componentDescriptorRegistryKey =
"ComponentDescriptorRegistry_DO_NOT_USE_PRETTY_PLEASE";
Expand Down
24 changes: 9 additions & 15 deletions ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp
Expand Up @@ -58,27 +58,23 @@ static bool checkGetCallableModuleIsActive(jsi::Runtime &runtime) {
return true;
}

std::shared_ptr<UIManagerBinding> UIManagerBinding::createAndInstallIfNeeded(
void UIManagerBinding::createAndInstallIfNeeded(
jsi::Runtime &runtime,
RuntimeExecutor const &runtimeExecutor) {
RuntimeExecutor const &runtimeExecutor,
std::shared_ptr<UIManager> const &uiManager) {
auto uiManagerModuleName = "nativeFabricUIManager";

auto uiManagerValue =
runtime.global().getProperty(runtime, uiManagerModuleName);
if (uiManagerValue.isUndefined()) {
// The global namespace does not have an instance of the binding;
// we need to create, install and return it.
auto uiManagerBinding = std::make_shared<UIManagerBinding>(runtimeExecutor);
auto uiManagerBinding =
std::make_shared<UIManagerBinding>(uiManager, runtimeExecutor);
auto object = jsi::Object::createFromHostObject(runtime, uiManagerBinding);
runtime.global().setProperty(
runtime, uiManagerModuleName, std::move(object));
return uiManagerBinding;
}

// The global namespace already has an instance of the binding;
// we need to return that.
auto uiManagerObject = uiManagerValue.asObject(runtime);
return uiManagerObject.getHostObject<UIManagerBinding>(runtime);
}

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

UIManagerBinding::UIManagerBinding(RuntimeExecutor const &runtimeExecutor)
: runtimeExecutor_(runtimeExecutor) {}
UIManagerBinding::UIManagerBinding(
std::shared_ptr<UIManager> const &uiManager,
RuntimeExecutor const &runtimeExecutor)
: uiManager_(uiManager), runtimeExecutor_(runtimeExecutor) {}

UIManagerBinding::~UIManagerBinding() {
LOG(WARNING) << "UIManagerBinding::~UIManagerBinding() was called (address: "
<< this << ").";
}

void UIManagerBinding::attach(std::shared_ptr<UIManager> const &uiManager) {
uiManager_ = uiManager;
}

static jsi::Value callMethodOfModule(
jsi::Runtime &runtime,
std::string const &moduleName,
Expand Down
18 changes: 6 additions & 12 deletions ReactCommon/react/renderer/uimanager/UIManagerBinding.h
Expand Up @@ -24,31 +24,25 @@ class UIManagerBinding : public jsi::HostObject {
/*
* Installs UIManagerBinding into JavaScript runtime if needed.
* Creates and sets `UIManagerBinding` into the global namespace.
* In case if the global namespace already has a `UIManagerBinding` installed,
* returns that.
* Thread synchronization must be enforced externally.
*/
static std::shared_ptr<UIManagerBinding> createAndInstallIfNeeded(
static void createAndInstallIfNeeded(
jsi::Runtime &runtime,
RuntimeExecutor const &runtimeExecutor);
RuntimeExecutor const &runtimeExecutor,
std::shared_ptr<UIManager> const &uiManager);

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

UIManagerBinding(RuntimeExecutor const &runtimeExecutor);
UIManagerBinding(
std::shared_ptr<UIManager> const &uiManager,
RuntimeExecutor const &runtimeExecutor);

~UIManagerBinding();

/*
* Establish a relationship between `UIManager` and `UIManagerBinding` by
* setting internal pointers to each other.
* Must be called on JavaScript thread or during VM destruction.
*/
void attach(std::shared_ptr<UIManager> const &uiManager);

/*
* Starts React Native Surface with given id, moduleName, and props.
* Thread synchronization must be enforced externally.
Expand Down

0 comments on commit b30ff98

Please sign in to comment.