From 98d4d24f9b784294bde0db4dd7a1dbed021f2c83 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 22 Apr 2023 19:29:54 -0400 Subject: [PATCH] Mind rejected promises from vAPI.storage API Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/2604 --- platform/common/vapi-background.js | 33 +++++++++++++++++++++++++++++- src/js/cachestorage.js | 21 +++++++++++-------- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/platform/common/vapi-background.js b/platform/common/vapi-background.js index 609750c47..ca090b85f 100644 --- a/platform/common/vapi-background.js +++ b/platform/common/vapi-background.js @@ -90,7 +90,38 @@ vAPI.app = { /******************************************************************************/ /******************************************************************************/ -vAPI.storage = webext.storage.local; +vAPI.storage = { + get(...args) { + return webext.storage.local.get(...args).catch(reason => { + console.log(reason); + }); + }, + set(...args) { + return webext.storage.local.set(...args).catch(reason => { + console.log(reason); + }); + }, + remove(...args) { + return webext.storage.local.remove(...args).catch(reason => { + console.log(reason); + }); + }, + clear(...args) { + return webext.storage.local.clear(...args).catch(reason => { + console.log(reason); + }); + }, + QUOTA_BYTES: browser.storage.local.QUOTA_BYTES, +}; + +// Not all platforms support getBytesInUse +if ( webext.storage.local.getBytesInUse instanceof Function ) { + vAPI.storage.getBytesInUse = function(...args) { + return webext.storage.local.getBytesInUse(...args).catch(reason => { + console.log(reason); + }); + }; +} /******************************************************************************/ /******************************************************************************/ diff --git a/src/js/cachestorage.js b/src/js/cachestorage.js index 241864201..eba68ed05 100644 --- a/src/js/cachestorage.js +++ b/src/js/cachestorage.js @@ -63,30 +63,26 @@ const storageLocal = webext.storage.local; const cacheStorage = { name: 'browser.storage.local', - get: (...args) => { + get(...args) { return storageLocal.get(...args).catch(reason => { console.log(reason); }); }, - set: (...args) => { + set(...args) { return storageLocal.set(...args).catch(reason => { console.log(reason); }); }, - remove: (...args) => { + remove(...args) { return storageLocal.remove(...args).catch(reason => { console.log(reason); }); }, - clear: (...args) => { + clear(...args) { return storageLocal.clear(...args).catch(reason => { console.log(reason); }); }, - // Not all platforms support getBytesInUse - getBytesInUse: storageLocal.getBytesInUse - ? storageLocal.getBytesInUse.bind(storageLocal) - : undefined, select: function(selectedBackend) { let actualBackend = selectedBackend; if ( actualBackend === undefined || actualBackend === 'unset' ) { @@ -113,6 +109,15 @@ const cacheStorage = { error: undefined }; +// Not all platforms support getBytesInUse +if ( storageLocal.getBytesInUse instanceof Function ) { + cacheStorage.getBytesInUse = function(...args) { + return storageLocal.getBytesInUse(...args).catch(reason => { + console.log(reason); + }); + }; +} + // Reassign API entries to that of indexedDB-based ones const selectIDB = async function() { let db;