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

Commit ebd93b5

Browse files
committed
es7
1 parent aac19e2 commit ebd93b5

29 files changed

+308
-424
lines changed

CodePush.js

Lines changed: 73 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function getPromisifiedSdk(requestFetchAdapter, config) {
9090
let sdk = new module.exports.AcquisitionSdk(requestFetchAdapter, config);
9191
sdk.queryUpdateWithCurrentPackage = (queryPackage) => {
9292
return new Promise((resolve, reject) => {
93-
sdk.queryUpdateWithCurrentPackage(queryPackage, (err, update) => {
93+
module.exports.AcquisitionSdk.prototype.queryUpdateWithCurrentPackage.call(sdk, queryPackage, (err, update) => {
9494
if (err) {
9595
reject(err);
9696
} else {
@@ -126,8 +126,8 @@ function setUpTestDependencies(testSdk, providedTestConfig, testNativeBridge) {
126126
* releases, and displaying a standard confirmation UI to the end-user
127127
* when an update is available.
128128
*/
129-
function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) {
130-
var syncOptions = {
129+
async function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) {
130+
let syncOptions = {
131131

132132
deploymentKey: null,
133133
ignoreFailedUpdates: true,
@@ -139,7 +139,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
139139

140140
syncStatusChangeCallback = typeof syncStatusChangeCallback == "function"
141141
? syncStatusChangeCallback
142-
: function(syncStatus) {
142+
: (syncStatus) => {
143143
switch(syncStatus) {
144144
case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
145145
log("Checking for update.");
@@ -178,93 +178,83 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback)
178178

179179
downloadProgressCallback = typeof downloadProgressCallback == "function"
180180
? downloadProgressCallback
181-
: function(downloadProgress) {
181+
: (downloadProgress) => {
182182
log(`Expecting ${downloadProgress.totalBytes} bytes, received ${downloadProgress.receivedBytes} bytes.`);
183183
};
184184

185-
return new Promise((resolve, reject) => {
186-
var rejectPromise = (error) => {
187-
syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR);
188-
log(error.message);
189-
reject(error);
185+
try {
186+
await CodePush.notifyApplicationReady();
187+
188+
syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE);
189+
let remotePackage = await checkForUpdate(syncOptions.deploymentKey);
190+
191+
let doDownloadAndInstall = async () => {
192+
syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE);
193+
let localPackage = await remotePackage.download(downloadProgressCallback);
194+
195+
syncStatusChangeCallback(CodePush.SyncStatus.INSTALLING_UPDATE);
196+
await localPackage.install(syncOptions.installMode, () => {
197+
syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_INSTALLED);
198+
});
199+
200+
return CodePush.SyncStatus.UPDATE_INSTALLED;
190201
};
191202

192-
CodePush.notifyApplicationReady()
193-
.then(() => {
194-
syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE);
195-
return checkForUpdate(syncOptions.deploymentKey);
196-
})
197-
.then((remotePackage) => {
198-
var doDownloadAndInstall = () => {
199-
syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE);
200-
remotePackage.download(downloadProgressCallback)
201-
.then((localPackage) => {
202-
syncStatusChangeCallback(CodePush.SyncStatus.INSTALLING_UPDATE);
203-
return localPackage.install(syncOptions.installMode, () => {
204-
syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_INSTALLED);
205-
resolve(CodePush.SyncStatus.UPDATE_INSTALLED);
206-
});
207-
})
208-
.catch(rejectPromise)
209-
.done();
210-
}
203+
if (!remotePackage || (remotePackage.failedInstall && syncOptions.ignoreFailedUpdates)) {
204+
syncStatusChangeCallback(CodePush.SyncStatus.UP_TO_DATE);
205+
return (CodePush.SyncStatus.UP_TO_DATE);
206+
} else if (syncOptions.updateDialog) {
207+
// updateDialog supports any truthy value (e.g. true, "goo", 12),
208+
// but we should treat a non-object value as just the default dialog
209+
if (typeof syncOptions.updateDialog !== "object") {
210+
syncOptions.updateDialog = CodePush.DEFAULT_UPDATE_DIALOG;
211+
} else {
212+
syncOptions.updateDialog = Object.assign({}, CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog);
213+
}
211214

212-
if (!remotePackage || (remotePackage.failedInstall && syncOptions.ignoreFailedUpdates)) {
213-
syncStatusChangeCallback(CodePush.SyncStatus.UP_TO_DATE);
214-
resolve(CodePush.SyncStatus.UP_TO_DATE);
215-
}
216-
else if (syncOptions.updateDialog) {
217-
// updateDialog supports any truthy value (e.g. true, "goo", 12),
218-
// but we should treat a non-object value as just the default dialog
219-
if (typeof syncOptions.updateDialog !== "object") {
220-
syncOptions.updateDialog = CodePush.DEFAULT_UPDATE_DIALOG;
221-
} else {
222-
syncOptions.updateDialog = Object.assign({}, CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog);
223-
}
224-
225-
var message = null;
226-
var dialogButtons = [
227-
{
228-
text: null,
229-
onPress: () => {
230-
doDownloadAndInstall();
231-
}
232-
}
233-
];
234-
235-
if (remotePackage.isMandatory) {
236-
message = syncOptions.updateDialog.mandatoryUpdateMessage;
237-
dialogButtons[0].text = syncOptions.updateDialog.mandatoryContinueButtonLabel;
238-
} else {
239-
message = syncOptions.updateDialog.optionalUpdateMessage;
240-
dialogButtons[0].text = syncOptions.updateDialog.optionalInstallButtonLabel;
241-
242-
// Since this is an optional update, add another button
243-
// to allow the end-user to ignore it
244-
dialogButtons.push({
245-
text: syncOptions.updateDialog.optionalIgnoreButtonLabel,
246-
onPress: () => {
247-
syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_IGNORED);
248-
resolve(CodePush.SyncStatus.UPDATE_IGNORED);
249-
}
250-
});
251-
}
252-
253-
// If the update has a description, and the developer
254-
// explicitly chose to display it, then set that as the message
255-
if (syncOptions.updateDialog.appendReleaseDescription && remotePackage.description) {
256-
message += `${syncOptions.updateDialog.descriptionPrefix} ${remotePackage.description}`;
215+
return await new Promise((resolve, reject) => {
216+
let message = null;
217+
let dialogButtons = [{
218+
text: null,
219+
onPress: async () => {
220+
resolve(await doDownloadAndInstall());
257221
}
258-
259-
syncStatusChangeCallback(CodePush.SyncStatus.AWAITING_USER_ACTION);
260-
Alert.alert(syncOptions.updateDialog.title, message, dialogButtons);
222+
}];
223+
224+
if (remotePackage.isMandatory) {
225+
message = syncOptions.updateDialog.mandatoryUpdateMessage;
226+
dialogButtons[0].text = syncOptions.updateDialog.mandatoryContinueButtonLabel;
261227
} else {
262-
doDownloadAndInstall();
228+
message = syncOptions.updateDialog.optionalUpdateMessage;
229+
dialogButtons[0].text = syncOptions.updateDialog.optionalInstallButtonLabel;
230+
// Since this is an optional update, add another button
231+
// to allow the end-user to ignore it
232+
dialogButtons.push({
233+
text: syncOptions.updateDialog.optionalIgnoreButtonLabel,
234+
onPress: () => {
235+
syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_IGNORED);
236+
resolve(CodePush.SyncStatus.UPDATE_IGNORED);
237+
}
238+
});
239+
}
240+
241+
// If the update has a description, and the developer
242+
// explicitly chose to display it, then set that as the message
243+
if (syncOptions.updateDialog.appendReleaseDescription && remotePackage.description) {
244+
message += `${syncOptions.updateDialog.descriptionPrefix} ${remotePackage.description}`;
263245
}
264-
})
265-
.catch(rejectPromise)
266-
.done();
267-
});
246+
247+
syncStatusChangeCallback(CodePush.SyncStatus.AWAITING_USER_ACTION);
248+
Alert.alert(syncOptions.updateDialog.title, message, dialogButtons);
249+
});
250+
} else {
251+
return await doDownloadAndInstall();
252+
}
253+
} catch (error) {
254+
syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR);
255+
log(error.message);
256+
throw error;
257+
}
268258
};
269259

270260
var CodePush = {
@@ -304,4 +294,4 @@ var CodePush = {
304294
}
305295
};
306296

307-
module.exports = CodePush;
297+
export default CodePush;

Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/CheckForUpdateTestApp.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ let CheckForUpdateTestApp = React.createClass({
3838
</ScrollView>
3939
);
4040
}
41+
4142
return (
4243
<View style={styles.container}>
4344
<Text style={styles.row}>

Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/FirstUpdateTest.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,13 @@ let FirstUpdateTest = createTestCaseComponent(
1818
let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage);
1919
let mockConfiguration = { appVersion : "1.5.0" };
2020
CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush);
21-
CodePush.getCurrentPackage = () => {
22-
return Promise.resolve(localPackage);
23-
}
24-
return Promise.resolve();
21+
CodePush.getCurrentPackage = async () => {
22+
return localPackage;
23+
};
2524
},
26-
() => {
27-
return CodePush.checkForUpdate()
28-
.then((update) => {
29-
if (update) {
30-
assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote));
31-
} else {
32-
throw new Error("checkForUpdate did not return the update from the server");
33-
}
34-
});
25+
async () => {
26+
let update = await CodePush.checkForUpdate()
27+
assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote), "checkForUpdate did not return the update from the server");
3528
}
3629
);
3730

Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NewUpdateTest.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,14 @@ let NewUpdateTest = createTestCaseComponent(
1717
let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage);
1818
let mockConfiguration = { appVersion : "1.5.0" };
1919
CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush);
20-
CodePush.getCurrentPackage = () => {
21-
return Promise.resolve(localPackage);
22-
}
23-
return Promise.resolve();
20+
CodePush.getCurrentPackage = async () => {
21+
return localPackage;
22+
};
2423
},
25-
() => {
26-
return CodePush.checkForUpdate()
27-
.then((update) => {
28-
if (update) {
29-
assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote));
30-
} else {
31-
throw new Error("checkForUpdate did not return the update from the server");
32-
}
33-
});
24+
async () => {
25+
let update = await CodePush.checkForUpdate()
26+
assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote), "checkForUpdate did not return the update from the server");
3427
}
3528
);
3629

