mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-02 09:21:44 +08:00
Show bundle download progress on iOS
Summary: This shows progress for the download of the JS bundle (different from the packager transform progress that we show already). This is useful especially when loading the JS bundle from a remote source or when developing on device (on simulator + localhost it pretty much just downloads instantly). This will be nice for the expo client since all bundles are loaded over the network and can take several seconds to load. This depends on https://github.com/facebook/metro-bundler/pull/28 to work but won't crash or anything without it, it just won't show the progress percentage.  **Test plan** Tested that bundle download progress is shown properly in RNTester on both localhost + simulator and on real device with network conditionner to simulate a slow loading bundle. Tested that it doesn't cause issues if the packager doesn't send the Content-Length header. Closes https://github.com/facebook/react-native/pull/15066 Differential Revision: D5449073 Pulled By: shergin fbshipit-source-id: 43a8fb559393bbdc04f77916500e21898695bac5
This commit is contained in:
committed by
Facebook Github Bot
parent
b58207e61f
commit
ef23d2bdcf
@@ -198,7 +198,6 @@ static void attemptAsynchronousLoadOfBundleAtURL(NSURL *scriptURL, RCTSourceLoad
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
RCTMultipartDataTask *task = [[RCTMultipartDataTask alloc] initWithURL:scriptURL partHandler:^(NSInteger statusCode, NSDictionary *headers, NSData *data, NSError *error, BOOL done) {
|
||||
if (!done) {
|
||||
if (onProgress) {
|
||||
@@ -261,6 +260,11 @@ static void attemptAsynchronousLoadOfBundleAtURL(NSURL *scriptURL, RCTSourceLoad
|
||||
}
|
||||
|
||||
onComplete(nil, data, data.length);
|
||||
} progressHandler:^(NSDictionary *headers, NSNumber *loaded, NSNumber *total) {
|
||||
// Only care about download progress events for the javascript bundle part.
|
||||
if ([headers[@"Content-Type"] isEqualToString:@"application/javascript"]) {
|
||||
onProgress(progressEventFromDownloadProgress(loaded, total));
|
||||
}
|
||||
}];
|
||||
|
||||
[task startTask];
|
||||
@@ -287,6 +291,16 @@ static RCTLoadingProgress *progressEventFromData(NSData *rawData)
|
||||
return progress;
|
||||
}
|
||||
|
||||
static RCTLoadingProgress *progressEventFromDownloadProgress(NSNumber *total, NSNumber *done)
|
||||
{
|
||||
RCTLoadingProgress *progress = [RCTLoadingProgress new];
|
||||
progress.status = @"Downloading JavaScript bundle";
|
||||
// Progress values are in bytes transform them to kilobytes for smaller numbers.
|
||||
progress.done = done != nil ? @([done integerValue] / 1024) : nil;
|
||||
progress.total = total != nil ? @([total integerValue] / 1024) : nil;
|
||||
return progress;
|
||||
}
|
||||
|
||||
static NSDictionary *userInfoForRawResponse(NSString *rawText)
|
||||
{
|
||||
NSDictionary *parsedResponse = RCTJSONParse(rawText, nil);
|
||||
|
||||
Reference in New Issue
Block a user