@@ -15,6 +15,9 @@ @implementation CodePush {
15
15
BOOL _isFirstRunAfterUpdate;
16
16
int _minimumBackgroundDuration;
17
17
NSDate *_lastResignedDate;
18
+ long long latestExpectedContentLength;
19
+ long long latestReceivedConentLength;
20
+ BOOL didUpdateProgress;
18
21
}
19
22
20
23
RCT_EXPORT_MODULE ()
@@ -225,6 +228,17 @@ - (void)dealloc
225
228
[[NSNotificationCenter defaultCenter ] removeObserver: self ];
226
229
}
227
230
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
+
228
242
/*
229
243
* This method ensures that the app was packaged with a JS bundle
230
244
* file, and if not, it throws the appropriate exception.
@@ -275,7 +289,7 @@ - (void)initializeUpdateAfterRestart
275
289
#ifdef DEBUG
276
290
[self clearDebugUpdates ];
277
291
#endif
278
- [ self pauseFrameObserver ] ;
292
+ _paused = YES ;
279
293
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults ];
280
294
NSDictionary *pendingUpdate = [preferences objectForKey: PendingUpdateKey];
281
295
if (pendingUpdate) {
@@ -493,7 +507,10 @@ - (void)applicationWillResignActive
493
507
}
494
508
495
509
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 ;
497
514
}
498
515
499
516
[CodePushPackage
@@ -502,11 +519,16 @@ - (void)applicationWillResignActive
502
519
operationQueue: _methodQueue
503
520
// The download is progressing forward
504
521
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.
508
529
if (expectedContentLength == receivedContentLength) {
509
- [self pauseFrameObserver ];
530
+ didUpdateProgress = NO ;
531
+ _paused = YES ;
510
532
[self dispatchDownloadProgressEvent ];
511
533
}
512
534
}
@@ -526,7 +548,9 @@ - (void)applicationWillResignActive
526
548
[self saveFailedUpdate: mutableUpdatePackage];
527
549
}
528
550
529
- [self pauseFrameObserver ];
551
+ // Stop observing frame updates if the download fails.
552
+ didUpdateProgress = NO ;
553
+ _paused = YES ;
530
554
reject ([NSString stringWithFormat: @" %lu " , (long )err.code], err.localizedDescription , err);
531
555
}];
532
556
}
@@ -764,10 +788,6 @@ - (void)applicationWillResignActive
764
788
765
789
#pragma mark - RCTFrameUpdateObserver Methods
766
790
767
- long long latestExpectedContentLength = -1 ;
768
- long long latestReceivedConentLength = -1 ;
769
- BOOL didUpdateProgress = NO ;
770
-
771
791
- (void )didUpdateFrame : (RCTFrameUpdate *)update
772
792
{
773
793
if (!didUpdateProgress) {
@@ -778,36 +798,4 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
778
798
didUpdateProgress = NO ;
779
799
}
780
800
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
-
813
801
@end
0 commit comments