mirror of
https://github.com/zhigang1992/react-native-code-push.git
synced 2026-05-14 02:14:52 +08:00
Merge pull request #343 from dbasedow/master
Add ability to delay restarts
This commit is contained in:
15
CodePush.js
15
CodePush.js
@@ -2,6 +2,8 @@ import { AcquisitionManager as Sdk } from "code-push/script/acquisition-sdk";
|
||||
import { Alert } from "./AlertAdapter";
|
||||
import requestFetchAdapter from "./request-fetch-adapter";
|
||||
import { AppState, Platform } from "react-native";
|
||||
import RestartManager from "./RestartManager";
|
||||
import log from './logging';
|
||||
|
||||
let NativeCodePush = require("react-native").NativeModules.CodePush;
|
||||
const PackageMixins = require("./package-mixins")(NativeCodePush);
|
||||
@@ -154,11 +156,6 @@ function getPromisifiedSdk(requestFetchAdapter, config) {
|
||||
return sdk;
|
||||
}
|
||||
|
||||
/* Logs messages to console with the [CodePush] prefix */
|
||||
function log(message) {
|
||||
console.log(`[CodePush] ${message}`)
|
||||
}
|
||||
|
||||
// This ensures that notifyApplicationReadyInternal is only called once
|
||||
// in the lifetime of this module instance.
|
||||
const notifyApplicationReady = (() => {
|
||||
@@ -213,10 +210,6 @@ async function tryReportStatus(resumeListener) {
|
||||
}
|
||||
}
|
||||
|
||||
function restartApp(onlyIfUpdateIsPending = false) {
|
||||
NativeCodePush.restartApp(onlyIfUpdateIsPending);
|
||||
}
|
||||
|
||||
var testConfig;
|
||||
|
||||
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
|
||||
@@ -409,9 +402,11 @@ if (NativeCodePush) {
|
||||
log,
|
||||
notifyAppReady: notifyApplicationReady,
|
||||
notifyApplicationReady,
|
||||
restartApp,
|
||||
restartApp: RestartManager.restartApp,
|
||||
setUpTestDependencies,
|
||||
sync,
|
||||
disallowRestart: RestartManager.disallow,
|
||||
allowRestart: RestartManager.allow,
|
||||
InstallMode: {
|
||||
IMMEDIATE: NativeCodePush.codePushInstallModeImmediate, // Restart the app immediately
|
||||
ON_NEXT_RESTART: NativeCodePush.codePushInstallModeOnNextRestart, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart
|
||||
|
||||
@@ -19,8 +19,7 @@ let CodePushDemoApp = React.createClass({
|
||||
try {
|
||||
return await CodePush.sync(
|
||||
{
|
||||
updateDialog: true,
|
||||
installMode: CodePush.InstallMode.ON_NEXT_RESUME
|
||||
installMode: CodePush.InstallMode.IMMEDIATE,
|
||||
},
|
||||
(syncStatus) => {
|
||||
switch(syncStatus) {
|
||||
@@ -86,7 +85,16 @@ let CodePushDemoApp = React.createClass({
|
||||
},
|
||||
|
||||
getInitialState() {
|
||||
return { };
|
||||
return { restartAllowed: true };
|
||||
},
|
||||
|
||||
toggleAllowRestart() {
|
||||
if (this.state.restartAllowed) {
|
||||
CodePush.disallowRestart();
|
||||
} else {
|
||||
CodePush.allowRestart();
|
||||
}
|
||||
this.setState({restartAllowed: !this.state.restartAllowed});
|
||||
},
|
||||
|
||||
render() {
|
||||
@@ -119,6 +127,9 @@ let CodePushDemoApp = React.createClass({
|
||||
{syncView}
|
||||
{progressView}
|
||||
<Image style={styles.image} resizeMode={Image.resizeMode.contain} source={require('./images/laptop_phone_howitworks.png')}/>
|
||||
<Button onPress={this.toggleAllowRestart}>
|
||||
Restart { this.state.restartAllowed ? "allowed" : "forbidden"}
|
||||
</Button>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
45
RestartManager.js
Normal file
45
RestartManager.js
Normal file
@@ -0,0 +1,45 @@
|
||||
let log = require('./logging');
|
||||
let NativeCodePush = require("react-native").NativeModules.CodePush;
|
||||
|
||||
const RestartManager = (() => {
|
||||
let _allowed = true;
|
||||
let _restartPending = false;
|
||||
|
||||
function restartApp(onlyIfUpdateIsPending = false) {
|
||||
if (_allowed) {
|
||||
NativeCodePush.restartApp(onlyIfUpdateIsPending);
|
||||
log('restaes');
|
||||
} else {
|
||||
log("restart not allowed");
|
||||
_restartPending = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function allow() {
|
||||
log("allow restart");
|
||||
_allowed = true;
|
||||
if (_restartPending) {
|
||||
log("executing pending restart");
|
||||
restartApp(true);
|
||||
}
|
||||
}
|
||||
|
||||
function disallow() {
|
||||
log("disallow restart");
|
||||
_allowed = false;
|
||||
}
|
||||
|
||||
function clearPendingRestart() {
|
||||
_restartPending = false;
|
||||
}
|
||||
|
||||
return {
|
||||
allow,
|
||||
disallow,
|
||||
restartApp,
|
||||
clearPendingRestart
|
||||
};
|
||||
})();
|
||||
|
||||
module.exports = RestartManager;
|
||||
6
logging.js
Normal file
6
logging.js
Normal file
@@ -0,0 +1,6 @@
|
||||
/* Logs messages to console with the [CodePush] prefix */
|
||||
function log(message) {
|
||||
console.log(`[CodePush] ${message}`);
|
||||
}
|
||||
|
||||
module.exports = log;
|
||||
@@ -1,5 +1,6 @@
|
||||
import { AcquisitionManager as Sdk } from "code-push/script/acquisition-sdk";
|
||||
import { DeviceEventEmitter } from "react-native";
|
||||
import RestartManager from "./RestartManager";
|
||||
|
||||
// This function is used to augment remote and local
|
||||
// package objects with additional functionality/properties
|
||||
@@ -42,8 +43,9 @@ module.exports = (NativeCodePush) => {
|
||||
await NativeCodePush.installUpdate(this, installMode, minimumBackgroundDuration);
|
||||
updateInstalledCallback && updateInstalledCallback();
|
||||
if (installMode == NativeCodePush.codePushInstallModeImmediate) {
|
||||
NativeCodePush.restartApp(false);
|
||||
RestartManager.restartApp(false);
|
||||
} else {
|
||||
RestartManager.clearPendingRestart();
|
||||
localPackage.isPending = true; // Mark the package as pending since it hasn't been applied yet
|
||||
}
|
||||
},
|
||||
|
||||
12
typings/react-native-code-push.d.ts
vendored
12
typings/react-native-code-push.d.ts
vendored
@@ -217,7 +217,17 @@ declare namespace CodePush {
|
||||
* Notifies the CodePush runtime that an installed update is considered successful.
|
||||
*/
|
||||
function notifyAppReady(): Promise<void>;
|
||||
|
||||
|
||||
/**
|
||||
* Allow CodePush to restart the app.
|
||||
*/
|
||||
function allowRestart(): void;
|
||||
|
||||
/**
|
||||
* Forbid CodePush to restart the app.
|
||||
*/
|
||||
function disallowRestart(): void;
|
||||
|
||||
/**
|
||||
* Immediately restarts the app.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user