From 088daeaf7dd0f2958258d8ea6aa1fdee8c220cc9 Mon Sep 17 00:00:00 2001 From: danielbasedow Date: Sun, 22 May 2016 11:53:20 +0200 Subject: [PATCH] add code to delay automatic restarts --- CodePush.js | 7 ++++++- RestartManager.js | 39 +++++++++++++++++++++++++++++++++++++++ package-mixins.js | 4 ++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 RestartManager.js diff --git a/CodePush.js b/CodePush.js index ff52fce..ab987bd 100644 --- a/CodePush.js +++ b/CodePush.js @@ -2,6 +2,7 @@ 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"; let NativeCodePush = require("react-native").NativeModules.CodePush; const PackageMixins = require("./package-mixins")(NativeCodePush); @@ -412,10 +413,14 @@ if (NativeCodePush) { restartApp, setUpTestDependencies, 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 - ON_NEXT_RESUME: NativeCodePush.codePushInstallModeOnNextResume // Restart the app the next time it is resumed from the background + ON_NEXT_RESUME: NativeCodePush.codePushInstallModeOnNextResume, // Restart the app the next time it is resumed from the background + ON_NEXT_RESTART_OPPORTUNITY: NativeCodePush.codePushInstallModeOnNextRestartOpportunity }, SyncStatus: { CHECKING_FOR_UPDATE: 0, diff --git a/RestartManager.js b/RestartManager.js new file mode 100644 index 0000000..0178f3e --- /dev/null +++ b/RestartManager.js @@ -0,0 +1,39 @@ +let NativeCodePush = require("react-native").NativeModules.CodePush; + +const RestartManager = (() => { + let _allowed = true; + let restartPending = false; + + function tryRestart() { + if (restartPending && _allowed == true) { + NativeCodePush.restartApp(true); + } + } + + function requestRestart() { + restartPending = true; + tryRestart(); + } + + function allow() { + _allowed = true; + tryRestart(); + } + + function allowed() { + return _allowed + } + + function disallow() { + _allowed = false; + } + + return { + allow, + disallow, + allowed, + requestRestart + }; +})(); + +module.exports = RestartManager; diff --git a/package-mixins.js b/package-mixins.js index 0357c41..9184da0 100644 --- a/package-mixins.js +++ b/package-mixins.js @@ -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 @@ -44,6 +45,9 @@ module.exports = (NativeCodePush) => { if (installMode == NativeCodePush.codePushInstallModeImmediate) { NativeCodePush.restartApp(false); } else { + if (installMode == NativeCodePush.codePushInstallModeOnNextRestartOpportunity) { + RestartManager.requestRestart(); + } localPackage.isPending = true; // Mark the package as pending since it hasn't been applied yet } },