feat(iOS): get TurboModules conforming to protocol#53282
Conversation
cipolleschi
left a comment
There was a problem hiding this comment.
Hi @tjzel thanks for the submission.
This code is critical and performance wise can be a problem. I don't think we can import this, to be fair.
It would be helpful to understand what do you want to achieve, to see if we can get there in a better and safer way.
| auto * moduleName = pair.first.c_str(); | ||
| Class moduleClass = [self _getModuleClassFromName:moduleName]; | ||
|
|
||
| if ([moduleClass conformsToProtocol:protocol]) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I don't think that we need an approach that works for everybody.
Also, we already have something similar in the Codegen config:
"ios": {
"modules": {
"TestModule": {
"className": "<iOS-class-implementing-the-RCTModuleProvider-protocol>",
"unstableRequiresMainQueueSetup": false,
"conformsToProtocols": ["RCTImageURLLoader", "RCTURLRequestHandler", "RCTImageDataDecoder"],
}
},
I'd say that the modules that conforms to the specific protocol you are interested into should add the conformace there. Then, depending on what you need from that protocol, we can have a specific hook somewhere in the codebase. for example, those three protocols are used here
There was a problem hiding this comment.
Yes, it's very useful and I previously prototyped with it on iOS but there seems to be no respective APIs for Android. How would you handle such cases on Android?
|
Closing in favor of #53928 |
Summary:
This is an analogous PR to
but on iOS.
The implementation is very simple.
Changelog:
[IOS] [ADDED] - Allow to get TurboModules conforming to given protocol
Test Plan: