From ae72dd05a8b1139f4147a4b20aa0dfb4ad764a0c Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Wed, 18 Nov 2015 12:01:12 -0800 Subject: [PATCH] merge syncstatus and syncresult --- CodePush.ios.js | 47 +++++++++++++++++++-------- Examples/CodePushDemoApp/index.ios.js | 35 ++++++++++++-------- README.md | 21 +++++++----- 3 files changed, 66 insertions(+), 37 deletions(-) diff --git a/CodePush.ios.js b/CodePush.ios.js index 06fad49..ad593f3 100644 --- a/CodePush.ios.js +++ b/CodePush.ios.js @@ -158,8 +158,25 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) case CodePush.SyncStatus.INSTALLING_UPDATE: log("Installing update."); break; - case CodePush.SyncStatus.IDLE: - log("Sync is idle."); + case CodePush.SyncStatus.UP_TO_DATE: + log("App is up to date."); + break; + case CodePush.SyncStatus.UPDATE_IGNORED: + log("User cancelled the update."); + break; + case CodePush.SyncStatus.UPDATE_INSTALLED: + /* + * If the install mode is IMMEDIATE, this will not get returned as the + * app will be restarted to a new Javascript context. + */ + if (syncOptions.installMode == CodePush.InstallMode.ON_NEXT_RESTART) { + log("Update is installed and will be run on the next app restart."); + } else { + log("Update is installed and will be run when the app next resumes."); + } + break; + case CodePush.SyncStatus.UNKNOWN_ERROR: + log("An unknown error occurred."); break; } }; @@ -182,16 +199,16 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) return localPackage.install(syncOptions.rollbackTimeout, syncOptions.installMode) }) .then(() => { - syncStatusChangeCallback(CodePush.SyncStatus.IDLE); - resolve(CodePush.SyncResult.UPDATE_INSTALLED) + syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_INSTALLED); + resolve(CodePush.SyncStatus.UPDATE_INSTALLED) }) .catch(reject) .done(); } if (!remotePackage || (remotePackage.failedInstall && syncOptions.ignoreFailedUpdates)) { - syncStatusChangeCallback(CodePush.SyncStatus.IDLE); - resolve(CodePush.SyncResult.UP_TO_DATE); + syncStatusChangeCallback(CodePush.SyncStatus.UP_TO_DATE); + resolve(CodePush.SyncStatus.UP_TO_DATE); } else if (syncOptions.updateDialog) { syncOptions.updateDialog = Object.assign(CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog); @@ -217,7 +234,7 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) // to allow the end-user to ignore it dialogButtons.push({ text: syncOptions.updateDialog.optionalIgnoreButtonLabel, - onPress: () => resolve(CodePush.SyncResult.UPDATE_IGNORED) + onPress: () => resolve(CodePush.SyncStatus.UPDATE_IGNORED) }); } @@ -233,7 +250,10 @@ function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) doDownloadAndInstall(); } }) - .catch(reject) + .catch((error) => { + syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR); + reject(error); + }) .done(); }); }; @@ -242,6 +262,7 @@ var CodePush = { checkForUpdate: checkForUpdate, getConfiguration: getConfiguration, getCurrentPackage: getCurrentPackage, + log: log, notifyApplicationReady: NativeCodePush.notifyApplicationReady, setUpTestDependencies: setUpTestDependencies, sync: sync, @@ -250,17 +271,15 @@ var CodePush = { ON_NEXT_RESTART: NativeCodePush.codePushInstallModeOnNextRestart, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart ON_NEXT_RESUME: NativeCodePush.codePushInstallModeOnNextResume // Restart the app the next time it is resumed from the background }, - SyncResult: { - UP_TO_DATE: 0, // The running app is up-to-date - UPDATE_IGNORED: 1, // The app had an optional update and the end-user chose to ignore it - UPDATE_INSTALLED: 2 // The app had an optional/mandatory update that was successfully downloaded and is about to be installed. - }, SyncStatus: { CHECKING_FOR_UPDATE: 0, AWAITING_USER_ACTION: 1, DOWNLOADING_PACKAGE: 2, INSTALLING_UPDATE: 3, - IDLE: 4 + UP_TO_DATE: 4, // The running app is up-to-date + UPDATE_IGNORED: 5, // The app had an optional update and the end-user chose to ignore it + UPDATE_INSTALLED: 6, // The app had an optional/mandatory update that was successfully downloaded and is about to be installed. + UNKNOWN_ERROR: -1 }, DEFAULT_UPDATE_DIALOG: { appendReleaseDescription: false, diff --git a/Examples/CodePushDemoApp/index.ios.js b/Examples/CodePushDemoApp/index.ios.js index 54f125c..e705f24 100644 --- a/Examples/CodePushDemoApp/index.ios.js +++ b/Examples/CodePushDemoApp/index.ios.js @@ -50,12 +50,30 @@ var CodePushDemoApp = React.createClass({ syncMessage: "Installing update." }); break; - case CodePush.SyncStatus.IDLE: + 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; } }, function(progress) { @@ -63,19 +81,8 @@ var CodePushDemoApp = React.createClass({ progress: progress }); } - ).then(function(syncResult) { - switch(syncResult) { - case CodePush.SyncResult.UP_TO_DATE: - self.setState({ - syncMessage: "App up to date." - }); - break; - case CodePush.SyncResult.UPDATE_IGNORED: - self.setState({ - syncMessage: "Update cancelled by user." - }); - break; - } + ).catch(function(error) { + CodePush.log(error); }); }, getInitialState: function() { diff --git a/README.md b/README.md index 7209050..196863e 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ If the `rollbackTimeout` parameter was not specified, the CodePush runtime will #### codePush.sync ```javascript -codePush.sync(options: Object, onSyncStatusChange: function(syncStatus: Number), onDownloadProgress: function(progress: DownloadProgress)): Promise; +codePush.sync(options: Object, syncStatusChangeCallback: function(syncStatus: Number), downloadProgressCallback: function(progress: DownloadProgress)): Promise; ``` Provides a simple option for checking for an update, displaying a notification to the user, downloading it and then installing it, all while also respecting the policy that your release was published with. This method effectively composes together the "advanced mode" APIs for you, so that you don't need to handle any of the following scenarios yourself: @@ -204,22 +204,25 @@ The method accepts an options object that allows you to customize numerous aspec In addition, the method also recieves two function arguments which serve as event handlers which are called at various points in the sync process: -* __onSyncStatusChange__ (function(syncStatus: Number)) - Called when the sync process moves to a different step. Below is the list of possible SyncStatus values: +* __syncStatusChangeCallback__ (function(syncStatus: Number)) - Called when the sync process moves to a different step. Below is the list of possible SyncStatus values: * __CodePush.SyncStatus.CHECKING_FOR_UPDATE__ *(0)* - Querying the CodePush server for an update. * __CodePush.SyncStatus.AWAITING_USER_ACTION__ *(1)* - Waiting for a response from the user (e.g. a confirmation dialog). * __CodePush.SyncStatus.DOWNLOADING_PACKAGE__ *(2)* - Downloading the updated package from the CodePush server. - * __CodePush.SyncStatus.INSTALLING_UPDATE__ *(3)* - Installing the downloaded update package. - * __CodePush.SyncStatus.IDLE__ *(4)* - The sync process has exited and is now idling. + * __CodePush.SyncStatus.INSTALLING_UPDATE__ *(3)* - The app had an optional or mandatory update that was successfully downloaded and is about to be installed. + * __CodePush.SyncStatus.UP_TO_DATE__ *(4)* - The app does not have an available update. + * __CodePush.SyncStatus.UPDATE_IGNORED__ *(5)* - The app has an optional update, that the user chose to ignore. + * __CodePush.SyncStatus.UPDATE_INSTALLED__ *(6)* - The update has been installed and will be run the next time the app resumes/restarts, depending on the `InstallMode` specified in `SyncOptions`. + * __CodePush.SyncStatus.UNKNOWN_ERROR__ *(-1)* - The sync operation encountered an unknown error. -* __onDownloadProgress__ (function(progress: DownloadProgress)) - Called periodically when the update package is being downloaded from the CodePush server to report the progress of the update. `DownloadProgress` contains two fields: +* __downloadProgressCallback__ (function(progress: DownloadProgress)) - Called periodically when the update package is being downloaded from the CodePush server to report the progress of the update. `DownloadProgress` contains two fields: * __totalBytes__ (Number) - The total number of bytes expected to be received for this update package * __receivedBytes__ (Number) - The number of bytes downloaded thus far. -The method returns a `Promise` that is resolved to a `SyncResult` integer code, which indicates why the `sync` call succeeded. This code can be one of the following values: +The method returns a `Promise` that is resolved to a `SyncStatus` integer code, which indicates why the `sync` call succeeded. This code can be one of the following values: -* __CodePush.SyncResult.UP_TO_DATE__ *(0)* - The app doesn't have an available update. -* __CodePush.SyncResult.UPDATE_IGNORED__ *(1)* - The app has an optional update, that the user chose to ignore. -* __CodePush.SyncResult.UPDATE_INSTALLED__ *(2)* - The app had an optional or mandatory update that was successfully downloaded and is about to be installed. If your app needs to do any data persistence/migration before restarting, this is the time to do it. + * __CodePush.SyncStatus.UP_TO_DATE__ *(4)* - The app does not have an available update. + * __CodePush.SyncStatus.UPDATE_IGNORED__ *(5)* - The app has an optional update, that the user chose to ignore. + * __CodePush.SyncStatus.UPDATE_INSTALLED__ *(6)* - The update has been installed and will be run the next time the app resumes/restarts, depending on the `InstallMode` specified in `SyncOptions`. If the update check and/or the subseqeuent download fails for any reason, the `Promise` object returned by `sync` will be rejected with the reason.