2015-03-23 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
#import "RCTRootView.h"
|
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTContextExecutor.h"
|
2015-03-26 01:19:28 +00:00
|
|
|
#import "RCTDevMenu.h"
|
2015-02-04 00:02:36 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
2015-02-06 23:43:59 +00:00
|
|
|
#import "RCTKeyCommands.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTRedBox.h"
|
2015-03-13 22:30:31 +00:00
|
|
|
#import "RCTSourceCode.h"
|
2015-01-30 01:10:49 +00:00
|
|
|
#import "RCTTouchHandler.h"
|
|
|
|
#import "RCTUIManager.h"
|
|
|
|
#import "RCTUtils.h"
|
2015-02-04 00:15:20 +00:00
|
|
|
#import "RCTWebViewExecutor.h"
|
2015-03-26 13:32:01 +00:00
|
|
|
#import "UIView+React.h"
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-03-02 19:36:55 +00:00
|
|
|
NSString *const RCTReloadNotification = @"RCTReloadNotification";
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-03-26 03:09:04 +00:00
|
|
|
/**
|
|
|
|
* HACK(t6568049) This should be removed soon, hiding to prevent people from
|
|
|
|
* relying on it
|
|
|
|
*/
|
|
|
|
@interface RCTBridge (RCTRootView)
|
|
|
|
|
|
|
|
- (void)setJavaScriptExecutor:(id<RCTJavaScriptExecutor>)executor;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
@implementation RCTRootView
|
|
|
|
{
|
2015-03-26 01:19:28 +00:00
|
|
|
RCTDevMenu *_devMenu;
|
2015-01-30 01:10:49 +00:00
|
|
|
RCTBridge *_bridge;
|
|
|
|
RCTTouchHandler *_touchHandler;
|
2015-03-02 19:36:55 +00:00
|
|
|
id<RCTJavaScriptExecutor> _executor;
|
2015-03-25 02:34:12 +00:00
|
|
|
BOOL _registered;
|
2015-03-26 03:09:04 +00:00
|
|
|
NSDictionary *_launchOptions;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
static Class _globalExecutorClass;
|
2015-02-04 00:15:20 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
#if TARGET_IPHONE_SIMULATOR
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
// Register Cmd-R as a global refresh key
|
|
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"r"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:^(UIKeyCommand *command) {
|
|
|
|
[self reloadAll];
|
|
|
|
}];
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-02-04 00:15:20 +00:00
|
|
|
// Cmd-D reloads using the web view executor, allows attaching from Safari dev tools.
|
|
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"d"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:^(UIKeyCommand *command) {
|
2015-03-20 15:43:51 +00:00
|
|
|
_globalExecutorClass = NSClassFromString(@"RCTWebSocketExecutor");
|
|
|
|
if (!_globalExecutorClass) {
|
2015-03-25 22:36:50 +00:00
|
|
|
RCTLogError(@"WebSocket debugger is not available. Did you forget to include RCTWebSocketExecutor?");
|
2015-03-20 15:43:51 +00:00
|
|
|
}
|
2015-02-04 00:15:20 +00:00
|
|
|
[self reloadAll];
|
|
|
|
}];
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
#endif
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 03:09:04 +00:00
|
|
|
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
|
|
|
|
moduleName:(NSString *)moduleName
|
|
|
|
launchOptions:(NSDictionary *)launchOptions
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
2015-03-26 03:09:04 +00:00
|
|
|
if ((self = [super init])) {
|
|
|
|
RCTAssert(bundleURL, @"A bundleURL is required to create an RCTRootView");
|
|
|
|
RCTAssert(moduleName, @"A bundleURL is required to create an RCTRootView");
|
|
|
|
_moduleName = moduleName;
|
|
|
|
_launchOptions = launchOptions;
|
2015-02-06 23:43:59 +00:00
|
|
|
[self setUp];
|
2015-03-26 03:09:04 +00:00
|
|
|
[self setScriptURL:bundleURL];
|
2015-02-06 23:43:59 +00:00
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-03-26 03:09:04 +00:00
|
|
|
/**
|
|
|
|
* HACK(t6568049) Private constructor for testing purposes
|
|
|
|
*/
|
|
|
|
- (instancetype)_initWithBundleURL:(NSURL *)bundleURL
|
|
|
|
moduleName:(NSString *)moduleName
|
|
|
|
launchOptions:(NSDictionary *)launchOptions
|
|
|
|
moduleProvider:(RCTBridgeModuleProviderBlock)moduleProvider
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
2015-03-26 03:09:04 +00:00
|
|
|
if ((self = [super init])) {
|
|
|
|
_moduleProvider = moduleProvider;
|
|
|
|
_moduleName = moduleName;
|
|
|
|
_launchOptions = launchOptions;
|
2015-02-06 23:43:59 +00:00
|
|
|
[self setUp];
|
2015-03-26 03:09:04 +00:00
|
|
|
[self setScriptURL:bundleURL];
|
2015-02-06 23:43:59 +00:00
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
// Every root view that is created must have a unique react tag.
|
|
|
|
// Numbering of these tags goes from 1, 11, 21, 31, etc
|
|
|
|
static NSInteger rootViewTag = 1;
|
|
|
|
self.reactTag = @(rootViewTag);
|
2015-03-26 01:19:28 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
self.enableDevMenu = YES;
|
|
|
|
#endif
|
2015-03-26 03:09:04 +00:00
|
|
|
self.backgroundColor = [UIColor whiteColor];
|
2015-01-30 01:10:49 +00:00
|
|
|
rootViewTag += 10;
|
|
|
|
|
|
|
|
// Add reload observer
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(reload)
|
2015-03-02 19:36:55 +00:00
|
|
|
name:RCTReloadNotification
|
2015-01-30 01:10:49 +00:00
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:19:28 +00:00
|
|
|
- (BOOL)canBecomeFirstResponder
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
|
|
|
|
{
|
|
|
|
if (motion == UIEventSubtypeMotionShake && self.enableDevMenu) {
|
|
|
|
if (!_devMenu) {
|
|
|
|
_devMenu = [[RCTDevMenu alloc] initWithRootView:self];
|
|
|
|
}
|
|
|
|
[_devMenu show];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 20:40:49 +00:00
|
|
|
+ (NSArray *)JSMethods
|
|
|
|
{
|
|
|
|
return @[
|
|
|
|
@"AppRegistry.runApplication",
|
|
|
|
@"ReactIOS.unmountComponentAtNodeAndRemoveContainer"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-02-25 20:40:49 +00:00
|
|
|
|
|
|
|
[_bridge enqueueJSCall:@"ReactIOS.unmountComponentAtNodeAndRemoveContainer"
|
|
|
|
args:@[self.reactTag]];
|
2015-03-25 02:34:12 +00:00
|
|
|
[self invalidate];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - RCTInvalidating
|
|
|
|
|
|
|
|
- (BOOL)isValid
|
|
|
|
{
|
|
|
|
return [_bridge isValid];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
// Clear view
|
|
|
|
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
|
|
|
|
|
|
|
|
[self removeGestureRecognizer:_touchHandler];
|
|
|
|
[_touchHandler invalidate];
|
|
|
|
[_executor invalidate];
|
2015-03-02 19:36:55 +00:00
|
|
|
|
|
|
|
// TODO: eventually we'll want to be able to share the bridge between
|
|
|
|
// multiple rootviews, in which case we'll need to move this elsewhere
|
|
|
|
[_bridge invalidate];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
#pragma mark Bundle loading
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
- (void)bundleFinishedLoading:(NSError *)error
|
|
|
|
{
|
|
|
|
if (error != nil) {
|
2015-01-30 18:17:28 +00:00
|
|
|
NSArray *stack = [[error userInfo] objectForKey:@"stack"];
|
|
|
|
if (stack) {
|
|
|
|
[[RCTRedBox sharedInstance] showErrorMessage:[error localizedDescription] withStack:stack];
|
|
|
|
} else {
|
|
|
|
[[RCTRedBox sharedInstance] showErrorMessage:[error localizedDescription] withDetails:[error localizedFailureReason]];
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
} else {
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-03-02 19:36:55 +00:00
|
|
|
[_bridge.uiManager registerRootView:self];
|
2015-03-25 02:34:12 +00:00
|
|
|
_registered = YES;
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
NSString *moduleName = _moduleName ?: @"";
|
|
|
|
NSDictionary *appParameters = @{
|
2015-02-06 23:43:59 +00:00
|
|
|
@"rootTag": self.reactTag,
|
2015-01-30 01:10:49 +00:00
|
|
|
@"initialProps": self.initialProperties ?: @{},
|
|
|
|
};
|
2015-02-19 14:57:05 +00:00
|
|
|
[_bridge enqueueJSCall:@"AppRegistry.runApplication"
|
2015-02-04 00:12:07 +00:00
|
|
|
args:@[moduleName, appParameters]];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadBundle
|
|
|
|
{
|
2015-03-25 02:34:12 +00:00
|
|
|
[self invalidate];
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
if (!_scriptURL) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
// Clean up
|
|
|
|
[self removeGestureRecognizer:_touchHandler];
|
2015-03-02 18:52:16 +00:00
|
|
|
[_touchHandler invalidate];
|
2015-01-30 01:10:49 +00:00
|
|
|
[_executor invalidate];
|
|
|
|
[_bridge invalidate];
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
_registered = NO;
|
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
// Choose local executor if specified, followed by global, followed by default
|
|
|
|
_executor = [[_executorClass ?: _globalExecutorClass ?: [RCTContextExecutor class] alloc] init];
|
2015-03-26 03:09:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HACK(t6568049) Most of the properties passed into the bridge are not used
|
|
|
|
* right now but it'll be changed soon so it's here for convenience.
|
|
|
|
*/
|
|
|
|
_bridge = [[RCTBridge alloc] initWithBundlePath:_scriptURL.absoluteString
|
|
|
|
moduleProvider:_moduleProvider
|
|
|
|
launchOptions:_launchOptions];
|
|
|
|
[_bridge setJavaScriptExecutor:_executor];
|
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:_bridge];
|
|
|
|
[self addGestureRecognizer:_touchHandler];
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
// Load the bundle
|
|
|
|
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:_scriptURL completionHandler:
|
|
|
|
^(NSData *data, NSURLResponse *response, NSError *error) {
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
// Handle general request errors
|
|
|
|
if (error) {
|
|
|
|
if ([[error domain] isEqualToString:NSURLErrorDomain]) {
|
|
|
|
NSDictionary *userInfo = @{
|
2015-03-26 13:32:01 +00:00
|
|
|
NSLocalizedDescriptionKey: @"Could not connect to development server. Ensure node server is running - run 'npm start' from React root",
|
2015-02-06 23:43:59 +00:00
|
|
|
NSLocalizedFailureReasonErrorKey: [error localizedDescription],
|
|
|
|
NSUnderlyingErrorKey: error,
|
|
|
|
};
|
|
|
|
error = [NSError errorWithDomain:@"JSServer"
|
|
|
|
code:error.code
|
|
|
|
userInfo:userInfo];
|
|
|
|
}
|
|
|
|
[self bundleFinishedLoading:error];
|
|
|
|
return;
|
|
|
|
}
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
// Parse response as text
|
|
|
|
NSStringEncoding encoding = NSUTF8StringEncoding;
|
|
|
|
if (response.textEncodingName != nil) {
|
|
|
|
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
|
|
|
|
if (cfEncoding != kCFStringEncodingInvalidId) {
|
|
|
|
encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NSString *rawText = [[NSString alloc] initWithData:data encoding:encoding];
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
// Handle HTTP errors
|
|
|
|
if ([response isKindOfClass:[NSHTTPURLResponse class]] && [(NSHTTPURLResponse *)response statusCode] != 200) {
|
|
|
|
NSDictionary *userInfo;
|
|
|
|
NSDictionary *errorDetails = RCTJSONParse(rawText, nil);
|
|
|
|
if ([errorDetails isKindOfClass:[NSDictionary class]]) {
|
|
|
|
userInfo = @{
|
|
|
|
NSLocalizedDescriptionKey: errorDetails[@"message"] ?: @"No message provided",
|
|
|
|
@"stack": @[@{
|
|
|
|
@"methodName": errorDetails[@"description"] ?: @"",
|
|
|
|
@"file": errorDetails[@"filename"] ?: @"",
|
|
|
|
@"lineNumber": errorDetails[@"lineNumber"] ?: @0
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
userInfo = @{NSLocalizedDescriptionKey: rawText};
|
|
|
|
}
|
|
|
|
error = [NSError errorWithDomain:@"JSServer"
|
|
|
|
code:[(NSHTTPURLResponse *)response statusCode]
|
|
|
|
userInfo:userInfo];
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
[self bundleFinishedLoading:error];
|
|
|
|
return;
|
|
|
|
}
|
2015-03-25 02:34:12 +00:00
|
|
|
if (!_bridge.isValid) {
|
|
|
|
return; // Bridge was invalidated in the meanwhile
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
// Success!
|
2015-03-13 22:30:31 +00:00
|
|
|
RCTSourceCode *sourceCodeModule = _bridge.modules[NSStringFromClass([RCTSourceCode class])];
|
|
|
|
sourceCodeModule.scriptURL = _scriptURL;
|
|
|
|
sourceCodeModule.scriptText = rawText;
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
[_bridge enqueueApplicationScript:rawText url:_scriptURL onComplete:^(NSError *_error) {
|
2015-02-06 23:43:59 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-03-25 02:34:12 +00:00
|
|
|
if (_bridge.isValid) {
|
|
|
|
[self bundleFinishedLoading:_error];
|
|
|
|
}
|
2015-02-06 23:43:59 +00:00
|
|
|
});
|
|
|
|
}];
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
}];
|
2015-02-19 14:57:05 +00:00
|
|
|
|
2015-02-06 23:43:59 +00:00
|
|
|
[task resume];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setScriptURL:(NSURL *)scriptURL
|
|
|
|
{
|
|
|
|
if ([_scriptURL isEqual:scriptURL]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_scriptURL = scriptURL;
|
|
|
|
[self loadBundle];
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
- (void)layoutSubviews
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
2015-03-25 02:34:12 +00:00
|
|
|
[super layoutSubviews];
|
|
|
|
if (_registered) {
|
|
|
|
[_bridge.uiManager setFrame:self.frame forRootView:self];
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reload
|
|
|
|
{
|
|
|
|
[self loadBundle];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)reloadAll
|
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification object:nil];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 18:52:16 +00:00
|
|
|
- (void)startOrResetInteractionTiming
|
|
|
|
{
|
|
|
|
[_touchHandler startOrResetInteractionTiming];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)endAndResetInteractionTiming
|
|
|
|
{
|
|
|
|
return [_touchHandler endAndResetInteractionTiming];
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
@end
|