Skip to content

Commit

Permalink
feat: move notifying observers to event dispatcher (facebook#44474)
Browse files Browse the repository at this point in the history
Summary:
Based on the discussion starting here: https://discord.com/channels/514829729862516747/1073566663825432587/1237407161991172157, I suggest moving the call to `_notifyEventDispatcherObserversOfEvent_DEPRECATED` straight to `RCTEventDispatcher.mm`. It was previously in `RCTInstance.mm` which is only relevant on bridgeless mode. We want to mimic the behavior of https://github.com/facebook/react-native/blob/06eea61c19cd730cf0c14a436f042d30791c3f4a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm#L75-L78 but without using `currentBridge` since it is considered bad practice: software-mansion/react-native-reanimated#5497 (comment).

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[IOS] [CHANGED] - Move `notifyObservers` straight to `RCTEventDispatcher.mm`.

Pull Request resolved: facebook#44474

Test Plan:
See that example with `stickyHeaders` still works correctly on both bridgeless and bridge mode.

Videos with it on https://github.com/facebook/react-native/blob/deee037c62a7d62a349d34db427b14d3560ddf83/packages/rn-tester/js/examples/FlatList/FlatList-stickyHeaders.js example with more items for visibility:

- bridgeless:

https://github.com/facebook/react-native/assets/32481228/8b78104a-226b-466a-9f32-60ba4ec14100

- bridge:

https://github.com/facebook/react-native/assets/32481228/f2ca67cb-578f-45d4-954f-3249c6fa9410

- old arch:

https://github.com/facebook/react-native/assets/32481228/7d642923-ddda-4dd3-8f14-c9982a03bc2e

Differential Revision: D57097880

Reviewed By: javache

Pulled By: cipolleschi
  • Loading branch information
WoLewicki authored and facebook-github-bot committed Jun 6, 2024
1 parent 7781c27 commit b6f0750
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
15 changes: 15 additions & 0 deletions packages/react-native/React/CoreModules/RCTEventDispatcher.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ - (void)initialize
_eventsDispatchScheduled = NO;
_observers = [NSHashTable weakObjectsHashTable];
_observersLock = [NSRecursiveLock new];

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(_notifyEventDispatcherObserversOfEvent_DEPRECATED:)
name:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil];
}

- (void)sendViewEventWithName:(NSString *)name reactTag:(NSNumber *)reactTag
Expand Down Expand Up @@ -225,6 +231,15 @@ - (void)flushEventsQueue
}
}

- (void)_notifyEventDispatcherObserversOfEvent_DEPRECATED:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
id<RCTEvent> event = [userInfo objectForKey:@"event"];
if (event) {
[self notifyObserversOfEvent:event];
}
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,10 @@ static void RCTSendScrollEventForNativeAnimations_DEPRECATED(UIScrollView *scrol
scrollViewZoomScale:scrollView.zoomScale
userData:nil
coalescingKey:coalescingKey];
RCTBridge *bridge = [RCTBridge currentBridge];
if (bridge) {
[bridge.eventDispatcher sendEvent:scrollEvent];
} else {
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:scrollEvent, @"event", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil
userInfo:userInfo];
}
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:scrollEvent, @"event", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil
userInfo:userInfo];
}

@interface RCTScrollViewComponentView () <
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,6 @@ - (instancetype)initWithDelegate:(id<RCTInstanceDelegate>)delegate
}
_launchOptions = launchOptions;

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

[defaultCenter addObserver:self
selector:@selector(_notifyEventDispatcherObserversOfEvent_DEPRECATED:)
name:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil];

[self _start];
}
return self;
Expand Down Expand Up @@ -481,19 +474,6 @@ - (void)_loadScriptFromSource:(RCTSource *)source
}
}

- (void)_notifyEventDispatcherObserversOfEvent_DEPRECATED:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
id<RCTEvent> event = [userInfo objectForKey:@"event"];

RCTModuleRegistry *moduleRegistry = _bridgeModuleDecorator.moduleRegistry;
if (event && moduleRegistry) {
id<RCTEventDispatcherProtocol> legacyEventDispatcher = [moduleRegistry moduleForName:"EventDispatcher"
lazilyLoadIfNecessary:YES];
[legacyEventDispatcher notifyObserversOfEvent:event];
}
}

- (void)_handleJSError:(const JsErrorHandler::ParsedError &)error
{
NSString *message = [NSString stringWithCString:error.message.c_str() encoding:[NSString defaultCStringEncoding]];
Expand Down

0 comments on commit b6f0750

Please sign in to comment.