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

Commit d95eed3

Browse files
committed
update to master
2 parents bc52e8f + 9efd662 commit d95eed3

File tree

6 files changed

+292
-169
lines changed

6 files changed

+292
-169
lines changed

CodePush.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
214214
};
215215

216216
return new Promise((resolve, reject) => {
217+
var rejectPromise = (error) => {
218+
syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR);
219+
log(error.message);
220+
reject(error);
221+
};
222+
217223
CodePush.notifyApplicationReady()
218224
.then(() => {
219225
syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE);
@@ -230,7 +236,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
230236
resolve(CodePush.SyncStatus.UPDATE_INSTALLED);
231237
});
232238
})
233-
.catch(reject)
239+
.catch(rejectPromise)
234240
.done();
235241
}
236242

@@ -287,10 +293,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
287293
doDownloadAndInstall();
288294
}
289295
})
290-
.catch((error) => {
291-
syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR);
292-
reject(error);
293-
})
296+
.catch(rejectPromise)
294297
.done();
295298
});
296299
};

CodePush.m

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ @implementation CodePush {
1515

1616
static BOOL testConfigurationFlag = NO;
1717

18+
// These keys represent the names we use to store data in NSUserDefaults
1819
static NSString *const FailedUpdatesKey = @"CODE_PUSH_FAILED_UPDATES";
1920
static NSString *const PendingUpdateKey = @"CODE_PUSH_PENDING_UPDATE";
2021

@@ -23,7 +24,10 @@ @implementation CodePush {
2324
static NSString *const PendingUpdateHashKey = @"hash";
2425
static NSString *const PendingUpdateIsLoadingKey = @"isLoading";
2526

26-
id saveTestModule = nil;
27+
// These keys are used to inspect/augment the metadata
28+
// that is associated with an update's package.
29+
static NSString *const PackageHashKey = @"packageHash";
30+
static NSString *const PackageIsPendingKey = @"isPending";
2731

2832
@synthesize bridge = _bridge;
2933

@@ -176,6 +180,25 @@ - (BOOL)isFailedHash:(NSString*)packageHash
176180
return (failedUpdates != nil && [failedUpdates containsObject:packageHash]);
177181
}
178182

183+
/*
184+
* This method checks to see whether a specific package hash
185+
* represents a downloaded and installed update, that hasn't
186+
* been applied yet via an app restart.
187+
*/
188+
- (BOOL)isPendingUpdate:(NSString*)packageHash
189+
{
190+
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
191+
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
192+
193+
// If there is a pending update, whose hash is equal to the one
194+
// specified, and its "state" isn't loading, then we consider it "pending".
195+
BOOL updateIsPending = pendingUpdate &&
196+
[pendingUpdate[PendingUpdateIsLoadingKey] boolValue] == NO &&
197+
[pendingUpdate[PendingUpdateHashKey] isEqualToString:packageHash];
198+
199+
return updateIsPending;
200+
}
201+
179202
/*
180203
* This method updates the React Native bridge's bundle URL
181204
* to point at the latest CodePush update, and then restarts
@@ -195,7 +218,6 @@ - (void)loadBundle
195218
_bridge.bundleURL = url;
196219
}
197220

198-
saveTestModule = _bridge.modules[@"RCTTestModule"];
199221
[_bridge reload];
200222
});
201223
}
@@ -296,7 +318,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
296318
// The download completed
297319
doneCallback:^{
298320
NSError *err;
299-
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[@"packageHash"] error:&err];
321+
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err];
300322

301323
if (err) {
302324
return reject(err);
@@ -330,12 +352,18 @@ - (void)savePendingUpdate:(NSString *)packageHash
330352
{
331353
dispatch_async(dispatch_get_main_queue(), ^{
332354
NSError *error;
333-
NSDictionary *package = [CodePushPackage getCurrentPackage:&error];
355+
NSMutableDictionary *package = [[CodePushPackage getCurrentPackage:&error] mutableCopy];
356+
334357
if (error) {
335358
reject(error);
336-
} else {
337-
resolve(package);
338359
}
360+
361+
// Add the "isPending" virtual property to the package at this point, so that
362+
// the script-side doesn't need to immediately call back into native to populate it.
363+
BOOL isPendingUpdate = [self isPendingUpdate:[package objectForKey:PackageHashKey]];
364+
[package setObject:@(isPendingUpdate) forKey:PackageIsPendingKey];
365+
366+
resolve(package);
339367
});
340368
}
341369

@@ -355,7 +383,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
355383
if (error) {
356384
reject(error);
357385
} else {
358-
[self savePendingUpdate:updatePackage[@"packageHash"]
386+
[self savePendingUpdate:updatePackage[PackageHashKey]
359387
isLoading:NO];
360388

361389
if (installMode == CodePushInstallModeOnNextResume) {
@@ -378,7 +406,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
378406

379407
/*
380408
* This method isn't publicly exposed via the "react-native-code-push"
381-
* module, and is only used internally to populate the RemotePackage.failedApply property.
409+
* module, and is only used internally to populate the RemotePackage.failedInstall property.
382410
*/
383411
RCT_EXPORT_METHOD(isFailedUpdate:(NSString *)packageHash
384412
resolve:(RCTPromiseResolveBlock)resolve

0 commit comments

Comments
 (0)