CR feedback

This commit is contained in:
Geoffrey Goh
2016-04-28 14:26:09 -07:00
parent b1a176ace5
commit a995a3c64d
3 changed files with 36 additions and 12 deletions

View File

@@ -458,27 +458,40 @@ public class CodePush implements ReactPackage {
return;
}
this.latestDownloadProgress = downloadProgress;
latestDownloadProgress = downloadProgress;
// If the download is completed, synchronously send the last event.
if (latestDownloadProgress.isCompleted()) {
dispatchDownloadProgressEvent();
return;
}
if (hasScheduledNextFrame) {
return;
}
hasScheduledNextFrame = true;
mainActivity.runOnUiThread(new Runnable() {
getReactApplicationContext().runOnUiQueueThread(new Runnable() {
@Override
public void run() {
ReactChoreographer.getInstance().postFrameCallback(ReactChoreographer.CallbackType.TIMERS_EVENTS, new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(DOWNLOAD_PROGRESS_EVENT_NAME, latestDownloadProgress.createWritableMap());
if (!latestDownloadProgress.isCompleted()) {
dispatchDownloadProgressEvent();
}
hasScheduledNextFrame = false;
}
});
}
});
}
public void dispatchDownloadProgressEvent() {
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(DOWNLOAD_PROGRESS_EVENT_NAME, latestDownloadProgress.createWritableMap());
}
});
WritableMap newPackage = codePushPackage.getPackage(CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY));

View File

@@ -23,4 +23,8 @@ class DownloadProgress {
}
return map;
}
public boolean isCompleted() {
return this.totalBytes == this.receivedBytes;
}
}

View File

@@ -275,7 +275,7 @@ static NSString *bundleResourceName = @"main";
#ifdef DEBUG
[self clearDebugUpdates];
#endif
_paused = YES;
[self pauseFrameObserver];
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
if (pendingUpdate) {
@@ -502,9 +502,13 @@ RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage
operationQueue:_methodQueue
// The download is progressing forward
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
// Notify the script-side about the progress
[self updateDownloadProgressForNextFrame:expectedContentLength
receivedContentLength:receivedContentLength];
// If the download is completed, stop observing frame updates and synchronously send the last event.
if (expectedContentLength == receivedContentLength) {
[self pauseFrameObserver];
[self dispatchDownloadProgressEvent];
}
}
// The download completed
doneCallback:^{
@@ -514,8 +518,6 @@ RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage
if (err) {
return reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
}
[self pauseFrameObserver];
resolve(newPackage);
}
// The download failed
@@ -772,15 +774,19 @@ BOOL didUpdateProgress = NO;
return;
}
[self dispatchDownloadProgressEvent];
didUpdateProgress = NO;
}
- (void)dispatchDownloadProgressEvent
{
// Notify the script-side about the progress
[self.bridge.eventDispatcher
sendDeviceEventWithName:@"CodePushDownloadProgress"
body:@{
@"totalBytes":[NSNumber numberWithLongLong:latestExpectedContentLength],
@"receivedBytes":[NSNumber numberWithLongLong:latestReceivedConentLength]
}];
didUpdateProgress = NO;
}];
}
- (void)updateDownloadProgressForNextFrame:(long long)expectedContentLength
@@ -800,6 +806,7 @@ BOOL didUpdateProgress = NO;
- (void)pauseFrameObserver
{
didUpdateProgress = NO;
_paused = YES;
}