Skip to content

Commit

Permalink
Fixes iOS reload through metro "r" command key (#28477)
Browse files Browse the repository at this point in the history
Summary:
This allows the iOS device to be reloaded through the metro command line, besides the fact that whenever packagerServerHost is called, it will only get the IP address once when debugging.

## Changelog

[iOS] [Fixed] - Fixed connection of metro reload command to iOS device
Pull Request resolved: #28477

Test Plan:
- Build any react-native project in debug mode to an iOS device connected through USB
- Press the “r” key on the terminal that is running metro
- The device should now reload the project

Reviewed By: cpojer

Differential Revision: D20818462

Pulled By: TheSavior

fbshipit-source-id: 6d9792447d205223dad8fbd955518885427cbba8
  • Loading branch information
reyalpsirc authored and facebook-github-bot committed Apr 3, 2020
1 parent 25836bc commit f9df933
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
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

0 comments on commit f9df933

Please sign in to comment.