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

Commit 60f8c8b

Browse files
committed
Merge pull request #343 from dbasedow/master
Add ability to delay restarts
2 parents e018ad6 + 4a0938f commit 60f8c8b

File tree

6 files changed

+84
-15
lines changed

6 files changed

+84
-15
lines changed

CodePush.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ 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";
6+
import log from './logging';
57

68
let NativeCodePush = require("react-native").NativeModules.CodePush;
79
const PackageMixins = require("./package-mixins")(NativeCodePush);
@@ -154,11 +156,6 @@ function getPromisifiedSdk(requestFetchAdapter, config) {
154156
return sdk;
155157
}
156158

157-
/* Logs messages to console with the [CodePush] prefix */
158-
function log(message) {
159-
console.log(`[CodePush] ${message}`)
160-
}
161-
162159
// This ensures that notifyApplicationReadyInternal is only called once
163160
// in the lifetime of this module instance.
164161
const notifyApplicationReady = (() => {
@@ -213,10 +210,6 @@ async function tryReportStatus(resumeListener) {
213210
}
214211
}
215212

216-
function restartApp(onlyIfUpdateIsPending = false) {
217-
NativeCodePush.restartApp(onlyIfUpdateIsPending);
218-
}
219-
220213
var testConfig;
221214

222215
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
@@ -409,9 +402,11 @@ if (NativeCodePush) {
409402
log,
410403
notifyAppReady: notifyApplicationReady,
411404
notifyApplicationReady,
412-
restartApp,
405+
restartApp: RestartManager.restartApp,
413406
setUpTestDependencies,
414407
sync,
408+
disallowRestart: RestartManager.disallow,
409+
allowRestart: RestartManager.allow,
415410
InstallMode: {
416411
IMMEDIATE: NativeCodePush.codePushInstallModeImmediate, // Restart the app immediately
417412
ON_NEXT_RESTART: NativeCodePush.codePushInstallModeOnNextRestart, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart

Examples/CodePushDemoApp/crossplatformdemo.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ let CodePushDemoApp = React.createClass({
1919
try {
2020
return await CodePush.sync(
2121
{
22-
updateDialog: true,
23-
installMode: CodePush.InstallMode.ON_NEXT_RESUME
22+
installMode: CodePush.InstallMode.IMMEDIATE,
2423
},
2524
(syncStatus) => {
2625
switch(syncStatus) {
@@ -86,7 +85,16 @@ let CodePushDemoApp = React.createClass({
8685
},
8786

8887
getInitialState() {
89-
return { };
88+
return { restartAllowed: true };
89+
},
90+
91+
toggleAllowRestart() {
92+
if (this.state.restartAllowed) {
93+
CodePush.disallowRestart();
94+
} else {
95+
CodePush.allowRestart();
96+
}
97+
this.setState({restartAllowed: !this.state.restartAllowed});
9098
},
9199

92100
render() {
@@ -119,6 +127,9 @@ let CodePushDemoApp = React.createClass({
119127
{syncView}
120128
{progressView}
121129
<Image style={styles.image} resizeMode={Image.resizeMode.contain} source={require('./images/laptop_phone_howitworks.png')}/>
130+
<Button onPress={this.toggleAllowRestart}>
131+
Restart { this.state.restartAllowed ? "allowed" : "forbidden"}
132+
</Button>
122133
</View>
123134
);
124135
}

RestartManager.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
let log = require('./logging');
2+
let NativeCodePush = require("react-native").NativeModules.CodePush;
3+
4+
const RestartManager = (() => {
5+
let _allowed = true;
6+
let _restartPending = false;
7+
8+
function restartApp(onlyIfUpdateIsPending = false) {
9+
if (_allowed) {
10+
NativeCodePush.restartApp(onlyIfUpdateIsPending);
11+
log('restaes');
12+
} else {
13+
log("restart not allowed");
14+
_restartPending = true;
15+
return true;
16+
}
17+
}
18+
19+
function allow() {
20+
log("allow restart");
21+
_allowed = true;
22+
if (_restartPending) {
23+
log("executing pending restart");
24+
restartApp(true);
25+
}
26+
}
27+
28+
function disallow() {
29+
log("disallow restart");
30+
_allowed = false;
31+
}
32+
33+
function clearPendingRestart() {
34+
_restartPending = false;
35+
}
36+
37+
return {
38+
allow,
39+
disallow,
40+
restartApp,
41+
clearPendingRestart
42+
};
43+
})();
44+
45+
module.exports = RestartManager;

logging.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Logs messages to console with the [CodePush] prefix */
2+
function log(message) {
3+
console.log(`[CodePush] ${message}`);
4+
}
5+
6+
module.exports = log;

package-mixins.js

Lines changed: 3 additions & 1 deletion
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
@@ -42,8 +43,9 @@ module.exports = (NativeCodePush) => {
4243
await NativeCodePush.installUpdate(this, installMode, minimumBackgroundDuration);
4344
updateInstalledCallback && updateInstalledCallback();
4445
if (installMode == NativeCodePush.codePushInstallModeImmediate) {
45-
NativeCodePush.restartApp(false);
46+
RestartManager.restartApp(false);
4647
} else {
48+
RestartManager.clearPendingRestart();
4749
localPackage.isPending = true; // Mark the package as pending since it hasn't been applied yet
4850
}
4951
},

typings/react-native-code-push.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,17 @@ declare namespace CodePush {
217217
* Notifies the CodePush runtime that an installed update is considered successful.
218218
*/
219219
function notifyAppReady(): Promise<void>;
220-
220+
221+
/**
222+
* Allow CodePush to restart the app.
223+
*/
224+
function allowRestart(): void;
225+
226+
/**
227+
* Forbid CodePush to restart the app.
228+
*/
229+
function disallowRestart(): void;
230+
221231
/**
222232
* Immediately restarts the app.
223233
*

0 commit comments

Comments
 (0)