forcing downloading to be synchronous to ensure successful writing

This commit is contained in:
buzz-lightsnack-2007 2024-06-08 22:26:57 +08:00
parent f38d690b57
commit 9c2759c41e

View file

@ -78,7 +78,7 @@ export default class FilterManager {
} }
} }
} }
} };
if (!filters.isEmpty()) { if (!filters.isEmpty()) {
while (!filters.isEmpty()) { while (!filters.isEmpty()) {
@ -87,32 +87,30 @@ export default class FilterManager {
// Inform the user of download state. // Inform the user of download state.
new logging (texts.localized(`settings_filters_update_status`), filter_URL); new logging (texts.localized(`settings_filters_update_status`), filter_URL);
// Create promise of downloading. try {
let filter_download = net.download(filter_URL, `JSON`, false, true); let DOWNLOAD = await net.download(filter_URL, `JSON`, false, true);
filter_download
.then(async function (result) {
// Only work when the filter is valid. // Only work when the filter is valid.
if (result) { if (DOWNLOAD) {
// Write the filter to storage. // Write the filter to storage.
if (!(await global.write(["filters", filter_URL], result, -1, {"silent": true, "strict": true}))) { if (!(await global.write(["filters", filter_URL], DOWNLOAD, -1, {"silent": true, "strict": true}))) {
throw ReferenceError; throw ReferenceError;
}; };
// Add the filter to the sync list. // Add the filter to the sync list.
if ((await global.read(["settings", `filters`])) ? !((Object.keys(await global.read(["settings", `filters`]))).includes(filter_URL)) : true) { if (((await global.read(["settings", `filters`])) ? !((Object.keys(await global.read(["settings", `filters`]))).includes(filter_URL)) : true)
if (!(global.write(["settings", `filters`, filter_URL], true, 1, {"silent": true}))) { ? (!(await global.write(["settings", `filters`, filter_URL], true, 1, {"silent": true})))
: false) {
throw ReferenceError; throw ReferenceError;
}; };
};
// Notify that the update is completed. // Notify that the update is completed.
new logging(texts.localized(`settings_filters_update_status_complete`),filter_URL); new logging(texts.localized(`settings_filters_update_status_complete`),filter_URL);
}; };
}) } catch (error) {
.catch((error) => {
// Inform the user of the download failure. // Inform the user of the download failure.
logging.error(error.name, texts.localized(`settings_filters_update_status_failure`, null, [error.name, filter_URL]), error.stack); logging.error(error.name, texts.localized(`settings_filters_update_status_failure`, null, [error.name, filter_URL]), error.stack);
}); };
} }
} else { } else {
// Inform the user of the download being unnecessary. // Inform the user of the download being unnecessary.