From 8a50bc3ab35a5c07cf01ccbc08d3bbe11375612a Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Wed, 30 Jan 2019 17:25:07 -0800 Subject: [PATCH] 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 --- React/Base/RCTBridgeModule.h | 5 ++--- .../core/platform/ios/RCTTurboModule.h | 19 +++++++++---------- .../platform/ios/RCTTurboModuleManager.mm | 9 +++++---- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/React/Base/RCTBridgeModule.h b/React/Base/RCTBridgeModule.h index a01e4117a..b51eb71e6 100644 --- a/React/Base/RCTBridgeModule.h +++ b/React/Base/RCTBridgeModule.h @@ -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 - -@end +@protocol RCTTurboModule; diff --git a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h index 3559540f0..084a23e53 100644 --- a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h +++ b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.h @@ -38,19 +38,18 @@ public: } // namespace react } // namespace facebook +@protocol RCTTurboModule + +@optional + +// This should be required, after migration is done. +- (std::shared_ptr)getTurboModuleWithJsInvoker:(std::shared_ptr)jsInvoker; + +@end + // TODO: Consolidate this extension with the one in RCTSurfacePresenter. @interface RCTBridge () - (std::shared_ptr)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 - -- (std::shared_ptr)getTurboModuleWithJsInvoker:(std::shared_ptr)jsInvoker; - -@end diff --git a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm index 46f69fb62..eb1e4c3c8 100644 --- a/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm +++ b/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm @@ -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)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([((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]; };