Skip to content

Commit a28881a

Browse files
cipolleschifacebook-github-bot
authored andcommitted
Implement multiple manager lookup for the interop layer (#38093)
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
1 parent f8f5988 commit a28881a

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropComponentDescriptor.mm

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace facebook::react {
2020

21-
static std::string moduleNameFromComponentName(const std::string &componentName)
21+
static std::string moduleNameFromComponentNameNoRCTPrefix(const std::string &componentName)
2222
{
2323
// TODO: remove FB specific code (T56174424)
2424
if (componentName == "StickerInputView") {
@@ -45,22 +45,41 @@
4545
return componentName + "Manager";
4646
}
4747

48-
return "RCT" + componentName + "Manager";
48+
return componentName + "Manager";
4949
}
5050

5151
inline NSString *RCTNSStringFromString(const std::string &string)
5252
{
53-
return [NSString stringWithCString:string.c_str() encoding:NSUTF8StringEncoding];
53+
return [NSString stringWithUTF8String:string.c_str()];
54+
}
55+
56+
static Class getViewManagerFromComponentName(const std::string &componentName)
57+
{
58+
auto viewManagerName = moduleNameFromComponentNameNoRCTPrefix(componentName);
59+
60+
// 1. Try to get the manager with the RCT prefix.
61+
auto rctViewManagerName = "RCT" + viewManagerName;
62+
Class viewManagerClass = NSClassFromString(RCTNSStringFromString(rctViewManagerName));
63+
if (viewManagerClass) {
64+
return viewManagerClass;
65+
}
66+
67+
// 2. Try to get the manager without the prefix.
68+
viewManagerClass = NSClassFromString(RCTNSStringFromString(viewManagerName));
69+
if (viewManagerClass) {
70+
return viewManagerClass;
71+
}
72+
73+
return nil;
5474
}
5575

5676
static std::shared_ptr<void> const constructCoordinator(
5777
ContextContainer::Shared const &contextContainer,
5878
ComponentDescriptor::Flavor const &flavor)
5979
{
60-
auto &componentName = *static_cast<std::string const *>(flavor.get());
61-
auto moduleName = moduleNameFromComponentName(componentName);
62-
Class module = NSClassFromString(RCTNSStringFromString(moduleName));
63-
assert(module);
80+
auto componentName = *std::static_pointer_cast<std::string const>(flavor);
81+
Class viewManagerClass = getViewManagerFromComponentName(componentName);
82+
assert(viewManagerClass);
6483
auto optionalBridge = contextContainer->find<std::shared_ptr<void>>("Bridge");
6584
RCTBridge *bridge;
6685
if (optionalBridge) {
@@ -79,7 +98,7 @@
7998
bridgeModuleDecorator = unwrapManagedObject(optionalModuleDecorator.value());
8099
}
81100

82-
RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:module
101+
RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:viewManagerClass
83102
bridge:bridge
84103
eventDispatcher:eventDispatcher];
85104
return wrapManagedObject([[RCTLegacyViewManagerInteropCoordinator alloc]

0 commit comments

Comments
 (0)