From 6cdcfd099f6f137855b1bc2588792dd830fff83f Mon Sep 17 00:00:00 2001 From: tartpvule <4371378-tartpvule@users.noreply.gitlab.com> Date: Thu, 12 Sep 2019 16:42:44 +0000 Subject: [PATCH 1/5] Save on storage change and deferred save, part 1 Implement saveOnDisk (previously dead code), deferSaveOnDisk Remove setInterval(saveOnExit, 60000) --- core_js/storage.js | 54 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/core_js/storage.js b/core_js/storage.js index a036fa2..13e8b64 100644 --- a/core_js/storage.js +++ b/core_js/storage.js @@ -47,13 +47,50 @@ 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; + } + }); + 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; } /** @@ -199,7 +236,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 +259,5 @@ function storeHashStatus(status_code) storage.hashStatus = status_code; } -/** -* Save every minute the temporary data to the disk. -*/ -setInterval(saveOnExit, 60000); - // Start storage getDataFromDisk(); From 891723b7b95cc721eae092b53069b9e385aa6df5 Mon Sep 17 00:00:00 2001 From: tartpvule <4371378-tartpvule@users.noreply.gitlab.com> Date: Thu, 12 Sep 2019 16:44:14 +0000 Subject: [PATCH 2/5] Save on storage change and deferred save, part 2 Insert calls to deferSaveOnDisk --- core_js/tools.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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'); } } From b0f1c85cd870ad2cc331c613fec55351f0239509 Mon Sep 17 00:00:00 2001 From: tartpvule <4371378-tartpvule@users.noreply.gitlab.com> Date: Thu, 12 Sep 2019 16:45:56 +0000 Subject: [PATCH 3/5] Save on storage change and deferred save, part 3 Insert calls to saveOnDisk and deferSaveOnDisk --- clearurls.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clearurls.js b/clearurls.js index ef07255..4b4b1e5 100644 --- a/clearurls.js +++ b/clearurls.js @@ -345,6 +345,7 @@ function start() } storage.ClearURLsData = JSON.parse(storage.ClearURLsData); toObject(storage.ClearURLsData); + saveOnDisk(['ClearURLsData', 'dataHash', 'hashStatus']); } }); } @@ -777,5 +778,6 @@ function start() "timestamp": Date.now() } ); + deferSaveOnDisk('log'); } } From b951cf1172a32763d9e9132454ee51a7ca15a360 Mon Sep 17 00:00:00 2001 From: tartpvule <4371378-tartpvule@users.noreply.gitlab.com> Date: Thu, 12 Sep 2019 16:53:33 +0000 Subject: [PATCH 4/5] Missed a storeHashStatus call point --- clearurls.js | 1 + 1 file changed, 1 insertion(+) diff --git a/clearurls.js b/clearurls.js index 4b4b1e5..a9160ed 100644 --- a/clearurls.js +++ b/clearurls.js @@ -308,6 +308,7 @@ function start() else { toObject(storage.ClearURLsData); storeHashStatus(1); + saveOnDisk(['hashStatus']); } } else { From a4a62b0d1e8769ebedc85f77f39a6852684d7c51 Mon Sep 17 00:00:00 2001 From: tartpvule <4371378-tartpvule@users.noreply.gitlab.com> Date: Thu, 12 Sep 2019 19:52:10 +0000 Subject: [PATCH 5/5] Add console logging in saveOnDisk --- core_js/storage.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core_js/storage.js b/core_js/storage.js index 13e8b64..329f046 100644 --- a/core_js/storage.js +++ b/core_js/storage.js @@ -68,6 +68,7 @@ function saveOnDisk(keys) json[key] = value; } }); + console.log(translate('core_save_on_disk')); browser.storage.local.set(json); } @@ -122,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 */