Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Add binaryUpdateCallback to checkForUpdate & sync methods #669

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ Additionally, the following objects and enums are also exposed globally as part
### codePush.checkForUpdate

```javascript
codePush.checkForUpdate(onSuccess, onError?, deploymentKey?: String);
codePush.checkForUpdate(onSuccess, onError?, deploymentKey?, onBinaryUpdate?);
```

Queries the CodePush service to see whether the configured app deployment has an update available. By default, it will use the deployment key that is configured in your `config.xml` file, but you can override that by specifying a value via the optional `deploymentKey` parameter. This can be useful when you want to dynamically "redirect" a user to a specific deployment, such as allowing "Early access" via an easter egg or a user setting switch.

When the update check completes, it will trigger the `onUpdateCheck` callback with one of two possible values:
When the update check completes, it will trigger the `onSuccess` callback with one of two possible values:

1. `null` if there is no update available. This occurs in the following scenarios:

Expand Down Expand Up @@ -277,6 +277,8 @@ codePush.checkForUpdate(function (update) {
});
```

- __onBinaryUpdate__: Optional callback that is invoked when there is an update available that is targeting a newer binary version than you are currently running.

### codePush.getCurrentPackage

```javascript
Expand Down Expand Up @@ -381,7 +383,7 @@ Immediately restarts the app. This method is for advanced scenarios, and is prim
### codePush.sync

```javascript
codePush.sync(syncCallback?, syncOptions?, downloadProgress?, syncErrback?);
codePush.sync(syncCallback?, syncOptions?, downloadProgress?, syncErrback?, onBinaryUpdate?);
```

Synchronizes your app's code and images with the latest release to the configured deployment. Unlike the `checkForUpdate` method, which simply checks for the presence of an update, and let's you control what to do next, `sync` handles the update check, download and installation experience for you.
Expand Down Expand Up @@ -420,6 +422,10 @@ While the sync method tries to make it easy to perform silent and active updates

- __receivedBytes__ *(Number)* - The number of bytes downloaded thus far, which can be used to track download progress.

- __syncErrback__: Optional callback that is invoked in the event of an error. The callback takes one error parameter, containing the details of the error.

- __onBinaryUpdate__: Optional callback that is invoked when there is an update available that is targeting a newer binary version than you are currently running.

#### SyncOptions

