Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes iOS reload through metro "r" command key #28477

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions React/Base/RCTBundleURLProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ extern const NSUInteger kRCTBundleURLProviderDefaultPort;
*/
- (void)resetToDefaults;

/**
* Return the server host. If its a development build and there's no jsLocation defined,
* it will return the server host IP address
*/
- (NSString *)packagerServerHost;

#if RCT_DEV
- (BOOL)isPackagerRunning:(NSString *)host;
#endif
Expand Down
18 changes: 9 additions & 9 deletions React/DevSupport/RCTPackagerConnection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ @implementation RCTPackagerConnection {
std::mutex _mutex; // protects all ivars
RCTReconnectingWebSocket *_socket;
BOOL _socketConnected;
NSString *_jsLocationForSocket;
NSString *_serverHostForSocket;
id _bundleURLChangeObserver;
uint32_t _nextToken;
std::vector<Registration<RCTNotificationHandler>> _notificationRegistrations;
Expand All @@ -62,8 +62,8 @@ - (instancetype)init
{
if (self = [super init]) {
_nextToken = 1; // Prevent randomly erasing a handler if you pass a bogus 0 token
_jsLocationForSocket = [RCTBundleURLProvider sharedSettings].jsLocation;
_socket = socketForLocation(_jsLocationForSocket);
_serverHostForSocket = [[RCTBundleURLProvider sharedSettings] packagerServerHost];
_socket = socketForLocation(_serverHostForSocket);
_socket.delegate = self;
[_socket start];

Expand All @@ -79,10 +79,10 @@ - (instancetype)init
return self;
}

static RCTReconnectingWebSocket *socketForLocation(NSString *const jsLocation)
static RCTReconnectingWebSocket *socketForLocation(NSString *const serverHost)
{
NSURLComponents *const components = [NSURLComponents new];
components.host = jsLocation ?: @"localhost";
components.host = serverHost ?: @"localhost";
components.scheme = @"http";
components.port = @(kRCTBundleURLProviderDefaultPort);
components.path = @"/message";
Expand Down Expand Up @@ -118,15 +118,15 @@ - (void)bundleURLSettingsChanged
return; // already stopped
}

NSString *const jsLocation = [RCTBundleURLProvider sharedSettings].jsLocation;
if ([jsLocation isEqual:_jsLocationForSocket]) {
NSString *const serverHost = [[RCTBundleURLProvider sharedSettings] packagerServerHost];
if ([serverHost isEqual:_serverHostForSocket]) {
return; // unchanged
}

_socket.delegate = nil;
[_socket stop];
_jsLocationForSocket = jsLocation;
_socket = socketForLocation(jsLocation);
_serverHostForSocket = serverHost;
_socket = socketForLocation(serverHost);
_socket.delegate = self;
[_socket start];
}
Expand Down