@@ -180,12 +180,16 @@ + (void)setUsingTestConfiguration:(BOOL)shouldUseTestConfiguration
180
180
*/
181
181
- (NSDictionary *)constantsToExport
182
182
{
183
- // Export the values of the CodePushInstallMode enum
184
- // so that the script-side can easily stay in sync
183
+ // Export the values of the CodePushInstallMode and CodePushUpdateState
184
+ // enums so that the script-side can easily stay in sync
185
185
return @{
186
186
@" codePushInstallModeOnNextRestart" :@(CodePushInstallModeOnNextRestart),
187
187
@" codePushInstallModeImmediate" : @(CodePushInstallModeImmediate),
188
- @" codePushInstallModeOnNextResume" : @(CodePushInstallModeOnNextResume)
188
+ @" codePushInstallModeOnNextResume" : @(CodePushInstallModeOnNextResume),
189
+
190
+ @" codePushUpdateStateRunning" : @(CodePushUpdateStateRunning),
191
+ @" codePushUpdateStatePending" : @(CodePushUpdateStatePending),
192
+ @" codePushUpdateStateLatest" : @(CodePushUpdateStateLatest)
189
193
};
190
194
};
191
195
@@ -532,35 +536,49 @@ - (void)applicationWillResignActive
532
536
}
533
537
534
538
/*
535
- * This method is the native side of the CodePush.getCurrentPackage method.
539
+ * This method is the native side of the CodePush.getUpdateMetadata method.
536
540
*/
537
- RCT_EXPORT_METHOD (getCurrentPackage:(RCTPromiseResolveBlock)resolve
541
+ RCT_EXPORT_METHOD (getUpdateMetadata:(CodePushUpdateState)updateState
542
+ resolver:(RCTPromiseResolveBlock)resolve
538
543
rejecter:(RCTPromiseRejectBlock)reject)
539
544
{
540
545
NSError *error;
541
546
NSMutableDictionary *package = [[CodePushPackage getCurrentPackage: &error] mutableCopy ];
542
547
543
548
if (error) {
544
- reject ([NSString stringWithFormat: @" %lu " , (long )error.code], error.localizedDescription , error);
545
- return ;
549
+ return reject ([NSString stringWithFormat: @" %lu " , (long )error.code], error.localizedDescription , error);
546
550
} else if (package == nil ) {
547
- resolve (nil );
548
- return ;
551
+ // The app hasn't downloaded any CodePush updates yet,
552
+ // so we simply return nil regardless if the user
553
+ // wanted to retrieve the pending or running update.
554
+ return resolve (nil );
549
555
}
550
556
551
- if (isRunningBinaryVersion) {
552
- // This only matters in Debug builds. Since we do not clear "outdated" updates,
553
- // we need to indicate to the JS side that somehow we have a current update on
554
- // disk that is not actually running.
555
- [package setObject: @(YES ) forKey: @" _isDebugOnly" ];
557
+ // We have a CodePush update, so let's see if it's currently in a pending state.
558
+ BOOL currentUpdateIsPending = [self isPendingUpdate: [package objectForKey: PackageHashKey]];
559
+
560
+ if (updateState == CodePushUpdateStatePending && !currentUpdateIsPending) {
561
+ // The caller wanted a pending update
562
+ // but there isn't currently one.
563
+ resolve (nil );
564
+ } else if (updateState == CodePushUpdateStateRunning && currentUpdateIsPending) {
565
+ // The caller wants the running update, but the current
566
+ // one is pending, so we need to grab the previous.
567
+ resolve ([CodePushPackage getPreviousPackage: nil ]);
568
+ } else {
569
+ // The current package satisfies the request:
570
+ // 1) Caller wanted a pending, and there is a pending update
571
+ // 2) Caller wanted the running update, and there isn't a pending
572
+ // 3) Calers wants the latest update, regardless if it's pending or not
573
+ if (isRunningBinaryVersion) {
574
+ // This only matters in Debug builds. Since we do not clear "outdated" updates,
575
+ // we need to indicate to the JS side that somehow we have a current update on
576
+ // disk that is not actually running.
577
+ [package setObject: @(YES ) forKey: @" _isDebugOnly" ];
578
+ }
579
+
580
+ resolve (package);
556
581
}
557
-
558
- // Add the "isPending" virtual property to the package at this point, so that
559
- // the script-side doesn't need to immediately call back into native to populate it.
560
- BOOL isPendingUpdate = [self isPendingUpdate: [package objectForKey: PackageHashKey]];
561
- [package setObject: @(isPendingUpdate) forKey: PackageIsPendingKey];
562
-
563
- resolve (package);
564
582
}
565
583
566
584
/*
0 commit comments