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

Commit e6abf60

Browse files
committed
Splitting restartApp into internal and public versions
1 parent 8539694 commit e6abf60

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

CodePush.ios.js

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
'use strict';
22

3-
var { AlertIOS } = require("react-native");
43
var NativeCodePush = require("react-native").NativeModules.CodePush;
5-
var packageMixins = require("./package-mixins")(NativeCodePush);
64
var requestFetchAdapter = require("./request-fetch-adapter.js");
75
var Sdk = require("code-push/script/acquisition-sdk").AcquisitionManager;
6+
var packageMixins = require("./package-mixins")(NativeCodePush);
7+
8+
var { AlertIOS } = require("react-native");
9+
10+
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
11+
function setUpTestDependencies(providedTestSdk, providedTestConfig, testNativeBridge){
12+
if (providedTestSdk) testSdk = providedTestSdk;
13+
if (providedTestConfig) testConfig = providedTestConfig;
14+
if (testNativeBridge) NativeCodePush = testNativeBridge;
15+
}
16+
var testConfig;
17+
var testSdk;
818

919
function checkForUpdate() {
1020
var config;
@@ -51,20 +61,17 @@ function checkForUpdate() {
5161
});
5262
}
5363

54-
var isConfigValid = true;
55-
5664
var getConfiguration = (() => {
5765
var config;
5866
return function getConfiguration() {
59-
if (config && isConfigValid) {
67+
if (config) {
6068
return Promise.resolve(config);
6169
} else if (testConfig) {
6270
return Promise.resolve(testConfig);
6371
} else {
6472
return NativeCodePush.getConfiguration()
6573
.then((configuration) => {
6674
if (!config) config = configuration;
67-
isConfigValid = true;
6875
return config;
6976
});
7077
}
@@ -114,30 +121,6 @@ function log(message) {
114121
console.log(`[CodePush] ${message}`)
115122
}
116123

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-
131-
var testConfig;
132-
var testSdk;
133-
134-
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
135-
function setUpTestDependencies(providedTestSdk, providedTestConfig, testNativeBridge) {
136-
if (providedTestSdk) testSdk = providedTestSdk;
137-
if (providedTestConfig) testConfig = providedTestConfig;
138-
if (testNativeBridge) NativeCodePush = testNativeBridge;
139-
}
140-
141124
/**
142125
* The sync method provides a simple, one-line experience for
143126
* incorporating the check, download and application of an update.
@@ -231,7 +214,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
231214
if (typeof syncOptions.updateDialog !== "object") {
232215
syncOptions.updateDialog = CodePush.DEFAULT_UPDATE_DIALOG;
233216
} else {
234-
syncOptions.updateDialog = Object.assign({}, CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog);
217+
syncOptions.updateDialog = Object.assign(CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog);
235218
}
236219

237220
var message = null;
@@ -275,6 +258,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
275258
}
276259
})
277260
.catch((error) => {
261+
console.log(error);
278262
syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR);
279263
reject(error);
280264
})
@@ -288,10 +272,15 @@ var CodePush = {
288272
getCurrentPackage: getCurrentPackage,
289273
log: log,
290274
notifyApplicationReady: NativeCodePush.notifyApplicationReady,
291-
restartApp: restartApp,
292-
setDeploymentKey: setDeploymentKey,
275+
restartApp: NativeCodePush.restartApp,
276+
setDeploymentKey: NativeCodePush.setDeploymentKey,
293277
setUpTestDependencies: setUpTestDependencies,
294278
sync: sync,
279+
AutoSyncMode: {
280+
NONE: NativeCodePush.codePushAutoSyncModeNone,
281+
ON_START: NativeCodePush.codePushAutoSyncModeOnStart,
282+
ON_RESUME: NativeCodePush.codePushAutoSyncModeOnResume
283+
},
295284
InstallMode: {
296285
IMMEDIATE: NativeCodePush.codePushInstallModeImmediate, // Restart the app immediately
297286
ON_NEXT_RESTART: NativeCodePush.codePushInstallModeOnNextRestart, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart

CodePush.m

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ - (NSDictionary *)constantsToExport
119119
return @{ @"codePushInstallModeOnNextRestart":@(CodePushInstallModeOnNextRestart),
120120
@"codePushInstallModeImmediate": @(CodePushInstallModeImmediate),
121121
@"codePushInstallModeOnNextResume": @(CodePushInstallModeOnNextResume)
122-
};
122+
};
123123
};
124124

125125
- (void)dealloc
@@ -326,8 +326,8 @@ - (void)startRollbackTimer:(int)rollbackTimeout
326326
}
327327

328328
RCT_EXPORT_METHOD(isFirstRun:(NSString *)packageHash
329-
resolve:(RCTPromiseResolveBlock)resolve
330-
rejecter:(RCTPromiseRejectBlock)reject)
329+
resolve:(RCTPromiseResolveBlock)resolve
330+
rejecter:(RCTPromiseRejectBlock)reject)
331331
{
332332
NSError *error;
333333
BOOL isFirstRun = didUpdate
@@ -339,19 +339,27 @@ - (void)startRollbackTimer:(int)rollbackTimeout
339339
}
340340

341341
RCT_EXPORT_METHOD(notifyApplicationReady:(RCTPromiseResolveBlock)resolve
342-
rejecter:(RCTPromiseRejectBlock)reject)
342+
rejecter:(RCTPromiseRejectBlock)reject)
343343
{
344344
[self cancelRollbackTimer];
345345
resolve([NSNull null]);
346346
}
347347

348-
RCT_EXPORT_METHOD(restartApp:(int)rollbackTimeout){
348+
RCT_EXPORT_METHOD(restartApp)
349+
{
350+
[self checkForPendingUpdate:YES];
351+
}
352+
353+
// This version of restart app is exposed solely for immediately installed
354+
// update support, and shouldn't be consumed directly by user code.
355+
RCT_EXPORT_METHOD(restartAppInternal:(int)rollbackTimeout)
356+
{
349357
[self initializeUpdateWithRollbackTimeout:rollbackTimeout needsRestart:YES];
350358
}
351359

352360
RCT_EXPORT_METHOD(setDeploymentKey:(NSString *)deploymentKey
353-
resolve:(RCTPromiseResolveBlock)resolve
354-
rejecter:(RCTPromiseRejectBlock)reject)
361+
resolve:(RCTPromiseResolveBlock)resolve
362+
rejecter:(RCTPromiseRejectBlock)reject)
355363
{
356364
[[CodePushConfig current] setDeploymentKey:deploymentKey];
357365
resolve(nil);
@@ -362,4 +370,4 @@ - (void)startRollbackTimer:(int)rollbackTimeout
362370
usingTestFolder = shouldUseTestFolder;
363371
}
364372

365-
@end
373+
@end

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ When you require the `react-native-code-push` module, that object provides the f
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)
127127
* [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.
128+
* [restartApp](#codepushrestartapp): Immediately restarts the app if a previously installed update is pending.
129129
* [setDeploymentKey](#codepushsetdeploymentkey): Dynamically updates the deployment key that the CodePush runtime will use to query for app updates.
130130
* [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
131131
@@ -178,15 +178,15 @@ If the `rollbackTimeout` parameter was not specified, the CodePush runtime will
178178
#### codePush.restartApp
179179

180180
```javascript
181-
codePush.restartApp(rollbackTimeout: Number = 0): void;
181+
codePush.restartApp(): void;
182182
```
183183

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:
184+
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:
185185

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)
186+
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)
187187
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.
188188

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.
189+
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`.
190190

191191
#### codePush.setDeploymentKey
192192

package-mixins.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var extend = require("extend");
12
var { NativeAppEventEmitter } = require("react-native");
23

34
module.exports = (NativeCodePush) => {
@@ -24,7 +25,7 @@ module.exports = (NativeCodePush) => {
2425
return NativeCodePush.downloadUpdate(this)
2526
.then((downloadedPackage) => {
2627
downloadProgressSubscription && downloadProgressSubscription.remove();
27-
return Object.assign({}, downloadedPackage, local);
28+
return extend({}, downloadedPackage, local);
2829
})
2930
.catch((error) => {
3031
downloadProgressSubscription && downloadProgressSubscription.remove();
@@ -40,7 +41,7 @@ module.exports = (NativeCodePush) => {
4041
.then(function() {
4142
updateInstalledCallback && updateInstalledCallback();
4243
if (installMode == NativeCodePush.codePushInstallModeImmediate) {
43-
NativeCodePush.restartApp(rollbackTimeout);
44+
NativeCodePush.restartAppInternal(rollbackTimeout);
4445
}
4546
});
4647
}

0 commit comments

Comments
 (0)