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

Commit 36e607c

Browse files
authored
Merge pull request #36 from Clovel/fix/syncCallback
Implemented syncCallbacks for CodePush.sync
2 parents ef4d990 + 3e34394 commit 36e607c

File tree

9 files changed

+381
-333
lines changed

9 files changed

+381
-333
lines changed

README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ While the `sync` method tries to make it easy to perform silent and active updat
415415

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

418+
- __onSyncStatusChanged__ *(`SuccessCallback<SyncStatus>`)* - A custom callback that is called when the `SyncStatus` changes.
419+
420+
- __onSyncError__ *(`ErrorCallback`)* - A custom callback that is called on a sync process error.
421+
418422
Example Usage:
419423

420424
```javascript
@@ -441,26 +445,29 @@ codePush.sync({
441445
installMode: InstallMode.IMMEDIATE
442446
});
443447

444-
// Silently check for the update, but
445-
// display a custom downloading UI
446-
// via the SyncStatus and DownloadProgress callbacks
447-
codePush.sync(null, downloadProgress).then(status => {
448-
switch (status) {
449-
case SyncStatus.DOWNLOADING_PACKAGE:
450-
// Show "downloading" modal
451-
break;
452-
case SyncStatus.INSTALLING_UPDATE:
453-
// Hide "downloading" modal
454-
break;
455-
}
456-
});
457-
458-
function downloadProgress(downloadProgress) {
448+
const downloadProgress = (downloadProgress) => {
459449
if (downloadProgress) {
460450
// Update "downloading" modal with current download %
461451
//console.log("Downloading " + downloadProgress.receivedBytes + " of " + downloadProgress.totalBytes);
462452
}
463453
}
454+
455+
// Silently check for the update, but
456+
// display a custom downloading UI
457+
// via the SyncStatus and DownloadProgress callbacks
458+
codePush.sync(null, downloadProgress)
459+
.then(
460+
(status) => {
461+
switch (status) {
462+
case SyncStatus.DOWNLOADING_PACKAGE:
463+
// Show "downloading" modal
464+
break;
465+
case SyncStatus.INSTALLING_UPDATE:
466+
// Hide "downloading" modal
467+
break;
468+
}
469+
}
470+
);
464471
```
465472

466473
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.

dist/esm/codePush.d.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface CodePushCapacitorPlugin {
1111
getCurrentPackage(): Promise<ILocalPackage>;
1212
/**
1313
* Gets the pending package information, if any. A pending package is one that has been installed but the application still runs the old code.
14-
* 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.
14+
* 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.
1515
*/
1616
getPendingPackage(): Promise<ILocalPackage>;
1717
/**
@@ -102,7 +102,7 @@ declare class CodePush implements CodePushCapacitorPlugin {
102102
getCurrentPackage(): Promise<ILocalPackage>;
103103
/**
104104
* Gets the pending package information, if any. A pending package is one that has been installed but the application still runs the old code.
105-
* 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.
105+
* 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.
106106
*/
107107
getPendingPackage(): Promise<ILocalPackage>;
108108
/**
@@ -131,14 +131,10 @@ declare class CodePush implements CodePushCapacitorPlugin {
131131
* - If no update is available on the server, the syncCallback will be invoked with the SyncStatus.UP_TO_DATE.
132132
* - If an error occurs during checking for update, downloading or installing it, the syncCallback will be invoked with the SyncStatus.ERROR.
133133
*
134-
* @param syncCallback Optional callback to be called with the status of the sync operation.
135-
* The callback will be called only once, and the possible statuses are defined by the SyncStatus enum.
136134
* @param syncOptions Optional SyncOptions parameter configuring the behavior of the sync operation.
137135
* @param downloadProgress Optional callback invoked during the download process. It is called several times with one DownloadProgress parameter.
138-
* @param syncErrback Optional errback invoked if an error occurs. The callback will be called only once
139-
*
140136
*/
141-
sync(syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): Promise<any>;
137+
sync(syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): Promise<SyncStatus>;
142138
/**
143139
* Convenience method for installing updates in one method call.
144140
* 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.

0 commit comments

Comments
 (0)