Skip to content

Commit bc3fe0d

Browse files
EdmondChuiHWfacebook-github-bot
authored andcommitted
disable "Open Debugger" item when packager is disconnected (react#42873)
Summary: Pull Request resolved: react#42873 Changelog: [iOS][Fixed] Disable the "Open Debugger" item from dev menu if packager is disconnected # Existing In our dev menu, the "Open Debugger" menu item is shown even if the packager isn't connected. {F1434746954} # In this PR The "Open Debugger" menu item is disabled when the packager is disconnected. {F1451344668} # Reference * Also on Android: D53428914 Reviewed By: robhogan Differential Revision: D53354110 fbshipit-source-id: 6eb4e826fe9317798c704a5441b5e462edab1c4b
1 parent 9ba56f6 commit bc3fe0d

4 files changed

Lines changed: 32 additions & 14 deletions

File tree

packages/react-native/React/CoreModules/RCTDevMenu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ typedef NSString * (^RCTDevMenuItemTitleBlock)(void);
9898
*/
9999
+ (instancetype)buttonItemWithTitleBlock:(RCTDevMenuItemTitleBlock)titleBlock handler:(dispatch_block_t)handler;
100100

101+
@property (nonatomic, assign, getter=isDisabled) BOOL disabled;
102+
101103
@end
102104

103105
/**

packages/react-native/React/CoreModules/RCTDevMenu.mm

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,20 @@ - (void)setDefaultJSBundle
265265
#if RCT_ENABLE_INSPECTOR
266266
if (devSettings.isDeviceDebuggingAvailable) {
267267
// On-device JS debugging (CDP). Render action to open debugger frontend.
268-
[items
269-
addObject:
270-
[RCTDevMenuItem
271-
buttonItemWithTitleBlock:^NSString * {
272-
return @"Open Debugger";
273-
}
274-
handler:^{
275-
[RCTInspectorDevServerHelper
276-
openDebugger:bundleManager.bundleURL
277-
withErrorMessage:
278-
@"Failed to open debugger. Please check that the dev server is running and reload the app."];
279-
}]];
268+
BOOL isDisconnected = RCTInspectorDevServerHelper.isPackagerDisconnected;
269+
NSString *title = isDisconnected
270+
? [NSString stringWithFormat:@"Connect to %@ to debug JavaScript", RCT_PACKAGER_NAME]
271+
: @"Open Debugger";
272+
RCTDevMenuItem *item = [RCTDevMenuItem
273+
buttonItemWithTitle:title
274+
handler:^{
275+
[RCTInspectorDevServerHelper
276+
openDebugger:bundleManager.bundleURL
277+
withErrorMessage:
278+
@"Failed to open debugger. Please check that the dev server is running and reload the app."];
279+
}];
280+
[item setDisabled:isDisconnected];
281+
[items addObject:item];
280282
}
281283
#endif
282284
}
@@ -390,9 +392,11 @@ - (void)setDefaultJSBundle
390392

391393
NSArray<RCTDevMenuItem *> *items = [self _menuItemsToPresent];
392394
for (RCTDevMenuItem *item in items) {
393-
[_actionSheet addAction:[UIAlertAction actionWithTitle:item.title
395+
UIAlertAction *action = [UIAlertAction actionWithTitle:item.title
394396
style:UIAlertActionStyleDefault
395-
handler:[self alertActionHandlerForDevItem:item]]];
397+
handler:[self alertActionHandlerForDevItem:item]];
398+
[action setEnabled:!item.isDisabled];
399+
[_actionSheet addAction:action];
396400
}
397401

398402
[_actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel"

packages/react-native/React/DevSupport/RCTInspectorDevServerHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
+ (id<RCTInspectorPackagerConnectionProtocol>)connectWithBundleURL:(NSURL *)bundleURL;
1919
+ (void)disableDebugger;
20+
+ (BOOL)isPackagerDisconnected;
2021
+ (void)openDebugger:(NSURL *)bundleURL withErrorMessage:(NSString *)errorMessage;
2122
@end
2223

packages/react-native/React/DevSupport/RCTInspectorDevServerHelper.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ static void sendEventToAllConnections(NSString *event)
118118
}
119119
}
120120

121+
+ (BOOL)isPackagerDisconnected
122+
{
123+
for (NSString *socketId in socketConnections) {
124+
if ([socketConnections[socketId] isConnected]) {
125+
return false;
126+
}
127+
}
128+
129+
return true;
130+
}
131+
121132
+ (void)openDebugger:(NSURL *)bundleURL withErrorMessage:(NSString *)errorMessage
122133
{
123134
NSString *appId = [[[NSBundle mainBundle] bundleIdentifier]

0 commit comments

Comments
 (0)