Run the checker if the extension is ready.
This commit is contained in:
parent
7a6c6deba9
commit
ff19ca4ec1
1 changed files with 98 additions and 81 deletions
|
@ -10,10 +10,25 @@ import {background, global} from "/scripts/secretariat.js";
|
||||||
|
|
||||||
export default class BackgroundCheck {
|
export default class BackgroundCheck {
|
||||||
update = {};
|
update = {};
|
||||||
|
status = {};
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
this.manager = new EntryManager();
|
const main = () => {
|
||||||
this.updater();
|
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() {
|
updater() {
|
||||||
|
@ -26,94 +41,96 @@ export default class BackgroundCheck {
|
||||||
if (!(typeof DURATION_PREFERENCES).includes(`obj`) || !DURATION_PREFERENCES || Array.isArray(DURATION_PREFERENCES)) {
|
if (!(typeof DURATION_PREFERENCES).includes(`obj`) || !DURATION_PREFERENCES || Array.isArray(DURATION_PREFERENCES)) {
|
||||||
DURATION_PREFERENCES = {};
|
DURATION_PREFERENCES = {};
|
||||||
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)};
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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) {
|
Reset the interval.
|
||||||
/*
|
*/
|
||||||
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.
|
const updater_reset = () => {
|
||||||
*/
|
// Cancel the interval.
|
||||||
async function updater_check() {
|
(this.update[`checker`]) ? clearInterval(this.update[`checker`]) : false;
|
||||||
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.
|
// Run the updater, if necessary.
|
||||||
return (TIME[`last`] ? ((TIME[`now`] - TIME[`last`]) > DURATION_PREFERENCES[`duration`]) : true);
|
(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.
|
||||||
Run the update.
|
new background(() => {return(this.update.background())});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(DURATION_PREFERENCES)) {
|
||||||
|
DURATION_PREFERENCES = {};
|
||||||
|
};
|
||||||
|
|
||||||
@return {Promise} the promise that, once resolved, contains the last update status.
|
// Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds.
|
||||||
*/
|
if (DURATION_PREFERENCES[`duration`]) {
|
||||||
const updater_run = async () => {
|
DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * (60 ** 2) * 1000;
|
||||||
filter.update();
|
};
|
||||||
|
|
||||||
// Update the last update time.
|
// When the browser is started, run the updater immediately only when the filters are not updated yet.
|
||||||
return(await global.write([`settings`,`sync`,`last`], Date.now(), -1, {"verify": false}));
|
(updater_check() || !DURATION_PREFERENCES)
|
||||||
};
|
? updater_run()
|
||||||
|
: false;
|
||||||
|
|
||||||
// Set the interval.
|
// Run the background.
|
||||||
let updater_set = () => {
|
updater_background();
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue