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

Commit 13d933e

Browse files
committed
es6/es7 changes
1 parent 35945e9 commit 13d933e

File tree

3 files changed

+75
-84
lines changed

3 files changed

+75
-84
lines changed

CodePush.js

Lines changed: 72 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
'use strict';
22

3-
var { Alert } = require("./AlertAdapter");
4-
var NativeCodePush = require("react-native").NativeModules.CodePush;
5-
var PackageMixins = require("./package-mixins")(NativeCodePush);
6-
var requestFetchAdapter = require("./request-fetch-adapter.js");
7-
var Sdk = require("code-push/script/acquisition-sdk").AcquisitionManager;
8-
var semver = require("semver");
3+
import { Alert } from "./AlertAdapter";
4+
let NativeCodePush = require("react-native").NativeModules.CodePush;
5+
let PackageMixins = require("./package-mixins")(NativeCodePush);
6+
import requestFetchAdapter from "./request-fetch-adapter.js";
7+
import { AcquisitionManager as Sdk } from "code-push/script/acquisition-sdk";
8+
import semver from "semver";
99

10-
function checkForUpdate(deploymentKey = null) {
11-
var config, sdk;
12-
10+
async function checkForUpdate(deploymentKey = null) {
1311
/*
1412
* Before we ask the server if an update exists, we
1513
* need to retrieve three pieces of information from the
@@ -19,81 +17,74 @@ function checkForUpdate(deploymentKey = null) {
1917
* for their specific deployment and version and which are actually
2018
* different from the CodePush update they have already installed.
2119
*/
22-
return getConfiguration()
23-
.then((configResult) => {
24-
/*
25-
* If a deployment key was explicitly provided,
26-
* then let's override the one we retrieved
27-
* from the native-side of the app. This allows
28-
* dynamically "redirecting" end-users at different
29-
* deployments (e.g. an early access deployment for insiders).
30-
*/
31-
if (deploymentKey) {
32-
config = Object.assign({}, configResult, { deploymentKey });
33-
} else {
34-
config = configResult;
35-
}
36-
37-
sdk = new module.exports.AcquisitionSdk(requestFetchAdapter, config);
38-
39-
// Allow dynamic overwrite of function. This is only to be used for tests.
40-
return module.exports.getCurrentPackage();
41-
})
42-
.then((localPackage) => {
43-
var queryPackage = { appVersion: config.appVersion };
44-
45-
/*
46-
* If the app has a previously installed update, and that update
47-
* was targetted at the same app version that is currently running,
48-
* then we want to use its package hash to determine whether a new
49-
* release has been made on the server. Otherwise, we only need
50-
* to send the app version to the server, since we are interested
51-
* in any updates for current app store version, regardless of hash.
52-
*/
53-
if (localPackage && localPackage.appVersion && semver.compare(localPackage.appVersion, config.appVersion) === 0) {
54-
queryPackage = localPackage;
55-
}
56-
57-
return new Promise((resolve, reject) => {
58-
sdk.queryUpdateWithCurrentPackage(queryPackage, (err, update) => {
59-
if (err) {
60-
return reject(err);
61-
}
62-
63-
/*
64-
* There are three cases where checkForUpdate will resolve to null:
65-
* ----------------------------------------------------------------
66-
* 1) The server said there isn't an update. This is the most common case.
67-
* 2) The server said there is an update but it requires a newer binary version.
68-
* This would occur when end-users are running an older app store version than
69-
* is available, and CodePush is making sure they don't get an update that
70-
* potentially wouldn't be compatible with what they are running.
71-
* 3) The server said there is an update, but the update's hash is the same as
72-
* the currently running update. This should _never_ happen, unless there is a
73-
* bug in the server, but we're adding this check just to double-check that the
74-
* client app is resilient to a potential issue with the update check.
75-
*/
76-
if (!update || update.updateAppVersion || (update.packageHash === localPackage.packageHash)) {
77-
return resolve(null);
78-
}
20+
let nativeConfig = await getConfiguration();
21+
/*
22+
* If a deployment key was explicitly provided,
23+
* then let's override the one we retrieved
24+
* from the native-side of the app. This allows
25+
* dynamically "redirecting" end-users at different
26+
* deployments (e.g. an early access deployment for insiders).
27+
*/
28+
let config = deploymentKey ? Object.assign({}, nativeConfig, { deploymentKey })
29+
: nativeConfig;
30+
let sdk = getPromisifiedSdk(requestFetchAdapter, config);
31+
// Use dynamically overridden getCurrentPackage() during tests.
32+
let localPackage = await module.exports.getCurrentPackage();
33+
/*
34+
* If the app has a previously installed update, and that update
35+
* was targetted at the same app version that is currently running,
36+
* then we want to use its package hash to determine whether a new
37+
* release has been made on the server. Otherwise, we only need
38+
* to send the app version to the server, since we are interested
39+
* in any updates for current app store version, regardless of hash.
40+
*/
41+
let queryPackage = localPackage && localPackage.appVersion && semver.compare(localPackage.appVersion, config.appVersion) === 0
42+
? localPackage
43+
: { appVersion: config.appVersion };
44+
let update = await sdk.queryUpdateWithCurrentPackage(queryPackage);
45+
/*
46+
* There are three cases where checkForUpdate will resolve to null:
47+
* ----------------------------------------------------------------
48+
* 1) The server said there isn't an update. This is the most common case.
49+
* 2) The server said there is an update but it requires a newer binary version.
50+
* This would occur when end-users are running an older app store version than
51+
* is available, and CodePush is making sure they don't get an update that
52+
* potentially wouldn't be compatible with what they are running.
53+
* 3) The server said there is an update, but the update's hash is the same as
54+
* the currently running update. This should _never_ happen, unless there is a
55+
* bug in the server, but we're adding this check just to double-check that the
56+
* client app is resilient to a potential issue with the update check.
57+
*/
58+
if (!update || update.updateAppVersion || (update.packageHash === localPackage.packageHash)) {
59+
return null;
60+
} else {
61+
let remotePackage = Object.assign(update, PackageMixins.remote);
62+
remotePackage.failedInstall = await NativeCodePush.isFailedUpdate(remotePackage.packageHash);
63+
return remotePackage;
64+
}
65+
}
7966

80-
update = Object.assign(update, PackageMixins.remote);
81-
82-
NativeCodePush.isFailedUpdate(update.packageHash)
83-
.then((isFailedHash) => {
84-
update.failedInstall = isFailedHash;
85-
resolve(update);
86-
})
87-
.catch(reject)
88-
.done();
89-
})
90-
});
67+
function getPromisifiedSdk(requestFetchAdapter, config) {
68+
// Use dynamically overridden AcquisitionSdk during tests.
69+
let sdk = new module.exports.AcquisitionSdk(requestFetchAdapter, config);
70+
sdk.queryUpdateWithCurrentPackage = (queryPackage) => {
71+
return new Promise((resolve, reject) => {
72+
sdk.queryUpdateWithCurrentPackage(queryPackage, (err, update) => {
73+
if (err) {
74+
reject(err);
75+
} else {
76+
resolve(update);
77+
}
78+
});
9179
});
80+
};
81+
82+
return sdk;
9283
}
9384

94-
var getConfiguration = (() => {
95-
var config;
96-
return function getConfiguration() {
85+
let getConfiguration = (() => {
86+
let config;
87+
return () => {
9788
if (config) {
9889
return Promise.resolve(config);
9990
} else if (testConfig) {
@@ -105,7 +96,7 @@ var getConfiguration = (() => {
10596
return config;
10697
});
10798
}
108-
}
99+
};
109100
})();
110101

111102
function getCurrentPackage() {

Examples/CodePushDemoApp/iOS/CodePushDemoApp/AppDelegate.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
3333
* on the same Wi-Fi network.
3434
*/
3535

36-
//jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.includeRequire.runModule.bundle?dev=true&platform=ios"];
36+
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.includeRequire.runModule.bundle?dev=true&platform=ios"];
3737

3838
/**
3939
* OPTION 2
@@ -45,7 +45,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4545
* see http://facebook.github.io/react-native/docs/runningondevice.html
4646
*/
4747

48-
jsCodeLocation = [CodePush bundleURL];
48+
//jsCodeLocation = [CodePush bundleURL];
4949

5050
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
5151
moduleName:@"CodePushDemoApp"

Examples/CodePushDemoApp/iOS/CodePushDemoApp/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@
4444
<key>NSLocationWhenInUseUsageDescription</key>
4545
<string></string>
4646
<key>CodePushDeploymentKey</key>
47-
<string>deployment-key-here</string>
47+
<string>5c73310a-cc93-4aa5-bf9f-81c6b648232c</string>
4848
</dict>
4949
</plist>

0 commit comments

Comments
 (0)