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

Commit 1c74ab3

Browse files
committed
add comments and remove native in-mem caching
1 parent c1187db commit 1c74ab3

File tree

5 files changed

+49
-63
lines changed

5 files changed

+49
-63
lines changed

CodePush.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async function checkForUpdate(deploymentKey = null) {
7070
* because we want to avoid having to install diff updates against the binary's
7171
* version, which we can't do yet on Android.
7272
*/
73-
if (!update || update.updateAppVersion || localPackage && (update.packageHash === localPackage.packageHash) || localPackage && config.packageHash === localPackage.packageHash) {
73+
if (!update || update.updateAppVersion || localPackage && (update.packageHash === localPackage.packageHash) || config.packageHash === update.packageHash) {
7474
if (update && update.updateAppVersion) {
7575
log("An update is available but it is targeting a newer binary version than you are currently running.");
7676
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ public void getConfiguration(Promise promise) {
404404
android.provider.Settings.Secure.ANDROID_ID));
405405
String binaryHash = CodePushUpdateUtils.getHashForBinaryContents(mainActivity, isDebugMode);
406406
if (binaryHash != null) {
407+
// binaryHash will be null if the React Native assets were not bundled into the APK
408+
// (e.g. in Debug builds)
407409
configMap.putString(PACKAGE_HASH_KEY, binaryHash);
408410
}
409411

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ public class CodePushUpdateUtils {
2424

2525
private static final String CODE_PUSH_HASH_FILE_NAME = "CodePushHash.json";
2626

27-
// These variables are used to cache the hash of the binary contents in memory.
28-
private static String binaryHash = null;
29-
private static boolean didLoadBinaryHash = false;
30-
3127
private static void addContentsOfFolderToManifest(String folderPath, String pathPrefix, ArrayList<String> manifest) {
3228
File folder = new File(folderPath);
3329
File[] folderFiles = folder.listFiles();
@@ -110,22 +106,17 @@ public static String findJSBundleInUpdateContents(String folderPath) {
110106
}
111107

112108
public static String getHashForBinaryContents(Activity mainActivity, boolean isDebugMode) {
113-
if (!didLoadBinaryHash) {
114-
didLoadBinaryHash = true;
115-
try {
116-
binaryHash = CodePushUtils.getStringFromInputStream(mainActivity.getAssets().open(CODE_PUSH_HASH_FILE_NAME));
117-
} catch (IOException e) {
118-
if (!isDebugMode) {
119-
// Only print this message in "Release" mode. In "Debug", we may not have the
120-
// hash if the build skips bundling the files.
121-
CodePushUtils.log("Unable to get the hash of the binary's bundled resources - \"codepush.gradle\" may have not been added to the build definition.");
122-
}
123-
124-
return null;
109+
try {
110+
return CodePushUtils.getStringFromInputStream(mainActivity.getAssets().open(CODE_PUSH_HASH_FILE_NAME));
111+
} catch (IOException e) {
112+
if (!isDebugMode) {
113+
// Only print this message in "Release" mode. In "Debug", we may not have the
114+
// hash if the build skips bundling the files.
115+
CodePushUtils.log("Unable to get the hash of the binary's bundled resources - \"codepush.gradle\" may have not been added to the build definition.");
125116
}
126-
}
127117

128-
return binaryHash;
118+
return null;
119+
}
129120
}
130121

131122
public static void verifyHashForDiffUpdate(String folderPath, String expectedHash) {

ios/CodePush/CodePush.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ - (void)savePendingUpdate:(NSString *)packageHash
447447
}
448448

449449
if (binaryHash == nil) {
450+
// The hash was not generated either due to a previous unknown error or the fact that
451+
// the React Native assets were not bundled in the binary (e.g. during dev/simulator)
452+
// builds.
450453
resolve(configuration);
451454
return;
452455
}

ios/CodePush/CodePushUpdateUtils.m

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ @implementation CodePushUpdateUtils
77
NSString * const BinaryHashKey = @"CodePushBinaryHash";
88
NSString * const ManifestFolderPrefix = @"CodePush";
99

10-
// These variables are used to cache the hash of the binary contents in memory.
11-
static NSString *binaryHash = nil;
12-
static BOOL didLoadBinaryHash = NO;
13-
1410
+ (void)addContentsOfFolderToManifest:(NSString *)folderPath
1511
pathPrefix:(NSString *)pathPrefix
1612
manifest:(NSMutableArray *)manifest
@@ -162,49 +158,43 @@ + (NSString *)getAssetsFolderName
162158
+ (NSString *)getHashForBinaryContents:(NSURL *)binaryBundleUrl
163159
error:(NSError **)error
164160
{
165-
if (!didLoadBinaryHash) {
166-
didLoadBinaryHash = YES;
167-
168-
// Get the cached hash from user preferences if it exists.
169-
NSString *binaryModifiedDate = [self modifiedDateStringOfFileAtURL:binaryBundleUrl];
170-
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
171-
NSMutableDictionary *binaryHashDictionary = [preferences objectForKey:BinaryHashKey];
172-
if (binaryHashDictionary != nil) {
173-
binaryHash = [binaryHashDictionary objectForKey:binaryModifiedDate];
174-
if (binaryHash == nil) {
175-
[preferences removeObjectForKey:BinaryHashKey];
176-
[preferences synchronize];
177-
} else {
178-
return binaryHash;
179-
}
180-
}
181-
182-
binaryHashDictionary = [NSMutableDictionary dictionary];
183-
184-
NSString *assetsPath = [CodePushPackage getBinaryAssetsPath];
185-
NSMutableArray *manifest = [NSMutableArray array];
186-
[self addContentsOfFolderToManifest:assetsPath
187-
pathPrefix:[NSString stringWithFormat:@"%@/%@", [self getManifestFolderPrefix], @"assets"]
188-
manifest:manifest
189-
error:error];
190-
if (*error) {
191-
return nil;
161+
// Get the cached hash from user preferences if it exists.
162+
NSString *binaryModifiedDate = [self modifiedDateStringOfFileAtURL:binaryBundleUrl];
163+
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
164+
NSMutableDictionary *binaryHashDictionary = [preferences objectForKey:BinaryHashKey];
165+
NSString *binaryHash = nil;
166+
if (binaryHashDictionary != nil) {
167+
binaryHash = [binaryHashDictionary objectForKey:binaryModifiedDate];
168+
if (binaryHash == nil) {
169+
[preferences removeObjectForKey:BinaryHashKey];
170+
[preferences synchronize];
171+
} else {
172+
return binaryHash;
192173
}
193-
194-
NSData *jsBundleContents = [NSData dataWithContentsOfURL:binaryBundleUrl];
195-
NSString *jsBundleContentsHash = [self computeHashForData:jsBundleContents];
196-
[manifest addObject:[[NSString stringWithFormat:@"%@/%@:", [self getManifestFolderPrefix], [binaryBundleUrl lastPathComponent]] stringByAppendingString:jsBundleContentsHash]];
197-
binaryHash = [self computeFinalHashFromManifest:manifest error:error];
198-
199-
// Cache the hash in user preferences. This assumes that the modified date for the
200-
// JS bundle changes every time a new bundle is generated by the packager.
201-
[binaryHashDictionary setObject:binaryHash forKey:binaryModifiedDate];
202-
[preferences setObject:binaryHashDictionary forKey:BinaryHashKey];
203-
[preferences synchronize];
204-
return binaryHash;
205174
}
206175

207-
// Use the cached hash in memory.
176+
binaryHashDictionary = [NSMutableDictionary dictionary];
177+
178+
NSString *assetsPath = [CodePushPackage getBinaryAssetsPath];
179+
NSMutableArray *manifest = [NSMutableArray array];
180+
[self addContentsOfFolderToManifest:assetsPath
181+
pathPrefix:[NSString stringWithFormat:@"%@/%@", [self getManifestFolderPrefix], @"assets"]
182+
manifest:manifest
183+
error:error];
184+
if (*error) {
185+
return nil;
186+
}
187+
188+
NSData *jsBundleContents = [NSData dataWithContentsOfURL:binaryBundleUrl];
189+
NSString *jsBundleContentsHash = [self computeHashForData:jsBundleContents];
190+
[manifest addObject:[[NSString stringWithFormat:@"%@/%@:", [self getManifestFolderPrefix], [binaryBundleUrl lastPathComponent]] stringByAppendingString:jsBundleContentsHash]];
191+
binaryHash = [self computeFinalHashFromManifest:manifest error:error];
192+
193+
// Cache the hash in user preferences. This assumes that the modified date for the
194+
// JS bundle changes every time a new bundle is generated by the packager.
195+
[binaryHashDictionary setObject:binaryHash forKey:binaryModifiedDate];
196+
[preferences setObject:binaryHashDictionary forKey:BinaryHashKey];
197+
[preferences synchronize];
208198
return binaryHash;
209199
}
210200

0 commit comments

Comments
 (0)