Skip to content

Commit

Permalink
fix: RCTRedBox not appearing in Bridgeless when metro is not running
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Mar 5, 2024
1 parent 27b54bd commit b41ab3b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
8 changes: 7 additions & 1 deletion packages/react-native/React/CoreModules/RCTRedBox.mm
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,13 @@ - (void)dismiss

- (void)reload
{
[_actionDelegate reloadFromRedBoxController:self];
if (_actionDelegate != nil) {
[_actionDelegate reloadFromRedBoxController:self];
} else {
// In bridgeless mode `RCTRedBox` gets deallocted, we need to notify listeners anyway.
RCTTriggerReloadCommandListeners(@"Redbox");
[self dismiss];
}
}

- (void)showExtraDataViewController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#import <React/RCTEventDispatcherProtocol.h>
#import <React/RCTFollyConvert.h>
#import <React/RCTJavaScriptLoader.h>
#import <React/RCTReloadCommand.h>
#import <React/RCTLog.h>
#import <React/RCTRedBox.h>
#import <React/RCTLogBox.h>
#import <React/RCTModuleData.h>
#import <React/RCTPerformanceLogger.h>
Expand Down Expand Up @@ -64,7 +66,7 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags)
sRuntimeDiagnosticFlags = [flags copy];
}

@interface RCTInstance () <RCTTurboModuleManagerDelegate, RCTTurboModuleManagerRuntimeHandler>
@interface RCTInstance () <RCTTurboModuleManagerDelegate, RCTTurboModuleManagerRuntimeHandler, RCTReloadListener>
@end

@implementation RCTInstance {
Expand Down Expand Up @@ -339,6 +341,10 @@ - (void)_start
if (RCTGetUseNativeViewConfigsInBridgelessMode()) {
installLegacyUIManagerConstantsProviderBinding(runtime);
}

RCTExecuteOnMainQueue(^{
RCTRegisterReloadCommandListener(self);
});

[strongSelf->_delegate instance:strongSelf didInitializeRuntime:runtime];

Expand Down Expand Up @@ -389,6 +395,27 @@ - (void)_attachBridgelessAPIsToModule:(id<RCTTurboModule>)module
}
}

- (void)handleError:(NSError *)error
{
if (!_valid) {
return;
}

RCTRedBox *redBox = [self->_turboModuleManager moduleForName:"RedBox"];

RCTExecuteOnMainQueue(^{
[[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptDidFailToLoadNotification
object:self
userInfo:@{@"error": error}];

// TODO: Attach error raw stack RCTJSRawStackTraceKey
[redBox showErrorMessage:[error localizedDescription]];

RCTFatal(error);
});
}


- (void)_loadJSBundle:(NSURL *)sourceURL
{
#if RCT_DEV_MENU && __has_include(<React/RCTDevLoadingViewProtocol.h>)
Expand Down Expand Up @@ -420,8 +447,7 @@ - (void)_loadJSBundle:(NSURL *)sourceURL
}

if (error) {
// TODO(T91461138): Properly address bundle loading errors.
RCTLogError(@"RCTInstance: Error while loading bundle: %@", error);
[strongSelf handleError:error];
[strongSelf invalidate];
return;
}
Expand Down Expand Up @@ -490,4 +516,8 @@ - (void)_handleJSErrorMap:(facebook::react::MapBuffer)errorMap
isFatal:errorMap.getBool(JSErrorHandlerKey::kIsFatal)];
}

- (void)didReceiveReloadCommand {
[self _loadJSBundle:[self->_bridgeModuleDecorator.bundleManager bundleURL]];
}

@end

0 comments on commit b41ab3b

Please sign in to comment.