[ReactNative] Fix WebView executor

This commit is contained in:
Tadeu Zagallo 2015-05-01 06:11:24 -07:00
parent 6cb7178098
commit 67196b36bb
1 changed files with 19 additions and 8 deletions

View File

@ -42,13 +42,17 @@ static void RCTReportError(RCTJavaScriptCallback callback, NSString *fmt, ...)
{
UIWebView *_webView;
NSMutableDictionary *_objectsToInject;
NSRegularExpression *_commentsRegex;
}
@synthesize valid = _valid;
- (instancetype)initWithWebView:(UIWebView *)webView
{
if ((self = [super init])) {
_objectsToInject = [[NSMutableDictionary alloc] init];
_webView = webView ?: [[UIWebView alloc] init];
_commentsRegex = [NSRegularExpression regularExpressionWithPattern:@"(^ *?\\/\\/.*?$|\\/\\*\\*[\\s\\S]+?\\*\\/)" options:NSRegularExpressionAnchorsMatchLines error:NULL];
_webView.delegate = self;
}
return self;
@ -59,13 +63,9 @@ static void RCTReportError(RCTJavaScriptCallback callback, NSString *fmt, ...)
return [self initWithWebView:nil];
}
- (BOOL)isValid
{
return _webView != nil;
}
- (void)invalidate
{
_valid = NO;
_webView.delegate = nil;
_webView = nil;
}
@ -129,10 +129,21 @@ static void RCTReportError(RCTJavaScriptCallback callback, NSString *fmt, ...)
}
RCTAssert(onComplete != nil, @"");
_onApplicationScriptLoaded = onComplete;
__weak RCTWebViewExecutor *weakSelf = self;
_onApplicationScriptLoaded = ^(NSError *error){
RCTWebViewExecutor *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
strongSelf->_valid = error == nil;
onComplete(error);
};
script = [_commentsRegex stringByReplacingMatchesInString:script
options:0
range:NSMakeRange(0, script.length)
withTemplate:@""];
script = [script stringByReplacingOccurrencesOfString:@"<script>" withString:@""];
script = [script stringByReplacingOccurrencesOfString:@"</script>" withString:@""];
if (_objectsToInject.count > 0) {
NSMutableString *scriptWithInjections = [[NSMutableString alloc] initWithString:@"/* BEGIN NATIVELY INJECTED OBJECTS */\n"];
[_objectsToInject enumerateKeysAndObjectsUsingBlock:^(NSString *objectName, NSString *blockScript, BOOL *stop) {