Run the checker if the extension is ready.

This commit is contained in:
buzz-lightsnack-2007 2024-06-08 23:39:59 +08:00
parent 7a6c6deba9
commit ff19ca4ec1

View file

@ -10,10 +10,25 @@ import {background, global} from "/scripts/secretariat.js";
export default class BackgroundCheck { export default class BackgroundCheck {
update = {}; update = {};
status = {};
constructor () { constructor () {
const main = () => {
global.read([`ready`]).then((STATE) => {
if (STATE && !this.status[`ready`]) {
this.manager = new EntryManager(); this.manager = new EntryManager();
this.updater(); 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() { updater() {
@ -28,12 +43,25 @@ export default class BackgroundCheck {
DURATION_PREFERENCES[`duration`] = 24; DURATION_PREFERENCES[`duration`] = 24;
// Write it. // Write it.
return(await global.write([`settings`, `sync`], DURATION_PREFERENCES, -1, {"silent": true})); return(global.write([`settings`, `sync`, `duration`], DURATION_PREFERENCES[`duration`], -1, {"silent": true}));
} else {return(true)}; } else {return(true)};
}; };
setDefaults().then((result) => { // Set the filter management.
if (result) { 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. 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.
*/ */
@ -46,18 +74,6 @@ export default class BackgroundCheck {
return (TIME[`last`] ? ((TIME[`now`] - TIME[`last`]) > DURATION_PREFERENCES[`duration`]) : true); return (TIME[`last`] ? ((TIME[`now`] - TIME[`last`]) > DURATION_PREFERENCES[`duration`]) : true);
}; };
/*
Run the update.
@return {Promise} the promise that, once resolved, contains 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, {"verify": false}));
};
// Set the interval. // Set the interval.
let updater_set = () => { let updater_set = () => {
this.update[`checker`] = setInterval(async () => { this.update[`checker`] = setInterval(async () => {
@ -99,21 +115,22 @@ export default class BackgroundCheck {
new background(() => {return(this.update.background())}); new background(() => {return(this.update.background())});
} }
// Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds. if (!(DURATION_PREFERENCES)) {
DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * (60 ** 2) * 1000; DURATION_PREFERENCES = {};
};
// Set the filter management. // Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds.
let filter = new FilterManager(); if (DURATION_PREFERENCES[`duration`]) {
DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * (60 ** 2) * 1000;
};
// When the browser is started, run the updater immediately only when the filters are not updated yet. // When the browser is started, run the updater immediately only when the filters are not updated yet.
(updater_check()) (updater_check() || !DURATION_PREFERENCES)
? updater_run() ? updater_run()
: false; : false;
// Run the background. // Run the background.
updater_background(); updater_background();
}
});
}) })
} }
}; };