mirror of
https://github.com/status-im/react-native.git
synced 2025-01-28 02:04:55 +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 kRCTEnableMinificationKey = @"RCT_enableMinification";
|
||||||
|
|
||||||
static NSString *const kDefaultPort = @"8081";
|
static NSString *const kDefaultPort = @"8081";
|
||||||
static NSString *ipGuess;
|
|
||||||
|
|
||||||
@implementation RCTBundleURLProvider
|
@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
|
- (instancetype)init
|
||||||
{
|
{
|
||||||
self = [super init];
|
self = [super init];
|
||||||
@ -69,46 +59,53 @@ static NSString *ipGuess;
|
|||||||
[self settingsUpdated];
|
[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)
|
static NSString *serverRootWithHost(NSString *host)
|
||||||
{
|
{
|
||||||
return [NSString stringWithFormat:@"http://%@:%@/", host, kDefaultPort];
|
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
|
- (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";
|
NSString *host = ipGuess ?: @"localhost";
|
||||||
if ([self isPackagerRunning:host]) {
|
if ([self isPackagerRunning:host]) {
|
||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
- (NSString *)packagerServerRoot
|
- (NSString *)packagerServerRoot
|
||||||
{
|
{
|
||||||
NSString *location = [self jsLocation];
|
NSString *location = [self jsLocation];
|
||||||
if (location != nil) {
|
if (location != nil) {
|
||||||
return serverRootWithHost(location);
|
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
|
- (NSURL *)packagerServerURL
|
||||||
@ -190,4 +187,5 @@ static NSString *serverRootWithHost(NSString *host)
|
|||||||
});
|
});
|
||||||
return sharedInstance;
|
return sharedInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user