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

Commit 088daea

Browse files
committed
add code to delay automatic restarts
1 parent dacbf48 commit 088daea

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

CodePush.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { AcquisitionManager as Sdk } from "code-push/script/acquisition-sdk";
22
import { Alert } from "./AlertAdapter";
33
import requestFetchAdapter from "./request-fetch-adapter";
44
import { AppState, Platform } from "react-native";
5+
import RestartManager from "./RestartManager";
56

67
let NativeCodePush = require("react-native").NativeModules.CodePush;
78
const PackageMixins = require("./package-mixins")(NativeCodePush);
@@ -412,10 +413,14 @@ if (NativeCodePush) {
412413
restartApp,
413414
setUpTestDependencies,
414415
sync,
416+
disallowRestart: RestartManager.disallow,
417+
allowRestart: RestartManager.allow,
418+
restartAllowed: RestartManager.allowed,
415419
InstallMode: {
416420
IMMEDIATE: NativeCodePush.codePushInstallModeImmediate, // Restart the app immediately
417421
ON_NEXT_RESTART: NativeCodePush.codePushInstallModeOnNextRestart, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart
418-
ON_NEXT_RESUME: NativeCodePush.codePushInstallModeOnNextResume // Restart the app the next time it is resumed from the background
422+
ON_NEXT_RESUME: NativeCodePush.codePushInstallModeOnNextResume, // Restart the app the next time it is resumed from the background
423+
ON_NEXT_RESTART_OPPORTUNITY: NativeCodePush.codePushInstallModeOnNextRestartOpportunity
419424
},
420425
SyncStatus: {
421426
CHECKING_FOR_UPDATE: 0,

RestartManager.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
let NativeCodePush = require("react-native").NativeModules.CodePush;
2+
3+
const RestartManager = (() => {
4+
let _allowed = true;
5+
let restartPending = false;
6+
7+
function tryRestart() {
8+
if (restartPending && _allowed == true) {
9+
NativeCodePush.restartApp(true);
10+
}
11+
}
12+
13+
function requestRestart() {
14+
restartPending = true;
15+
tryRestart();
16+
}
17+
18+
function allow() {
19+
_allowed = true;
20+
tryRestart();
21+
}
22+
23+
function allowed() {
24+
return _allowed
25+
}
26+
27+
function disallow() {
28+
_allowed = false;
29+
}
30+
31+
return {
32+
allow,
33+
disallow,
34+
allowed,
35+
requestRestart
36+
};
37+
})();
38+
39+
module.exports = RestartManager;

package-mixins.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AcquisitionManager as Sdk } from "code-push/script/acquisition-sdk";
22
import { DeviceEventEmitter } from "react-native";
3+
import RestartManager from "./RestartManager";
34

45
// This function is used to augment remote and local
56
// package objects with additional functionality/properties
@@ -44,6 +45,9 @@ module.exports = (NativeCodePush) => {
4445
if (installMode == NativeCodePush.codePushInstallModeImmediate) {
4546
NativeCodePush.restartApp(false);
4647
} else {
48+
if (installMode == NativeCodePush.codePushInstallModeOnNextRestartOpportunity) {
49+
RestartManager.requestRestart();
50+
}
4751
localPackage.isPending = true; // Mark the package as pending since it hasn't been applied yet
4852
}
4953
},

0 commit comments

Comments
 (0)