sync: install update even if an error occurs in callbacks (#787)

Wrap up syncStatusChangeCallback and downloadProgressCallback methods with try/catch block to make it possible to install CodePush updates even if an error occurs while trying to run syncStatusChangeCallback or downloadProgressCallback methods
This commit is contained in:
Sergey Akhalkov
2017-04-27 23:34:44 +03:00
committed by Patrick NIkoletich
parent 5ece19bc93
commit 2cd2ef0ca2

View File

@@ -238,15 +238,36 @@ const sync = (() => {
const setSyncCompleted = () => { syncInProgress = false; };
return (options = {}, syncStatusChangeCallback, downloadProgressCallback) => {
let syncStatusCallbackWithTryCatch, downloadProgressCallbackkWithTryCatch;
if (typeof syncStatusChangeCallback === "function") {
syncStatusCallbackWithTryCatch = (...args) => {
try {
syncStatusChangeCallback(...args);
} catch (error) {
log(`An error has occurred : ${error.stack}`);
}
}
}
if (typeof downloadProgressCallback === "function") {
downloadProgressCallbackkWithTryCatch = (...args) => {
try {
downloadProgressCallback(...args);
} catch (error) {
log(`An error has occurred: ${error.stack}`);
}
}
}
if (syncInProgress) {
typeof syncStatusChangeCallback === "function"
? syncStatusChangeCallback(CodePush.SyncStatus.SYNC_IN_PROGRESS)
typeof syncStatusCallbackWithTryCatch === "function"
? syncStatusCallbackWithTryCatch(CodePush.SyncStatus.SYNC_IN_PROGRESS)
: log("Sync already in progress.");
return Promise.resolve(CodePush.SyncStatus.SYNC_IN_PROGRESS);
}
syncInProgress = true;
const syncPromise = syncInternal(options, syncStatusChangeCallback, downloadProgressCallback);
const syncPromise = syncInternal(options, syncStatusCallbackWithTryCatch, downloadProgressCallbackkWithTryCatch);
syncPromise
.then(setSyncCompleted)
.catch(setSyncCompleted);