From ebd93b53ebb6c5c1f698865c015f0b32af81eb24 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Wed, 23 Dec 2015 21:24:29 -0800 Subject: [PATCH] es7 --- CodePush.js | 158 ++++++++---------- .../CheckForUpdateTestApp.js | 1 + .../testcases/FirstUpdateTest.js | 19 +-- .../testcases/NewUpdateTest.js | 21 +-- .../testcases/NoRemotePackageTest.js | 19 +-- .../RemotePackageAppVersionNewerTest.js | 27 ++- .../testcases/SamePackageTest.js | 19 +-- .../testcases/SwitchDeploymentKeyTest.js | 21 +-- .../testcases/DownloadProgressTest.js | 2 +- .../resources/CheckIsFirstRunAndPassTest.js | 25 ++- .../resources/IsFailedUpdateTestBundleV1.js | 39 ++--- .../resources/IsFailedUpdateTestBundleV2.js | 1 + .../NotifyApplicationReadyAndRestart.js | 21 +-- .../resources/RollbackTestBundleV1.js | 47 ++---- .../testcases/InstallModeImmediateTest.js | 9 +- .../testcases/InstallModeOnNextRestartTest.js | 13 +- .../testcases/InstallModeOnNextResumeTest.js | 13 +- .../testcases/IsFailedUpdateTest.js | 9 +- .../testcases/IsFirstRunTest.js | 9 +- .../testcases/IsPendingTest.js | 19 +-- .../testcases/NotifyApplicationReadyTest.js | 9 +- .../testcases/RollbackTest.js | 9 +- .../utils/createTestCaseComponent.js | 23 ++- Examples/CodePushDemoApp/crossplatformdemo.js | 144 ++++++++-------- .../iOS/CodePushDemoApp/AppDelegate.m | 4 +- .../iOS/CodePushDemoApp/Info.plist | 2 +- Examples/CodePushDemoApp/index.android.js | 2 +- Examples/CodePushDemoApp/index.ios.js | 2 +- package-mixins.js | 47 +++--- 29 files changed, 309 insertions(+), 425 deletions(-) diff --git a/CodePush.js b/CodePush.js index bae587a..a688669 100644 --- a/CodePush.js +++ b/CodePush.js @@ -90,7 +90,7 @@ function getPromisifiedSdk(requestFetchAdapter, config) { let sdk = new module.exports.AcquisitionSdk(requestFetchAdapter, config); sdk.queryUpdateWithCurrentPackage = (queryPackage) => { return new Promise((resolve, reject) => { - sdk.queryUpdateWithCurrentPackage(queryPackage, (err, update) => { + module.exports.AcquisitionSdk.prototype.queryUpdateWithCurrentPackage.call(sdk, queryPackage, (err, update) => { if (err) { reject(err); } else { @@ -126,8 +126,8 @@ function setUpTestDependencies(testSdk, providedTestConfig, testNativeBridge) { * releases, and displaying a standard confirmation UI to the end-user * when an update is available. */ -function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) { - var syncOptions = { +async function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) { + let syncOptions = { deploymentKey: null, ignoreFailedUpdates: true, @@ -139,7 +139,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) syncStatusChangeCallback = typeof syncStatusChangeCallback == "function" ? syncStatusChangeCallback - : function(syncStatus) { + : (syncStatus) => { switch(syncStatus) { case CodePush.SyncStatus.CHECKING_FOR_UPDATE: log("Checking for update."); @@ -178,93 +178,83 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) downloadProgressCallback = typeof downloadProgressCallback == "function" ? downloadProgressCallback - : function(downloadProgress) { + : (downloadProgress) => { log(`Expecting ${downloadProgress.totalBytes} bytes, received ${downloadProgress.receivedBytes} bytes.`); }; - return new Promise((resolve, reject) => { - var rejectPromise = (error) => { - syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR); - log(error.message); - reject(error); + try { + await CodePush.notifyApplicationReady(); + + syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE); + let remotePackage = await checkForUpdate(syncOptions.deploymentKey); + + let doDownloadAndInstall = async () => { + syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE); + let localPackage = await remotePackage.download(downloadProgressCallback); + + syncStatusChangeCallback(CodePush.SyncStatus.INSTALLING_UPDATE); + await localPackage.install(syncOptions.installMode, () => { + syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_INSTALLED); + }); + + return CodePush.SyncStatus.UPDATE_INSTALLED; }; - CodePush.notifyApplicationReady() - .then(() => { - syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE); - return checkForUpdate(syncOptions.deploymentKey); - }) - .then((remotePackage) => { - var doDownloadAndInstall = () => { - syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE); - remotePackage.download(downloadProgressCallback) - .then((localPackage) => { - syncStatusChangeCallback(CodePush.SyncStatus.INSTALLING_UPDATE); - return localPackage.install(syncOptions.installMode, () => { - syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_INSTALLED); - resolve(CodePush.SyncStatus.UPDATE_INSTALLED); - }); - }) - .catch(rejectPromise) - .done(); + if (!remotePackage || (remotePackage.failedInstall && syncOptions.ignoreFailedUpdates)) { + syncStatusChangeCallback(CodePush.SyncStatus.UP_TO_DATE); + return (CodePush.SyncStatus.UP_TO_DATE); + } else if (syncOptions.updateDialog) { + // updateDialog supports any truthy value (e.g. true, "goo", 12), + // but we should treat a non-object value as just the default dialog + if (typeof syncOptions.updateDialog !== "object") { + syncOptions.updateDialog = CodePush.DEFAULT_UPDATE_DIALOG; + } else { + syncOptions.updateDialog = Object.assign({}, CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog); + } + + return await new Promise((resolve, reject) => { + let message = null; + let dialogButtons = [{ + text: null, + onPress: async () => { + resolve(await doDownloadAndInstall()); + } + }]; + + if (remotePackage.isMandatory) { + message = syncOptions.updateDialog.mandatoryUpdateMessage; + dialogButtons[0].text = syncOptions.updateDialog.mandatoryContinueButtonLabel; + } else { + message = syncOptions.updateDialog.optionalUpdateMessage; + dialogButtons[0].text = syncOptions.updateDialog.optionalInstallButtonLabel; + // Since this is an optional update, add another button + // to allow the end-user to ignore it + dialogButtons.push({ + text: syncOptions.updateDialog.optionalIgnoreButtonLabel, + onPress: () => { + syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_IGNORED); + resolve(CodePush.SyncStatus.UPDATE_IGNORED); + } + }); } - if (!remotePackage || (remotePackage.failedInstall && syncOptions.ignoreFailedUpdates)) { - syncStatusChangeCallback(CodePush.SyncStatus.UP_TO_DATE); - resolve(CodePush.SyncStatus.UP_TO_DATE); + // If the update has a description, and the developer + // explicitly chose to display it, then set that as the message + if (syncOptions.updateDialog.appendReleaseDescription && remotePackage.description) { + message += `${syncOptions.updateDialog.descriptionPrefix} ${remotePackage.description}`; } - else if (syncOptions.updateDialog) { - // updateDialog supports any truthy value (e.g. true, "goo", 12), - // but we should treat a non-object value as just the default dialog - if (typeof syncOptions.updateDialog !== "object") { - syncOptions.updateDialog = CodePush.DEFAULT_UPDATE_DIALOG; - } else { - syncOptions.updateDialog = Object.assign({}, CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog); - } - - var message = null; - var dialogButtons = [ - { - text: null, - onPress: () => { - doDownloadAndInstall(); - } - } - ]; - - if (remotePackage.isMandatory) { - message = syncOptions.updateDialog.mandatoryUpdateMessage; - dialogButtons[0].text = syncOptions.updateDialog.mandatoryContinueButtonLabel; - } else { - message = syncOptions.updateDialog.optionalUpdateMessage; - dialogButtons[0].text = syncOptions.updateDialog.optionalInstallButtonLabel; - - // Since this is an optional update, add another button - // to allow the end-user to ignore it - dialogButtons.push({ - text: syncOptions.updateDialog.optionalIgnoreButtonLabel, - onPress: () => { - syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_IGNORED); - resolve(CodePush.SyncStatus.UPDATE_IGNORED); - } - }); - } - - // If the update has a description, and the developer - // explicitly chose to display it, then set that as the message - if (syncOptions.updateDialog.appendReleaseDescription && remotePackage.description) { - message += `${syncOptions.updateDialog.descriptionPrefix} ${remotePackage.description}`; - } - - syncStatusChangeCallback(CodePush.SyncStatus.AWAITING_USER_ACTION); - Alert.alert(syncOptions.updateDialog.title, message, dialogButtons); - } else { - doDownloadAndInstall(); - } - }) - .catch(rejectPromise) - .done(); - }); + + syncStatusChangeCallback(CodePush.SyncStatus.AWAITING_USER_ACTION); + Alert.alert(syncOptions.updateDialog.title, message, dialogButtons); + }); + } else { + return await doDownloadAndInstall(); + } + } catch (error) { + syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR); + log(error.message); + throw error; + } }; var CodePush = { @@ -304,4 +294,4 @@ var CodePush = { } }; -module.exports = CodePush; +export default CodePush; diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/CheckForUpdateTestApp.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/CheckForUpdateTestApp.js index 62393c2..22250cc 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/CheckForUpdateTestApp.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/CheckForUpdateTestApp.js @@ -38,6 +38,7 @@ let CheckForUpdateTestApp = React.createClass({ ); } + return ( diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/FirstUpdateTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/FirstUpdateTest.js index 33b286b..bf27f6a 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/FirstUpdateTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/FirstUpdateTest.js @@ -18,20 +18,13 @@ let FirstUpdateTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote)); - } else { - throw new Error("checkForUpdate did not return the update from the server"); - } - }); + async () => { + let update = await CodePush.checkForUpdate() + assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote), "checkForUpdate did not return the update from the server"); } ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NewUpdateTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NewUpdateTest.js index f020fce..393fdd2 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NewUpdateTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NewUpdateTest.js @@ -17,21 +17,14 @@ let NewUpdateTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote)); - } else { - throw new Error("checkForUpdate did not return the update from the server"); - } - }); + async () => { + let update = await CodePush.checkForUpdate() + assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote), "checkForUpdate did not return the update from the server"); } ); -module.exports = NewUpdateTest; \ No newline at end of file +export default NewUpdateTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NoRemotePackageTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NoRemotePackageTest.js index d3fb582..5464e7a 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NoRemotePackageTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/NoRemotePackageTest.js @@ -18,19 +18,14 @@ let NoRemotePackageTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - throw new Error("checkForUpdate should not return an update if there is none on the server"); - } - }); + async () => { + let update = await CodePush.checkForUpdate(); + assert(!update, "checkForUpdate should not return an update if there is none on the server"); } ); -module.exports = NoRemotePackageTest; \ No newline at end of file +export default NoRemotePackageTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/RemotePackageAppVersionNewerTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/RemotePackageAppVersionNewerTest.js index 4b4af51..50f9d57 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/RemotePackageAppVersionNewerTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/RemotePackageAppVersionNewerTest.js @@ -15,24 +15,17 @@ let RemotePackageAppVersionNewerTest = createTestCaseComponent( "RemotePackageAppVersionNewerTest", "should drop the update when the server reports one with a newer binary version", () => { - return new Promise((resolve, reject) => { - let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); - let mockConfiguration = { appVersion : "1.0.0" }; - CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - resolve(); - }); + let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); + let mockConfiguration = { appVersion : "1.0.0" }; + CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - throw new Error("checkForUpdate should not return an update if remote package is of a different binary version"); - } - }); + async () => { + let update = await CodePush.checkForUpdate() + assert(!update, "checkForUpdate should not return an update if remote package is of a different binary version"); } ); -module.exports = RemotePackageAppVersionNewerTest; \ No newline at end of file +export default RemotePackageAppVersionNewerTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SamePackageTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SamePackageTest.js index 88288c8..ba401f7 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SamePackageTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SamePackageTest.js @@ -18,19 +18,14 @@ let SamePackageTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate() - .then((update) => { - if (update) { - throw new Error("checkForUpdate should not return a package when local package is identical"); - } - }); + async () => { + let update = await CodePush.checkForUpdate(); + assert(!update, "checkForUpdate should not return a package when local package is identical"); } ); -module.exports = SamePackageTest; \ No newline at end of file +export default SamePackageTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SwitchDeploymentKeyTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SwitchDeploymentKeyTest.js index 4205559..caceded 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SwitchDeploymentKeyTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/CheckForUpdateTests/testcases/SwitchDeploymentKeyTest.js @@ -20,21 +20,14 @@ let SwitchDeploymentKeyTest = createTestCaseComponent( let mockAcquisitionSdk = createMockAcquisitionSdk(serverPackage, localPackage, deploymentKey); let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.getCurrentPackage = () => { - return Promise.resolve(localPackage); - } - return Promise.resolve(); + CodePush.getCurrentPackage = async () => { + return localPackage; + }; }, - () => { - return CodePush.checkForUpdate(deploymentKey) - .then((update) => { - if (update) { - assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote)); - } else { - throw new Error("checkForUpdate did not return the update from the server"); - } - }); + async () => { + let update = await CodePush.checkForUpdate(deploymentKey) + assert.deepEqual(update, Object.assign(serverPackage, PackageMixins.remote), "checkForUpdate did not return the update from the server"); } ); -module.exports = SwitchDeploymentKeyTest; \ No newline at end of file +export default SwitchDeploymentKeyTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/testcases/DownloadProgressTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/testcases/DownloadProgressTest.js index b54f3e5..7841f66 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/testcases/DownloadProgressTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/DownloadProgressTests/testcases/DownloadProgressTest.js @@ -49,4 +49,4 @@ let DownloadProgressTest = createTestCaseComponent( } ); -module.exports = DownloadProgressTest; \ No newline at end of file +export default DownloadProgressTest; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/CheckIsFirstRunAndPassTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/CheckIsFirstRunAndPassTest.js index 52ddf35..54e2ad5 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/CheckIsFirstRunAndPassTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/CheckIsFirstRunAndPassTest.js @@ -1,27 +1,24 @@ "use strict"; -import React from "react-native"; -import CodePush from "react-native-code-push"; - -let { +import React, { AppRegistry, Text, View, -} = React; +} from "react-native"; + +import CodePush from "react-native-code-push"; let IsFirstRunTest = React.createClass({ getInitialState() { return {}; }, - componentDidMount() { - CodePush.getCurrentPackage() - .then((localPackage) => { - if (localPackage.isFirstRun) { - this.setState({ passed: true }); - } else { - this.setState({ passed: false }); - } - }); + async componentDidMount() { + let localPackage = await CodePush.getCurrentPackage(); + if (localPackage.isFirstRun) { + this.setState({ passed: true }); + } else { + this.setState({ passed: false }); + } }, render() { let text = "Testing..."; diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV1.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV1.js index d7f2eb2..72ee0dc 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV1.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV1.js @@ -1,23 +1,22 @@ "use strict"; -import React from "react-native"; +import React, { + AppRegistry, + Platform, + Text, + View, +} from "react-native"; + import CodePush from "react-native-code-push"; let NativeCodePush = React.NativeModules.CodePush; let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); import createMockAcquisitionSdk from "../../utils/mockAcquisitionSdk"; -let { - AppRegistry, - Platform, - Text, - View, -} = React; - let IsFailedUpdateTest = React.createClass({ getInitialState() { return {}; }, - componentDidMount() { + async componentDidMount() { let serverPackage = { description: "Angry flappy birds", appVersion: "1.5.0", @@ -39,20 +38,14 @@ let IsFailedUpdateTest = React.createClass({ let mockConfiguration = { appVersion : "1.5.0" }; CodePush.setUpTestDependencies(mockAcquisitionSdk, mockConfiguration, NativeCodePush); - CodePush.notifyApplicationReady() - .then(() => { - return CodePush.checkForUpdate(); - }) - .then((remotePackage) => { - if (remotePackage.failedInstall) { - this.setState({ passed: true }); - } else { - return remotePackage.download(); - } - }) - .then((localPackage) => { - return localPackage && localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + await CodePush.notifyApplicationReady() + let remotePackage = await CodePush.checkForUpdate(); + if (remotePackage.failedInstall) { + this.setState({ passed: true }); + } else { + let localPackage = await remotePackage.download(); + return localPackage && await localPackage.install(NativeCodePush.codePushInstallModeImmediate); + } }, render() { let text = "Testing..."; diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV2.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV2.js index bd8fc8a..da8f2f4 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV2.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/IsFailedUpdateTestBundleV2.js @@ -11,6 +11,7 @@ let { let IsFailedUpdateTest = React.createClass({ componentDidMount() { + // Should trigger a rollback. CodePush.restartApp(); }, render() { diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/NotifyApplicationReadyAndRestart.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/NotifyApplicationReadyAndRestart.js index fcd9f2d..338e2ae 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/NotifyApplicationReadyAndRestart.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/NotifyApplicationReadyAndRestart.js @@ -10,18 +10,15 @@ let NotifyApplicationReadyTest = React.createClass({ getInitialState() { return {}; }, - componentDidMount() { - CodePush.notifyApplicationReady() - .then(() => { - if (Platform.OS === "android") { - return NativeCodePush.downloadAndReplaceCurrentBundle("http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.includeRequire.runModule.bundle?platform=android&dev=true"); - } else if (Platform.OS === "ios") { - return NativeCodePush.downloadAndReplaceCurrentBundle("http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.includeRequire.runModule.bundle?platform=ios&dev=true"); - } - }) - .then(() => { - CodePush.restartApp(); - }); + async componentDidMount() { + await CodePush.notifyApplicationReady(); + if (Platform.OS === "android") { + await NativeCodePush.downloadAndReplaceCurrentBundle("http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.includeRequire.runModule.bundle?platform=android&dev=true"); + } else if (Platform.OS === "ios") { + await NativeCodePush.downloadAndReplaceCurrentBundle("http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/PassNotifyApplicationReadyTest.includeRequire.runModule.bundle?platform=ios&dev=true"); + } + + CodePush.restartApp(); }, render() { return ( diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1.js index 12eb9c5..1877c84 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1.js @@ -1,51 +1,36 @@ "use strict"; -import React from "react-native"; +import React, { + AppRegistry, + Platform, + Text, + View, +} from "react-native"; + import CodePush from "react-native-code-push"; let RCTTestModule = React.NativeModules.TestModule; let NativeCodePush = React.NativeModules.CodePush; let PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush); -let { - AppRegistry, - Platform, - Text, - View, -} = React; - let RollbackTest = React.createClass({ - componentDidMount() { - let remotePackage = { - description: "Angry flappy birds", - appVersion: "1.5.0", - label: "2.4.0", - isMandatory: false, - isAvailable: true, - updateAppVersion: false, - packageHash: "hash241", - packageSize: 1024 - }; + async componentDidMount() { + let remotePackage = require("./remotePackage"); + remotePackage.packageHash = "hash241"; if (Platform.OS === "android") { remotePackage.downloadUrl = "http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV2.includeRequire.runModule.bundle?platform=android&dev=true" - CodePush.notifyApplicationReady() - .then(() => { - NativeCodePush.downloadAndReplaceCurrentBundle("http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.includeRequire.runModule.bundle?platform=android&dev=true"); - }); + await CodePush.notifyApplicationReady(); + await NativeCodePush.downloadAndReplaceCurrentBundle("http://10.0.3.2:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.includeRequire.runModule.bundle?platform=android&dev=true"); } else if (Platform.OS === "ios") { remotePackage.downloadUrl = "http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV2.includeRequire.runModule.bundle?platform=ios&dev=true" - CodePush.notifyApplicationReady() - .then(() => { - NativeCodePush.downloadAndReplaceCurrentBundle("http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.includeRequire.runModule.bundle?platform=ios&dev=true"); - }); + await CodePush.notifyApplicationReady() + await NativeCodePush.downloadAndReplaceCurrentBundle("http://localhost:8081/CodePushDemoAppTests/InstallUpdateTests/resources/RollbackTestBundleV1Pass.includeRequire.runModule.bundle?platform=ios&dev=true"); } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + let localPackage = await remotePackage.download(); + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, render() { return ( diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeImmediateTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeImmediateTest.js index aa83023..640b0fe 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeImmediateTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeImmediateTest.js @@ -21,13 +21,10 @@ let InstallModeImmediateTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download(); + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextRestartTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextRestartTest.js index 6429de8..5fa78fa 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextRestartTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextRestartTest.js @@ -22,16 +22,11 @@ let InstallModeOnNextRestartTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeOnNextRestart); - }) - .then(() => { - CodePush.restartApp(); - }); + async () => { + let localPackage = await remotePackage.download(); + await localPackage.install(NativeCodePush.codePushInstallModeOnNextRestart); + CodePush.restartApp(); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextResumeTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextResumeTest.js index 942d17e..8596d61 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextResumeTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/InstallModeOnNextResumeTest.js @@ -21,16 +21,11 @@ let InstallModeOnNextResumeTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeOnNextResume); - }) - .then(() => { - CodePush.restartApp(); - }); + async () => { + let localPackage = await remotePackage.download() + await localPackage.install(NativeCodePush.codePushInstallModeOnNextResume); + CodePush.restartApp(); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFailedUpdateTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFailedUpdateTest.js index 78c4c6a..59a787f 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFailedUpdateTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFailedUpdateTest.js @@ -21,13 +21,10 @@ let IsFailedUpdateTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download(); + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFirstRunTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFirstRunTest.js index 0c0866b..d31f910 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFirstRunTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsFirstRunTest.js @@ -21,13 +21,10 @@ let IsFirstRunTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download(); + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsPendingTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsPendingTest.js index 901ef12..bd6825a 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsPendingTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/IsPendingTest.js @@ -21,20 +21,13 @@ let IsPendingTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeOnNextRestart); - }) - .then((localPackage) => { - assert(localPackage.isPending, "isPending should be set to \"true\" after an install"); - return CodePush.getCurrentPackage(); - }) - .then((localPackage) => { - assert(localPackage.isPending, "isPending should be set to \"true\" after an install"); - }); + async () => { + let localPackage = await remotePackage.download(); + await localPackage.install(NativeCodePush.codePushInstallModeOnNextRestart); + assert(localPackage.isPending, "isPending should be set to \"true\" after an install"); + localPackage = await CodePush.getCurrentPackage(); + assert(localPackage.isPending, "isPending should be set to \"true\" after an install"); } ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/NotifyApplicationReadyTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/NotifyApplicationReadyTest.js index a38fcf2..df233f1 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/NotifyApplicationReadyTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/NotifyApplicationReadyTest.js @@ -21,13 +21,10 @@ let NotifyApplicationReadyTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download() + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/RollbackTest.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/RollbackTest.js index 3c098ba..6d4cd3e 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/RollbackTest.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/InstallUpdateTests/testcases/RollbackTest.js @@ -21,13 +21,10 @@ let RollbackTest = createTestCaseComponent( } remotePackage = Object.assign(remotePackage, PackageMixins.remote); - return Promise.resolve(); }, - () => { - remotePackage.download() - .then((localPackage) => { - return localPackage.install(NativeCodePush.codePushInstallModeImmediate); - }); + async () => { + let localPackage = await remotePackage.download() + return await localPackage.install(NativeCodePush.codePushInstallModeImmediate); }, /*passAfterRun*/ false ); diff --git a/Examples/CodePushDemoApp/CodePushDemoAppTests/utils/createTestCaseComponent.js b/Examples/CodePushDemoApp/CodePushDemoAppTests/utils/createTestCaseComponent.js index 22d300c..9af45e3 100644 --- a/Examples/CodePushDemoApp/CodePushDemoAppTests/utils/createTestCaseComponent.js +++ b/Examples/CodePushDemoApp/CodePushDemoAppTests/utils/createTestCaseComponent.js @@ -18,18 +18,17 @@ function createTestCaseComponent(displayName, description, setUp, runTest, passA done: false, }; }, - componentDidMount() { - setUp() - .then(runTest) - .then(() => { - if (passAfterRun) { - this.setState({done: true}, RCTTestModule.markTestCompleted); - } - }) - .catch((err) => { - console.error(err); - throw err; - }); + async componentDidMount() { + try { + await setUp(); + await runTest(); + if (passAfterRun) { + this.setState({done: true}, RCTTestModule.markTestCompleted); + } + } catch (err) { + console.error(err); + throw err; + } }, render() { return ( diff --git a/Examples/CodePushDemoApp/crossplatformdemo.js b/Examples/CodePushDemoApp/crossplatformdemo.js index b05a128..b340b3f 100644 --- a/Examples/CodePushDemoApp/crossplatformdemo.js +++ b/Examples/CodePushDemoApp/crossplatformdemo.js @@ -1,7 +1,6 @@ 'use strict'; -var React = require('react-native'); -var { +import React, { AppRegistry, Dimensions, Image, @@ -9,84 +8,85 @@ var { Text, TouchableOpacity, View, -} = React; +} from "react-native"; -var Button = require("react-native-button"); +import Button from "react-native-button"; +import CodePush from "react-native-code-push"; -var CodePush = require('react-native-code-push'); - -var CodePushDemoApp = React.createClass({ - sync() { - var self = this; - CodePush.sync( - { - updateDialog: true, - installMode: CodePush.InstallMode.ON_NEXT_RESUME - }, - function(syncStatus) { - switch(syncStatus) { - case CodePush.SyncStatus.CHECKING_FOR_UPDATE: - self.setState({ - syncMessage: "Checking for update." - }); - break; - case CodePush.SyncStatus.DOWNLOADING_PACKAGE: - self.setState({ - syncMessage: "Downloading package." - }); - break; - case CodePush.SyncStatus.AWAITING_USER_ACTION: - self.setState({ - syncMessage: "Awaiting user action." - }); - break; - case CodePush.SyncStatus.INSTALLING_UPDATE: - self.setState({ - syncMessage: "Installing update." - }); - break; - case CodePush.SyncStatus.UP_TO_DATE: - self.setState({ - syncMessage: "App up to date.", - progress: false - }); - break; - case CodePush.SyncStatus.UPDATE_IGNORED: - self.setState({ - syncMessage: "Update cancelled by user.", - progress: false - }); - break; - case CodePush.SyncStatus.UPDATE_INSTALLED: - self.setState({ - syncMessage: "Update installed and will be run when the app next resumes.", - progress: false - }); - break; - case CodePush.SyncStatus.UNKNOWN_ERROR: - self.setState({ - syncMessage: "An unknown error occurred.", - progress: false - }); - break; +let CodePushDemoApp = React.createClass({ + async sync() { + let self = this; + try { + return await CodePush.sync( + { + updateDialog: true, + installMode: CodePush.InstallMode.ON_NEXT_RESUME + }, + (syncStatus) => { + switch(syncStatus) { + case CodePush.SyncStatus.CHECKING_FOR_UPDATE: + self.setState({ + syncMessage: "Checking for update." + }); + break; + case CodePush.SyncStatus.DOWNLOADING_PACKAGE: + self.setState({ + syncMessage: "Downloading package." + }); + break; + case CodePush.SyncStatus.AWAITING_USER_ACTION: + self.setState({ + syncMessage: "Awaiting user action." + }); + break; + case CodePush.SyncStatus.INSTALLING_UPDATE: + self.setState({ + syncMessage: "Installing update." + }); + break; + case CodePush.SyncStatus.UP_TO_DATE: + self.setState({ + syncMessage: "App up to date.", + progress: false + }); + break; + case CodePush.SyncStatus.UPDATE_IGNORED: + self.setState({ + syncMessage: "Update cancelled by user.", + progress: false + }); + break; + case CodePush.SyncStatus.UPDATE_INSTALLED: + self.setState({ + syncMessage: "Update installed and will be run when the app next resumes.", + progress: false + }); + break; + case CodePush.SyncStatus.UNKNOWN_ERROR: + self.setState({ + syncMessage: "An unknown error occurred.", + progress: false + }); + break; + } + }, + (progress) => { + self.setState({ + progress: progress + }); } - }, - function(progress) { - self.setState({ - progress: progress - }); - } - ).catch(function(error) { + ); + } catch (error) { CodePush.log(error); - }); + } }, + getInitialState() { return { }; }, + render() { - var syncView; - var syncButton; - var progressView; + let syncView, syncButton, progressView; if (this.state.syncMessage) { syncView = ( @@ -120,7 +120,7 @@ var CodePushDemoApp = React.createClass({ } }); -var styles = StyleSheet.create({ +let styles = StyleSheet.create({ image: { marginTop: 50, width: Dimensions.get('window').width - 100, diff --git a/Examples/CodePushDemoApp/iOS/CodePushDemoApp/AppDelegate.m b/Examples/CodePushDemoApp/iOS/CodePushDemoApp/AppDelegate.m index 11de946..aac98cc 100644 --- a/Examples/CodePushDemoApp/iOS/CodePushDemoApp/AppDelegate.m +++ b/Examples/CodePushDemoApp/iOS/CodePushDemoApp/AppDelegate.m @@ -33,7 +33,7 @@ * on the same Wi-Fi network. */ - jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.includeRequire.runModule.bundle?dev=true&platform=ios"]; + //jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.includeRequire.runModule.bundle?dev=true&platform=ios"]; /** * OPTION 2 @@ -45,7 +45,7 @@ * see http://facebook.github.io/react-native/docs/runningondevice.html */ - //jsCodeLocation = [CodePush bundleURL]; + jsCodeLocation = [CodePush bundleURL]; RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"CodePushDemoApp" diff --git a/Examples/CodePushDemoApp/iOS/CodePushDemoApp/Info.plist b/Examples/CodePushDemoApp/iOS/CodePushDemoApp/Info.plist index 6cb683d..e1033b9 100644 --- a/Examples/CodePushDemoApp/iOS/CodePushDemoApp/Info.plist +++ b/Examples/CodePushDemoApp/iOS/CodePushDemoApp/Info.plist @@ -44,6 +44,6 @@ NSLocationWhenInUseUsageDescription CodePushDeploymentKey - 5c73310a-cc93-4aa5-bf9f-81c6b648232c + deployment-key-here diff --git a/Examples/CodePushDemoApp/index.android.js b/Examples/CodePushDemoApp/index.android.js index f682949..d946285 100644 --- a/Examples/CodePushDemoApp/index.android.js +++ b/Examples/CodePushDemoApp/index.android.js @@ -1 +1 @@ -require("./crossplatformdemo.js"); \ No newline at end of file +import "./crossplatformdemo.js"; \ No newline at end of file diff --git a/Examples/CodePushDemoApp/index.ios.js b/Examples/CodePushDemoApp/index.ios.js index f682949..d946285 100644 --- a/Examples/CodePushDemoApp/index.ios.js +++ b/Examples/CodePushDemoApp/index.ios.js @@ -1 +1 @@ -require("./crossplatformdemo.js"); \ No newline at end of file +import "./crossplatformdemo.js"; \ No newline at end of file diff --git a/package-mixins.js b/package-mixins.js index d539ab9..a74ee11 100644 --- a/package-mixins.js +++ b/package-mixins.js @@ -3,18 +3,14 @@ import { DeviceEventEmitter } from "react-native"; // This function is used to augment remote and local // package objects with additional functionality/properties // beyond what is included in the metadata sent by the server. -module.exports = (NativeCodePush) => { +export default (NativeCodePush) => { const remote = { - abortDownload() { - return NativeCodePush.abortDownload(this); - }, - - download(downloadProgressCallback) { + async download(downloadProgressCallback) { if (!this.downloadUrl) { - return Promise.reject(new Error("Cannot download an update without a download url")); + throw new Error("Cannot download an update without a download url"); } - var downloadProgressSubscription; + let downloadProgressSubscription; if (downloadProgressCallback) { // Use event subscription to obtain download progress. downloadProgressSubscription = DeviceEventEmitter.addListener( @@ -25,33 +21,28 @@ module.exports = (NativeCodePush) => { // Use the downloaded package info. Native code will save the package info // so that the client knows what the current package version is. - return NativeCodePush.downloadUpdate(this) - .then((downloadedPackage) => { - downloadProgressSubscription && downloadProgressSubscription.remove(); - return Object.assign({}, downloadedPackage, local); - }) - .catch((error) => { - downloadProgressSubscription && downloadProgressSubscription.remove(); - // Rethrow the error for subsequent handlers down the promise chain. - throw error; - }); + try { + let downloadedPackage = await NativeCodePush.downloadUpdate(this); + downloadProgressSubscription && downloadProgressSubscription.remove(); + return Object.assign({}, downloadedPackage, local); + } finally { + downloadProgressSubscription && downloadProgressSubscription.remove(); + } }, isPending: false // A remote package could never be in a pending state }; const local = { - install(installMode = NativeCodePush.codePushInstallModeOnNextRestart, updateInstalledCallback) { + async install(installMode = NativeCodePush.codePushInstallModeOnNextRestart, updateInstalledCallback) { let localPackage = this; - return NativeCodePush.installUpdate(this, installMode) - .then(() => { - updateInstalledCallback && updateInstalledCallback(); - if (installMode == NativeCodePush.codePushInstallModeImmediate) { - NativeCodePush.restartApp(); - } else { - localPackage.isPending = true; // Mark the package as pending since it hasn't been applied yet - } - }); + await NativeCodePush.installUpdate(this, installMode); + updateInstalledCallback && updateInstalledCallback(); + if (installMode == NativeCodePush.codePushInstallModeImmediate) { + NativeCodePush.restartApp(); + } else { + localPackage.isPending = true; // Mark the package as pending since it hasn't been applied yet + } }, isPending: false // A local package wouldn't be pending until it was installed