mirror of
https://github.com/status-im/react-native.git
synced 2025-01-27 01:40:08 +00:00
Refactor RCTBundleURLProvider
Summary: - Avoid using `+initialize`, use dispatch_once instead; it should be equivalent but more performant. - Don't even let the `isPackagerRunning:` and `guessPackagerHost` methods exist unless `RCT_DEV` is on; they didn't do anything interesting when it is off. Reviewed By: javache Differential Revision: D3556645 fbshipit-source-id: 7dcdb4ae27f6625010e15846d757269f6f04155c
This commit is contained in:
parent
235dd0051d
commit
b4e267a993
@ -19,19 +19,9 @@ static NSString *const kRCTEnableDevKey = @"RCT_enableDev";
|
||||
static NSString *const kRCTEnableMinificationKey = @"RCT_enableMinification";
|
||||
|
||||
static NSString *const kDefaultPort = @"8081";
|
||||
static NSString *ipGuess;
|
||||
|
||||
@implementation RCTBundleURLProvider
|
||||
|
||||
#if RCT_DEV
|
||||
+ (void)initialize
|
||||
{
|
||||
NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"];
|
||||
NSString *ip = [NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil];
|
||||
ipGuess = [ip stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
|
||||
}
|
||||
#endif
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
@ -69,46 +59,53 @@ static NSString *ipGuess;
|
||||
[self settingsUpdated];
|
||||
}
|
||||
|
||||
- (BOOL)isPackagerRunning:(NSString *)host
|
||||
{
|
||||
if (RCT_DEV) {
|
||||
NSURL *url = [[NSURL URLWithString:serverRootWithHost(host)] URLByAppendingPathComponent:@"status"];
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:url];
|
||||
NSURLResponse *response;
|
||||
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
|
||||
NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
return [status isEqualToString:@"packager-status:running"];
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
static NSString *serverRootWithHost(NSString *host)
|
||||
{
|
||||
return [NSString stringWithFormat:@"http://%@:%@/", host, kDefaultPort];
|
||||
}
|
||||
|
||||
#if RCT_DEV
|
||||
- (BOOL)isPackagerRunning:(NSString *)host
|
||||
{
|
||||
NSURL *url = [[NSURL URLWithString:serverRootWithHost(host)] URLByAppendingPathComponent:@"status"];
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:url];
|
||||
NSURLResponse *response;
|
||||
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
|
||||
NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
return [status isEqualToString:@"packager-status:running"];
|
||||
}
|
||||
|
||||
- (NSString *)guessPackagerHost
|
||||
{
|
||||
static NSString *ipGuess;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"];
|
||||
ipGuess = [[NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil]
|
||||
stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
|
||||
});
|
||||
|
||||
NSString *host = ipGuess ?: @"localhost";
|
||||
if ([self isPackagerRunning:host]) {
|
||||
return host;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
#endif
|
||||
|
||||
- (NSString *)packagerServerRoot
|
||||
{
|
||||
NSString *location = [self jsLocation];
|
||||
if (location != nil) {
|
||||
return serverRootWithHost(location);
|
||||
} else {
|
||||
NSString *host = [self guessPackagerHost];
|
||||
if (!host) {
|
||||
return nil;
|
||||
} else {
|
||||
return serverRootWithHost(host);
|
||||
}
|
||||
}
|
||||
#if RCT_DEV
|
||||
NSString *host = [self guessPackagerHost];
|
||||
if (host) {
|
||||
return serverRootWithHost(host);
|
||||
}
|
||||
#endif
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSURL *)packagerServerURL
|
||||
@ -190,4 +187,5 @@ static NSString *serverRootWithHost(NSString *host)
|
||||
});
|
||||
return sharedInstance;
|
||||
}
|
||||
|
||||
@end
|
||||
|
Loading…
x
Reference in New Issue
Block a user