Skip to content

feat(iOS): get TurboModules conforming to protocol #53282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-native/React/Base/RCTTurboModuleRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
* TurboModules to TurboModules is complete.
*/
- (id)moduleForName:(const char *)moduleName warnOnLookupFailure:(BOOL)warnOnLookupFailure;
- (id)getModulesConformingToProtocol:(Protocol *)protocol;
- (BOOL)moduleIsInitialized:(const char *)moduleName;
@end
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,24 @@ - (id)moduleForName:(const char *)moduleName warnOnLookupFailure:(BOOL)warnOnLoo
return module;
}

- (id)getModulesConformingToProtocol:(Protocol *)protocol{
NSMutableArray *modules = [NSMutableArray new];

for (auto &pair : _moduleHolders) {
auto * moduleName = pair.first.c_str();
Class moduleClass = [self _getModuleClassFromName:moduleName];

if ([moduleClass conformsToProtocol:protocol]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking whether a module conforms to a protocol is a very expensive operation (see the official docs).
Here we are also iterating over all the modules that are loaded in the the app. If this happens at startup or at critical time, for apps that have many modules, this can cause a startup time regression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was afraid this would be a blocker.

This is change is a part I extracted from #50788 because I figured out it could be useful anyway. I personally need it for the BundleConsumer interface from the aforementioned PR - I wanted to make an API usable for everyone, therefore needed a way to detect all the TurboModules that implement a given interface. More on that is in the proposal we talked about https://docs.google.com/document/d/1jo93F1gPD3xrXU1UMb5ahuZpIh4JITHd_-FC6lIzB_k

I also had an implementation that instead of conformsToProtocol uses respondsToSelector but I figured it's more or less the same implementation wise and performance wise.

Initially I did it using (a bit obscure) RCTModulesConformingToInterface but there's no respective codegen functionality for Android and I wanted to make the implementations similar.

id module = [self moduleForName:moduleName];
if (module) {
[modules addObject:module];
}
}
}

return modules;
}

- (BOOL)moduleIsInitialized:(const char *)moduleName
{
std::unique_lock<std::mutex> guard(_moduleHoldersMutex);
Expand Down
Loading