forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRestartManager.js
81 lines (67 loc) · 2.48 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const log = require("./logging");
const NativeCodePush = require("react-native").NativeModules.CodePush;
const CodePush = require("./CodePush");
const RestartManager = (() => {
let _inProgressPromise = null;
let _inProgressOnUpdateOnly = false;
let _allowed = true;
let _restartPending = false;
let _restartPendingOnUpdateOnly = false;
function allow() {
log("Re-allowing restarts");
_allowed = true;
if (_restartPending) {
log("Executing pending restart");
restartApp(_restartPendingOnUpdateOnly);
}
}
function clearPendingRestart() {
_restartPending = false;
}
function disallow() {
log("Disallowing restarts");
_allowed = false;
}
function restartApp(onlyIfUpdateIsPending = false) {
(async function(onlyIfUpdateIsPending) {
var didRestartSucceed = false;
if (_restartPending) {
_restartPendingOnUpdateOnly = _restartPendingOnUpdateOnly && onlyIfUpdateIsPending;
log("Restart request queued until restarts are re-allowed");
return;
}
if (!!_inProgressPromise) {
didRestartSucceed = await _inProgressPromise;
if (didRestartSucceed) {
log("A restart is already in progress.");
return;
}
}
_inProgressPromise = new Promise(async function(resolve, reject) {
resolve(await restartAppInternal(onlyIfUpdateIsPending));
});
_inProgressOnUpdateOnly = onlyIfUpdateIsPending;
didRestartSucceed = await _inProgressPromise;
if (!didRestartSucceed) _inProgressPromise = null;
})(onlyIfUpdateIsPending);
};
async function restartAppInternal(onlyIfUpdateIsPending = false) {
if (_allowed) {
var didRestartSucceed = await NativeCodePush.restartApp(onlyIfUpdateIsPending);
log("Restarting app");
return didRestartSucceed;
} else {
log("Restart request queued until restarts are re-allowed");
_restartPending = true;
_restartPendingOnUpdateOnly = onlyIfUpdateIsPending;
return true;
}
}
return {
allow,
clearPendingRestart,
disallow,
restartApp
};
})();
module.exports = RestartManager;