diff --git a/src/scripts/background/check.js b/src/scripts/background/check.js index 1a6b90c..80141b6 100644 --- a/src/scripts/background/check.js +++ b/src/scripts/background/check.js @@ -10,10 +10,25 @@ import {background, global} from "/scripts/secretariat.js"; export default class BackgroundCheck { update = {}; + status = {}; constructor () { - this.manager = new EntryManager(); - this.updater(); + const main = () => { + global.read([`ready`]).then((STATE) => { + if (STATE && !this.status[`ready`]) { + this.manager = new EntryManager(); + this.updater(); + + this.status[`ready`] = true; + }; + + if (this.status[`ready`]) { + (this.status.wait) ? this.status.wait.cancel() : false; + }; + }); + }; + + this.status.wait = new background(() => {main();}) }; updater() { @@ -26,94 +41,96 @@ export default class BackgroundCheck { if (!(typeof DURATION_PREFERENCES).includes(`obj`) || !DURATION_PREFERENCES || Array.isArray(DURATION_PREFERENCES)) { DURATION_PREFERENCES = {}; DURATION_PREFERENCES[`duration`] = 24; - + // Write it. - return(await global.write([`settings`, `sync`], DURATION_PREFERENCES, -1, {"silent": true})); - } else {return (true)}; + return(global.write([`settings`, `sync`, `duration`], DURATION_PREFERENCES[`duration`], -1, {"silent": true})); + } else {return(true)}; + }; + + // Set the filter management. + let filter = new FilterManager(); + + /* + Run the update. + + @return {Promise} the last update status. + */ + const updater_run = async () => { + filter.update(); + + // Update the last update time. + return(await global.write([`settings`,`sync`,`last`], Date.now(), -1, {"silent": true})); + }; + + /* + Check if it's time to update the filters through comparing the difference of the current time and the last updated time to the expected duration. + */ + async function updater_check() { + let TIME = {}; + TIME[`now`] = Date.now(); + TIME[`last`] = await global.read([`settings`,`sync`,`last`], -1); + + // Run if the last time is not set or if the difference is greater than the expected duration. + return (TIME[`last`] ? ((TIME[`now`] - TIME[`last`]) > DURATION_PREFERENCES[`duration`]) : true); + }; + + // Set the interval. + let updater_set = () => { + this.update[`checker`] = setInterval(async () => { + // Update the filters. + updater_run(); + }, DURATION_PREFERENCES[`duration`]); }; - setDefaults().then((result) => { - if (result) { - /* - Check if it's time to update the filters through comparing the difference of the current time and the last updated time to the expected duration. - */ - async function updater_check() { - let TIME = {}; - TIME[`now`] = Date.now(); - TIME[`last`] = await global.read([`settings`,`sync`,`last`], -1); + /* + Reset the interval. + */ + const updater_reset = () => { + // Cancel the interval. + (this.update[`checker`]) ? clearInterval(this.update[`checker`]) : false; - // Run if the last time is not set or if the difference is greater than the expected duration. - return (TIME[`last`] ? ((TIME[`now`] - TIME[`last`]) > DURATION_PREFERENCES[`duration`]) : true); + // Run the updater, if necessary. + (updater_check()) + ? updater_run() + : false; + + // Start the new interval. + updater_set(); + } + + const updater_background = () => { + this.update[`background`] = async () => { + if ((await global.read([`settings`, `sync`, `duration`])) ? (await global.read([`settings`, `sync`, `duration`] * (60 ** 2) * 1000 != DURATION_PREFERENCES[`duration`])) : false) { + if (await global.global.read([`settings`, `sync`, `duration`])) { + // Get the new time. + DURATION_PREFERENCES[`duration`] = await global.read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; + + // Reset the updater. + updater_reset(); + } }; + }; - /* - Run the update. + // Set the background checker. + new background(() => {return(this.update.background())}); + } + + if (!(DURATION_PREFERENCES)) { + DURATION_PREFERENCES = {}; + }; - @return {Promise} the promise that, once resolved, contains the last update status. - */ - const updater_run = async () => { - filter.update(); + // Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds. + if (DURATION_PREFERENCES[`duration`]) { + DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * (60 ** 2) * 1000; + }; - // Update the last update time. - return(await global.write([`settings`,`sync`,`last`], Date.now(), -1, {"verify": false})); - }; + // When the browser is started, run the updater immediately only when the filters are not updated yet. + (updater_check() || !DURATION_PREFERENCES) + ? updater_run() + : false; - // Set the interval. - let updater_set = () => { - this.update[`checker`] = setInterval(async () => { - // Update the filters. - updater_run(); - }, DURATION_PREFERENCES[`duration`]); - }; - - /* - Reset the interval. - */ - const updater_reset = () => { - // Cancel the interval. - (this.update[`checker`]) ? clearInterval(this.update[`checker`]) : false; - - // Run the updater, if necessary. - (updater_check()) - ? updater_run() - : false; - - // Start the new interval. - updater_set(); - } - - const updater_background = () => { - this.update[`background`] = async () => { - if ((await global.read([`settings`, `sync`, `duration`])) ? (await global.read([`settings`, `sync`, `duration`] * (60 ** 2) * 1000 != DURATION_PREFERENCES[`duration`])) : false) { - if (await global.global.read([`settings`, `sync`, `duration`])) { - // Get the new time. - DURATION_PREFERENCES[`duration`] = await global.read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; - - // Reset the updater. - updater_reset(); - } - }; - }; - - // Set the background checker. - new background(() => {return(this.update.background())}); - } - - // Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds. - DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * (60 ** 2) * 1000; - - // Set the filter management. - let filter = new FilterManager(); - - // When the browser is started, run the updater immediately only when the filters are not updated yet. - (updater_check()) - ? updater_run() - : false; - - // Run the background. - updater_background(); - } - }); + // Run the background. + updater_background(); }) } };