From 9d193e8c7fd46c64b979c5d9842aa84ac2088275 Mon Sep 17 00:00:00 2001 From: Cristiano Santos Date: Fri, 3 Apr 2020 12:08:22 -0700 Subject: [PATCH] Fixes iOS reload through metro "r" command key (#28477) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/facebook/react-native/pull/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 --- React/Base/RCTBundleURLProvider.h | 6 ++++++ React/DevSupport/RCTPackagerConnection.mm | 18 +++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/React/Base/RCTBundleURLProvider.h b/React/Base/RCTBundleURLProvider.h index ba84d0ebb199b5..1e03949470bdb5 100644 --- a/React/Base/RCTBundleURLProvider.h +++ b/React/Base/RCTBundleURLProvider.h @@ -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 diff --git a/React/DevSupport/RCTPackagerConnection.mm b/React/DevSupport/RCTPackagerConnection.mm index 0586680570d45b..3be7e65c20ac9d 100644 --- a/React/DevSupport/RCTPackagerConnection.mm +++ b/React/DevSupport/RCTPackagerConnection.mm @@ -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> _notificationRegistrations; @@ -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]; @@ -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"; @@ -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]; }