From 74fd6fb1da10fc31d6cb82e19d7eda46a93ffdac Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Fri, 11 Dec 2020 10:39:18 -0800 Subject: [PATCH] Codemod: Migrate from bridge.eventDispatcher to RCTModuleRegistry Summary: All NativeModules that use the bridge to require the eventDispatcher are now instead using the RCTModuleRegistry I introduced in D25412847 (https://github.com/facebook/react-native/commit/0ed81b28d3d786ea3b1cf0b932a008ef1f806ec4). ## What does this codemod do? For all ObjC files that contain `synthesize bridge = _bridge`, migrate calls that access the React Native bridge from `self`, and use it to load the event dispatcher. **Thoughts on Codemod Safety:** If we can access the bridge from self, then that means that if we synthesize the module registry, we can access the module registry from self. Therefore, this codemod is safe. Changelog: [Internal] Reviewed By: PeteTheHeat Differential Revision: D25449795 fbshipit-source-id: 2f7235d14659e73d673ae08763dc2cccdde55a19 --- Libraries/Settings/RCTSettingsManager.mm | 3 ++- React/CoreModules/RCTAccessibilityManager.mm | 21 +++++++++++++------- React/CoreModules/RCTDeviceInfo.mm | 9 ++++++--- React/CoreModules/RCTRedBox.mm | 4 +++- React/Modules/RCTUIManager.m | 7 +++++-- packages/rn-tester/RCTTest/RCTTestModule.mm | 3 ++- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Libraries/Settings/RCTSettingsManager.mm b/Libraries/Settings/RCTSettingsManager.mm index 8628857bc8a..1adf33c4477 100644 --- a/Libraries/Settings/RCTSettingsManager.mm +++ b/Libraries/Settings/RCTSettingsManager.mm @@ -25,6 +25,7 @@ } @synthesize bridge = _bridge; +@synthesize moduleRegistry = _moduleRegistry; RCT_EXPORT_MODULE() @@ -72,7 +73,7 @@ RCT_EXPORT_MODULE() #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"settingsUpdated" body:RCTJSONClean([_defaults dictionaryRepresentation])]; #pragma clang diagnostic pop diff --git a/React/CoreModules/RCTAccessibilityManager.mm b/React/CoreModules/RCTAccessibilityManager.mm index fc1741c21ee..7426794b925 100644 --- a/React/CoreModules/RCTAccessibilityManager.mm +++ b/React/CoreModules/RCTAccessibilityManager.mm @@ -29,6 +29,7 @@ NSString *const RCTAccessibilityManagerDidUpdateMultiplierNotification = @implementation RCTAccessibilityManager @synthesize bridge = _bridge; +@synthesize moduleRegistry = _moduleRegistry; @synthesize multipliers = _multipliers; RCT_EXPORT_MODULE() @@ -111,7 +112,7 @@ RCT_EXPORT_MODULE() #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher sendDeviceEventWithName:@"announcementFinished" body:response]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"announcementFinished" body:response]; #pragma clang diagnostic pop } @@ -122,7 +123,8 @@ RCT_EXPORT_MODULE() _isBoldTextEnabled = newBoldTextEnabled; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher sendDeviceEventWithName:@"boldTextChanged" body:@(_isBoldTextEnabled)]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"boldTextChanged" + body:@(_isBoldTextEnabled)]; #pragma clang diagnostic pop } } @@ -134,7 +136,8 @@ RCT_EXPORT_MODULE() _isGrayscaleEnabled = newGrayscaleEnabled; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher sendDeviceEventWithName:@"grayscaleChanged" body:@(_isGrayscaleEnabled)]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"grayscaleChanged" + body:@(_isGrayscaleEnabled)]; #pragma clang diagnostic pop } } @@ -146,7 +149,8 @@ RCT_EXPORT_MODULE() _isInvertColorsEnabled = newInvertColorsEnabled; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher sendDeviceEventWithName:@"invertColorsChanged" body:@(_isInvertColorsEnabled)]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"invertColorsChanged" + body:@(_isInvertColorsEnabled)]; #pragma clang diagnostic pop } } @@ -158,7 +162,8 @@ RCT_EXPORT_MODULE() _isReduceMotionEnabled = newReduceMotionEnabled; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher sendDeviceEventWithName:@"reduceMotionChanged" body:@(_isReduceMotionEnabled)]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"reduceMotionChanged" + body:@(_isReduceMotionEnabled)]; #pragma clang diagnostic pop } } @@ -170,7 +175,8 @@ RCT_EXPORT_MODULE() _isReduceTransparencyEnabled = newReduceTransparencyEnabled; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher sendDeviceEventWithName:@"reduceTransparencyChanged" body:@(_isReduceTransparencyEnabled)]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"reduceTransparencyChanged" + body:@(_isReduceTransparencyEnabled)]; #pragma clang diagnostic pop } } @@ -182,7 +188,8 @@ RCT_EXPORT_MODULE() _isVoiceOverEnabled = newIsVoiceOverEnabled; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher sendDeviceEventWithName:@"screenReaderChanged" body:@(_isVoiceOverEnabled)]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"screenReaderChanged" + body:@(_isVoiceOverEnabled)]; #pragma clang diagnostic pop } } diff --git a/React/CoreModules/RCTDeviceInfo.mm b/React/CoreModules/RCTDeviceInfo.mm index b102059c279..7d540950e7e 100644 --- a/React/CoreModules/RCTDeviceInfo.mm +++ b/React/CoreModules/RCTDeviceInfo.mm @@ -28,6 +28,7 @@ using namespace facebook::react; } @synthesize bridge = _bridge; +@synthesize moduleRegistry = _moduleRegistry; @synthesize turboModuleRegistry = _turboModuleRegistry; RCT_EXPORT_MODULE() @@ -180,8 +181,9 @@ static NSDictionary *RCTExportedDimensions(RCTBridge *bridge, id_bridge.eventDispatcher sendDeviceEventWithName:@"collectRedBoxExtraData" body:nil]; + [[self->_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"collectRedBoxExtraData" + body:nil]; #pragma clang diagnostic pop if (!self->_window) { diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index b6dfefa3e60..74579d2ba07 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -81,6 +81,7 @@ NSString *const RCTUIManagerWillUpdateViewsDueToContentSizeMultiplierChangeNotif } @synthesize bridge = _bridge; +@synthesize moduleRegistry = _moduleRegistry; RCT_EXPORT_MODULE() @@ -196,7 +197,8 @@ RCT_EXPORT_MODULE() id multiplier = [[self->_bridge moduleForName:@"AccessibilityManager" lazilyLoadIfNecessary:YES] valueForKey:@"multiplier"]; if (multiplier) { - [_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateContentSizeMultiplier" body:multiplier]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"didUpdateContentSizeMultiplier" + body:multiplier]; } #pragma clang diagnostic pop @@ -256,7 +258,8 @@ static NSDictionary *deviceOrientationEventBody(UIDeviceOrientation orientation) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher sendDeviceEventWithName:@"namedOrientationDidChange" body:orientationEvent]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"namedOrientationDidChange" + body:orientationEvent]; #pragma clang diagnostic pop } diff --git a/packages/rn-tester/RCTTest/RCTTestModule.mm b/packages/rn-tester/RCTTest/RCTTestModule.mm index 33e5f090983..97db1f32a2b 100644 --- a/packages/rn-tester/RCTTest/RCTTestModule.mm +++ b/packages/rn-tester/RCTTest/RCTTestModule.mm @@ -79,6 +79,7 @@ namespace facebook { } @synthesize bridge = _bridge; +@synthesize moduleRegistry = _moduleRegistry; RCT_EXPORT_MODULE() @@ -120,7 +121,7 @@ RCT_EXPORT_METHOD(sendAppEvent:(NSString *)name body:(nullable id)body) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_bridge.eventDispatcher sendAppEventWithName:name body:body]; + [[_moduleRegistry moduleForName:"EventDispatcher"] sendAppEventWithName:name body:body]; #pragma clang diagnostic pop }