While the `sync` method tries to make it easy to perform silent and active updates with little configuration, it accepts an "options" object that allows you to customize numerous aspects of the default behavior mentioned above:
Expand Down
11 changes: 6 additions & 5 deletions bin/www/codePush.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var CodePush = (function () {
}
});
};
CodePush.prototype.checkForUpdate = function (querySuccess, queryError, deploymentKey) {
CodePush.prototype.checkForUpdate = function (querySuccess, queryError, deploymentKey, binaryUpdateCallback) {
try {
var callback = function (error, remotePackageOrUpdateNotification) {
if (error) {
Expand All @@ -96,6 +96,7 @@ var CodePush = (function () {
if (remotePackageOrUpdateNotification) {
if (remotePackageOrUpdateNotification.updateAppVersion) {
CodePushUtil.logMessage("An update is available, but it is targeting a newer binary version than you are currently running.");
binaryUpdateCallback && binaryUpdateCallback(remotePackageOrUpdateNotification);
appUpToDate();
}
else {
Expand Down Expand Up @@ -160,7 +161,7 @@ var CodePush = (function () {
CodePushUtil.invokeErrorCallback(new Error("An error occurred while querying for updates." + CodePushUtil.getErrorMessage(e)), queryError);
}
};
CodePush.prototype.sync = function (syncCallback, syncOptions, downloadProgress, syncErrback) {
CodePush.prototype.sync = function (syncCallback, syncOptions, downloadProgress, syncErrback, binaryUpdateCallback) {
if (CodePush.SyncInProgress) {
CodePushUtil.logMessage("Sync already in progress.");
syncCallback && syncCallback(SyncStatus.IN_PROGRESS);
Expand All @@ -183,10 +184,10 @@ var CodePush = (function () {
syncCallback && syncCallback(result);
};
CodePush.SyncInProgress = true;
this.syncInternal(syncCallbackAndUpdateSyncInProgress, syncOptions, downloadProgress);
this.syncInternal(syncCallbackAndUpdateSyncInProgress, syncOptions, downloadProgress, binaryUpdateCallback);
}
};
CodePush.prototype.syncInternal = function (syncCallback, syncOptions, downloadProgress) {
CodePush.prototype.syncInternal = function (syncCallback, syncOptions, downloadProgress, onBinaryUpdate) {
if (!syncOptions) {
syncOptions = this.getDefaultSyncOptions();
}
Expand Down Expand Up @@ -276,7 +277,7 @@ var CodePush = (function () {
}
};
syncCallback && syncCallback(null, SyncStatus.CHECKING_FOR_UPDATE);
window.codePush.checkForUpdate(onUpdate, onError, syncOptions.deploymentKey);
window.codePush.checkForUpdate(onUpdate, onError, syncOptions.deploymentKey, onBinaryUpdate);
};
CodePush.prototype.getDefaultSyncOptions = function () {
if (!CodePush.DefaultSyncOptions) {
Expand Down
8 changes: 5 additions & 3 deletions typings/codePush.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ interface CodePushCordovaPlugin {
* A null package means the application is up to date for the current native application version.
* @param queryError Optional callback invoked in case of an error.
* @param deploymentKey Optional deployment key that overrides the config.xml setting.
* @param binaryUpdateCallback Optional callback invoked when there is an update available, but it is targeting a newer binary version than you are currently running.
*/
checkForUpdate(querySuccess: SuccessCallback<IRemotePackage>, queryError?: ErrorCallback, deploymentKey?: string): void;
checkForUpdate(querySuccess: SuccessCallback<IRemotePackage>, queryError?: ErrorCallback, deploymentKey?: string, binaryUpdateCallback?: SuccessCallback<NativeUpdateNotification>): void;

/**
* Notifies the plugin that the update operation succeeded and that the application is ready.
Expand Down Expand Up @@ -301,9 +302,10 @@ interface CodePushCordovaPlugin {
* The callback will be called only once, and the possible statuses are defined by the SyncStatus enum.
* @param syncOptions Optional SyncOptions parameter configuring the behavior of the sync operation.
* @param downloadProgress Optional callback invoked during the download process. It is called several times with one DownloadProgress parameter.
*
* @param syncErrback Optional errback invoked if an error occurs. The callback will be called only once
* @param binaryUpdateCallback Optional callback invoked when there is an update available, but it is targeting a newer binary version than you are currently running.
*/
sync(syncCallback?: SuccessCallback<SyncStatus>, syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): void;
sync(syncCallback?: SuccessCallback<SyncStatus>, syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>, syncErrback?: ErrorCallback, binaryUpdateCallback?: SuccessCallback<NativeUpdateNotification>): void;
}

/**
Expand Down
18 changes: 10 additions & 8 deletions www/codePush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ class CodePush implements CodePushCordovaPlugin {
* A null package means the application is up to date for the current native application version.
* @param queryError Optional callback invoked in case of an error.
* @param deploymentKey Optional deployment key that overrides the config.xml setting.
* @param binaryUpdateCallback Optional callback invoked when there is an update available that is targeting a newer binary version than you are currently running.
*/
public checkForUpdate(querySuccess: SuccessCallback<RemotePackage>, queryError?: ErrorCallback, deploymentKey?: string): void {
public checkForUpdate(querySuccess: SuccessCallback<RemotePackage>, queryError?: ErrorCallback, deploymentKey?: string, binaryUpdateCallback?: SuccessCallback<NativeUpdateNotification>): void {
try {
var callback: Callback<RemotePackage | NativeUpdateNotification> = (error: Error, remotePackageOrUpdateNotification: IRemotePackage | NativeUpdateNotification) => {
if (error) {
Expand All @@ -162,8 +163,9 @@ class CodePush implements CodePushCordovaPlugin {

if (remotePackageOrUpdateNotification) {
if ((<NativeUpdateNotification>remotePackageOrUpdateNotification).updateAppVersion) {
/* There is an update available for a different version. In the current version of the plugin, we treat that as no update. */
/* There is an update available for a different version. */
CodePushUtil.logMessage("An update is available, but it is targeting a newer binary version than you are currently running.");
binaryUpdateCallback && binaryUpdateCallback(<NativeUpdateNotification>remotePackageOrUpdateNotification);
appUpToDate();
} else {
/* There is an update available for the current version. */
Expand Down Expand Up @@ -248,9 +250,9 @@ class CodePush implements CodePushCordovaPlugin {
* @param syncOptions Optional SyncOptions parameter configuring the behavior of the sync operation.
* @param downloadProgress Optional callback invoked during the download process. It is called several times with one DownloadProgress parameter.
* @param syncErrback Optional errback invoked if an error occurs. The callback will be called only once
*
* @param binaryUpdateCallback Optional callback invoked when there is an update available that is targeting a newer binary version than you are currently running.
*/
public sync(syncCallback?: SuccessCallback<any>, syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>, syncErrback?: ErrorCallback): void {
public sync(syncCallback?: SuccessCallback<any>, syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>, syncErrback?: ErrorCallback, binaryUpdateCallback?: SuccessCallback<NativeUpdateNotification>): void {
/* Check if a sync is already in progress */
if (CodePush.SyncInProgress) {
/* A sync is already in progress */
Expand Down Expand Up @@ -285,7 +287,7 @@ class CodePush implements CodePushCordovaPlugin {

/* Begin the sync */
CodePush.SyncInProgress = true;
this.syncInternal(syncCallbackAndUpdateSyncInProgress, syncOptions, downloadProgress);
this.syncInternal(syncCallbackAndUpdateSyncInProgress, syncOptions, downloadProgress, binaryUpdateCallback);
}
}

Expand All @@ -299,9 +301,9 @@ class CodePush implements CodePushCordovaPlugin {
* The callback will be called only once, and the possible statuses are defined by the SyncStatus enum.
* @param syncOptions Optional SyncOptions parameter configuring the behavior of the sync operation.
* @param downloadProgress Optional callback invoked during the download process. It is called several times with one DownloadProgress parameter.
*
* @param onBinaryUpdate Optional callback invoked when there is an update available, but it is targeting a newer binary version than you are currently running.
*/
private syncInternal(syncCallback?: Callback<any>, syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): void {
private syncInternal(syncCallback?: Callback<any>, syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>, onBinaryUpdate?: SuccessCallback<NativeUpdateNotification>): void {

/* No options were specified, use default */
if (!syncOptions) {
Expand Down Expand Up @@ -410,7 +412,7 @@ class CodePush implements CodePushCordovaPlugin {
};

syncCallback && syncCallback(null, SyncStatus.CHECKING_FOR_UPDATE);
window.codePush.checkForUpdate(onUpdate, onError, syncOptions.deploymentKey);
window.codePush.checkForUpdate(onUpdate, onError, syncOptions.deploymentKey, onBinaryUpdate);
}

/**
Expand Down