mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
Added RCTBundleURLProvider
Reviewed By: javache Differential Revision: D3352568 fbshipit-source-id: fbba6771a1c581e2676bd0f81d3da62dbf21916b
This commit is contained in:
committed by
Facebook Github Bot 6
parent
c2c370c886
commit
3ccd99fb53
38
React/Base/RCTBundleURLProvider.h
Normal file
38
React/Base/RCTBundleURLProvider.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface RCTBundleURLProvider : NSObject
|
||||
|
||||
extern NSString *const RCTBundleURLProviderUpdatedNotification;
|
||||
|
||||
/**
|
||||
* Set default settings on NSUserDefaults.
|
||||
*/
|
||||
- (void)setDefaults;
|
||||
|
||||
/**
|
||||
* Returns the jsBundleURL for a given bundle entrypoint and
|
||||
* the fallback offline JS bundle if the packager is not running.
|
||||
*/
|
||||
- (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot
|
||||
fallbackResource:(NSString *)resourceName;
|
||||
|
||||
/**
|
||||
* The IP address or hostname of the packager.
|
||||
*/
|
||||
@property (nonatomic, copy) NSString *jsLocation;
|
||||
|
||||
@property (nonatomic, assign) BOOL enableLiveReload;
|
||||
@property (nonatomic, assign) BOOL enableMinification;
|
||||
@property (nonatomic, assign) BOOL enableDev;
|
||||
|
||||
+ (instancetype)sharedSettings;
|
||||
@end
|
||||
167
React/Base/RCTBundleURLProvider.m
Normal file
167
React/Base/RCTBundleURLProvider.m
Normal file
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#import "RCTBundleURLProvider.h"
|
||||
#import "RCTDefines.h"
|
||||
#import "RCTConvert.h"
|
||||
|
||||
NSString *const RCTBundleURLProviderUpdatedNotification = @"RCTBundleURLProviderUpdatedNotification";
|
||||
|
||||
static NSString *const kRCTJsLocationKey = @"RCT_jsLocation";
|
||||
static NSString *const kRCTEnableLiveReloadKey = @"RCT_enableLiveReload";
|
||||
static NSString *const kRCTEnableDevKey = @"RCT_enableDev";
|
||||
static NSString *const kRCTEnableMinificationKey = @"RCT_enableMinification";
|
||||
|
||||
static NSString *const kDefaultPort = @"8081";
|
||||
|
||||
@implementation RCTBundleURLProvider
|
||||
|
||||
- (NSDictionary *)defaults
|
||||
{
|
||||
static NSDictionary *defaults;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
defaults = @{
|
||||
kRCTEnableLiveReloadKey: @NO,
|
||||
kRCTEnableDevKey: @YES,
|
||||
kRCTEnableMinificationKey: @NO,
|
||||
};
|
||||
});
|
||||
return defaults;
|
||||
}
|
||||
|
||||
- (void)settingsUpdated
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RCTBundleURLProviderUpdatedNotification object:self];
|
||||
}
|
||||
|
||||
- (void)setDefaults
|
||||
{
|
||||
[[NSUserDefaults standardUserDefaults] registerDefaults:[self defaults]];
|
||||
[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];
|
||||
}
|
||||
|
||||
- (NSString *)guessPackagerHost
|
||||
{
|
||||
NSString *host = @"localhost";
|
||||
//TODO: Implement automatic IP address detection
|
||||
if ([self isPackagerRunning:host]) {
|
||||
return host;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
- (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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot fallbackResource:(NSString *)resourceName
|
||||
{
|
||||
resourceName = resourceName ?: @"main";
|
||||
NSString *serverRoot = [self packagerServerRoot];
|
||||
if (!serverRoot) {
|
||||
return [[NSBundle mainBundle] URLForResource:resourceName withExtension:@"jsbundle"];
|
||||
} else {
|
||||
NSString *fullBundlePath = [serverRoot stringByAppendingFormat:@"%@.bundle", bundleRoot];
|
||||
if ([fullBundlePath hasPrefix:@"http"]) {
|
||||
NSString *dev = [self enableDev] ? @"true" : @"false";
|
||||
NSString *min = [self enableMinification] ? @"true": @"false";
|
||||
fullBundlePath = [fullBundlePath stringByAppendingFormat:@"?platform=ios&dev=%@&minify=%@", dev, min];
|
||||
}
|
||||
return [NSURL URLWithString:fullBundlePath];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)updateDefaults:(id)object forKey:(NSString *)key
|
||||
{
|
||||
[[NSUserDefaults standardUserDefaults] setObject:object forKey:key];
|
||||
[[NSUserDefaults standardUserDefaults] synchronize];
|
||||
[self settingsUpdated];
|
||||
}
|
||||
|
||||
- (BOOL)enableDev
|
||||
{
|
||||
return [[NSUserDefaults standardUserDefaults] boolForKey:kRCTEnableDevKey];
|
||||
}
|
||||
|
||||
- (BOOL)enableLiveReload
|
||||
{
|
||||
return [[NSUserDefaults standardUserDefaults] boolForKey:kRCTEnableLiveReloadKey];
|
||||
}
|
||||
|
||||
- (BOOL)enableMinification
|
||||
{
|
||||
return [[NSUserDefaults standardUserDefaults] boolForKey:kRCTEnableMinificationKey];
|
||||
}
|
||||
|
||||
- (NSString *)jsLocation
|
||||
{
|
||||
return [[NSUserDefaults standardUserDefaults] stringForKey:kRCTJsLocationKey];
|
||||
}
|
||||
|
||||
- (void)setEnableDev:(BOOL)enableDev
|
||||
{
|
||||
[self updateDefaults:@(enableDev) forKey:kRCTEnableDevKey];
|
||||
}
|
||||
|
||||
- (void)setEnableEnableLiveReload:(BOOL)enableLiveReload
|
||||
{
|
||||
[self updateDefaults:@(enableLiveReload) forKey:kRCTEnableLiveReloadKey];
|
||||
}
|
||||
|
||||
- (void)setJsLocation:(NSString *)jsLocation
|
||||
{
|
||||
[self updateDefaults:jsLocation forKey:kRCTJsLocationKey];
|
||||
}
|
||||
|
||||
- (void)setEnableMinification:(BOOL)enableMinification
|
||||
{
|
||||
[self updateDefaults:@(enableMinification) forKey:kRCTEnableMinificationKey];
|
||||
}
|
||||
|
||||
+ (instancetype)sharedSettings
|
||||
{
|
||||
static RCTBundleURLProvider *sharedInstance;
|
||||
static dispatch_once_t once_token;
|
||||
dispatch_once(&once_token, ^{
|
||||
sharedInstance = [RCTBundleURLProvider new];
|
||||
});
|
||||
return sharedInstance;
|
||||
}
|
||||
@end
|
||||
Reference in New Issue
Block a user