WebUI: replace callback with promise chaining

This commit is contained in:
Thomas Piccirello 2025-08-14 20:47:19 -07:00
commit 41dae509b4
No known key found for this signature in database
3 changed files with 39 additions and 71 deletions

View file

@ -76,77 +76,52 @@ window.qBittorrent.Cache ??= (() => {
class PreferencesCache { class PreferencesCache {
#m_store = {}; #m_store = {};
// obj: { async init() {
// onFailure: () => {}, return await fetch("api/v2/app/preferences", {
// onSuccess: () => {}
// }
init(obj = {}) {
return fetch("api/v2/app/preferences", {
method: "GET", method: "GET",
cache: "no-store" cache: "no-store"
}) })
.then(async (response) => { .then(async (response) => {
if (!response.ok) if (!response.ok)
return; return;
const responseText = await response.text(); const responseText = await response.text();
const responseJSON = JSON.parse(responseText); const responseJSON = JSON.parse(responseText);
deepFreeze(responseJSON); deepFreeze(responseJSON);
this.#m_store = responseJSON; this.#m_store = responseJSON;
if (typeof obj.onSuccess === "function") return responseJSON;
obj.onSuccess(responseJSON, responseText); });
},
(error) => {
if (typeof obj.onFailure === "function")
obj.onFailure(error);
});
} }
get() { get() {
return this.#m_store; return this.#m_store;
} }
// obj: { async set(data) {
// data: {}, if (typeof data !== "object")
// onFailure: () => {},
// onSuccess: () => {}
// }
set(obj) {
if (typeof obj !== "object")
throw new Error("`obj` is not an object.");
if (typeof obj.data !== "object")
throw new Error("`data` is not an object."); throw new Error("`data` is not an object.");
fetch("api/v2/app/setPreferences", { return await fetch("api/v2/app/setPreferences", {
method: "POST", method: "POST",
body: new URLSearchParams({ body: new URLSearchParams({
json: JSON.stringify(obj.data) json: JSON.stringify(data)
}) })
}) })
.then(async (response) => { .then((response) => {
if (!response.ok) if (!response.ok)
return; return;
this.#m_store = structuredClone(this.#m_store); this.#m_store = structuredClone(this.#m_store);
for (const key in obj.data) { for (const key in data) {
if (!Object.hasOwn(obj.data, key)) if (!Object.hasOwn(data, key))
continue; continue;
const value = obj.data[key]; const value = data[key];
this.#m_store[key] = value; this.#m_store[key] = value;
} }
deepFreeze(this.#m_store); deepFreeze(this.#m_store);
});
if (typeof obj.onSuccess === "function") {
const responseText = await response.text();
obj.onSuccess(responseText);
}
},
(error) => {
if (typeof obj.onFailure === "function")
obj.onFailure(error);
});
} }
} }

View file

@ -58,13 +58,10 @@
// Set current "Delete files" choice as the default // Set current "Delete files" choice as the default
rememberButton.addEventListener("click", (e) => { rememberButton.addEventListener("click", (e) => {
window.qBittorrent.Cache.preferences.set({ window.qBittorrent.Cache.preferences.set({
data: { delete_torrent_content_files: deleteCB.checked
delete_torrent_content_files: deleteCB.checked }).then(() => {
}, prefDeleteContentFiles = deleteCB.checked;
onSuccess: () => { setRememberBtnEnabled(false);
prefDeleteContentFiles = deleteCB.checked;
setRememberBtnEnabled(false);
}
}); });
}); });

View file

@ -2228,8 +2228,8 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
}; };
const loadPreferences = () => { const loadPreferences = () => {
window.parent.qBittorrent.Cache.preferences.init({ window.parent.qBittorrent.Cache.preferences.init()
onSuccess: (pref) => { .then((pref) => {
// Behavior tab // Behavior tab
// Language // Language
updateWebuiLocaleSelect(pref.locale); updateWebuiLocaleSelect(pref.locale);
@ -2650,8 +2650,7 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
document.getElementById("i2pOutboundQuantity").value = pref.i2p_outbound_quantity; document.getElementById("i2pOutboundQuantity").value = pref.i2p_outbound_quantity;
document.getElementById("i2pInboundLength").value = pref.i2p_inbound_length; document.getElementById("i2pInboundLength").value = pref.i2p_inbound_length;
document.getElementById("i2pOutboundLength").value = pref.i2p_outbound_length; document.getElementById("i2pOutboundLength").value = pref.i2p_outbound_length;
} });
});
}; };
const applyPreferences = () => { const applyPreferences = () => {
@ -3169,18 +3168,15 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
settings["i2p_outbound_length"] = Number(document.getElementById("i2pOutboundLength").value); settings["i2p_outbound_length"] = Number(document.getElementById("i2pOutboundLength").value);
// Send it to qBT // Send it to qBT
window.parent.qBittorrent.Cache.preferences.set({ window.parent.qBittorrent.Cache.preferences.set(settings)
data: settings, .then(() => {
onFailure: () => {
alert("QBT_TR(Unable to save program preferences, qBittorrent is probably unreachable.)QBT_TR[CONTEXT=HttpServer]");
window.parent.qBittorrent.Client.closeWindow(document.getElementById("preferencesPage"));
},
onSuccess: () => {
// Close window // Close window
window.parent.location.reload(); window.parent.location.reload();
window.parent.qBittorrent.Client.closeWindow(document.getElementById("preferencesPage")); window.parent.qBittorrent.Client.closeWindow(document.getElementById("preferencesPage"));
} }).catch((error) => {
}); alert("QBT_TR(Unable to save program preferences, qBittorrent is probably unreachable.)QBT_TR[CONTEXT=HttpServer]");
window.parent.qBittorrent.Client.closeWindow(document.getElementById("preferencesPage"));
});
}; };
const setup = () => { const setup = () => {