From 9f69c922cbfcd08bbdfa16c36bd57153d77b9b1b Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 2 Sep 2021 16:48:53 -0700 Subject: [PATCH] Remove RCTRootView.bridge usage from RCTBaseViewController Summary: RCTBaseViewController uses RCTRootView.bridge to call RCTViewEventEmitter.emit. ## Changes - RCTEventDispatcher now exposes a method to dispatch RCTViewEventEmitter events. - RCTRootView (for paper), and RCTSurfaceHostingProxyRootView (for fabric and bridgeless mode) now exports the RCTEventDispatcher by grabbing it from the bridge - RCTBaseViewController now uses the event dispatcher exported from RCTRootView to dispatch the RCTViewEventEmitter event. ## Benefits - RCTBaseViewController no longer uses the bridge to dispatch RCTViewEventEmitter events - In the future, we'll make RCTSurfaceHostingProxyRootView work with bridgeless mode, which'll allow us to remove the bridge/bridgeless fork in RCTBaseViewController. Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D30434884 fbshipit-source-id: d961a56ac2abc08a661f8fe7c37926f219b731d0 --- React/Base/RCTEventDispatcherProtocol.h | 2 ++ React/Base/RCTRootView.h | 2 ++ React/Base/RCTRootView.m | 5 +++++ .../SurfaceHostingView/RCTSurfaceHostingProxyRootView.h | 1 + .../SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm | 5 +++++ React/CoreModules/RCTEventDispatcher.mm | 5 +++++ 6 files changed, 20 insertions(+) diff --git a/React/Base/RCTEventDispatcherProtocol.h b/React/Base/RCTEventDispatcherProtocol.h index 9e54ccbf187..913cf852283 100644 --- a/React/Base/RCTEventDispatcherProtocol.h +++ b/React/Base/RCTEventDispatcherProtocol.h @@ -81,6 +81,8 @@ typedef NS_ENUM(NSInteger, RCTTextEventType) { */ @protocol RCTEventDispatcherProtocol +- (void)sendViewEventWithName:(NSString *)name reactTag:(NSNumber *)reactTag; + /** * Deprecated, do not use. */ diff --git a/React/Base/RCTRootView.h b/React/Base/RCTRootView.h index 27eec8bf3df..dd563453153 100644 --- a/React/Base/RCTRootView.h +++ b/React/Base/RCTRootView.h @@ -88,6 +88,8 @@ extern */ @property (nonatomic, strong, readonly) RCTModuleRegistry *moduleRegistry; +@property (nonatomic, strong, readonly) id eventDispatcher; + /** * 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 a8a8c2ccfa2..4da26ee42b1 100644 --- a/React/Base/RCTRootView.m +++ b/React/Base/RCTRootView.m @@ -126,6 +126,11 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder) return _bridge.moduleRegistry; } +- (id)eventDispatcher +{ + return [self.moduleRegistry moduleForName:"EventDispatcher"]; +} + #pragma mark - passThroughTouches - (BOOL)passThroughTouches diff --git a/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.h b/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.h index 110b6e4f23b..7a42041d0fd 100644 --- a/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.h +++ b/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.h @@ -28,6 +28,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, strong, readonly) RCTBridge *bridge; @property (nonatomic, readonly) BOOL hasBridge; @property (nonatomic, strong, readonly) RCTModuleRegistry *moduleRegistry; +@property (nonatomic, strong, readonly) id eventDispatcher; @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 6ca24ae80bb..ad12ace6ddc 100644 --- a/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm +++ b/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm @@ -104,6 +104,11 @@ static RCTRootViewSizeFlexibility convertToRootViewSizeFlexibility(RCTSurfaceSiz return _bridge.moduleRegistry; } +- (id)eventDispatcher +{ + return [self.moduleRegistry moduleForName:"EventDispatcher"]; +} + RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame) RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder) diff --git a/React/CoreModules/RCTEventDispatcher.mm b/React/CoreModules/RCTEventDispatcher.mm index d9c69053896..5b2dad2d61b 100644 --- a/React/CoreModules/RCTEventDispatcher.mm +++ b/React/CoreModules/RCTEventDispatcher.mm @@ -56,6 +56,11 @@ RCT_EXPORT_MODULE() _observersLock = [NSLock new]; } +- (void)sendViewEventWithName:(NSString *)name reactTag:(NSNumber *)reactTag +{ + [_callableJSModules invokeModule:@"RCTViewEventEmitter" method:@"emit" withArgs:@[ name, RCTNullIfNil(reactTag) ]]; +} + - (void)sendAppEventWithName:(NSString *)name body:(id)body { [_callableJSModules invokeModule:@"RCTNativeAppEventEmitter"