Show packager progress in UI

Reviewed By: javache

Differential Revision: D3941904

fbshipit-source-id: 4ea3b61e9d636eeaddbadbe4ba6c62069955f022
This commit is contained in:
Alex Kotliarskyi
2016-10-13 11:42:30 -07:00
committed by Facebook Github Bot
parent ef91708fdb
commit 5f548e15f9
7 changed files with 82 additions and 10 deletions

View File

@@ -22,11 +22,27 @@ uint32_t const RCTRAMBundleMagicNumber = 0xFB0BD1E5;
NSString *const RCTJavaScriptLoaderErrorDomain = @"RCTJavaScriptLoaderErrorDomain";
@implementation RCTLoadingProgress
- (NSString *)description
{
NSMutableString *desc = [NSMutableString new];
[desc appendString:_status ?: @"Loading"];
if (_total > 0) {
[desc appendFormat:@" %ld%% (%@/%@)", (long)(100 * [_done integerValue] / [_total integerValue]), _done, _total];
}
[desc appendString:@"…"];
return desc;
}
@end
@implementation RCTJavaScriptLoader
RCT_NOT_IMPLEMENTED(- (instancetype)init)
+ (void)loadBundleAtURL:(NSURL *)scriptURL onComplete:(RCTSourceLoadBlock)onComplete
+ (void)loadBundleAtURL:(NSURL *)scriptURL onProgress:(RCTSourceLoadProgressBlock)onProgress onComplete:(RCTSourceLoadBlock)onComplete
{
int64_t sourceLength;
NSError *error;
@@ -43,7 +59,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
&& error.code == RCTJavaScriptLoaderErrorCannotBeLoadedSynchronously;
if (isCannotLoadSyncError) {
attemptAsynchronousLoadOfBundleAtURL(scriptURL, onComplete);
attemptAsynchronousLoadOfBundleAtURL(scriptURL, onProgress, onComplete);
} else {
onComplete(error, nil, 0);
}
@@ -136,7 +152,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
return [NSData dataWithBytes:&magicNumber length:sizeof(magicNumber)];
}
static void attemptAsynchronousLoadOfBundleAtURL(NSURL *scriptURL, RCTSourceLoadBlock onComplete)
static void attemptAsynchronousLoadOfBundleAtURL(NSURL *scriptURL, RCTSourceLoadProgressBlock onProgress, RCTSourceLoadBlock onComplete)
{
scriptURL = sanitizeURL(scriptURL);
@@ -155,7 +171,9 @@ static void attemptAsynchronousLoadOfBundleAtURL(NSURL *scriptURL, RCTSourceLoad
RCTMultipartDataTask *task = [[RCTMultipartDataTask alloc] initWithURL:scriptURL partHandler:^(NSInteger statusCode, NSDictionary *headers, NSData *data, NSError *error, BOOL done) {
if (!done) {
// TODO(frantic): Emit progress event
if (onProgress) {
onProgress(progressEventFromData(data));
}
return;
}
@@ -206,6 +224,21 @@ static NSURL *sanitizeURL(NSURL *url)
return [RCTConvert NSURL:url.absoluteString];
}
static RCTLoadingProgress *progressEventFromData(NSData *rawData)
{
NSString *text = [[NSString alloc] initWithData:rawData encoding:NSUTF8StringEncoding];
id info = RCTJSONParse(text, nil);
if (!info || ![info isKindOfClass:[NSDictionary class]]) {
return nil;
}
RCTLoadingProgress *progress = [RCTLoadingProgress new];
progress.status = [info valueForKey:@"status"];
progress.done = [info valueForKey:@"done"];
progress.total = [info valueForKey:@"total"];
return progress;
}
static NSDictionary *userInfoForRawResponse(NSString *rawText)
{
NSDictionary *parsedResponse = RCTJSONParse(rawText, nil);