Database changes automatically update storage stats

This commit is contained in:
toasted-nutbread 2019-02-26 21:03:34 -05:00
parent d96d4b0658
commit f2a5d50959

View File

@ -364,6 +364,10 @@ async function onDictionaryPurge(e) {
dictControls.show(); dictControls.show();
dictProgress.hide(); dictProgress.hide();
if (storageEstimate.mostRecent !== null) {
storageUpdateStats();
}
} }
} }
@ -377,7 +381,12 @@ async function onDictionaryImport(e) {
dictionarySpinnerShow(true); dictionarySpinnerShow(true);
const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`); const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`);
const updateProgress = (total, current) => setProgress(current / total * 100.0); const updateProgress = (total, current) => {
setProgress(current / total * 100.0);
if (storageEstimate.mostRecent !== null && !storageUpdateStats.isUpdating) {
storageUpdateStats();
}
};
setProgress(0.0); setProgress(0.0);
const exceptions = []; const exceptions = [];
@ -588,16 +597,17 @@ function storageBytesToLabeledString(size) {
size /= base; size /= base;
++labelIndex; ++labelIndex;
} }
const label = size.toFixed(1).replace(/\.0+$/, ""); const label = size.toFixed(1);
return `${label}${labels[labelIndex]}`; return `${label}${labels[labelIndex]}`;
} }
async function storageEstimate() { async function storageEstimate() {
try { try {
return await navigator.storage.estimate(); return (storageEstimate.mostRecent = await navigator.storage.estimate());
} catch (e) { } } catch (e) { }
return null; return null;
} }
storageEstimate.mostRecent = null;
async function storageInfoInitialize() { async function storageInfoInitialize() {
const browser = await getBrowser(); const browser = await getBrowser();
@ -611,8 +621,8 @@ async function storageInfoInitialize() {
document.querySelector("#storage-refresh").addEventListener('click', () => storageShowInfo(), false); document.querySelector("#storage-refresh").addEventListener('click', () => storageShowInfo(), false);
} }
async function storageShowInfo() { async function storageUpdateStats() {
storageSpinnerShow(true); storageUpdateStats.isUpdating = true;
const estimate = await storageEstimate(); const estimate = await storageEstimate();
const valid = (estimate !== null); const valid = (estimate !== null);
@ -621,6 +631,16 @@ async function storageShowInfo() {
document.querySelector("#storage-usage").textContent = storageBytesToLabeledString(estimate.usage); document.querySelector("#storage-usage").textContent = storageBytesToLabeledString(estimate.usage);
document.querySelector("#storage-quota").textContent = storageBytesToLabeledString(estimate.quota); document.querySelector("#storage-quota").textContent = storageBytesToLabeledString(estimate.quota);
} }
storageUpdateStats.isUpdating = false;
return valid;
}
storageUpdateStats.isUpdating = false;
async function storageShowInfo() {
storageSpinnerShow(true);
const valid = await storageUpdateStats();
document.querySelector("#storage-use").classList.toggle("storage-hidden", !valid); document.querySelector("#storage-use").classList.toggle("storage-hidden", !valid);
document.querySelector("#storage-error").classList.toggle("storage-hidden", valid); document.querySelector("#storage-error").classList.toggle("storage-hidden", valid);