Skip to content

Commit

Permalink
Fixed CodePush.sync's return type
Browse files Browse the repository at this point in the history
Signed-off-by: Clovis Durand <[email protected]>
  • Loading branch information
Clovel committed Jul 8, 2021
1 parent 27be0fd commit de86925
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions src/codePush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ interface CodePushCapacitorPlugin {
* @returns The status of the sync operation. The possible statuses are defined by the SyncStatus enum.
*
*/
sync(syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): SyncStatus | Promise<SyncStatus>;
sync(syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): Promise<SyncStatus>;
}

/**
Expand Down Expand Up @@ -295,52 +295,54 @@ class CodePush implements CodePushCapacitorPlugin {
* @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.
*/
public sync(syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): SyncStatus | Promise<SyncStatus> {
/* Check if a sync is already in progress */
if (CodePush.SyncInProgress) {
/* A sync is already in progress */
CodePushUtil.logMessage("Sync already in progress.");
return SyncStatus.IN_PROGRESS;
}

return new Promise((resolve, reject) => {
/* Create a callback that resets the SyncInProgress flag when the sync is complete
* If the sync status is a result status, then the sync must be complete and the flag must be updated
* Otherwise, do not change the flag and trigger the syncCallback as usual
*/
const syncCallbackAndUpdateSyncInProgress: Callback<SyncStatus> = (err: Error | null, result: SyncStatus | null): void => {
if (err) {
syncOptions.onSyncError && syncOptions.onSyncError(err);
reject(err);
} else {
/* Call the user's callback */
syncOptions.onSyncStatusChanged && syncOptions.onSyncStatusChanged(result);

/* Check if the sync operation is over */
switch (result) {
case SyncStatus.ERROR:
case SyncStatus.UP_TO_DATE:
case SyncStatus.UPDATE_IGNORED:
case SyncStatus.UPDATE_INSTALLED:
/* The sync has completed */
CodePush.SyncInProgress = false;
resolve(result);
break;
default:
/* The sync is not yet complete, so do nothing */
break;
}
public async sync(syncOptions?: SyncOptions, downloadProgress?: SuccessCallback<DownloadProgress>): Promise<SyncStatus> {
return await new Promise(
(resolve, reject) => {
/* Check if a sync is already in progress */
if (CodePush.SyncInProgress) {
/* A sync is already in progress */
CodePushUtil.logMessage("Sync already in progress.");
resolve(SyncStatus.IN_PROGRESS);
}
};

/* Begin the sync */
CodePush.SyncInProgress = true;
this.syncInternal(
syncCallbackAndUpdateSyncInProgress,
syncOptions,
downloadProgress,
);
});
/* Create a callback that resets the SyncInProgress flag when the sync is complete
* If the sync status is a result status, then the sync must be complete and the flag must be updated
* Otherwise, do not change the flag and trigger the syncCallback as usual
*/
const syncCallbackAndUpdateSyncInProgress: Callback<SyncStatus> = (err: Error | null, result: SyncStatus | null): void => {
if (err) {
syncOptions.onSyncError && syncOptions.onSyncError(err);
reject(err);
} else {
/* Call the user's callback */
syncOptions.onSyncStatusChanged && syncOptions.onSyncStatusChanged(result);

/* Check if the sync operation is over */
switch (result) {
case SyncStatus.ERROR:
case SyncStatus.UP_TO_DATE:
case SyncStatus.UPDATE_IGNORED:
case SyncStatus.UPDATE_INSTALLED:
/* The sync has completed */
CodePush.SyncInProgress = false;
resolve(result);
break;
default:
/* The sync is not yet complete, so do nothing */
break;
}
}
};

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

/**
Expand Down

0 comments on commit de86925

Please sign in to comment.