From de869257ea3caf4efbd6cea1c814db74d300274d Mon Sep 17 00:00:00 2001 From: Clovis Durand Date: Tue, 15 Jun 2021 15:13:30 +0200 Subject: [PATCH] Fixed CodePush.sync's return type Signed-off-by: Clovis Durand --- src/codePush.ts | 92 +++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/src/codePush.ts b/src/codePush.ts index 14088983..9a268903 100644 --- a/src/codePush.ts +++ b/src/codePush.ts @@ -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): SyncStatus | Promise; + sync(syncOptions?: SyncOptions, downloadProgress?: SuccessCallback): Promise; } /** @@ -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): SyncStatus | Promise { - /* 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 = (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): Promise { + 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 = (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, + ); + } + ); } /**