iOS: Make each module implement getTurboModuleWithJsInvoker: instead of having centralized provider

Summary:
For better modularity, each module conforming to RCTTurboModule should provide a getter for the specific TurboModule instance for itself. This is a bit more extra work for devs, but simplify tooling and allow better modularity vs having a central function that provides the correct instance based on name.

Note: Android may or may not follow this new pattern -- TBD.

Reviewed By: RSNara

Differential Revision: D13882073

fbshipit-source-id: 6d5f82af67278c39c43c4f7970995690d4a82a98
This commit is contained in:
Kevin Gozali 2019-01-30 17:25:07 -08:00 committed by Facebook Github Bot
parent bbcb97a29a
commit 8a50bc3ab3
3 changed files with 16 additions and 17 deletions

View File

@ -326,7 +326,6 @@ RCT_EXTERN void RCTRegisterModule(Class); \
* Experimental.
* A protocol to declare that a class supports TurboModule.
* This may be removed in the future.
* See RCTTurboModule.h for actual signature.
*/
@protocol RCTTurboModule <NSObject>
@end
@protocol RCTTurboModule;

View File

@ -38,19 +38,18 @@ public:
} // namespace react
} // namespace facebook
@protocol RCTTurboModule <NSObject>
@optional
// This should be required, after migration is done.
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker;
@end
// TODO: Consolidate this extension with the one in RCTSurfacePresenter.
@interface RCTBridge ()
- (std::shared_ptr<facebook::react::MessageQueueThread>)jsMessageThread;
@end
/**
* A backward-compatible protocol to be adopted by an existing RCTCxxModule-based class
* so that it can support the TurboModule system.
*/
@protocol RCTTurboCxxModule <RCTTurboModule>
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker;
@end

View File

@ -111,17 +111,18 @@ static Class getFallbackClassFromName(const char *name) {
}
}
if ([module respondsToSelector:@selector(getTurboModuleWithJsInvoker:)]) {
return [module getTurboModuleWithJsInvoker:strongSelf->_jsInvoker];
}
// RCTCxxModule compatibility layer.
if ([moduleClass isSubclassOfClass:RCTCxxModule.class]) {
if ([module respondsToSelector:@selector(getTurboModuleWithJsInvoker:)]) {
return [((id<RCTTurboCxxModule>)module) getTurboModuleWithJsInvoker:strongSelf->_jsInvoker];
}
// Use TurboCxxModule compat class to wrap the CxxModule instance.
// This is only for migration convenience, despite less performant.
return std::make_shared<react::TurboCxxModule>([((RCTCxxModule *)module) createModule], strongSelf->_jsInvoker);
}
// This may be needed for migration purpose in case the module class doesn't provide the static getter.
return [strongSelf->_delegate getTurboModule:name instance:module jsInvoker:strongSelf->_jsInvoker];
};