From f5c888c2d7c6dfe009de5df2cc795aff26286c19 Mon Sep 17 00:00:00 2001 From: Wojciech Lewicki Date: Fri, 7 Jun 2024 03:15:09 -0700 Subject: [PATCH] feat: move notifying observers to event dispatcher (#44474) 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: https://github.com/software-mansion/react-native-reanimated/issues/5497#issuecomment-2083400038. ## Changelog: [IOS] [CHANGED] - Move `notifyObservers` straight to `RCTEventDispatcher.mm`. Pull Request resolved: https://github.com/facebook/react-native/pull/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 Reviewed By: javache Differential Revision: D57097880 Pulled By: cipolleschi fbshipit-source-id: de1504e90529fe4f001f44f02ace329386cf7727 --- .../React/CoreModules/RCTEventDispatcher.mm | 15 ++++++++++++++ .../ScrollView/RCTScrollViewComponentView.mm | 13 ++++-------- .../platform/ios/ReactCommon/RCTInstance.mm | 20 ------------------- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTEventDispatcher.mm b/packages/react-native/React/CoreModules/RCTEventDispatcher.mm index 3669e2e7d64..36082d18200 100644 --- a/packages/react-native/React/CoreModules/RCTEventDispatcher.mm +++ b/packages/react-native/React/CoreModules/RCTEventDispatcher.mm @@ -54,6 +54,12 @@ RCT_EXPORT_MODULE() _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 @@ -225,6 +231,15 @@ RCT_EXPORT_MODULE() } } +- (void)_notifyEventDispatcherObserversOfEvent_DEPRECATED:(NSNotification *)notification +{ + NSDictionary *userInfo = notification.userInfo; + id event = [userInfo objectForKey:@"event"]; + if (event) { + [self notifyObserversOfEvent:event]; + } +} + - (std::shared_ptr)getTurboModule: (const facebook::react::ObjCTurboModule::InitParams &)params { diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index c1a2eeb1e00..98cf0495932 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -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 () < diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm index 957ab73cceb..1de5cff31d3 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm @@ -128,13 +128,6 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) } _launchOptions = launchOptions; - NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; - - [defaultCenter addObserver:self - selector:@selector(_notifyEventDispatcherObserversOfEvent_DEPRECATED:) - name:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED" - object:nil]; - [self _start]; } return self; @@ -481,19 +474,6 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) } } -- (void)_notifyEventDispatcherObserversOfEvent_DEPRECATED:(NSNotification *)notification -{ - NSDictionary *userInfo = notification.userInfo; - id event = [userInfo objectForKey:@"event"]; - - RCTModuleRegistry *moduleRegistry = _bridgeModuleDecorator.moduleRegistry; - if (event && moduleRegistry) { - id 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]];