Skip to content

Commit

Permalink
fix r & d not working from Metro sometimes
Browse files Browse the repository at this point in the history
Summary:
Changelog:
[iOS][Fixed] - fix `r` & `d` not working from Metro sometimes

While investigating these bugs, I've come across some cases where `r` (Reload) & `d` (Open Dev Menu) not working in Metro.

* T206141946 / [WP: Reconnecting dev tools does not work after restarting the app](https://fb.workplace.com/groups/rn.debugger.feedback/posts/1107620434125533)
* T206754760 / [WP: Can't launch DevTools from Metro sometimes](https://fb.workplace.com/groups/rn.debugger.feedback/posts/1112235073664069/)

This is because when we
1. Start app without Metro
1. Start Metro
1. Reload from Dev Menu (rage shake)

`RCTPackagerConnection` did not get notified about the change in bundle URL. It'd stay "listening" to the commands from the local bundle instead of Metro
.

Differential Revision: D65973309
  • Loading branch information
EdmondChuiHW authored and facebook-github-bot committed Nov 14, 2024
1 parent 1afde8b commit c90ad74
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/react-native/React/DevSupport/RCTPackagerConnection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import <React/RCTLog.h>
#import <React/RCTPackagerClient.h>
#import <React/RCTReconnectingWebSocket.h>
#import <React/RCTReloadCommand.h>
#import <React/RCTUtils.h>

#if RCT_DEV
Expand All @@ -44,6 +45,7 @@ @implementation RCTPackagerConnection {
NSString *_serverHostPortForSocket;
NSString *_serverSchemeForSocket;
id _bundleURLChangeObserver;
id _reloadWithPotentiallyNewURLObserver;
uint32_t _nextToken;
std::vector<Registration<RCTNotificationHandler>> _notificationRegistrations;
std::vector<Registration<RCTRequestHandler>> _requestRegistrations;
Expand Down Expand Up @@ -78,6 +80,13 @@ - (instancetype)init
usingBlock:^(NSNotification *_Nonnull __unused note) {
[weakSelf bundleURLSettingsChanged];
}];
_reloadWithPotentiallyNewURLObserver =
[[NSNotificationCenter defaultCenter] addObserverForName:RCTTriggerReloadCommandNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *_Nonnull __unused note) {
[weakSelf bundleURLSettingsChanged];
}];
}
return self;
}
Expand Down Expand Up @@ -119,6 +128,8 @@ - (void)stop
}
[[NSNotificationCenter defaultCenter] removeObserver:_bundleURLChangeObserver];
_bundleURLChangeObserver = nil;
[[NSNotificationCenter defaultCenter] removeObserver:_reloadWithPotentiallyNewURLObserver];
_reloadWithPotentiallyNewURLObserver = nil;
_socketConnected = NO;
[_socket stop];
_socket = nil;
Expand Down

0 comments on commit c90ad74

Please sign in to comment.