forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RestartManager.js
59 lines (50 loc) · 1.63 KB
/
RestartManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const log = require("./logging");
const NativeCodePush = require("react-native").NativeModules.CodePush;
const RestartManager = (() => {
let _allowed = true;
let _restartInProgress = false;
let _restartQueue = [];
function allow() {
log("Re-allowing restarts");
_allowed = true;
if (_restartQueue.length) {
log("Executing pending restart");
restartApp(_restartQueue.shift(1));
}
}
function clearPendingRestart() {
_restartQueue = [];
}
function disallow() {
log("Disallowing restarts");
_allowed = false;
}
async function restartApp(onlyIfUpdateIsPending = false) {
if (_restartInProgress) {
log("Restart request queued until the current restart is completed");
_restartQueue.push(onlyIfUpdateIsPending);
} else if (!_allowed) {
log("Restart request queued until restarts are re-allowed");
_restartQueue.push(onlyIfUpdateIsPending);
} else {
_restartInProgress = true;
if (await NativeCodePush.restartApp(onlyIfUpdateIsPending)) {
// The app has already restarted, so there is no need to
// process the remaining queued restarts.
log("Restarting app");
return;
}
_restartInProgress = false;
if (_restartQueue.length) {
restartApp(_restartQueue.shift(1));
}
}
}
return {
allow,
clearPendingRestart,
disallow,
restartApp
};
})();
module.exports = RestartManager;