2015-02-20 04:10:52 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#import "RCTRootView.h"
|
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTContextExecutor.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "RCTKeyCommands.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTRedBox.h"
|
2015-03-13 00:49:54 +00:00
|
|
|
#import "RCTSourceCode.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTTouchHandler.h"
|
|
|
|
#import "RCTUIManager.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
#import "RCTWebViewExecutor.h"
|
|
|
|
#import "UIView+ReactKit.h"
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
NSString *const RCTReloadNotification = @"RCTReloadNotification";
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
@implementation RCTRootView
|
|
|
|
{
|
|
|
|
RCTBridge *_bridge;
|
|
|
|
RCTTouchHandler *_touchHandler;
|
2015-03-01 23:33:55 +00:00
|
|
|
id<RCTJavaScriptExecutor> _executor;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Class _globalExecutorClass;
|
|
|
|
|
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
// Register Cmd-R as a global refresh key
|
|
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"r"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:^(UIKeyCommand *command) {
|
|
|
|
[self reloadAll];
|
|
|
|
}];
|
|
|
|
|
|
|
|
// Cmd-D reloads using the web view executor, allows attaching from Safari dev tools.
|
|
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"d"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:^(UIKeyCommand *command) {
|
|
|
|
_globalExecutorClass = [RCTWebViewExecutor class];
|
|
|
|
[self reloadAll];
|
|
|
|
}];
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithCoder:(NSCoder *)aDecoder
|
|
|
|
{
|
|
|
|
if ((self = [super initWithCoder:aDecoder])) {
|
|
|
|
[self setUp];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
if ((self = [super initWithFrame:frame])) {
|
|
|
|
self.backgroundColor = [UIColor whiteColor];
|
|
|
|
[self setUp];
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
rootViewTag += 10;
|
|
|
|
|
|
|
|
// Add reload observer
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(reload)
|
2015-03-01 23:33:55 +00:00
|
|
|
name:RCTReloadNotification
|
2015-02-20 04:10:52 +00:00
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:06:57 +00:00
|
|
|
+ (NSArray *)JSMethods
|
|
|
|
{
|
|
|
|
return @[
|
|
|
|
@"AppRegistry.runApplication",
|
|
|
|
@"ReactIOS.unmountComponentAtNodeAndRemoveContainer"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-02-24 17:06:57 +00:00
|
|
|
|
|
|
|
[_bridge enqueueJSCall:@"ReactIOS.unmountComponentAtNodeAndRemoveContainer"
|
|
|
|
args:@[self.reactTag]];
|
2015-03-01 23:33: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-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)bundleFinishedLoading:(NSError *)error
|
|
|
|
{
|
|
|
|
if (error != nil) {
|
|
|
|
NSArray *stack = [[error userInfo] objectForKey:@"stack"];
|
|
|
|
if (stack) {
|
|
|
|
[[RCTRedBox sharedInstance] showErrorMessage:[error localizedDescription] withStack:stack];
|
|
|
|
} else {
|
|
|
|
[[RCTRedBox sharedInstance] showErrorMessage:[error localizedDescription] withDetails:[error localizedFailureReason]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
[_bridge.uiManager registerRootView:self];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
NSString *moduleName = _moduleName ?: @"";
|
|
|
|
NSDictionary *appParameters = @{
|
|
|
|
@"rootTag": self.reactTag,
|
|
|
|
@"initialProps": self.initialProperties ?: @{},
|
|
|
|
};
|
|
|
|
[_bridge enqueueJSCall:@"AppRegistry.runApplication"
|
|
|
|
args:@[moduleName, appParameters]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadBundle
|
|
|
|
{
|
|
|
|
// Clear view
|
|
|
|
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
|
|
|
|
|
|
|
|
if (!_scriptURL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
[self removeGestureRecognizer:_touchHandler];
|
2015-02-27 12:05:35 +00:00
|
|
|
[_touchHandler invalidate];
|
2015-02-20 04:10:52 +00:00
|
|
|
[_executor invalidate];
|
|
|
|
[_bridge invalidate];
|
|
|
|
|
|
|
|
// Choose local executor if specified, followed by global, followed by default
|
|
|
|
_executor = [[_executorClass ?: _globalExecutorClass ?: [RCTContextExecutor class] alloc] init];
|
2015-03-14 01:32:38 +00:00
|
|
|
_bridge = [[RCTBridge alloc] initWithExecutor:_executor moduleProvider:_moduleProvider];
|
2015-02-20 04:10:52 +00:00
|
|
|
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:_bridge];
|
|
|
|
[self addGestureRecognizer:_touchHandler];
|
|
|
|
|
|
|
|
// Load the bundle
|
|
|
|
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:_scriptURL completionHandler:
|
|
|
|
^(NSData *data, NSURLResponse *response, NSError *error) {
|
|
|
|
|
|
|
|
// Handle general request errors
|
|
|
|
if (error) {
|
|
|
|
if ([[error domain] isEqualToString:NSURLErrorDomain]) {
|
|
|
|
NSDictionary *userInfo = @{
|
|
|
|
NSLocalizedDescriptionKey: @"Could not connect to development server. Ensure node server is running - run 'npm start' from ReactKit root",
|
|
|
|
NSLocalizedFailureReasonErrorKey: [error localizedDescription],
|
|
|
|
NSUnderlyingErrorKey: error,
|
|
|
|
};
|
|
|
|
error = [NSError errorWithDomain:@"JSServer"
|
|
|
|
code:error.code
|
|
|
|
userInfo:userInfo];
|
|
|
|
}
|
|
|
|
[self bundleFinishedLoading:error];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
|
|
|
[self bundleFinishedLoading:error];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success!
|
2015-03-13 00:49:54 +00:00
|
|
|
RCTSourceCode *sourceCodeModule = _bridge.modules[NSStringFromClass([RCTSourceCode class])];
|
|
|
|
sourceCodeModule.scriptURL = _scriptURL;
|
|
|
|
sourceCodeModule.scriptText = rawText;
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
[_bridge enqueueApplicationScript:rawText url:_scriptURL onComplete:^(NSError *error) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self bundleFinishedLoading:error];
|
|
|
|
});
|
|
|
|
}];
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
[task resume];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setScriptURL:(NSURL *)scriptURL
|
|
|
|
{
|
|
|
|
if ([_scriptURL isEqual:scriptURL]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_scriptURL = scriptURL;
|
|
|
|
[self loadBundle];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isReactRootView
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reload
|
|
|
|
{
|
|
|
|
[self loadBundle];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)reloadAll
|
|
|
|
{
|
2015-03-01 23:33:55 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification object:nil];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 12:05:35 +00:00
|
|
|
- (void)startOrResetInteractionTiming
|
|
|
|
{
|
|
|
|
[_touchHandler startOrResetInteractionTiming];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)endAndResetInteractionTiming
|
|
|
|
{
|
|
|
|
return [_touchHandler endAndResetInteractionTiming];
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|