From 8dded42b68bee4516a2a3d569026643e45a478f3 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Tue, 21 Jun 2022 07:36:01 -0700 Subject: [PATCH] Make ScrollView sticky headers work w/o dispatching RCTEventEmitter.receiveEvent Summary: # Context ScrollView sticky headers rely on this bit of code to work: ``` AnimatedImplementation.attachNativeEvent( this._scrollViewRef, 'onScroll', [{nativeEvent: {contentOffset: {y: this._scrollAnimatedValue}}}], ); ``` What this code means: When the ScrollView host component receives the "onScroll" event, assign event.nativeEvent.contentOffSet.y to the this._scrollAnimatedValue AnimatedValue. How this subscription mechanism is set up: NativeAnimatedTurboModule subscribes to events dispatched by RCTEventDispatcher sendEvent. Then, whenever RCTEventEmitter sendEvent executes, NativeAnimatedTurboModule also updates the AnimatedValue for that event. # Problem Previously, in bridgeless, we started dispatching RCTScrollView via the RCTEventDispatcher sendEvent to update the AnimatedValue for ScrollView. This meant that we started dispatching the onScroll event to JavaScript via RCTEventEmitter.receiveEvent JSModule, which isn't supported in the Fabric renderer. With this diff, we dialed back that solution. Instead of (1) notifying NativeAnimatedTurboModule and (2) sending the onScroll event to JavaScript, we're only doing (1) (i.e: notifying NativeAnimatedTurboModule). Changelog: [Internal] Reviewed By: fkgozali Differential Revision: D37257719 fbshipit-source-id: 7dea3cf8b9ae78f6b0fd40414b8f224d43367a5a --- React/Base/RCTEventDispatcherProtocol.h | 5 +++++ React/CoreModules/RCTEventDispatcher.mm | 7 ++++++- .../ScrollView/RCTScrollViewComponentView.mm | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/React/Base/RCTEventDispatcherProtocol.h b/React/Base/RCTEventDispatcherProtocol.h index 9e80332a007638..4125e9829443a0 100644 --- a/React/Base/RCTEventDispatcherProtocol.h +++ b/React/Base/RCTEventDispatcherProtocol.h @@ -102,6 +102,11 @@ typedef NS_ENUM(NSInteger, RCTTextEventType) { key:(NSString *)key eventCount:(NSInteger)eventCount; +/** + * Notify Observers of event + */ +- (void)notifyObserversOfEvent:(id)event; + /** * Send a pre-prepared event object. * diff --git a/React/CoreModules/RCTEventDispatcher.mm b/React/CoreModules/RCTEventDispatcher.mm index ef6a895f12bece..f2cf17c947eedf 100644 --- a/React/CoreModules/RCTEventDispatcher.mm +++ b/React/CoreModules/RCTEventDispatcher.mm @@ -110,7 +110,7 @@ - (void)sendTextEventWithType:(RCTTextEventType)type [self sendEvent:event]; } -- (void)sendEvent:(id)event +- (void)notifyObserversOfEvent:(id)event { [_observersLock lock]; @@ -119,6 +119,11 @@ - (void)sendEvent:(id)event } [_observersLock unlock]; +} + +- (void)sendEvent:(id)event +{ + [self notifyObserversOfEvent:event]; [_eventQueueLock lock]; diff --git a/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index c67865e1122630..e699640940d88e 100644 --- a/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -73,7 +73,7 @@ static void RCTSendScrollEventForNativeAnimations_DEPRECATED(UIScrollView *scrol [bridge.eventDispatcher sendEvent:scrollEvent]; } else { NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:scrollEvent, @"event", nil]; - [[NSNotificationCenter defaultCenter] postNotificationName:@"RCTSendEventToLegacyEventDispatcher" + [[NSNotificationCenter defaultCenter] postNotificationName:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED" object:nil userInfo:userInfo]; }