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

Commit cad43b8

Browse files
committed
Merge pull request #129 from Microsoft/restart_improvement
Adding conditional check to restartApp method
2 parents 7d24ac8 + 725adc8 commit cad43b8

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

CodePush.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ function log(message) {
109109
console.log(`[CodePush] ${message}`)
110110
}
111111

112+
function restartApp(onlyIfUpdateIsPending = false) {
113+
NativeCodePush.restartApp(onlyIfUpdateIsPending);
114+
}
115+
112116
var testConfig;
113117

114118
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
@@ -265,7 +269,7 @@ const CodePush = {
265269
getCurrentPackage,
266270
log,
267271
notifyApplicationReady: NativeCodePush.notifyApplicationReady,
268-
restartApp: NativeCodePush.restartApp,
272+
restartApp,
269273
setUpTestDependencies,
270274
sync,
271275
InstallMode: {

CodePush.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ - (BOOL)isPendingUpdate:(NSString*)packageHash
203203
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
204204
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
205205

206-
// If there is a pending update, whose hash is equal to the one
207-
// specified, and its "state" isn't loading, then we consider it "pending".
206+
// If there is a pending update whose "state" isn't loading, then we consider it "pending".
207+
// Additionally, if a specific hash was provided, we ensure it matches that of the pending update.
208208
BOOL updateIsPending = pendingUpdate &&
209209
[pendingUpdate[PendingUpdateIsLoadingKey] boolValue] == NO &&
210-
[pendingUpdate[PendingUpdateHashKey] isEqualToString:packageHash];
210+
(!packageHash || [pendingUpdate[PendingUpdateHashKey] isEqualToString:packageHash]);
211211

212212
return updateIsPending;
213213
}
@@ -467,9 +467,13 @@ - (void)savePendingUpdate:(NSString *)packageHash
467467
/*
468468
* This method is the native side of the CodePush.restartApp() method.
469469
*/
470-
RCT_EXPORT_METHOD(restartApp)
470+
RCT_EXPORT_METHOD(restartApp:(BOOL)onlyIfUpdateIsPending)
471471
{
472-
[self loadBundle];
472+
// If this is an unconditional restart request, or there
473+
// is current pending update, then reload the app.
474+
if (!onlyIfUpdateIsPending || [self isPendingUpdate:nil]) {
475+
[self loadBundle];
476+
}
473477
}
474478

475479
/*

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,15 @@ If you are using the `sync` function, and doing your update check on app start,
336336
#### codePush.restartApp
337337
338338
```javascript
339-
codePush.restartApp(): void;
339+
codePush.restartApp(onlyIfUpdateIsPending: Boolean = false): void;
340340
```
341341
342-
Immediately restarts the app. If there is an update pending, it will be presented to the end user and the rollback timer (if specified when installing the update) will begin. Otherwise, calling this method simply has the same behavior as the end user killing and restarting the process. This method is for advanced scenarios, and is primarily useful when the following conditions are true:
342+
Immediately restarts the app. If a truthy value is provided to the `onlyIfUpdateIsPending` parameter, then the app will only restart if there is actually a pending update waiting to be applied.
343+
344+
This method is for advanced scenarios, and is primarily useful when the following conditions are true:
343345

344-
1. Your app is specifying an install mode value of `ON_NEXT_RESTART` or `ON_NEXT_RESUME` when calling the `sync` or `LocalPackage.install` methods. This has the effect of not applying your update until the app has been restarted (by either the end user or OS) or resumed, and therefore, the update won't be immediately displayed to the end user .
346+
1. Your app is specifying an install mode value of `ON_NEXT_RESTART` or `ON_NEXT_RESUME` when calling the `sync` or `LocalPackage.install` methods. This has the effect of not applying your update until the app has been restarted (by either the end user or OS) or resumed, and therefore, the update won't be immediately displayed to the end user.
347+
345348
2. You have an app-specific user event (e.g. the end user navigated back to the app's home route) that allows you to apply the update in an unobtrusive way, and potentially gets the update in front of the end user sooner then waiting until the next restart or resume.
346349

347350
#### codePush.sync

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private boolean isPendingUpdate(String packageHash) {
241241
try {
242242
boolean updateIsPending = pendingUpdate != null &&
243243
pendingUpdate.getBoolean(PENDING_UPDATE_IS_LOADING_KEY) == false &&
244-
pendingUpdate.getString(PENDING_UPDATE_HASH_KEY).equals(packageHash);
244+
(packageHash == null || pendingUpdate.getString(PENDING_UPDATE_HASH_KEY).equals(packageHash));
245245
return updateIsPending;
246246
}
247247
catch (JSONException e) {
@@ -483,8 +483,12 @@ public void notifyApplicationReady(Promise promise) {
483483
}
484484

485485
@ReactMethod
486-
public void restartApp() {
487-
loadBundle();
486+
public void restartApp(boolean onlyIfUpdateIsPending) {
487+
// If this is an unconditional restart request, or there
488+
// is current pending update, then reload the app.
489+
if (!onlyIfUpdateIsPending || CodePush.this.isPendingUpdate(null)) {
490+
loadBundle();
491+
}
488492
}
489493

490494
@ReactMethod

0 commit comments

Comments
 (0)