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

Commit c9a8f72

Browse files
committed
use queue instead
1 parent ab8f338 commit c9a8f72

File tree

1 file changed

+25
-48
lines changed

1 file changed

+25
-48
lines changed

RestartManager.js

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,55 @@
11
const log = require("./logging");
22
const NativeCodePush = require("react-native").NativeModules.CodePush;
3-
const CodePush = require("./CodePush");
43

54
const RestartManager = (() => {
6-
let _inProgressPromise = null;
7-
let _inProgressOnUpdateOnly = false;
8-
95
let _allowed = true;
10-
let _restartPending = false;
11-
let _restartPendingOnUpdateOnly = false;
6+
let _restartQueue = [];
127

138
function allow() {
149
log("Re-allowing restarts");
1510
_allowed = true;
16-
17-
if (_restartPending) {
11+
12+
if (_restartQueue.length) {
1813
log("Executing pending restart");
19-
restartApp(_restartPendingOnUpdateOnly);
14+
restartApp(_restartQueue.shift(1));
2015
}
2116
}
22-
17+
2318
function clearPendingRestart() {
24-
_restartPending = false;
19+
_restartQueue = [];
2520
}
26-
21+
2722
function disallow() {
2823
log("Disallowing restarts");
2924
_allowed = false;
3025
}
3126

32-
function restartApp(onlyIfUpdateIsPending = false) {
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");
27+
async function restartApp(onlyIfUpdateIsPending = false) {
28+
if (_restartInProgress) {
29+
log("Restart request queued until the current restart is completed");
30+
_restartQueue.push(onlyIfUpdateIsPending);
31+
} else if (!_allowed) {
32+
log("Restart request queued until restarts are re-allowed");
33+
_restartQueue.push(onlyIfUpdateIsPending);
34+
} else {
35+
_restartInProgress = true;
36+
log("Restarting app");
37+
if (await NativeCodePush.restartApp(onlyIfUpdateIsPending)) {
38+
// The app has already restarted, so there is no need to
39+
// process the remaining queued restarts.
3940
return;
4041
}
41-
42-
if (!!_inProgressPromise) {
43-
didRestartSucceed = await _inProgressPromise;
44-
if (didRestartSucceed) {
45-
log("A restart is already in progress.");
46-
return;
47-
}
42+
43+
_restartInProgress = false;
44+
if (_restartQueue.length) {
45+
restartApp(_restartQueue.shift(1));
4846
}
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);
63-
log("Restarting app");
64-
return didRestartSucceed;
65-
} else {
66-
log("Restart request queued until restarts are re-allowed");
67-
_restartPending = true;
68-
_restartPendingOnUpdateOnly = onlyIfUpdateIsPending;
69-
return true;
7047
}
7148
}
7249

7350
return {
7451
allow,
75-
clearPendingRestart,
52+
clearPendingRestart,
7653
disallow,
7754
restartApp
7855
};

0 commit comments

Comments
 (0)