Skip to content

Commit

Permalink
Implement multiple manager lookup for the interop layer (#38093)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #38093

In [this issue](#37905), the community detected a strict assumption in the interop layer for which, given a component `XXXView`, its ViewManager must be called `RCTXXXViewManager`.

That's not the case for some components, for example, `BVLinearGradient`, which View manager is `BVLinearGradientManager` and not `RCTBVLinearGradientManager`.

This diff adds a secondary lookup logic:
1. We look for the `RCTXXXViewManager`.
2. If not found, we look for `XXXViewManager`.

We will assess whether to generalize this logic once and for all or to expand other lookup cases on an example/failure basis as it's not a goal to have a 100% accurate interop layer. The Goal is to cover most use cases.

[iOS][Added] - Allow to lookup for ViewManager without the RCT prefix in the Interop Layer

Reviewed By: sammy-SC

Differential Revision: D47055969

fbshipit-source-id: 1d31f3f4bc6f1f543edbd157ce04ad9daf23dbc6
  • Loading branch information
cipolleschi authored and Riccardo Cipolleschi committed Jun 28, 2023
1 parent 95db9f9 commit f537257
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace facebook {
namespace react {

static std::string moduleNameFromComponentName(const std::string &componentName)
static std::string moduleNameFromComponentNameNoRCTPrefix(const std::string &componentName)
{
// TODO: remove FB specific code (T56174424)
if (componentName == "StickerInputView") {
Expand All @@ -46,22 +46,41 @@
return componentName + "Manager";
}

return "RCT" + componentName + "Manager";
return componentName + "Manager";
}

inline NSString *RCTNSStringFromString(const std::string &string)
{
return [NSString stringWithCString:string.c_str() encoding:NSUTF8StringEncoding];
return [NSString stringWithUTF8String:string.c_str()];
}

static Class getViewManagerFromComponentName(const std::string &componentName)
{
auto viewManagerName = moduleNameFromComponentNameNoRCTPrefix(componentName);

// 1. Try to get the manager with the RCT prefix.
auto rctViewManagerName = "RCT" + viewManagerName;
Class viewManagerClass = NSClassFromString(RCTNSStringFromString(rctViewManagerName));
if (viewManagerClass) {
return viewManagerClass;
}

// 2. Try to get the manager without the prefix.
viewManagerClass = NSClassFromString(RCTNSStringFromString(viewManagerName));
if (viewManagerClass) {
return viewManagerClass;
}

return nil;
}

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);
Class viewManagerClass = getViewManagerFromComponentName(componentName);
assert(viewManagerClass);
auto optionalBridge = contextContainer->find<std::shared_ptr<void>>("Bridge");
RCTBridge *bridge;
if (optionalBridge) {
Expand All @@ -80,7 +99,7 @@
bridgeModuleDecorator = unwrapManagedObject(optionalModuleDecorator.value());
}

RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:module
RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:viewManagerClass
bridge:bridge
eventDispatcher:eventDispatcher];
return wrapManagedObject([[RCTLegacyViewManagerInteropCoordinator alloc]
Expand Down

0 comments on commit f537257

Please sign in to comment.