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

Commit 6aab2a5

Browse files
committed
Merge pull request #64 from Microsoft/restart_fix
Updating restartApp and setDeploymentKey
2 parents 8539694 + b668f74 commit 6aab2a5

File tree

4 files changed

+51
-80
lines changed

4 files changed

+51
-80
lines changed

CodePush.ios.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ var packageMixins = require("./package-mixins")(NativeCodePush);
66
var requestFetchAdapter = require("./request-fetch-adapter.js");
77
var Sdk = require("code-push/script/acquisition-sdk").AcquisitionManager;
88

9-
function checkForUpdate() {
9+
function checkForUpdate(deploymentKey = null) {
1010
var config;
1111
var sdk;
1212

1313
return getConfiguration()
1414
.then((configResult) => {
1515
config = configResult;
16+
17+
// If a deployment key was explicitly provided,
18+
// then let's override the one we retrieved
19+
// from the native-side of the app.
20+
if (deploymentKey) {
21+
config.deploymentKey = deploymentKey;
22+
}
23+
1624
return getSdk();
1725
})
1826
.then((sdkResult) => {
@@ -51,20 +59,17 @@ function checkForUpdate() {
5159
});
5260
}
5361

54-
var isConfigValid = true;
55-
5662
var getConfiguration = (() => {
5763
var config;
5864
return function getConfiguration() {
59-
if (config && isConfigValid) {
65+
if (config) {
6066
return Promise.resolve(config);
6167
} else if (testConfig) {
6268
return Promise.resolve(testConfig);
6369
} else {
6470
return NativeCodePush.getConfiguration()
6571
.then((configuration) => {
6672
if (!config) config = configuration;
67-
isConfigValid = true;
6873
return config;
6974
});
7075
}
@@ -114,20 +119,6 @@ function log(message) {
114119
console.log(`[CodePush] ${message}`)
115120
}
116121

117-
function restartApp(rollbackTimeout = 0) {
118-
NativeCodePush.restartApp(rollbackTimeout);
119-
}
120-
121-
function setDeploymentKey(deploymentKey) {
122-
return NativeCodePush.setDeploymentKey(deploymentKey)
123-
.then(() => {
124-
// Mark the local copy of the config data
125-
// as invalid since we just modified it
126-
// on the native end.
127-
isConfigValid = false;
128-
});
129-
}
130-
131122
var testConfig;
132123
var testSdk;
133124

@@ -150,6 +141,7 @@ function setUpTestDependencies(providedTestSdk, providedTestConfig, testNativeBr
150141
function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) {
151142
var syncOptions = {
152143

144+
deploymentKey: null,
153145
ignoreFailedUpdates: true,
154146
installMode: CodePush.InstallMode.ON_NEXT_RESTART,
155147
rollbackTimeout: 0,
@@ -205,7 +197,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
205197

206198
return new Promise((resolve, reject) => {
207199
syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE);
208-
checkForUpdate()
200+
checkForUpdate(syncOptions.deploymentKey)
209201
.then((remotePackage) => {
210202
var doDownloadAndInstall = () => {
211203
syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE);
@@ -288,8 +280,7 @@ var CodePush = {
288280
getCurrentPackage: getCurrentPackage,
289281
log: log,
290282
notifyApplicationReady: NativeCodePush.notifyApplicationReady,
291-
restartApp: restartApp,
292-
setDeploymentKey: setDeploymentKey,
283+
restartPendingUpdate: NativeCodePush.restartPendingUpdate,
293284
setUpTestDependencies: setUpTestDependencies,
294285
sync: sync,
295286
InstallMode: {

CodePush.m

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ - (NSDictionary *)constantsToExport
116116
{
117117
// Export the values of the CodePushInstallMode enum
118118
// so that the script-side can easily stay in sync
119-
return @{ @"codePushInstallModeOnNextRestart":@(CodePushInstallModeOnNextRestart),
119+
return @{
120+
@"codePushInstallModeOnNextRestart":@(CodePushInstallModeOnNextRestart),
120121
@"codePushInstallModeImmediate": @(CodePushInstallModeImmediate),
121122
@"codePushInstallModeOnNextResume": @(CodePushInstallModeOnNextResume)
122123
};
@@ -243,8 +244,8 @@ - (void)startRollbackTimer:(int)rollbackTimeout
243244

244245
// JavaScript-exported module methods
245246
RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage
246-
resolver:(RCTPromiseResolveBlock)resolve
247-
rejecter:(RCTPromiseRejectBlock)reject)
247+
resolver:(RCTPromiseResolveBlock)resolve
248+
rejecter:(RCTPromiseRejectBlock)reject)
248249
{
249250
[CodePushPackage downloadPackage:updatePackage
250251
progressCallback:^(long expectedContentLength, long receivedContentLength) {
@@ -273,13 +274,13 @@ - (void)startRollbackTimer:(int)rollbackTimeout
273274
}
274275

275276
RCT_EXPORT_METHOD(getConfiguration:(RCTPromiseResolveBlock)resolve
276-
rejecter:(RCTPromiseRejectBlock)reject)
277+
rejecter:(RCTPromiseRejectBlock)reject)
277278
{
278279
resolve([[CodePushConfig current] configuration]);
279280
}
280281

281282
RCT_EXPORT_METHOD(getCurrentPackage:(RCTPromiseResolveBlock)resolve
282-
rejecter:(RCTPromiseRejectBlock)reject)
283+
rejecter:(RCTPromiseRejectBlock)reject)
283284
{
284285
dispatch_async(dispatch_get_main_queue(), ^{
285286
NSError *error;
@@ -293,10 +294,10 @@ - (void)startRollbackTimer:(int)rollbackTimeout
293294
}
294295

295296
RCT_EXPORT_METHOD(installUpdate:(NSDictionary*)updatePackage
296-
rollbackTimeout:(int)rollbackTimeout
297-
installMode:(CodePushInstallMode)installMode
298-
resolver:(RCTPromiseResolveBlock)resolve
299-
rejecter:(RCTPromiseRejectBlock)reject)
297+
rollbackTimeout:(int)rollbackTimeout
298+
installMode:(CodePushInstallMode)installMode
299+
resolver:(RCTPromiseResolveBlock)resolve
300+
rejecter:(RCTPromiseRejectBlock)reject)
300301
{
301302
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
302303
NSError *error;
@@ -318,8 +319,8 @@ - (void)startRollbackTimer:(int)rollbackTimeout
318319
}
319320

320321
RCT_EXPORT_METHOD(isFailedUpdate:(NSString *)packageHash
321-
resolve:(RCTPromiseResolveBlock)resolve
322-
reject:(RCTPromiseRejectBlock)reject)
322+
resolve:(RCTPromiseResolveBlock)resolve
323+
reject:(RCTPromiseRejectBlock)reject)
323324
{
324325
BOOL isFailedHash = [self isFailedHash:packageHash];
325326
resolve(@(isFailedHash));
@@ -345,21 +346,21 @@ - (void)startRollbackTimer:(int)rollbackTimeout
345346
resolve([NSNull null]);
346347
}
347348

348-
RCT_EXPORT_METHOD(restartApp:(int)rollbackTimeout){
349+
// This function is exposed solely for immediately installed
350+
// update support, and shouldn't be consumed directly by user code.
351+
RCT_EXPORT_METHOD(restartImmedidateUpdate:(int)rollbackTimeout)
352+
{
349353
[self initializeUpdateWithRollbackTimeout:rollbackTimeout needsRestart:YES];
350354
}
351355

352-
RCT_EXPORT_METHOD(setDeploymentKey:(NSString *)deploymentKey
353-
resolve:(RCTPromiseResolveBlock)resolve
354-
rejecter:(RCTPromiseRejectBlock)reject)
356+
RCT_EXPORT_METHOD(restartPendingUpdate)
355357
{
356-
[[CodePushConfig current] setDeploymentKey:deploymentKey];
357-
resolve(nil);
358+
[self checkForPendingUpdate:YES];
358359
}
359360

360361
RCT_EXPORT_METHOD(setUsingTestFolder:(BOOL)shouldUseTestFolder)
361362
{
362363
usingTestFolder = shouldUseTestFolder;
363364
}
364365

365-
@end
366+
@end

README.md

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,17 @@ When you require the `react-native-code-push` module, that object provides the f
124124
125125
* [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.
126126
* [getCurrentPackage](#codepushgetcurrentpackage): Gets information about the currently installed package (e.g. description, installation time)
127-
* [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
128-
* [restartApp](#codepushrestartapp): Installs a pending update by immediately restarting the app.
129-
* [setDeploymentKey](#codepushsetdeploymentkey): Dynamically updates the deployment key that the CodePush runtime will use to query for app updates.
127+
* [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.
128+
* [restartPendingUpdate](#codepushrestartPendingUpdate): Conditionally restarts the app if a previously installed update is currently pending (e.g. it was installed using the `ON_NEXT_RESTART` or `ON_NEXT_RESUME` modes, and the user hasn't restarted or resumed the app yet).
130129
* [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
131130
132131
#### codePush.checkForUpdate
133132
134133
```javascript
135-
codePush.checkForUpdate(): Promise<RemotePackage>;
134+
codePush.checkForUpdate(deploymentKey: String = null): Promise<RemotePackage>;
136135
```
137136

138-
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.
137+
Queries the CodePush service for an update using the deployment configured either in your `Info.plist` file or specified using the optional `deploymentKey` parameter. This method returns a promise which resolves to a `RemotePackage` that can be subsequently downloaded.
139138

140139
`checkForUpdate` returns a Promise that resolves to one of two values:
141140

@@ -175,39 +174,18 @@ Notifies the CodePush runtime that an update is considered successful, and there
175174

176175
If the `rollbackTimeout` parameter was not specified, the CodePush runtime will not enforce any automatic rollback behavior, and therefore, calling this function is not required and will result in a no-op.
177176

178-
#### codePush.restartApp
179-
180-
```javascript
181-
codePush.restartApp(rollbackTimeout: Number = 0): void;
182-
```
183-
184-
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:
185-
186-
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)
187-
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.
188-
189-
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.
190-
191-
#### codePush.setDeploymentKey
192-
193-
```javascript
194-
codePush.setDeploymentKey(deploymentKey: String): Promise<void>;
195-
```
196-
197-
Dynamically updates the deployment key that the CodePush runtime will use to query for app updates. This is beneficial if your app has a default deployment key which you added to your `Info.plist` file, but you want to dynamically change it at runtime based on some app-specific policy (e.g. you want to give early access to certain users, by pointing them at your staging deployment).
198-
199-
The method simply takes a string representing the new deployment, and returns a `Promise` that will resolve once the specified deployment key has been applied, and calls to `sync` and/or `checkForUpdate` could be successfully called.
200-
201-
Example Usage:
202-
203-
```javascript
204-
codePush.setDeploymentKey("SOME_VALID_KEY_VALUE").then(() => {
205-
// The following call to sync with query the updated
206-
// app deployment for an update
207-
codePush.sync();
208-
});
209-
210-
```
177+
#### codePush.restartPendingUpdate
178+
179+
```javascript
180+
codePush.restartPendingUpdate(): void;
181+
```
182+
183+
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 only useful when the following conditions are true:
184+
185+
1. Your app is specifying an install mode value of `ON_NEXT_RESTART` or `ON_NEXT_RESUME` when calling the `sync` or `LocalPackage.install` methods. This has the effect of not applying your update until the app has been restarted (by either the end-user or OS) or resumed, and therefore, the update won't be immediately displayed to the end-user .
186+
2. You have an app-specific user event (e.g. the end-user navigated back to the app's home route) 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 or resume.
187+
188+
If you call this method, and there isn't a pending update, it will result in a no-op. Otherwise, the app will be restarted in order to display the update to the end-user.
211189

212190
#### codePush.sync
213191

@@ -226,6 +204,7 @@ If you want to pivot whether you check and/or download an available update based
226204

227205
The method accepts an options object that allows you to customize numerous aspects of the default behavior, all of which provide sensible values by default:
228206

207+
* __deploymentKey__ (String) - Specifies the deployment key you want to query for an update against. By default, this value is derived from the `Info.plist` file, but this option allows you to override it from the script-side if you need to dynamically change your app's current deployment.
229208
* __ignoreFailedUpdates__ (Boolean) - Indicates whether you would like to automatically ignored updates which are available, but have been previously attemped to install, but failed. Defaults to `true`.
230209
* __installMode__ (CodePush.InstallMode) - Indicates whether you would like to restart the app immediately after the update has been installed, or wait until the next app resume or restart. Defaults to `CodePush.InstallMode.ON_NEXT_RESTART`
231210
* __rollbackTimeout__ (Number) - The number of seconds that you want the runtime to wait after an update has been installed before considering it failed and rolling it back. Defaults to `0`, which disables rollback protection.

package-mixins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ module.exports = (NativeCodePush) => {
4040
.then(function() {
4141
updateInstalledCallback && updateInstalledCallback();
4242
if (installMode == NativeCodePush.codePushInstallModeImmediate) {
43-
NativeCodePush.restartApp(rollbackTimeout);
44-
}
43+
NativeCodePush.restartImmedidateUpdate(rollbackTimeout);
44+
};
4545
});
4646
}
4747
};

0 commit comments

Comments
 (0)