From b5db214d2a6cc6e2336edd423ceeaec06dd7b4fc Mon Sep 17 00:00:00 2001 From: Soe Lynn Date: Thu, 4 Apr 2024 20:27:32 -0700 Subject: [PATCH] Deprecate RedBox from RCTBridge to RCTModuleRegistry (#43717) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43717 As we are going to bridgeless in new architecture, we want to clean up the usage of RCTBridge to use RCTModuleRegistry to access NativeModule. Changelog: [iOS][Breaking] Remove `RCTRedBox` access through `RCTBridge` Reviewed By: philIip Differential Revision: D55532209 fbshipit-source-id: 62aa2a24b60ab54d7f3cf25c34beda4449aaeaed --- .../react-native/React/Base/RCTBridgeModule.h | 3 ++ .../React/Base/RCTModuleRegistry.m | 5 +++ .../React/CoreModules/RCTRedBox.h | 18 --------- .../React/CoreModules/RCTRedBox.mm | 38 ------------------- .../React/CxxBridge/RCTCxxBridge.mm | 6 ++- 5 files changed, 12 insertions(+), 58 deletions(-) diff --git a/packages/react-native/React/Base/RCTBridgeModule.h b/packages/react-native/React/Base/RCTBridgeModule.h index 6805512777d..9446059be48 100644 --- a/packages/react-native/React/Base/RCTBridgeModule.h +++ b/packages/react-native/React/Base/RCTBridgeModule.h @@ -370,6 +370,9 @@ RCT_EXTERN_C_END - (id)moduleForName:(const char *)moduleName; - (id)moduleForName:(const char *)moduleName lazilyLoadIfNecessary:(BOOL)lazilyLoad; - (BOOL)moduleIsInitialized:(Class)moduleClass; + +// Note: This method lazily load the module as necessary. +- (id)moduleForClass:(Class)moduleClass; @end typedef UIView * (^RCTBridgelessComponentViewProvider)(NSNumber *); diff --git a/packages/react-native/React/Base/RCTModuleRegistry.m b/packages/react-native/React/Base/RCTModuleRegistry.m index 4b2c15e0a5d..1ebc5a14c77 100644 --- a/packages/react-native/React/Base/RCTModuleRegistry.m +++ b/packages/react-native/React/Base/RCTModuleRegistry.m @@ -64,4 +64,9 @@ return NO; } +- (id)moduleForClass:(Class)moduleClass +{ + return [self moduleForName:RCTBridgeModuleNameForClass(moduleClass).UTF8String]; +} + @end diff --git a/packages/react-native/React/CoreModules/RCTRedBox.h b/packages/react-native/React/CoreModules/RCTRedBox.h index e4eed8732c4..a8031aa3aed 100644 --- a/packages/react-native/React/CoreModules/RCTRedBox.h +++ b/packages/react-native/React/CoreModules/RCTRedBox.h @@ -7,9 +7,7 @@ #import -#import #import -#import #import @class RCTJSStackFrame; @@ -48,19 +46,3 @@ typedef void (^RCTRedBoxButtonPressHandler)(void); @property (nonatomic, strong) dispatch_block_t overrideReloadAction; @end - -/** - * This category makes the red box instance available via the RCTBridge, which - * is useful for any class that needs to access the red box or error log. - */ -@interface RCTBridge (RCTRedBox) - -@property (nonatomic, readonly) RCTRedBox *redBox; - -@end - -@interface RCTBridgeProxy (RCTRedBox) - -@property (nonatomic, readonly) RCTRedBox *redBox; - -@end diff --git a/packages/react-native/React/CoreModules/RCTRedBox.mm b/packages/react-native/React/CoreModules/RCTRedBox.mm index 15cf2f023aa..5c40dbbcd33 100644 --- a/packages/react-native/React/CoreModules/RCTRedBox.mm +++ b/packages/react-native/React/CoreModules/RCTRedBox.mm @@ -8,14 +8,12 @@ #import "RCTRedBox.h" #import -#import #import #import #import #import #import #import -#import #import #import @@ -712,24 +710,6 @@ RCT_EXPORT_METHOD(dismiss) @end -@implementation RCTBridge (RCTRedBox) - -- (RCTRedBox *)redBox -{ - return RCTRedBoxGetEnabled() ? [self moduleForClass:[RCTRedBox class]] : nil; -} - -@end - -@implementation RCTBridgeProxy (RCTRedBox) - -- (RCTRedBox *)redBox -{ - return RCTRedBoxGetEnabled() ? [self moduleForClass:[RCTRedBox class]] : nil; -} - -@end - #else // Disabled @interface RCTRedBox () @@ -806,24 +786,6 @@ RCT_EXPORT_METHOD(dismiss) @end -@implementation RCTBridge (RCTRedBox) - -- (RCTRedBox *)redBox -{ - return nil; -} - -@end - -@implementation RCTBridgeProxy (RCTRedBox) - -- (RCTRedBox *)redBox -{ - return nil; -} - -@end - #endif Class RCTRedBoxCls(void) diff --git a/packages/react-native/React/CxxBridge/RCTCxxBridge.mm b/packages/react-native/React/CxxBridge/RCTCxxBridge.mm index 310cdcb8cca..34fa43f8856 100644 --- a/packages/react-native/React/CxxBridge/RCTCxxBridge.mm +++ b/packages/react-native/React/CxxBridge/RCTCxxBridge.mm @@ -29,6 +29,7 @@ #import #import #import +#import #import #import #import @@ -1085,7 +1086,8 @@ struct RCTInstanceCallback : public InstanceCallback { if (self->_valid && !self->_loading) { if ([error userInfo][RCTJSRawStackTraceKey]) { - [self.redBox showErrorMessage:[error localizedDescription] withRawStack:[error userInfo][RCTJSRawStackTraceKey]]; + RCTRedBox *redBox = RCTRedBoxGetEnabled() ? [self.moduleRegistry moduleForName:"RedBox"] : nil; + [redBox showErrorMessage:[error localizedDescription] withRawStack:[error userInfo][RCTJSRawStackTraceKey]]; } RCTFatal(error); @@ -1100,7 +1102,7 @@ struct RCTInstanceCallback : public InstanceCallback { // Hack: once the bridge is invalidated below, it won't initialize any new native // modules. Initialize the redbox module now so we can still report this error. - RCTRedBox *redBox = [self redBox]; + RCTRedBox *redBox = RCTRedBoxGetEnabled() ? [self.moduleRegistry moduleForName:"RedBox"] : nil; _loading = NO; _valid = NO;