4545using namespace facebook ;
4646using 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.
4954void 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