Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit a995a3c

Browse files
committed
CR feedback
1 parent b1a176a commit a995a3c

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,27 +458,40 @@ public void call(DownloadProgress downloadProgress) {
458458
return;
459459
}
460460

461-
this.latestDownloadProgress = downloadProgress;
461+
latestDownloadProgress = downloadProgress;
462+
// If the download is completed, synchronously send the last event.
463+
if (latestDownloadProgress.isCompleted()) {
464+
dispatchDownloadProgressEvent();
465+
return;
466+
}
467+
462468
if (hasScheduledNextFrame) {
463469
return;
464470
}
465471

466472
hasScheduledNextFrame = true;
467-
mainActivity.runOnUiThread(new Runnable() {
473+
getReactApplicationContext().runOnUiQueueThread(new Runnable() {
468474
@Override
469475
public void run() {
470476
ReactChoreographer.getInstance().postFrameCallback(ReactChoreographer.CallbackType.TIMERS_EVENTS, new Choreographer.FrameCallback() {
471477
@Override
472478
public void doFrame(long frameTimeNanos) {
473-
getReactApplicationContext()
474-
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
475-
.emit(DOWNLOAD_PROGRESS_EVENT_NAME, latestDownloadProgress.createWritableMap());
479+
if (!latestDownloadProgress.isCompleted()) {
480+
dispatchDownloadProgressEvent();
481+
}
482+
476483
hasScheduledNextFrame = false;
477484
}
478485
});
479486
}
480487
});
481488
}
489+
490+
public void dispatchDownloadProgressEvent() {
491+
getReactApplicationContext()
492+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
493+
.emit(DOWNLOAD_PROGRESS_EVENT_NAME, latestDownloadProgress.createWritableMap());
494+
}
482495
});
483496

484497
WritableMap newPackage = codePushPackage.getPackage(CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY));

android/app/src/main/java/com/microsoft/codepush/react/DownloadProgress.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ public WritableMap createWritableMap() {
2323
}
2424
return map;
2525
}
26+
27+
public boolean isCompleted() {
28+
return this.totalBytes == this.receivedBytes;
29+
}
2630
}

ios/CodePush/CodePush.m

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ - (void)initializeUpdateAfterRestart
275275
#ifdef DEBUG
276276
[self clearDebugUpdates];
277277
#endif
278-
_paused = YES;
278+
[self pauseFrameObserver];
279279
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
280280
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
281281
if (pendingUpdate) {
@@ -502,9 +502,13 @@ - (void)applicationWillResignActive
502502
operationQueue:_methodQueue
503503
// The download is progressing forward
504504
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
505-
// Notify the script-side about the progress
506505
[self updateDownloadProgressForNextFrame:expectedContentLength
507506
receivedContentLength:receivedContentLength];
507+
// If the download is completed, stop observing frame updates and synchronously send the last event.
508+
if (expectedContentLength == receivedContentLength) {
509+
[self pauseFrameObserver];
510+
[self dispatchDownloadProgressEvent];
511+
}
508512
}
509513
// The download completed
510514
doneCallback:^{
@@ -514,8 +518,6 @@ - (void)applicationWillResignActive
514518
if (err) {
515519
return reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
516520
}
517-
518-
[self pauseFrameObserver];
519521
resolve(newPackage);
520522
}
521523
// The download failed
@@ -772,15 +774,19 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
772774
return;
773775
}
774776

777+
[self dispatchDownloadProgressEvent];
778+
didUpdateProgress = NO;
779+
}
780+
781+
- (void)dispatchDownloadProgressEvent
782+
{
775783
// Notify the script-side about the progress
776784
[self.bridge.eventDispatcher
777785
sendDeviceEventWithName:@"CodePushDownloadProgress"
778786
body:@{
779787
@"totalBytes":[NSNumber numberWithLongLong:latestExpectedContentLength],
780788
@"receivedBytes":[NSNumber numberWithLongLong:latestReceivedConentLength]
781-
}];
782-
didUpdateProgress = NO;
783-
789+
}];
784790
}
785791

786792
- (void)updateDownloadProgressForNextFrame:(long long)expectedContentLength
@@ -800,6 +806,7 @@ - (void)setupFrameObserverForDownloadProgress
800806

801807
- (void)pauseFrameObserver
802808
{
809+
didUpdateProgress = NO;
803810
_paused = YES;
804811
}
805812

0 commit comments

Comments
 (0)