mirror of
https://github.com/zhigang1992/react-native-code-push.git
synced 2026-05-19 19:39:54 +08:00
Splitting restartApp into internal and public versions
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
var { AlertIOS } = require("react-native");
|
||||
var NativeCodePush = require("react-native").NativeModules.CodePush;
|
||||
var packageMixins = require("./package-mixins")(NativeCodePush);
|
||||
var requestFetchAdapter = require("./request-fetch-adapter.js");
|
||||
var Sdk = require("code-push/script/acquisition-sdk").AcquisitionManager;
|
||||
var packageMixins = require("./package-mixins")(NativeCodePush);
|
||||
|
||||
var { AlertIOS } = require("react-native");
|
||||
|
||||
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
|
||||
function setUpTestDependencies(providedTestSdk, providedTestConfig, testNativeBridge){
|
||||
if (providedTestSdk) testSdk = providedTestSdk;
|
||||
if (providedTestConfig) testConfig = providedTestConfig;
|
||||
if (testNativeBridge) NativeCodePush = testNativeBridge;
|
||||
}
|
||||
var testConfig;
|
||||
var testSdk;
|
||||
|
||||
function checkForUpdate() {
|
||||
var config;
|
||||
@@ -51,12 +61,10 @@ function checkForUpdate() {
|
||||
});
|
||||
}
|
||||
|
||||
var isConfigValid = true;
|
||||
|
||||
var getConfiguration = (() => {
|
||||
var config;
|
||||
return function getConfiguration() {
|
||||
if (config && isConfigValid) {
|
||||
if (config) {
|
||||
return Promise.resolve(config);
|
||||
} else if (testConfig) {
|
||||
return Promise.resolve(testConfig);
|
||||
@@ -64,7 +72,6 @@ var getConfiguration = (() => {
|
||||
return NativeCodePush.getConfiguration()
|
||||
.then((configuration) => {
|
||||
if (!config) config = configuration;
|
||||
isConfigValid = true;
|
||||
return config;
|
||||
});
|
||||
}
|
||||
@@ -114,30 +121,6 @@ function log(message) {
|
||||
console.log(`[CodePush] ${message}`)
|
||||
}
|
||||
|
||||
function restartApp(rollbackTimeout = 0) {
|
||||
NativeCodePush.restartApp(rollbackTimeout);
|
||||
}
|
||||
|
||||
function setDeploymentKey(deploymentKey) {
|
||||
return NativeCodePush.setDeploymentKey(deploymentKey)
|
||||
.then(() => {
|
||||
// Mark the local copy of the config data
|
||||
// as invalid since we just modified it
|
||||
// on the native end.
|
||||
isConfigValid = false;
|
||||
});
|
||||
}
|
||||
|
||||
var testConfig;
|
||||
var testSdk;
|
||||
|
||||
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
|
||||
function setUpTestDependencies(providedTestSdk, providedTestConfig, testNativeBridge) {
|
||||
if (providedTestSdk) testSdk = providedTestSdk;
|
||||
if (providedTestConfig) testConfig = providedTestConfig;
|
||||
if (testNativeBridge) NativeCodePush = testNativeBridge;
|
||||
}
|
||||
|
||||
/**
|
||||
* The sync method provides a simple, one-line experience for
|
||||
* incorporating the check, download and application of an update.
|
||||
@@ -231,7 +214,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
|
||||
if (typeof syncOptions.updateDialog !== "object") {
|
||||
syncOptions.updateDialog = CodePush.DEFAULT_UPDATE_DIALOG;
|
||||
} else {
|
||||
syncOptions.updateDialog = Object.assign({}, CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog);
|
||||
syncOptions.updateDialog = Object.assign(CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog);
|
||||
}
|
||||
|
||||
var message = null;
|
||||
@@ -275,6 +258,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR);
|
||||
reject(error);
|
||||
})
|
||||
@@ -288,10 +272,15 @@ var CodePush = {
|
||||
getCurrentPackage: getCurrentPackage,
|
||||
log: log,
|
||||
notifyApplicationReady: NativeCodePush.notifyApplicationReady,
|
||||
restartApp: restartApp,
|
||||
setDeploymentKey: setDeploymentKey,
|
||||
restartApp: NativeCodePush.restartApp,
|
||||
setDeploymentKey: NativeCodePush.setDeploymentKey,
|
||||
setUpTestDependencies: setUpTestDependencies,
|
||||
sync: sync,
|
||||
AutoSyncMode: {
|
||||
NONE: NativeCodePush.codePushAutoSyncModeNone,
|
||||
ON_START: NativeCodePush.codePushAutoSyncModeOnStart,
|
||||
ON_RESUME: NativeCodePush.codePushAutoSyncModeOnResume
|
||||
},
|
||||
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
|
||||
|
||||
24
CodePush.m
24
CodePush.m
@@ -119,7 +119,7 @@ static NSString * const PendingUpdateRollbackTimeoutKey = @"rollbackTimeout";
|
||||
return @{ @"codePushInstallModeOnNextRestart":@(CodePushInstallModeOnNextRestart),
|
||||
@"codePushInstallModeImmediate": @(CodePushInstallModeImmediate),
|
||||
@"codePushInstallModeOnNextResume": @(CodePushInstallModeOnNextResume)
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
- (void)dealloc
|
||||
@@ -326,8 +326,8 @@ RCT_EXPORT_METHOD(isFailedUpdate:(NSString *)packageHash
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(isFirstRun:(NSString *)packageHash
|
||||
resolve:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
resolve:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
NSError *error;
|
||||
BOOL isFirstRun = didUpdate
|
||||
@@ -339,19 +339,27 @@ RCT_EXPORT_METHOD(isFirstRun:(NSString *)packageHash
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(notifyApplicationReady:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
[self cancelRollbackTimer];
|
||||
resolve([NSNull null]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(restartApp:(int)rollbackTimeout){
|
||||
RCT_EXPORT_METHOD(restartApp)
|
||||
{
|
||||
[self checkForPendingUpdate:YES];
|
||||
}
|
||||
|
||||
// This version of restart app is exposed solely for immediately installed
|
||||
// update support, and shouldn't be consumed directly by user code.
|
||||
RCT_EXPORT_METHOD(restartAppInternal:(int)rollbackTimeout)
|
||||
{
|
||||
[self initializeUpdateWithRollbackTimeout:rollbackTimeout needsRestart:YES];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(setDeploymentKey:(NSString *)deploymentKey
|
||||
resolve:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
resolve:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
[[CodePushConfig current] setDeploymentKey:deploymentKey];
|
||||
resolve(nil);
|
||||
@@ -362,4 +370,4 @@ RCT_EXPORT_METHOD(setUsingTestFolder:(BOOL)shouldUseTestFolder)
|
||||
usingTestFolder = shouldUseTestFolder;
|
||||
}
|
||||
|
||||
@end
|
||||
@end
|
||||
10
README.md
10
README.md
@@ -125,7 +125,7 @@ When you require the `react-native-code-push` module, that object provides the f
|
||||
* [checkForUpdate](#codepushcheckforupdate): Queries the CodePush service for an update against the configured deployment. This method returns a promise which resolves to a `RemotePackage` that can be subsequently downloaded.
|
||||
* [getCurrentPackage](#codepushgetcurrentpackage): Gets information about the currently installed package (e.g. description, installation time)
|
||||
* [notifyApplicationReady](#codepushnotifyapplicationready): Notifies the CodePush runtime that an installed update is considered successful. This is an optional API, but is useful when you want to expicitly enable "rollback protection" in the event that an exception occurs in any code that you've deployed to production
|
||||
* [restartApp](#codepushrestartapp): Installs a pending update by immediately restarting the app.
|
||||
* [restartApp](#codepushrestartapp): Immediately restarts the app if a previously installed update is pending.
|
||||
* [setDeploymentKey](#codepushsetdeploymentkey): Dynamically updates the deployment key that the CodePush runtime will use to query for app updates.
|
||||
* [sync](#codepushsync): Allows checking for an update, downloading it and installing it, all with a single call. Unless you need custom UI and/or behavior, we recommend most developers to use this method when integrating CodePush into their apps
|
||||
|
||||
@@ -178,15 +178,15 @@ If the `rollbackTimeout` parameter was not specified, the CodePush runtime will
|
||||
#### codePush.restartApp
|
||||
|
||||
```javascript
|
||||
codePush.restartApp(rollbackTimeout: Number = 0): void;
|
||||
codePush.restartApp(): void;
|
||||
```
|
||||
|
||||
Installs the pending update (if applicable) by immediately restarting the app, and optionally starting the rollback timer. This method is for advanced scenarios, and is useful when the following conditions are true:
|
||||
Installs a pending update (if applicable) by immediately restarting the app, and optionally starting the rollback timer. This method is for advanced scenarios, and is useful when the following conditions are true:
|
||||
|
||||
1. Your app is specifying an install mode value of `ON_NEXT_RESTART` when calling `sync` or `LocalPackage.install`, which has the effect of not applying your update until the app has been restarted (by either the end-user or OS)
|
||||
1. Your app is specifying an install mode value of `ON_NEXT_RESTART` or `ON_NEXT_RESUME` when calling `sync` or `LocalPackage.install`, which has the effect of not applying your update until the app has been restarted (by either the end-user or OS)
|
||||
2. You have an app-specific user event (e.g. the end-user navigated back to the app's home page) that allows you to apply the update in an unobtrusive way, and potentially gets the update in front of the end-user sooner then waiting until the next restart.
|
||||
|
||||
The `rollbackTimeout` parameter has the same behavior as the equivalent in the `sync` and `checkForUpdate` method, and allows your app to have control over the point that an update is installed, while still benefitting from rollback production.
|
||||
If your app doesn't have a pending update, then calling this method results in a no-op. Otherwise, calling it will restart the app and start the rollback timer based on the timeout value that was specified in the previous call to `sync` or `LocalPackage.install`.
|
||||
|
||||
#### codePush.setDeploymentKey
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
var extend = require("extend");
|
||||
var { NativeAppEventEmitter } = require("react-native");
|
||||
|
||||
module.exports = (NativeCodePush) => {
|
||||
@@ -24,7 +25,7 @@ module.exports = (NativeCodePush) => {
|
||||
return NativeCodePush.downloadUpdate(this)
|
||||
.then((downloadedPackage) => {
|
||||
downloadProgressSubscription && downloadProgressSubscription.remove();
|
||||
return Object.assign({}, downloadedPackage, local);
|
||||
return extend({}, downloadedPackage, local);
|
||||
})
|
||||
.catch((error) => {
|
||||
downloadProgressSubscription && downloadProgressSubscription.remove();
|
||||
@@ -40,7 +41,7 @@ module.exports = (NativeCodePush) => {
|
||||
.then(function() {
|
||||
updateInstalledCallback && updateInstalledCallback();
|
||||
if (installMode == NativeCodePush.codePushInstallModeImmediate) {
|
||||
NativeCodePush.restartApp(rollbackTimeout);
|
||||
NativeCodePush.restartAppInternal(rollbackTimeout);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user