make filters async

since most of its content is async anyway
This commit is contained in:
buzzcode2007 2024-04-02 16:36:24 +08:00
parent 532295a610
commit b5c0f7ac68

View file

@ -10,7 +10,9 @@ export default class filters {
chrome.runtime.getURL("scripts/secretariat.js")
);
return ((await Promise.all([secretariat.read([`filters`], -1)]))[0])
return(secretariat.read(`filters`, -1).then((filters) => {
return(filters)
}));
})
}
@ -18,7 +20,7 @@ export default class filters {
@param {string} URL the current URL
*/
static async select(URL = window.location.href) {
async select(URL = window.location.href) {
this.one = await (async () => {
// Import the secretariat.
const secretariat = await import(
@ -40,109 +42,107 @@ export default class filters {
@param {string} URL the URL to update
@return {boolean} the state
*/
static update(URL) {
this.all = (async () => {
// Import the updater.
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
const net = await import(chrome.runtime.getURL("scripts/net.js"));
const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js")))
.default;
const alerts = (
await import(chrome.runtime.getURL("gui/scripts/alerts.js"))
).default;
async update(URL) {
// Import the updater.
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
const net = await import(chrome.runtime.getURL("scripts/net.js"));
const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js")))
.default;
const alerts = (
await import(chrome.runtime.getURL("gui/scripts/alerts.js"))
).default;
// Apparently, JS doesn't have a native queueing system, but it might best work here.
class Queue {
constructor() {
this.elements = [];
}
enqueue(element) {
this.elements.push(element);
}
dequeue() {
return this.elements.shift();
}
isEmpty() {
return this.elements.length <= 0;
}
// Apparently, JS doesn't have a native queueing system, but it might best work here.
class Queue {
constructor() {
this.elements = [];
}
// Create a queue of the filters.
let filters = new Queue();
if (URL) {
// Check if the URL is in a valid protocol
if (URL.includes(`://`)) {
// Append that to the queue.
filters.enqueue(URL);
}
} else {
// Add every item to the queue based on what was loaded first.
if ((await Promise.all([secretariat.read(`filters`, -1)]))[0]) {
Object.keys(
(await Promise.all([secretariat.read(`filters`, -1)]))[0],
).every((filter_URL) => {
if (filter_URL.includes(`://`)) {
filters.enqueue(filter_URL);
}
});
}
enqueue(element) {
this.elements.push(element);
}
if (!filters.isEmpty()) {
while (!filters.isEmpty()) {
let filter_URL = filters.dequeue();
dequeue() {
return this.elements.shift();
}
// Inform the user of download state.
alerts.log(
texts.localized(`settings_filters_update_status`, null, [
filter_URL,
]),
);
isEmpty() {
return this.elements.length <= 0;
}
}
// Create promise of downloading.
let filter_download = net.download(filter_URL);
filter_download
.then((result) => {
// Only work when the filter is valid.
if (result) {
// Write the filter to storage.
secretariat.write(["filters", filter_URL], result, -1);
console.log(
texts.localized(
`settings_filters_update_status_complete`,
null,
[filter_URL],
),
);
}
// Create a queue of the filters.
let filters = new Queue();
})
.catch((error) => {
// Inform the user of the download failure.
console.log(
if (URL) {
// Check if the URL is in a valid protocol
if (URL.includes(`://`)) {
// Append that to the queue.
filters.enqueue(URL);
}
} else {
// Add every item to the queue based on what was loaded first.
if ((await Promise.all([secretariat.read(`filters`, -1)]))[0]) {
Object.keys(
(await Promise.all([secretariat.read(`filters`, -1)]))[0],
).every((filter_URL) => {
if (filter_URL.includes(`://`)) {
filters.enqueue(filter_URL);
}
});
}
}
if (!filters.isEmpty()) {
while (!filters.isEmpty()) {
let filter_URL = filters.dequeue();
// Inform the user of download state.
alerts.log(
texts.localized(`settings_filters_update_status`, null, [
filter_URL,
]),
);
// Create promise of downloading.
let filter_download = net.download(filter_URL);
filter_download
.then((result) => {
// Only work when the filter is valid.
if (result) {
// Write the filter to storage.
secretariat.write(["filters", filter_URL], result, -1);
alerts.log(
texts.localized(
`settings_filters_update_status_failure`,
`settings_filters_update_status_complete`,
null,
[error, filter_URL],
[filter_URL],
),
);
});
}
} else {
// Inform the user of the download being unnecessary.
alerts.warn(texts.localized(`settings_filters_update_stop`));
}
}
// Regardless of the download result, update will also mean setting the filters object to whatever is in storage.
return(secretariat.read(`filters`, -1).then((filters) => {
return(filters)
}));
})();
})
.catch((error) => {
// Inform the user of the download failure.
alerts.error(
texts.localized(
`settings_filters_update_status_failure`,
null,
[error, filter_URL],
),
);
});
}
} else {
// Inform the user of the download being unnecessary.
alerts.warn(texts.localized(`settings_filters_update_stop`));
}
// Regardless of the download result, update will also mean setting the filters object to whatever is in storage.
this.all = (await secretariat.read(`filters`, -1));
return(this.all);
}
}