mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: This diff changes the signature of ComponentDescriptor constructor to make it simpler and easier to support: now all arguments are passed via struct that contains all these arguments as fields. Now the ComponentDescriptor constructor accepts three arguments one of which is optional. This causes some confusion and the possibility of bugs in all subclasses that needs to implement a custom constructor. Mostly because in every case we need to ensure that the constructor: * Accepts and pass down all parameters/arguments; * Accepts the right types of those parameters (shared vs weak pointers, references vs values). * Accepts all thee arguments and pass them (including flavor!). We failed this point several times. Overal that makes the code simpler and allows changing the set of parameters relatively easy. (There is no plan for it!) Look at the LOC balance: less code! Changelog: [INTERNAL] Reviewed By: sammy-SC Differential Revision: D18548173 fbshipit-source-id: 5d038b135e004f6c054026b3235ed57db99c086d
84 lines
3.1 KiB
Plaintext
84 lines
3.1 KiB
Plaintext
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "LegacyViewManagerInteropComponentDescriptor.h"
|
|
#include <React/RCTBridge.h>
|
|
#include <React/RCTComponentData.h>
|
|
#include <React/RCTModuleData.h>
|
|
#include <React/RCTUIManager.h>
|
|
#include <react/utils/ContextContainer.h>
|
|
#include <react/utils/ManagedObjectWrapper.h>
|
|
#include "LegacyViewManagerInteropState.h"
|
|
#include "RCTLegacyViewManagerInteropCoordinator.h"
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
static std::string moduleNameFromComponentName(const std::string &componentName)
|
|
{
|
|
// TODO: remove FB specific code (T56174424)
|
|
if (componentName == "StickerInputView") {
|
|
return "FBStickerInputViewManager";
|
|
}
|
|
std::string fbPrefix("FB");
|
|
if (std::mismatch(fbPrefix.begin(), fbPrefix.end(), componentName.begin()).first == fbPrefix.end()) {
|
|
// If `moduleName` has "FB" prefix.
|
|
return componentName + "Manager";
|
|
}
|
|
return "RCT" + componentName + "Manager";
|
|
}
|
|
|
|
inline NSString *RCTNSStringFromString(const std::string &string)
|
|
{
|
|
return [NSString stringWithCString:string.c_str() encoding:NSUTF8StringEncoding];
|
|
}
|
|
|
|
static std::shared_ptr<void> const constructCoordinator(
|
|
ContextContainer::Shared const &contextContainer,
|
|
ComponentDescriptor::Flavor const &flavor)
|
|
{
|
|
auto componentName = *std::static_pointer_cast<std::string const>(flavor);
|
|
auto moduleName = moduleNameFromComponentName(componentName);
|
|
Class module = NSClassFromString(RCTNSStringFromString(moduleName));
|
|
assert(module);
|
|
RCTBridge *bridge = (RCTBridge *)unwrapManagedObjectWeakly(contextContainer->at<std::shared_ptr<void>>("Bridge"));
|
|
RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:module bridge:bridge];
|
|
return wrapManagedObject([[RCTLegacyViewManagerInteropCoordinator alloc] initWithComponentData:componentData
|
|
bridge:bridge]);
|
|
}
|
|
|
|
LegacyViewManagerInteropComponentDescriptor::LegacyViewManagerInteropComponentDescriptor(
|
|
ComponentDescriptorParameters const ¶meters)
|
|
: ConcreteComponentDescriptor(parameters), _coordinator(constructCoordinator(contextContainer_, flavor_))
|
|
{
|
|
}
|
|
|
|
ComponentHandle LegacyViewManagerInteropComponentDescriptor::getComponentHandle() const
|
|
{
|
|
return reinterpret_cast<ComponentHandle>(getComponentName());
|
|
}
|
|
|
|
ComponentName LegacyViewManagerInteropComponentDescriptor::getComponentName() const
|
|
{
|
|
return std::static_pointer_cast<std::string const>(this->flavor_)->c_str();
|
|
}
|
|
|
|
void LegacyViewManagerInteropComponentDescriptor::adopt(ShadowNode::Unshared shadowNode) const
|
|
{
|
|
ConcreteComponentDescriptor::adopt(shadowNode);
|
|
|
|
assert(std::dynamic_pointer_cast<LegacyViewManagerInteropShadowNode>(shadowNode));
|
|
auto legacyViewManagerInteropShadowNode = std::static_pointer_cast<LegacyViewManagerInteropShadowNode>(shadowNode);
|
|
|
|
auto state = LegacyViewManagerInteropState{};
|
|
state.coordinator = _coordinator;
|
|
|
|
legacyViewManagerInteropShadowNode->setStateData(std::move(state));
|
|
}
|
|
} // namespace react
|
|
} // namespace facebook
|