Skip to content

Commit

Permalink
Make ScrollView sticky headers work w/o dispatching RCTEventEmitter.r…
Browse files Browse the repository at this point in the history
…eceiveEvent

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
  • Loading branch information
RSNara authored and facebook-github-bot committed Jun 21, 2022
1 parent f97c6a5 commit 8dded42
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions React/Base/RCTEventDispatcherProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ typedef NS_ENUM(NSInteger, RCTTextEventType) {
key:(NSString *)key
eventCount:(NSInteger)eventCount;

/**
* Notify Observers of event
*/
- (void)notifyObserversOfEvent:(id<RCTEvent>)event;

/**
* Send a pre-prepared event object.
*
Expand Down
7 changes: 6 additions & 1 deletion React/CoreModules/RCTEventDispatcher.mm
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ - (void)sendTextEventWithType:(RCTTextEventType)type
[self sendEvent:event];
}

- (void)sendEvent:(id<RCTEvent>)event
- (void)notifyObserversOfEvent:(id<RCTEvent>)event
{
[_observersLock lock];

Expand All @@ -119,6 +119,11 @@ - (void)sendEvent:(id<RCTEvent>)event
}

[_observersLock unlock];
}

- (void)sendEvent:(id<RCTEvent>)event
{
[self notifyObserversOfEvent:event];

[_eventQueueLock lock];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down

0 comments on commit 8dded42

Please sign in to comment.