From: Mike Frysinger Date: Sun, 10 Dec 2023 03:32:07 +0000 (-0700) Subject: change deleteBackup to promise/async X-Git-Tag: v1.0~4 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=bc29ea6cfeeac45c9ec0816e7f8a67767dc2f736;p=chrome-ext%2Ftabs-backup.git change deleteBackup to promise/async --- diff --git a/advanced.js b/advanced.js index 62f332c..91e4843 100644 --- a/advanced.js +++ b/advanced.js @@ -297,29 +297,6 @@ function insertBackupItem (backupName, backupObj, insertAtBeginning, doAnimation // }; //})(backupName); - //var deleteFuncHandler = (function(backupName, elem) { - // return function(event) { - /*if (!confirm("Delete backup '" + backupName + "'?")) { - return; - }*/ - - // bootbox.confirm("Delete backup '" + backupName + "'?", function(confirmed) { - // if (confirmed) { - // chrome.extension.getBackgroundPage().deleteBackup(backupName, function() { - - // }); - - //if (elem.parentNode) { - // elem.parentNode.removeChild(elem); - // - // removeBackupItemDiv (backupName); - // } - // }); - - - // }; - //})(backupName, elem); - if (insertAtBeginning && backupsDiv.childNodes.length > 0) { // some items already exist var firstNode = backupsDiv.childNodes[0]; diff --git a/background.js b/background.js index da06677..0add75b 100644 --- a/background.js +++ b/background.js @@ -122,20 +122,12 @@ function deleteOldestBackup () { return; } - deleteBackup (backupsList[i], loopFunc); + deleteBackup(backupsList[i]).then(loopFunc); i++; }; loopFunc (); } - - //for (var i = 0; i < numItemsToDelete; i++) { - // TODO WARNING: I'm calling deleteBackup rapidly, while deleting is async...(I should wait for each delete to complete before deleting the next) - //deleteBackup (backupsList[i], function() { - - //}); - //} - }); } @@ -293,44 +285,30 @@ function updateBrowserActionIcon (status) { chrome.action.setIcon({path: icon}); } -function deleteBackup (backupName, callback) { +async function deleteBackup(backupName) { console.log("Deleting backup " + backupName); - chrome.storage.local.remove(backupName, function() { - //console.log ("=> Deleted backup " + backupName); - - chrome.storage.local.get("backups_list", function(items) { - //console.log ("==> got backups_list " + backupName); - - if(!items.backups_list) { - callback(); - return; - } - - var backupsList = items.backups_list; - - var index = backupsList.indexOf(backupName); - if (index >= 0) { - backupsList.splice(index, 1); - } + await chrome.storage.local.remove(backupName); + //console.log("=> Deleted backup " + backupName); - //console.log ("===> Updating backups_list (removing " + backupName + ")"); + const items = await chrome.storage.local.get("backups_list"); + //console.log("==> got backups_list " + backupName); - chrome.storage.local.set({"backups_list": backupsList}, function() { - //console.log ("===> Updated backups_list (removed " + backupName + ")"); - - callback(); - }); - - //console.log ("==> EXIT got backups_list " + backupName); - }); + if (!items.backups_list) { + return; + } - //console.log ("=> EXIT Deleted backup " + backupName); - }); + var backupsList = items.backups_list; - //console.log("EXIT Deleting backup " + backupName); + var index = backupsList.indexOf(backupName); + if (index >= 0) { + backupsList.splice(index, 1); + } + //console.log("===> Updating backups_list (removing " + backupName + ")"); + await chrome.storage.local.set({"backups_list": backupsList}); + //console.log("===> Updated backups_list (removed " + backupName + ")"); } function restoreNow(backupName) { @@ -440,7 +418,7 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { break; case 'deleteBackup': - deleteBackup(...request.args, sendResponse); + deleteBackup(...request.args).then(sendResponse); asyncResponse = true; break;