Skip to content

Commit

Permalink
Merge pull request #36 from Clovel/fix/syncCallback
Browse files Browse the repository at this point in the history
Implemented syncCallbacks for CodePush.sync
  • Loading branch information
leo6104 authored Jul 8, 2021
2 parents ef4d990 + 3e34394 commit 36e607c
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 333 deletions.
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ While the `sync` method tries to make it easy to perform silent and active updat

- __updateTitle__ *(String)* - The text used as the header of an update notification that is displayed to the end user. Defaults to `"Update available"`.

- __onSyncStatusChanged__ *(`SuccessCallback<SyncStatus>`)* - A custom callback that is called when the `SyncStatus` changes.

- __onSyncError__ *(`ErrorCallback`)* - A custom callback that is called on a sync process error.

Example Usage:

```javascript
Expand All @@ -441,26 +445,29 @@ codePush.sync({
installMode: InstallMode.IMMEDIATE
});

// Silently check for the update, but
// display a custom downloading UI
// via the SyncStatus and DownloadProgress callbacks
codePush.sync(null, downloadProgress).then(status => {
switch (status) {
case SyncStatus.DOWNLOADING_PACKAGE:
// Show "downloading" modal
break;
case SyncStatus.INSTALLING_UPDATE:
// Hide "downloading" modal
break;
}
});

function downloadProgress(downloadProgress) {
const downloadProgress = (downloadProgress) => {
if (downloadProgress) {
// Update "downloading" modal with current download %
//console.log("Downloading " + downloadProgress.receivedBytes + " of " + downloadProgress.totalBytes);
}
}

// Silently check for the update, but
// display a custom downloading UI
// via the SyncStatus and DownloadProgress callbacks
codePush.sync(null, downloadProgress)
.then(
(status) => {
switch (status) {
case SyncStatus.DOWNLOADING_PACKAGE:
// Show "downloading" modal
break;
case SyncStatus.INSTALLING_UPDATE:
// Hide "downloading" modal
break;
}
}
);
```

The `sync` method can be called anywhere you'd like to check for an update. That could be in the `deviceready` event handler, the `click` event of a button, in the callback of a periodic timer, or whatever else makes sense for your needs. Just like the `checkForUpdate` method, it will perform the network request to check for an update in the background, so it won't impact your UI thread and/or JavaScript thread's responsiveness.
Expand Down
10 changes: 3 additions & 7 deletions dist/esm/codePush.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface CodePushCapacitorPlugin {
getCurrentPackage(): Promise<ILocalPackage>;
/**
* Gets the pending package information, if any. A pending package is one that has been installed but the application still runs the old code.
* This happends only after a package has been installed using ON_NEXT_RESTART or ON_NEXT_RESUME mode, but the application was not restarted/resumed yet.
* This happens only after a package has been installed using ON_NEXT_RESTART or ON_NEXT_RESUME mode, but the application was not restarted/resumed yet.
*/
getPendingPackage(): Promise<ILocalPackage>;
/**
Expand Down Expand Up @@ -102,7 +102,7 @@ declare class CodePush implements CodePushCapacitorPlugin {
getCurrentPackage(): Promise<ILocalPackage>;
/**
* Gets the pending package information, if any. A pending package is one that has been installed but the application still runs the old code.
* This happends only after a package has been installed using ON_NEXT_RESTART or ON_NEXT_RESUME mode, but the application was not restarted/resumed yet.
* This happens only after a package has been installed using ON_NEXT_RESTART or ON_NEXT_RESUME mode, but the application was not restarted/resumed yet.
*/
getPendingPackage(): Promise<ILocalPackage>;
/**
Expand Down Expand Up @@ -131,14 +131,10 @@ declare class CodePush implements CodePushCapacitorPlugin {
* - If no update is available on the server, the syncCallback will be invoked with the SyncStatus.UP_TO_DATE.
* - If an error occurs during checking for update, downloading or installing it, the syncCallback will be invoked with the SyncStatus.ERROR.
*
* @param syncCallback Optional callback to be called with the status of the sync operation.
* 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
*
*/
sync(syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): Promise<any>;
sync(syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): Promise<SyncStatus>;
/**
* Convenience method for installing updates in one method call.
* This method is provided for simplicity, and its behavior can be replicated by using window.codePush.checkForUpdate(), RemotePackage's download() and LocalPackage's install() methods.
Expand Down
Loading

0 comments on commit 36e607c

Please sign in to comment.