diff --git a/clearurls.js b/clearurls.js index ef07255..a9160ed 100644 --- a/clearurls.js +++ b/clearurls.js @@ -308,6 +308,7 @@ function start() else { toObject(storage.ClearURLsData); storeHashStatus(1); + saveOnDisk(['hashStatus']); } } else { @@ -345,6 +346,7 @@ function start() } storage.ClearURLsData = JSON.parse(storage.ClearURLsData); toObject(storage.ClearURLsData); + saveOnDisk(['ClearURLsData', 'dataHash', 'hashStatus']); } }); } @@ -777,5 +779,6 @@ function start() "timestamp": Date.now() } ); + deferSaveOnDisk('log'); } } diff --git a/core_js/storage.js b/core_js/storage.js index a036fa2..329f046 100644 --- a/core_js/storage.js +++ b/core_js/storage.js @@ -47,13 +47,51 @@ function saveOnExit() } /** -* Save the value under the key on the disk. -* @param {String} key -* @param {Object} value +* Save multiple keys on the disk. +* @param {String[]} keys */ -function saveOnDisk(key, value) +function saveOnDisk(keys) { - browser.storage.local.set({key: value}); + var json = {}; + + keys.forEach(function(key) { + var value = storage[key]; + switch (key) { + case "ClearURLsData": + case "log": + json[key] = JSON.stringify(value); + break; + case "types": + json[key] = value.toString(); + break; + default: + json[key] = value; + } + }); + console.log(translate('core_save_on_disk')); + browser.storage.local.set(json); +} + +var hasPendingSaves = false; +var pendingSaves = new Set(); + +/** +* Schedule to save a key to disk in 30 seconds. +* @param {String} key +*/ +function deferSaveOnDisk(key) +{ + if (hasPendingSaves) { + pendingSaves.add(key); + return; + } + + setTimeout(function() { + saveOnDisk(Array.from(pendingSaves)); + pendingSaves.clear(); + hasPendingSaves = false; + }, 30000); + hasPendingSaves = true; } /** @@ -85,6 +123,10 @@ function getEntireData() /** * Save the value under the key on the RAM. +* +* Note: To store the data on the hard disk, one of +* deferSaveOnDisk(), saveOnDisk(), or saveOnExit() +* must be called. * @param {String} key * @param {Object} value */ @@ -199,7 +241,7 @@ function loadOldDataFromStore() } /** -* Save the hash status to the local storage. +* Save the hash status to the local storage (RAM). * The status can have the following values: * 1 "up to date" * 2 "updated" @@ -222,10 +264,5 @@ function storeHashStatus(status_code) storage.hashStatus = status_code; } -/** -* Save every minute the temporary data to the disk. -*/ -setInterval(saveOnExit, 60000); - // Start storage getDataFromDisk(); diff --git a/core_js/tools.js b/core_js/tools.js index aa66cc2..d02aa61 100644 --- a/core_js/tools.js +++ b/core_js/tools.js @@ -199,7 +199,7 @@ function loadOldDataFromStore() } /** -* Save the hash status to the local storage. +* Save the hash status to the local storage (RAM). * The status can have the following values: * 1 "up to date" * 2 "updated" @@ -231,6 +231,7 @@ function increaseGlobalURLCounter(number) if(storage.statisticsStatus) { storage.globalurlcounter += number; + deferSaveOnDisk('globalurlcounter'); } } @@ -242,6 +243,7 @@ function increaseURLCounter() if(storage.statisticsStatus) { storage.globalCounter++; + deferSaveOnDisk('globalCounter'); } }