37-
module.exports = NewUpdateTest;
30+
export default NewUpdateTest;

Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NoRemotePackageTest.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@ let NoRemotePackageTest = createTestCaseComponent(
1818
let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage);
1919
let mockConfiguration = { appVersion : "1.5.0" };
2020
CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush);
21-
CodePush.getCurrentPackage = () => {
22-
return Promise.resolve(localPackage);
23-
}
24-
return Promise.resolve();
21+
CodePush.getCurrentPackage = async () => {
22+
return localPackage;
23+
};
2524
},
26-
() => {
27-
return CodePush.checkForUpdate()
28-
.then((update) => {
29-
if (update) {
30-
throw new Error("checkForUpdate should not return an update if there is none on the server");
31-
}
32-
});
25+
async () => {
26+
let update = await CodePush.checkForUpdate();
27+
assert(!update, "checkForUpdate should not return an update if there is none on the server");
3328
}
3429
);
3530

36-
module.exports = NoRemotePackageTest;
31+
export default NoRemotePackageTest;

Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/RemotePackageAppVersionNewerTest.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,17 @@ let RemotePackageAppVersionNewerTest = createTestCaseComponent(
1515
"RemotePackageAppVersionNewerTest",
1616
"should drop the update when the server reports one with a newer binary version",
1717
() => {
18-
return new Promise((resolve, reject) => {
19-
let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage);
20-
let mockConfiguration = { appVersion : "1.0.0" };
21-
CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush);
22-
CodePush.getCurrentPackage = () => {
23-
return Promise.resolve(localPackage);
24-
}
25-
resolve();
26-
});
18+
let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage);
19+
let mockConfiguration = { appVersion : "1.0.0" };
20+
CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush);
21+
CodePush.getCurrentPackage = async () => {
22+
return localPackage;
23+
};
2724
},
28-
() => {
29-
return CodePush.checkForUpdate()
30-
.then((update) => {
31-
if (update) {
32-
throw new Error("checkForUpdate should not return an update if remote package is of a different binary version");
33-
}
34-
});
25+
async () => {
26+
let update = await CodePush.checkForUpdate()
27+
assert(!update, "checkForUpdate should not return an update if remote package is of a different binary version");
3528
}
3629
);
3730

