From bfead74e69f69fac6ae87110737e8935d293922e Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 2 Sep 2021 16:48:53 -0700 Subject: [PATCH] Allow RCTRootView users to require NativeModules without the bridge Summary: RCTRootVeiw exports the bridge. One reason why is to allow users of RCTRootView users to access NativeModules. ## Changes - RCTBridge now exports the RCTModuleRegistry - RCTRootView now exports the RCTModuleRegistry exported by the bridge - Users of RCTRootView use the RCTModuleRegistry exported by RCTRootView to access NativeModules ## Benefits Down the line, we'll change how RCTRootView gets the RCTModuleRegistry (i.e: it won't use the bridge in bridgeless mode). Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D30434886 fbshipit-source-id: 875fce24d2fd9ee6350f128c8612e613e61e390e --- React/Base/RCTBridge+Private.h | 8 ++++++++ React/Base/RCTBridge.m | 5 +++++ React/Base/RCTRootView.h | 8 ++++++++ React/Base/RCTRootView.m | 5 +++++ .../SurfaceHostingView/RCTSurfaceHostingProxyRootView.h | 1 + .../SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm | 6 ++++++ React/CxxBridge/RCTCxxBridge.mm | 5 +++++ 7 files changed, 38 insertions(+) diff --git a/React/Base/RCTBridge+Private.h b/React/Base/RCTBridge+Private.h index 70a20f0b00f..9dd96a5cd14 100644 --- a/React/Base/RCTBridge+Private.h +++ b/React/Base/RCTBridge+Private.h @@ -7,6 +7,7 @@ #import +@class RCTModuleRegistry; @class RCTModuleData; @protocol RCTJavaScriptExecutor; @@ -62,6 +63,13 @@ RCT_EXTERN void RCTRegisterModule(Class); */ @property (nonatomic, strong, readwrite) NSURL *bundleURL; +/** + * An object that allows one to require NativeModules/TurboModules. + * RCTModuleRegistry is implemented in bridgeless mode and bridge mode. + * Used by RCTRootView. + */ +@property (nonatomic, strong, readonly) RCTModuleRegistry *moduleRegistry; + @end @interface RCTBridge (RCTCxxBridge) diff --git a/React/Base/RCTBridge.m b/React/Base/RCTBridge.m index 850c78c135a..10503261d72 100644 --- a/React/Base/RCTBridge.m +++ b/React/Base/RCTBridge.m @@ -257,6 +257,11 @@ RCT_NOT_IMPLEMENTED(-(instancetype)init) }); } +- (RCTModuleRegistry *)moduleRegistry +{ + return self.batchedBridge.moduleRegistry; +} + - (NSArray *)moduleClasses { return self.batchedBridge.moduleClasses; diff --git a/React/Base/RCTRootView.h b/React/Base/RCTRootView.h index 5074c54bda6..27eec8bf3df 100644 --- a/React/Base/RCTRootView.h +++ b/React/Base/RCTRootView.h @@ -8,6 +8,8 @@ #import #import +#import +#import @protocol RCTRootViewDelegate; @@ -80,6 +82,12 @@ extern */ @property (nonatomic, readonly) BOOL hasBridge; +/** + * This API allows users of RCTRootView to access other NativeModules, without + * directly accessing the bridge. + */ +@property (nonatomic, strong, readonly) RCTModuleRegistry *moduleRegistry; + /** * The name of the JavaScript module to execute within the * specified scriptURL (required). Setting this will not have diff --git a/React/Base/RCTRootView.m b/React/Base/RCTRootView.m index c5c5881739c..a8a8c2ccfa2 100644 --- a/React/Base/RCTRootView.m +++ b/React/Base/RCTRootView.m @@ -121,6 +121,11 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder) return _bridge != nil; } +- (RCTModuleRegistry *)moduleRegistry +{ + return _bridge.moduleRegistry; +} + #pragma mark - passThroughTouches - (BOOL)passThroughTouches diff --git a/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.h b/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.h index 3d903719653..110b6e4f23b 100644 --- a/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.h +++ b/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.h @@ -27,6 +27,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, copy, readonly) NSString *moduleName; @property (nonatomic, strong, readonly) RCTBridge *bridge; @property (nonatomic, readonly) BOOL hasBridge; +@property (nonatomic, strong, readonly) RCTModuleRegistry *moduleRegistry; @property (nonatomic, copy, readwrite) NSDictionary *appProperties; @property (nonatomic, assign) RCTRootViewSizeFlexibility sizeFlexibility; @property (nonatomic, weak) id delegate; diff --git a/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm b/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm index 2f69e6d4932..6ca24ae80bb 100644 --- a/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm +++ b/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm @@ -10,6 +10,7 @@ #import #import "RCTAssert.h" +#import "RCTBridge+Private.h" #import "RCTBridge.h" #import "RCTLog.h" #import "RCTPerformanceLogger.h" @@ -98,6 +99,11 @@ static RCTRootViewSizeFlexibility convertToRootViewSizeFlexibility(RCTSurfaceSiz return _bridge != nil; } +- (RCTModuleRegistry *)moduleRegistry +{ + return _bridge.moduleRegistry; +} + RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame) RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder) diff --git a/React/CxxBridge/RCTCxxBridge.mm b/React/CxxBridge/RCTCxxBridge.mm index 0f1a601f4a6..a33b656d9cf 100644 --- a/React/CxxBridge/RCTCxxBridge.mm +++ b/React/CxxBridge/RCTCxxBridge.mm @@ -244,6 +244,11 @@ struct RCTInstanceCallback : public InstanceCallback { @synthesize performanceLogger = _performanceLogger; @synthesize valid = _valid; +- (RCTModuleRegistry *)moduleRegistry +{ + return _objCModuleRegistry; +} + - (void)setRCTTurboModuleRegistry:(id)turboModuleRegistry { _turboModuleRegistry = turboModuleRegistry;