mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-20 13:23:34 -07:00
WebUI: replace callback with promise chaining
This commit is contained in:
parent
7a1a214f73
commit
41dae509b4
3 changed files with 39 additions and 71 deletions
|
@ -76,12 +76,8 @@ 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"
|
||||||
})
|
})
|
||||||
|
@ -94,12 +90,7 @@ window.qBittorrent.Cache ??= (() => {
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,45 +98,29 @@ window.qBittorrent.Cache ??= (() => {
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(() => {
|
||||||
onSuccess: () => {
|
|
||||||
prefDeleteContentFiles = deleteCB.checked;
|
prefDeleteContentFiles = deleteCB.checked;
|
||||||
setRememberBtnEnabled(false);
|
setRememberBtnEnabled(false);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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,7 +2650,6 @@ 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;
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -3169,17 +3168,14 @@ 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"));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue