Skip to content

Commit fa9d082

Browse files
RSNarafacebook-github-bot
authored andcommitted
Bridgeless: Fix unstable_hasComponent
Summary: unstable_hasComponent(component) first registers the component with react native, if possible. Then, it returns you whether registration succeeded. ## Problem For unregistered components, the initial call returns false. But, all subsequent calls return true. The reason why: After the initial call fails, react native registers unimplemented view under that component name. So, all subsequent calls return true. ## Solution Just record the initial loopup result. And always return that initial lookup result from this method. Changelog: [iOS][Fixed] Fix bug: unstable_hasComponent(*) = true for unregistered components for n > 1th call. Reviewed By: yungsters Differential Revision: D73127468 fbshipit-source-id: ee30bde486a6bb970f40f654c65a8946452f49b3
1 parent f144b53 commit fa9d082

2 files changed

Lines changed: 35 additions & 37 deletions

File tree

‎packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ void RCTInstallNativeComponentRegistryBinding(facebook::jsi::Runtime &runtime);
4848
*/
4949
- (void)registerComponentViewClass:(Class<RCTComponentViewProtocol>)componentViewClass;
5050

51-
/**
52-
* Registers component if there is a matching class. Returns true if it matching class is found or the component has
53-
* already been registered, false otherwise.
54-
*/
55-
- (BOOL)registerComponentIfPossible:(const std::string &)componentName;
56-
5751
/**
5852
* Creates a component view with given component handle.
5953
*/

‎packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm‎

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@
4545
using namespace facebook;
4646
using namespace facebook::react;
4747

48+
@interface RCTComponentViewFactory ()
49+
- (void)_registerComponentIfPossible:(const std::string &)name;
50+
- (BOOL)_wasComponentRegistered:(const std::string &)name;
51+
@end
52+
4853
// Allow JS runtime to register native components as needed. For static view configs.
4954
void RCTInstallNativeComponentRegistryBinding(facebook::jsi::Runtime &runtime)
5055
{
5156
auto hasComponentProvider = [](const std::string &name) -> bool {
52-
return [[RCTComponentViewFactory currentComponentViewFactory]
53-
registerComponentIfPossible:componentNameByReactViewName(name)];
57+
auto globalComponentViewFactory = [RCTComponentViewFactory currentComponentViewFactory];
58+
[globalComponentViewFactory _registerComponentIfPossible:name];
59+
return [globalComponentViewFactory _wasComponentRegistered:name];
5460
};
5561
bindHasComponentProvider(runtime, std::move(hasComponentProvider));
5662
}
@@ -62,7 +68,7 @@ void RCTInstallNativeComponentRegistryBinding(facebook::jsi::Runtime &runtime)
6268

6369
@implementation RCTComponentViewFactory {
6470
std::unordered_map<ComponentHandle, RCTComponentViewClassDescriptor> _componentViewClasses;
65-
std::unordered_set<std::string> _registeredComponentsNames;
71+
std::unordered_map<std::string, bool> _registrationStatusMap;
6672
ComponentDescriptorProviderRegistry _providerRegistry;
6773
std::shared_mutex _mutex;
6874
}
@@ -81,7 +87,7 @@ + (RCTComponentViewFactory *)currentComponentViewFactory
8187

8288
componentViewFactory->_providerRegistry.setComponentDescriptorProviderRequest(
8389
[](ComponentName requestedComponentName) {
84-
[componentViewFactory registerComponentIfPossible:requestedComponentName];
90+
[componentViewFactory _registerComponentIfPossible:requestedComponentName];
8591
});
8692
});
8793

@@ -106,11 +112,16 @@ - (RCTComponentViewClassDescriptor)_componentViewClassDescriptorFromClass:(Class
106112
#pragma clang diagnostic pop
107113
}
108114

109-
- (BOOL)registerComponentIfPossible:(const std::string &)name
115+
- (BOOL)_wasComponentRegistered:(const std::string &)name
110116
{
111-
if (_registeredComponentsNames.find(name) != _registeredComponentsNames.end()) {
112-
// Component has already been registered.
113-
return YES;
117+
auto registrationResult = _registrationStatusMap.find(name);
118+
return registrationResult != _registrationStatusMap.end() && (registrationResult->second);
119+
}
120+
121+
- (void)_registerComponentIfPossible:(const std::string &)name
122+
{
123+
if (_registrationStatusMap.find(name) != _registrationStatusMap.end()) {
124+
return;
114125
}
115126

116127
// Paper name: we prepare this variables to warn the user
@@ -122,7 +133,7 @@ - (BOOL)registerComponentIfPossible:(const std::string &)name
122133
Class<RCTComponentViewProtocol> klass = RCTComponentViewClassWithName(name.c_str());
123134
if (klass) {
124135
[self registerComponentViewClass:klass];
125-
return YES;
136+
return;
126137
}
127138

128139
// Fallback 2: Ask the provider and check in the dictionary provided
@@ -133,7 +144,7 @@ - (BOOL)registerComponentIfPossible:(const std::string &)name
133144
klass = self.thirdPartyFabricComponentsProvider.thirdPartyFabricComponents[objcName];
134145
if (klass) {
135146
[self registerComponentViewClass:klass];
136-
return YES;
147+
return;
137148
}
138149
}
139150

@@ -153,52 +164,45 @@ - (BOOL)registerComponentIfPossible:(const std::string &)name
153164
auto componentHandle = reinterpret_cast<ComponentHandle>(componentName);
154165
auto constructor = [RCTLegacyViewManagerInteropComponentView componentDescriptorProvider].constructor;
155166

156-
[self _addDescriptorToProviderRegistry:ComponentDescriptorProvider{
157-
componentHandle, componentName, flavor, constructor}];
167+
auto provider = ComponentDescriptorProvider{componentHandle, componentName, flavor, constructor};
158168

169+
_providerRegistry.add(provider);
159170
_componentViewClasses[componentHandle] =
160171
[self _componentViewClassDescriptorFromClass:[RCTLegacyViewManagerInteropComponentView class]];
161-
return YES;
172+
_registrationStatusMap.insert({provider.name, true});
173+
return;
162174
}
163175

164176
// Fallback 4: use <UnimplementedView> if component doesn't exist.
165177
auto flavor = std::make_shared<const std::string>(name);
166178
auto componentName = ComponentName{flavor->c_str()};
167179
auto componentHandle = reinterpret_cast<ComponentHandle>(componentName);
168180
auto constructor = [RCTUnimplementedViewComponentView componentDescriptorProvider].constructor;
181+
auto provider = ComponentDescriptorProvider{componentHandle, componentName, flavor, constructor};
169182

170-
[self _addDescriptorToProviderRegistry:ComponentDescriptorProvider{
171-
componentHandle, componentName, flavor, constructor}];
172-
183+
_providerRegistry.add(provider);
173184
_componentViewClasses[componentHandle] =
174185
[self _componentViewClassDescriptorFromClass:[RCTUnimplementedViewComponentView class]];
175-
176-
// No matching class exists for `name`.
177-
return NO;
186+
_registrationStatusMap.insert({provider.name, false});
178187
}
179188

180189
- (void)registerComponentViewClass:(Class<RCTComponentViewProtocol>)componentViewClass
181190
{
182191
RCTAssert(componentViewClass, @"RCTComponentViewFactory: Provided `componentViewClass` is `nil`.");
183192
std::unique_lock lock(_mutex);
184193

185-
auto componentDescriptorProvider = [componentViewClass componentDescriptorProvider];
186-
_componentViewClasses[componentDescriptorProvider.handle] =
187-
[self _componentViewClassDescriptorFromClass:componentViewClass];
188-
[self _addDescriptorToProviderRegistry:componentDescriptorProvider];
194+
auto provider = [componentViewClass componentDescriptorProvider];
195+
_componentViewClasses[provider.handle] = [self _componentViewClassDescriptorFromClass:componentViewClass];
196+
_providerRegistry.add(provider);
197+
_registrationStatusMap.insert({provider.name, true});
189198

190199
auto supplementalComponentDescriptorProviders = [componentViewClass supplementalComponentDescriptorProviders];
191-
for (const auto &provider : supplementalComponentDescriptorProviders) {
192-
[self _addDescriptorToProviderRegistry:provider];
200+
for (const auto &supplementalProvider : supplementalComponentDescriptorProviders) {
201+
_providerRegistry.add(supplementalProvider);
202+
_registrationStatusMap.insert({supplementalProvider.name, true});
193203
}
194204
}
195205

196-
- (void)_addDescriptorToProviderRegistry:(const ComponentDescriptorProvider &)provider
197-
{
198-
_registeredComponentsNames.insert(provider.name);
199-
_providerRegistry.add(provider);
200-
}
201-
202206
- (RCTComponentViewDescriptor)createComponentViewWithComponentHandle:(facebook::react::ComponentHandle)componentHandle
203207
{
204208
RCTAssertMainQueue();

0 commit comments

Comments
 (0)