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.

## Changelog:
[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 facebook-github-bot committed Jun 27, 2023
1 parent f8f5988 commit a28881a
Showing 1 changed file with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace facebook::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 @@ -45,22 +45,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 = *static_cast<std::string const *>(flavor.get());
auto moduleName = moduleNameFromComponentName(componentName);
Class module = NSClassFromString(RCTNSStringFromString(moduleName));
assert(module);
auto componentName = *std::static_pointer_cast<std::string const>(flavor);
Class viewManagerClass = getViewManagerFromComponentName(componentName);
assert(viewManagerClass);
auto optionalBridge = contextContainer->find<std::shared_ptr<void>>("Bridge");
RCTBridge *bridge;
if (optionalBridge) {
Expand All @@ -79,7 +98,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 a28881a

Please sign in to comment.