|
1 | 1 | const log = require("./logging");
|
2 | 2 | const NativeCodePush = require("react-native").NativeModules.CodePush;
|
3 |
| -const CodePush = require("./CodePush"); |
4 | 3 |
|
5 | 4 | const RestartManager = (() => {
|
6 |
| - let _inProgressPromise = null; |
7 |
| - let _inProgressOnUpdateOnly = false; |
8 |
| - |
9 | 5 | let _allowed = true;
|
10 |
| - let _restartPending = false; |
11 |
| - let _restartPendingOnUpdateOnly = false; |
| 6 | + let _restartQueue = []; |
12 | 7 |
|
13 | 8 | function allow() {
|
14 | 9 | log("Re-allowing restarts");
|
15 | 10 | _allowed = true;
|
16 |
| - |
17 |
| - if (_restartPending) { |
| 11 | + |
| 12 | + if (_restartQueue.length) { |
18 | 13 | log("Executing pending restart");
|
19 |
| - restartApp(_restartPendingOnUpdateOnly); |
| 14 | + restartApp(_restartQueue.shift(1)); |
20 | 15 | }
|
21 | 16 | }
|
22 |
| - |
| 17 | + |
23 | 18 | function clearPendingRestart() {
|
24 |
| - _restartPending = false; |
| 19 | + _restartQueue = []; |
25 | 20 | }
|
26 |
| - |
| 21 | + |
27 | 22 | function disallow() {
|
28 | 23 | log("Disallowing restarts");
|
29 | 24 | _allowed = false;
|
30 | 25 | }
|
31 | 26 |
|
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. |
39 | 40 | return;
|
40 | 41 | }
|
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)); |
48 | 46 | }
|
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; |
70 | 47 | }
|
71 | 48 | }
|
72 | 49 |
|
73 | 50 | return {
|
74 | 51 | allow,
|
75 |
| - clearPendingRestart, |
| 52 | + clearPendingRestart, |
76 | 53 | disallow,
|
77 | 54 | restartApp
|
78 | 55 | };
|
|
0 commit comments