Converted RCTDevLoadingView to a module and fixed url display when using delegate

This commit is contained in:
Nick Lockwood 2015-08-08 02:58:30 -07:00
Родитель b82ac9bf07
Коммит 94caefa9c2
8 изменённых файлов: 142 добавлений и 171 удалений

Просмотреть файл

@ -70,7 +70,6 @@ id<RCTJavaScriptExecutor> RCTGetLatestExecutor(void)
NSMutableSet *_frameUpdateObservers;
NSMutableArray *_scheduledCalls;
RCTSparseArray *_scheduledCallbacks;
NSURL *_sourceURL;
}
@synthesize valid = _valid;
@ -114,8 +113,6 @@ id<RCTJavaScriptExecutor> RCTGetLatestExecutor(void)
- (void)start
{
_sourceURL = self.delegate ? [self.delegate sourceURLForBridge:_parentBridge] : self.bundleURL;
__weak RCTBatchedBridge *weakSelf = self;
__block NSString *sourceCode;
@ -198,9 +195,8 @@ id<RCTJavaScriptExecutor> RCTGetLatestExecutor(void)
if ([self.delegate respondsToSelector:@selector(loadSourceForBridge:withBlock:)]) {
[self.delegate loadSourceForBridge:_parentBridge withBlock:onSourceLoad];
} else if (_sourceURL) {
[RCTJavaScriptLoader loadBundleAtURL:_sourceURL
onComplete:onSourceLoad];
} else if (self.bundleURL) {
[RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onComplete:onSourceLoad];
} else {
// Allow testing without a script
dispatch_async(dispatch_get_main_queue(), ^{
@ -307,7 +303,6 @@ id<RCTJavaScriptExecutor> RCTGetLatestExecutor(void)
NSMutableDictionary *config = [[NSMutableDictionary alloc] init];
for (RCTModuleData *moduleData in _moduleDataByID) {
config[moduleData.name] = moduleData.config;
if ([moduleData.instance conformsToProtocol:@protocol(RCTFrameUpdateObserver)]) {
[_frameUpdateObservers addObject:moduleData];
}
@ -344,7 +339,7 @@ id<RCTJavaScriptExecutor> RCTGetLatestExecutor(void)
}
RCTSourceCode *sourceCodeModule = self.modules[RCTBridgeModuleNameForClass([RCTSourceCode class])];
sourceCodeModule.scriptURL = _sourceURL;
sourceCodeModule.scriptURL = self.bundleURL;
sourceCodeModule.scriptText = sourceCode;
static BOOL shouldDismiss = NO;
@ -356,7 +351,7 @@ id<RCTJavaScriptExecutor> RCTGetLatestExecutor(void)
shouldDismiss = YES;
});
[self enqueueApplicationScript:sourceCode url:_sourceURL onComplete:^(NSError *loadError) {
[self enqueueApplicationScript:sourceCode url:self.bundleURL onComplete:^(NSError *loadError) {
if (loadError) {
[[RCTRedBox sharedInstance] showError:loadError];
@ -818,7 +813,7 @@ RCT_NOT_IMPLEMENTED(-initWithBundleURL:(__unused NSURL *)bundleURL
{
RCTAssertMainThread();
if (![_sourceURL.scheme isEqualToString:@"http"]) {
if (![self.bundleURL.scheme isEqualToString:@"http"]) {
RCTLogError(@"To run the profiler you must be running from the dev server");
return;
}
@ -834,7 +829,7 @@ RCT_NOT_IMPLEMENTED(-initWithBundleURL:(__unused NSURL *)bundleURL
[_javaScriptExecutor executeBlockOnJavaScriptQueue:^{
NSString *log = RCTProfileEnd(self);
NSString *URLString = [NSString stringWithFormat:@"%@://%@:%@/profile", _sourceURL.scheme, _sourceURL.host, _sourceURL.port];
NSString *URLString = [NSString stringWithFormat:@"%@://%@:%@/profile", self.bundleURL.scheme, self.bundleURL.host, self.bundleURL.port];
NSURL *URL = [NSURL URLWithString:URLString];
NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:URL];
URLRequest.HTTPMethod = @"POST";

Просмотреть файл

@ -235,6 +235,7 @@ RCT_NOT_IMPLEMENTED(-init)
{
RCTAssertMainThread();
_bundleURL = [self.delegate sourceURLForBridge:self] ?: _bundleURL;
_batchedBridge = [[RCTBatchedBridge alloc] initWithParentBridge:self];
}

Просмотреть файл

@ -1,140 +0,0 @@
/**
* 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 <QuartzCore/QuartzCore.h>
#import "RCTBridge.h"
#import "RCTDevLoadingView.h"
#import "RCTDefines.h"
#import "RCTUtils.h"
#if RCT_DEV
@implementation RCTDevLoadingView
{
UIWindow *_window;
UILabel *_label;
NSDate *_showDate;
}
__attribute__((constructor))
static void RCTDevLoadingViewSetup()
{
[RCTDevLoadingView sharedInstance];
}
+ (instancetype)sharedInstance
{
static dispatch_once_t onceToken;
static RCTDevLoadingView *instance;
dispatch_once(&onceToken, ^{
instance = [[RCTDevLoadingView alloc] init];
});
return instance;
}
- (instancetype)init
{
if ((self = [super init]) && !RCTRunningInTestEnvironment()) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willStartLoading:)
name:RCTJavaScriptWillStartLoadingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didFinishLoading:)
name:RCTJavaScriptDidLoadNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didFinishLoading:)
name:RCTJavaScriptDidFailToLoadNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)willStartLoading:(NSNotification *)notification
{
NSURL *url = [[notification object] bundleURL];
dispatch_async(dispatch_get_main_queue(), ^{
[self showWithURL:url];
});
}
- (void)didFinishLoading:(__unused NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
[self hide];
});
}
- (void)showWithURL:(NSURL *)URL
{
_showDate = [NSDate date];
if (!_window) {
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
_window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 22)];
_window.backgroundColor = [UIColor blackColor];
_window.windowLevel = UIWindowLevelStatusBar + 1;
_label = [[UILabel alloc] initWithFrame:_window.bounds];
_label.font = [UIFont systemFontOfSize:12.0];
_label.textColor = [UIColor grayColor];
_label.textAlignment = NSTextAlignmentCenter;
[_window addSubview:_label];
[_window makeKeyAndVisible];
}
NSString *source;
if ([URL isFileURL]) {
source = @"pre-bundled file";
} else {
source = [NSString stringWithFormat:@"%@:%@", [URL host], [URL port]];
}
_label.text = [NSString stringWithFormat:@"Loading from %@...", source];
_window.hidden = NO;
}
- (void)hide
{
const NSTimeInterval MIN_PRESENTED_TIME = 0.6;
NSTimeInterval presentedTime = [[NSDate date] timeIntervalSinceDate:_showDate];
NSTimeInterval delay = MAX(0, MIN_PRESENTED_TIME - presentedTime);
CGRect windowFrame = _window.frame;
[UIView animateWithDuration:0.25
delay:delay
options:0
animations:^{
_window.frame = CGRectOffset(windowFrame, 0, -windowFrame.size.height);
} completion:^(__unused BOOL finished) {
_window.frame = windowFrame;
_window.hidden = YES;
}];
}
@end
#else
@implementation RCTDevLoadingView
+ (instancetype)sharedInstance { return nil; }
- (void)showWithURL:(NSURL *)URL {}
- (void)hide {}
@end
#endif

Просмотреть файл

@ -7,14 +7,8 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "RCTBridgeModule.h"
@interface RCTDevLoadingView : NSObject
+ (instancetype)sharedInstance;
- (void)showWithURL:(NSURL *)URL;
- (void)hide;
@interface RCTDevLoadingView : NSObject <RCTBridgeModule>
@end

Просмотреть файл

@ -0,0 +1,121 @@
/**
* 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 <QuartzCore/QuartzCore.h>
#import "RCTBridge.h"
#import "RCTDevLoadingView.h"
#import "RCTDefines.h"
#import "RCTUtils.h"
#if RCT_DEV
@implementation RCTDevLoadingView
{
UIWindow *_window;
UILabel *_label;
NSDate *_showDate;
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
- (instancetype)init
{
if ((self = [super init])) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(hide)
name:RCTJavaScriptDidLoadNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(hide)
name:RCTJavaScriptDidFailToLoadNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
[self showWithURL:bridge.bundleURL];
}
- (void)showWithURL:(NSURL *)URL
{
dispatch_async(dispatch_get_main_queue(), ^{
_showDate = [NSDate date];
if (!_window && !RCTRunningInTestEnvironment()) {
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
_window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 22)];
_window.backgroundColor = [UIColor blackColor];
_window.windowLevel = UIWindowLevelStatusBar + 1;
_label = [[UILabel alloc] initWithFrame:_window.bounds];
_label.font = [UIFont systemFontOfSize:12.0];
_label.textColor = [UIColor grayColor];
_label.textAlignment = NSTextAlignmentCenter;
[_window addSubview:_label];
[_window makeKeyAndVisible];
}
NSString *source;
if ([URL isFileURL]) {
source = @"pre-bundled file";
} else {
source = [NSString stringWithFormat:@"%@:%@", [URL host], [URL port]];
}
_label.text = [NSString stringWithFormat:@"Loading from %@...", source];
_window.hidden = NO;
});
}
- (void)hide
{
dispatch_async(dispatch_get_main_queue(), ^{
const NSTimeInterval MIN_PRESENTED_TIME = 0.6;
NSTimeInterval presentedTime = [[NSDate date] timeIntervalSinceDate:_showDate];
NSTimeInterval delay = MAX(0, MIN_PRESENTED_TIME - presentedTime);
CGRect windowFrame = _window.frame;
[UIView animateWithDuration:0.25
delay:delay
options:0
animations:^{
_window.frame = CGRectOffset(windowFrame, 0, -windowFrame.size.height);
} completion:^(__unused BOOL finished) {
_window.frame = windowFrame;
_window.hidden = YES;
_window = nil;
}];
});
}
@end
#else
@implementation RCTDevLoadingView
@end
#endif

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -8,8 +8,6 @@
/* Begin PBXBuildFile section */
000E6CEB1AB0E980000CDF4D /* RCTSourceCode.m in Sources */ = {isa = PBXBuildFile; fileRef = 000E6CEA1AB0E980000CDF4D /* RCTSourceCode.m */; };
00C1A2B31AC0B7E000E89A1C /* RCTDevMenu.m in Sources */ = {isa = PBXBuildFile; fileRef = 00C1A2B21AC0B7E000E89A1C /* RCTDevMenu.m */; };
00EAC16C1B62E089003B7CEE /* RCTDevLoadingView.m in Sources */ = {isa = PBXBuildFile; fileRef = 00EAC16B1B62E089003B7CEE /* RCTDevLoadingView.m */; };
131B6AF41AF1093D00FFC3E0 /* RCTSegmentedControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 131B6AF11AF1093D00FFC3E0 /* RCTSegmentedControl.m */; };
131B6AF51AF1093D00FFC3E0 /* RCTSegmentedControlManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 131B6AF31AF1093D00FFC3E0 /* RCTSegmentedControlManager.m */; };
13456E931ADAD2DE009F94A7 /* RCTConvert+CoreLocation.m in Sources */ = {isa = PBXBuildFile; fileRef = 13456E921ADAD2DE009F94A7 /* RCTConvert+CoreLocation.m */; };
@ -26,6 +24,8 @@
137327EA1AA5CF210034F82E /* RCTTabBarManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 137327E61AA5CF210034F82E /* RCTTabBarManager.m */; };
1385D0341B665AAE000A309B /* RCTModuleMap.m in Sources */ = {isa = PBXBuildFile; fileRef = 1385D0331B665AAE000A309B /* RCTModuleMap.m */; };
138D6A141B53CD290074A87E /* RCTCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 138D6A131B53CD290074A87E /* RCTCache.m */; };
13A0C2891B74F71200B29F6F /* RCTDevLoadingView.m in Sources */ = {isa = PBXBuildFile; fileRef = 13A0C2861B74F71200B29F6F /* RCTDevLoadingView.m */; };
13A0C28A1B74F71200B29F6F /* RCTDevMenu.m in Sources */ = {isa = PBXBuildFile; fileRef = 13A0C2881B74F71200B29F6F /* RCTDevMenu.m */; };
13A1F71E1A75392D00D3D453 /* RCTKeyCommands.m in Sources */ = {isa = PBXBuildFile; fileRef = 13A1F71D1A75392D00D3D453 /* RCTKeyCommands.m */; };
13AB90C11B6FA36700713B4F /* RCTComponentData.m in Sources */ = {isa = PBXBuildFile; fileRef = 13AB90C01B6FA36700713B4F /* RCTComponentData.m */; };
13AF20451AE707F9005F5298 /* RCTSlider.m in Sources */ = {isa = PBXBuildFile; fileRef = 13AF20441AE707F9005F5298 /* RCTSlider.m */; };
@ -99,10 +99,6 @@
/* Begin PBXFileReference section */
000E6CE91AB0E97F000CDF4D /* RCTSourceCode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTSourceCode.h; sourceTree = "<group>"; };
000E6CEA1AB0E980000CDF4D /* RCTSourceCode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTSourceCode.m; sourceTree = "<group>"; };
00C1A2B11AC0B7E000E89A1C /* RCTDevMenu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTDevMenu.h; sourceTree = "<group>"; };
00C1A2B21AC0B7E000E89A1C /* RCTDevMenu.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDevMenu.m; sourceTree = "<group>"; };
00EAC16A1B62E089003B7CEE /* RCTDevLoadingView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTDevLoadingView.h; sourceTree = "<group>"; };
00EAC16B1B62E089003B7CEE /* RCTDevLoadingView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDevLoadingView.m; sourceTree = "<group>"; };
131B6AF01AF1093D00FFC3E0 /* RCTSegmentedControl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTSegmentedControl.h; sourceTree = "<group>"; };
131B6AF11AF1093D00FFC3E0 /* RCTSegmentedControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTSegmentedControl.m; sourceTree = "<group>"; };
131B6AF21AF1093D00FFC3E0 /* RCTSegmentedControlManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTSegmentedControlManager.h; sourceTree = "<group>"; };
@ -138,6 +134,10 @@
1385D0351B6661DB000A309B /* RCTModuleMap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTModuleMap.h; sourceTree = "<group>"; };
138D6A121B53CD290074A87E /* RCTCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTCache.h; sourceTree = "<group>"; };
138D6A131B53CD290074A87E /* RCTCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTCache.m; sourceTree = "<group>"; };
13A0C2851B74F71200B29F6F /* RCTDevLoadingView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTDevLoadingView.h; sourceTree = "<group>"; };
13A0C2861B74F71200B29F6F /* RCTDevLoadingView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDevLoadingView.m; sourceTree = "<group>"; };
13A0C2871B74F71200B29F6F /* RCTDevMenu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTDevMenu.h; sourceTree = "<group>"; };
13A0C2881B74F71200B29F6F /* RCTDevMenu.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDevMenu.m; sourceTree = "<group>"; };
13A1F71C1A75392D00D3D453 /* RCTKeyCommands.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTKeyCommands.h; sourceTree = "<group>"; };
13A1F71D1A75392D00D3D453 /* RCTKeyCommands.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTKeyCommands.m; sourceTree = "<group>"; };
13AB90BF1B6FA36700713B4F /* RCTComponentData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTComponentData.h; sourceTree = "<group>"; };
@ -303,6 +303,10 @@
1372B7091AB030C200659ED6 /* RCTAppState.m */,
58114A4F1AAE93D500E7D092 /* RCTAsyncLocalStorage.h */,
58114A4E1AAE93D500E7D092 /* RCTAsyncLocalStorage.m */,
13A0C2851B74F71200B29F6F /* RCTDevLoadingView.h */,
13A0C2861B74F71200B29F6F /* RCTDevLoadingView.m */,
13A0C2871B74F71200B29F6F /* RCTDevMenu.h */,
13A0C2881B74F71200B29F6F /* RCTDevMenu.m */,
13B07FE91A69327A00A75B9A /* RCTExceptionsManager.h */,
13B07FEA1A69327A00A75B9A /* RCTExceptionsManager.m */,
63F014BE1B02080B003B75D2 /* RCTPointAnnotation.h */,
@ -451,10 +455,6 @@
83CBBACA1A6023D300E9B192 /* RCTConvert.h */,
83CBBACB1A6023D300E9B192 /* RCTConvert.m */,
13AF1F851AE6E777005F5298 /* RCTDefines.h */,
00C1A2B11AC0B7E000E89A1C /* RCTDevMenu.h */,
00C1A2B21AC0B7E000E89A1C /* RCTDevMenu.m */,
00EAC16A1B62E089003B7CEE /* RCTDevLoadingView.h */,
00EAC16B1B62E089003B7CEE /* RCTDevLoadingView.m */,
83CBBA651A601EF300E9B192 /* RCTEventDispatcher.h */,
83CBBA661A601EF300E9B192 /* RCTEventDispatcher.m */,
146459241B06C49500B389AA /* RCTFPSGraph.h */,
@ -597,12 +597,12 @@
830A229E1A66C68A008503DA /* RCTRootView.m in Sources */,
13B07FF01A69327A00A75B9A /* RCTExceptionsManager.m in Sources */,
83CBBA5A1A601E9000E9B192 /* RCTRedBox.m in Sources */,
13A0C28A1B74F71200B29F6F /* RCTDevMenu.m in Sources */,
14C2CA711B3AC63800E6CBB2 /* RCTModuleMethod.m in Sources */,
13CC8A821B17642100940AE7 /* RCTBorderDrawing.m in Sources */,
83CBBA511A601E3B00E9B192 /* RCTAssert.m in Sources */,
13AF20451AE707F9005F5298 /* RCTSlider.m in Sources */,
58114A501AAE93D500E7D092 /* RCTAsyncLocalStorage.m in Sources */,
00EAC16C1B62E089003B7CEE /* RCTDevLoadingView.m in Sources */,
832348161A77A5AA00B55238 /* Layout.c in Sources */,
14F4D38B1AE1B7E40049C042 /* RCTProfile.m in Sources */,
13513F3C1B1F43F400FCE529 /* RCTProgressViewManager.m in Sources */,
@ -618,6 +618,7 @@
13B080261A694A8400A75B9A /* RCTWrapperViewController.m in Sources */,
13B080051A6947C200A75B9A /* RCTScrollView.m in Sources */,
E9B20B7B1B500126007A2DA7 /* RCTAccessibilityManager.m in Sources */,
13A0C2891B74F71200B29F6F /* RCTDevLoadingView.m in Sources */,
13B07FF21A69327A00A75B9A /* RCTTiming.m in Sources */,
1372B70A1AB030C200659ED6 /* RCTAppState.m in Sources */,
134FCB3D1A6E7F0800051CC8 /* RCTContextExecutor.m in Sources */,
@ -647,7 +648,6 @@
58114A171AAE854800E7D092 /* RCTPickerManager.m in Sources */,
13B0801A1A69489C00A75B9A /* RCTNavigator.m in Sources */,
137327E71AA5CF210034F82E /* RCTTabBar.m in Sources */,
00C1A2B31AC0B7E000E89A1C /* RCTDevMenu.m in Sources */,
63F014C01B02080B003B75D2 /* RCTPointAnnotation.m in Sources */,
83392EB31B6634E10013B15F /* RCTModalHostViewController.m in Sources */,
14435CE51AAC4AE100FC20F4 /* RCTMap.m in Sources */,