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

Commit 3b05073

Browse files
committed
inline progress update methods
1 parent a995a3c commit 3b05073

File tree

1 file changed

+31
-43
lines changed

1 file changed

+31
-43
lines changed

ios/CodePush/CodePush.m

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ @implementation CodePush {
1515
BOOL _isFirstRunAfterUpdate;
1616
int _minimumBackgroundDuration;
1717
NSDate *_lastResignedDate;
18+
long long latestExpectedContentLength;
19+
long long latestReceivedConentLength;
20+
BOOL didUpdateProgress;
1821
}
1922

2023
RCT_EXPORT_MODULE()
@@ -225,6 +228,17 @@ - (void)dealloc
225228
[[NSNotificationCenter defaultCenter] removeObserver:self];
226229
}
227230

231+
- (void)dispatchDownloadProgressEvent
232+
{
233+
// Notify the script-side about the progress
234+
[self.bridge.eventDispatcher
235+
sendDeviceEventWithName:@"CodePushDownloadProgress"
236+
body:@{
237+
@"totalBytes":[NSNumber numberWithLongLong:latestExpectedContentLength],
238+
@"receivedBytes":[NSNumber numberWithLongLong:latestReceivedConentLength]
239+
}];
240+
}
241+
228242
/*
229243
* This method ensures that the app was packaged with a JS bundle
230244
* file, and if not, it throws the appropriate exception.
@@ -275,7 +289,7 @@ - (void)initializeUpdateAfterRestart
275289
#ifdef DEBUG
276290
[self clearDebugUpdates];
277291
#endif
278-
[self pauseFrameObserver];
292+
_paused = YES;
279293
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
280294
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
281295
if (pendingUpdate) {
@@ -493,7 +507,10 @@ - (void)applicationWillResignActive
493507
}
494508

495509
if (notifyProgress) {
496-
[self setupFrameObserverForDownloadProgress];
510+
// Set up and unpause the frame observer so that it can emit
511+
// progress events every frame if the progress is updated.
512+
didUpdateProgress = NO;
513+
_paused = NO;
497514
}
498515

499516
[CodePushPackage
@@ -502,11 +519,16 @@ - (void)applicationWillResignActive
502519
operationQueue:_methodQueue
503520
// The download is progressing forward
504521
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
505-
[self updateDownloadProgressForNextFrame:expectedContentLength
506-
receivedContentLength:receivedContentLength];
507-
// If the download is completed, stop observing frame updates and synchronously send the last event.
522+
// Update the download progress so that the frame observer can notify the JS side
523+
latestExpectedContentLength = expectedContentLength;
524+
latestReceivedConentLength = receivedContentLength;
525+
didUpdateProgress = YES;
526+
527+
// If the download is completed, stop observing frame
528+
// updates and synchronously send the last event.
508529
if (expectedContentLength == receivedContentLength) {
509-
[self pauseFrameObserver];
530+
didUpdateProgress = NO;
531+
_paused = YES;
510532
[self dispatchDownloadProgressEvent];
511533
}
512534
}
@@ -526,7 +548,9 @@ - (void)applicationWillResignActive
526548
[self saveFailedUpdate:mutableUpdatePackage];
527549
}
528550

529-
[self pauseFrameObserver];
551+
// Stop observing frame updates if the download fails.
552+
didUpdateProgress = NO;
553+
_paused = YES;
530554
reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
531555
}];
532556
}
@@ -764,10 +788,6 @@ - (void)applicationWillResignActive
764788

765789
#pragma mark - RCTFrameUpdateObserver Methods
766790

767-
long long latestExpectedContentLength = -1;
768-
long long latestReceivedConentLength = -1;
769-
BOOL didUpdateProgress = NO;
770-
771791
- (void)didUpdateFrame:(RCTFrameUpdate *)update
772792
{
773793
if (!didUpdateProgress) {
@@ -778,36 +798,4 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
778798
didUpdateProgress = NO;
779799
}
780800

781-
- (void)dispatchDownloadProgressEvent
782-
{
783-
// Notify the script-side about the progress
784-
[self.bridge.eventDispatcher
785-
sendDeviceEventWithName:@"CodePushDownloadProgress"
786-
body:@{
787-
@"totalBytes":[NSNumber numberWithLongLong:latestExpectedContentLength],
788-
@"receivedBytes":[NSNumber numberWithLongLong:latestReceivedConentLength]
789-
}];
790-
}
791-
792-
- (void)updateDownloadProgressForNextFrame:(long long)expectedContentLength
793-
receivedContentLength:(long long)receivedContentLength
794-
{
795-
latestExpectedContentLength = expectedContentLength;
796-
latestReceivedConentLength = receivedContentLength;
797-
didUpdateProgress = YES;
798-
}
799-
800-
801-
- (void)setupFrameObserverForDownloadProgress
802-
{
803-
didUpdateProgress = NO;
804-
_paused = NO;
805-
}
806-
807-
- (void)pauseFrameObserver
808-
{
809-
didUpdateProgress = NO;
810-
_paused = YES;
811-
}
812-
813801
@end

0 commit comments

Comments
 (0)