38-
module.exports = RemotePackageAppVersionNewerTest;
31+
export default RemotePackageAppVersionNewerTest;

Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SamePackageTest.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@ let SamePackageTest = createTestCaseComponent(
1818
let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage);
1919
let mockConfiguration = { appVersion : "1.5.0" };
2020
CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush);
21-
CodePush.getCurrentPackage = () => {
22-
return Promise.resolve(localPackage);
23-
}
24-
return Promise.resolve();
21+
CodePush.getCurrentPackage = async () => {
22+
return localPackage;
23+
};
2524
},
26-
() => {
27-
return CodePush.checkForUpdate()
28-
.then((update) => {
29-
if (update) {
30-
throw new Error("checkForUpdate should not return a package when local package is identical");
31-
}
32-
});
25+
async () => {
26+
let update = await CodePush.checkForUpdate();
27+
assert(!update, "checkForUpdate should not return a package when local package is identical");
3328
}
3429
);
3530

36-
module.exports = SamePackageTest;
31+
export default SamePackageTest;

Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SwitchDeploymentKeyTest.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,14 @@ let SwitchDeploymentKeyTest = createTestCaseComponent(
2020
let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage, deploymentKey);
2121
let mockConfiguration = { appVersion : "1.5.0" };
2222
CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush);
23-
CodePush.getCurrentPackage = () => {
24-
return Promise.resolve(localPackage);
25-
}
26-
return Promise.resolve();
23+
CodePush.getCurrentPackage = async () => {
24+
return localPackage;
25+
};
2726
},
28-
() => {
29-
return CodePush.checkForUpdate(deploymentKey)
30-
.then((update) => {
31-
if (update) {
32-
assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote));
33-
} else {
34-
throw new Error("checkForUpdate did not return the update from the server");
35-
}
36-
});
27+
async () => {
28+
let update = await CodePush.checkForUpdate(deploymentKey)
29+
assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote), "checkForUpdate did not return the update from the server");
3730
}
3831
);
3932

40-
module.exports = SwitchDeploymentKeyTest;
33+
export default SwitchDeploymentKeyTest;

Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/testcases/DownloadProgressTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ let DownloadProgressTest = createTestCaseComponent(
4949
}
5050
);
5151

52-
module.exports = DownloadProgressTest;
52+
export default DownloadProgressTest;

0 commit comments

Comments
 (0)