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

Commit a37c0b9

Browse files
author
scottbommarito
committed
use promises and an async function to improve behavior
1 parent 7e5ac6b commit a37c0b9

File tree

7 files changed

+63
-29
lines changed

7 files changed

+63
-29
lines changed

RestartManager.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ const NativeCodePush = require("react-native").NativeModules.CodePush;
33
const CodePush = require("./CodePush");
44

55
const RestartManager = (() => {
6-
let _inProgress = false;
6+
let _inProgressPromise = null;
7+
let _inProgressOnUpdateOnly = false;
8+
79
let _allowed = true;
810
let _restartPending = false;
11+
let _restartPendingOnUpdateOnly = false;
912

1013
function allow() {
1114
log("Re-allowing restarts");
1215
_allowed = true;
1316

1417
if (_restartPending) {
1518
log("Executing pending restart");
16-
restartApp(true);
19+
restartApp(_restartPendingOnUpdateOnly);
1720
}
1821
}
1922

@@ -27,18 +30,42 @@ const RestartManager = (() => {
2730
}
2831

2932
function restartApp(onlyIfUpdateIsPending = false) {
30-
if (_allowed) {
31-
if (_inProgress) {
32-
log("A restart request is already in progress or queued");
33+
(async function(onlyIfUpdateIsPending) {
34+
var didRestartSucceed = false;
35+
36+
if (_restartPending) {
37+
_restartPendingOnUpdateOnly = _restartPendingOnUpdateOnly && onlyIfUpdateIsPending;
38+
log("Restart request queued until restarts are re-allowed");
3339
return;
3440
}
35-
// The restart won't execute if `onlyIfUpdateIsPending === true` and there is no pending update.
36-
_inProgress = !onlyIfUpdateIsPending || !!(NativeCodePush.getUpdateMetadata(CodePush.UpdateState.PENDING));
37-
NativeCodePush.restartApp(onlyIfUpdateIsPending);
41+
42+
if (!!_inProgressPromise) {
43+
didRestartSucceed = await _inProgressPromise;
44+
if (didRestartSucceed) {
45+
log("A restart is already in progress.");
46+
return;
47+
}
48+
}
49+
50+
_inProgressPromise = new Promise(async function(resolve, reject) {
51+
resolve(await restartAppInternal(onlyIfUpdateIsPending));
52+
});
53+
_inProgressOnUpdateOnly = onlyIfUpdateIsPending;
54+
55+
didRestartSucceed = await _inProgressPromise;
56+
if (!didRestartSucceed) _inProgressPromise = null;
57+
})(onlyIfUpdateIsPending);
58+
};
59+
60+
async function restartAppInternal(onlyIfUpdateIsPending = false) {
61+
if (_allowed) {
62+
var didRestartSucceed = await NativeCodePush.restartApp(onlyIfUpdateIsPending);
3863
log("Restarting app");
64+
return didRestartSucceed;
3965
} else {
4066
log("Restart request queued until restarts are re-allowed");
4167
_restartPending = true;
68+
_restartPendingOnUpdateOnly = onlyIfUpdateIsPending;
4269
return true;
4370
}
4471
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,12 +738,14 @@ public void recordStatusReported(ReadableMap statusReport) {
738738
}
739739

740740
@ReactMethod
741-
public void restartApp(boolean onlyIfUpdateIsPending) {
741+
public void restartApp(boolean onlyIfUpdateIsPending, Promise promise) {
742742
// If this is an unconditional restart request, or there
743743
// is current pending update, then reload the app.
744744
if (!onlyIfUpdateIsPending || CodePush.this.isPendingUpdate(null)) {
745745
loadBundle();
746+
promise.resolve(true);
746747
}
748+
promise.resolve(false);
747749
}
748750

749751
@ReactMethod

code-push-plugin-testing-framework/script/testBuilder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function itInternal(func, expectation, isCoreTest, assertion) {
6464
if ((!TestConfig.onlyRunCoreTests || isCoreTest)) {
6565
// Create a wrapper around the assertion to set the timeout on the test to 10 minutes.
6666
var assertionWithTimeout = function (done) {
67-
this.timeout(10 * 60 * 1000);
67+
this.timeout(20 * 60 * 1000);
6868
assertion(done);
6969
};
7070
return it(expectation, assertionWithTimeout);

code-push-plugin-testing-framework/script/testUtil.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var TestUtil = (function () {
4848
if (options.maxBuffer === undefined)
4949
options.maxBuffer = 1024 * 500;
5050
if (options.timeout === undefined)
51-
options.timeout = 10 * 60 * 1000;
51+
options.timeout = 20 * 60 * 1000;
5252
if (!options.noLogCommand)
5353
console.log("Running command: " + command);
5454
var execProcess = child_process.exec(command, options, function (error, stdout, stderr) {

ios/CodePush/CodePush.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,13 +740,17 @@ - (void)applicationWillResignActive
740740
/*
741741
* This method is the native side of the CodePush.restartApp() method.
742742
*/
743-
RCT_EXPORT_METHOD(restartApp:(BOOL)onlyIfUpdateIsPending)
743+
RCT_EXPORT_METHOD(restartApp:(BOOL)onlyIfUpdateIsPending
744+
resolve:(RCTPromiseResolveBlock)resolve
745+
rejecter:(RCTPromiseRejectBlock)reject)
744746
{
745747
// If this is an unconditional restart request, or there
746748
// is current pending update, then reload the app.
747749
if (!onlyIfUpdateIsPending || [self isPendingUpdate:nil]) {
748750
[self loadBundle];
751+
resolve(@(YES));
749752
}
753+
resolve(@(NO));
750754
}
751755

752756
#pragma mark - JavaScript-exported module methods (Private)

test/template/scenarios/scenarioRestart2x.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var CodePushWrapper = require("../codePushWrapper.js");
12
import CodePush from "react-native-code-push";
23

34
module.exports = {

test/test.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -990,28 +990,28 @@ PluginTestingFramework.initializeTests(new RNProjectManager(), supportedTargetPl
990990

991991
TestBuilder.describe("#codePush.restartApplication.2x",
992992
() => {
993-
// TestBuilder.it("blocks when a restart is in progress and doesn't crash if there is a pending package", false,
994-
// (done: MochaDone) => {
995-
// ServerUtil.updateResponse = { updateInfo: ServerUtil.createUpdateResponse(false, targetPlatform) };
996-
// setupTestRunScenario(projectManager, targetPlatform, ScenarioInstallRestart2x)
997-
// .then(setupUpdateScenario.bind(this, projectManager, targetPlatform, UpdateDeviceReady, "Good Update"))
998-
// .then<void>((updatePath: string) => {
999-
// ServerUtil.updatePackagePath = updatePath;
1000-
// projectManager.runApplication(TestConfig.testRunDirectory, targetPlatform);
1001-
// return ServerUtil.expectTestMessages([
1002-
// ServerUtil.TestMessage.CHECK_UPDATE_AVAILABLE,
1003-
// ServerUtil.TestMessage.DOWNLOAD_SUCCEEDED,
1004-
// ServerUtil.TestMessage.UPDATE_INSTALLED,
1005-
// ServerUtil.TestMessage.DEVICE_READY_AFTER_UPDATE]);
1006-
// })
1007-
// .done(() => { done(); }, (e) => { done(e); });
1008-
// });
993+
TestBuilder.it("blocks when a restart is in progress and doesn't crash if there is a pending package", false,
994+
(done: MochaDone) => {
995+
ServerUtil.updateResponse = { updateInfo: ServerUtil.createUpdateResponse(false, targetPlatform) };
996+
setupTestRunScenario(projectManager, targetPlatform, ScenarioInstallRestart2x)
997+
.then(setupUpdateScenario.bind(this, projectManager, targetPlatform, UpdateDeviceReady, "Update 1"))
998+
.then<void>((updatePath: string) => {
999+
ServerUtil.updatePackagePath = updatePath;
1000+
projectManager.runApplication(TestConfig.testRunDirectory, targetPlatform);
1001+
return ServerUtil.expectTestMessages([
1002+
ServerUtil.TestMessage.CHECK_UPDATE_AVAILABLE,
1003+
ServerUtil.TestMessage.DOWNLOAD_SUCCEEDED,
1004+
ServerUtil.TestMessage.UPDATE_INSTALLED,
1005+
ServerUtil.TestMessage.DEVICE_READY_AFTER_UPDATE]);
1006+
})
1007+
.done(() => { done(); }, (e) => { done(e); });
1008+
});
10091009

10101010
TestBuilder.it("doesn't block when the restart is ignored", false,
10111011
(done: MochaDone) => {
10121012
ServerUtil.updateResponse = { updateInfo: ServerUtil.createUpdateResponse(false, targetPlatform) };
10131013
setupTestRunScenario(projectManager, targetPlatform, ScenarioRestart2x)
1014-
.then(setupUpdateScenario.bind(this, projectManager, targetPlatform, UpdateDeviceReady, "Good Update"))
1014+
.then(setupUpdateScenario.bind(this, projectManager, targetPlatform, UpdateDeviceReady, "Update 1"))
10151015
.then<void>((updatePath: string) => {
10161016
ServerUtil.updatePackagePath = updatePath;
10171017
projectManager.runApplication(TestConfig.testRunDirectory, targetPlatform);

0 commit comments

Comments
 (0)