diff --git a/CodePush.js b/CodePush.js index d38970b41..48040632e 100644 --- a/CodePush.js +++ b/CodePush.js @@ -3,6 +3,7 @@ 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); @@ -155,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 = (() => { @@ -411,7 +407,6 @@ if (NativeCodePush) { sync, disallowRestart: RestartManager.disallow, allowRestart: RestartManager.allow, - restartAllowed: RestartManager.allowed, 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 diff --git a/Examples/CodePushDemoApp/crossplatformdemo.js b/Examples/CodePushDemoApp/crossplatformdemo.js index ef950b919..ff7de195c 100644 --- a/Examples/CodePushDemoApp/crossplatformdemo.js +++ b/Examples/CodePushDemoApp/crossplatformdemo.js @@ -85,16 +85,16 @@ let CodePushDemoApp = React.createClass({ }, getInitialState() { - return { }; + return { restartAllowed: true }; }, toggleAllowRestart() { - if (CodePush.restartAllowed()) { + if (this.state.restartAllowed) { CodePush.disallowRestart(); } else { CodePush.allowRestart(); } - this.forceUpdate(); + this.setState({restartAllowed: !this.state.restartAllowed}); }, render() { @@ -128,7 +128,7 @@ let CodePushDemoApp = React.createClass({ {progressView} ); diff --git a/RestartManager.js b/RestartManager.js index 83a9cfce5..60ca20ef8 100644 --- a/RestartManager.js +++ b/RestartManager.js @@ -1,31 +1,36 @@ +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); + } else { + log("restart not allowed"); + _restartPending = true; } } function allow() { + log("allow restart"); _allowed = true; - restartApp(true); - } - - function allowed() { - return _allowed + if (_restartPending) { + log("executing pending restart"); + restartApp(true); + } } function disallow() { + log("disallow restart"); _allowed = false; } return { allow, disallow, - allowed, restartApp, }; })(); diff --git a/logging.js b/logging.js new file mode 100644 index 000000000..7523589d7 --- /dev/null +++ b/logging.js @@ -0,0 +1,6 @@ +/* Logs messages to console with the [CodePush] prefix */ +function log(message) { + console.log(`[CodePush] ${message}`); +} + +module.exports = log;