WebUI: ensure cached info are initialized properly

PR #21893.
This commit is contained in:
Chocobo1 2024-11-25 14:04:28 +08:00 committed by GitHub
parent 8d847eeb18
commit b0fe6e6c59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 84 additions and 63 deletions

View file

@ -53,19 +53,20 @@ window.qBittorrent.Cache ??= (() => {
class BuildInfoCache { class BuildInfoCache {
#m_store = {}; #m_store = {};
init() { async init() {
new Request.JSON({ return fetch("api/v2/app/buildInfo", {
url: "api/v2/app/buildInfo", method: "GET",
method: "get", cache: "no-store"
noCache: true, })
onSuccess: (responseJSON) => { .then(async (response) => {
if (!responseJSON) if (!response.ok)
return; return;
const responseText = await response.text();
const responseJSON = JSON.parse(responseText);
deepFreeze(responseJSON); deepFreeze(responseJSON);
this.#m_store = responseJSON; this.#m_store = responseJSON;
} });
}).send();
} }
get() { get() {
@ -80,26 +81,27 @@ window.qBittorrent.Cache ??= (() => {
// onFailure: () => {}, // onFailure: () => {},
// onSuccess: () => {} // onSuccess: () => {}
// } // }
init(obj = {}) { async init(obj = {}) {
new Request.JSON({ return fetch("api/v2/app/preferences", {
url: "api/v2/app/preferences", method: "GET",
method: "get", cache: "no-store"
noCache: true, })
onFailure: (xhr) => { .then(async (response) => {
if (typeof obj.onFailure === "function") if (!response.ok)
obj.onFailure(xhr);
},
onSuccess: (responseJSON, responseText) => {
if (!responseJSON)
return; return;
const responseText = await response.text();
const responseJSON = JSON.parse(responseText);
deepFreeze(responseJSON); deepFreeze(responseJSON);
this.#m_store = responseJSON; this.#m_store = responseJSON;
if (typeof obj.onSuccess === "function") if (typeof obj.onSuccess === "function")
obj.onSuccess(responseJSON, responseText); obj.onSuccess(responseJSON, responseText);
} },
}).send(); (error) => {
if (typeof obj.onFailure === "function")
obj.onFailure(error);
});
} }
get() { get() {
@ -117,17 +119,16 @@ window.qBittorrent.Cache ??= (() => {
if (typeof obj.data !== "object") if (typeof obj.data !== "object")
throw new Error("`data` is not an object."); throw new Error("`data` is not an object.");
new Request({ fetch("api/v2/app/setPreferences", {
url: "api/v2/app/setPreferences", method: "POST",
method: "post", body: new URLSearchParams({
data: {
"json": JSON.stringify(obj.data) "json": JSON.stringify(obj.data)
}, })
onFailure: (xhr) => { })
if (typeof obj.onFailure === "function") .then(async (response) => {
obj.onFailure(xhr); if (!response.ok)
}, return;
onSuccess: (responseText, responseXML) => {
this.#m_store = structuredClone(this.#m_store); this.#m_store = structuredClone(this.#m_store);
for (const key in obj.data) { for (const key in obj.data) {
if (!Object.hasOwn(obj.data, key)) if (!Object.hasOwn(obj.data, key))
@ -138,27 +139,33 @@ window.qBittorrent.Cache ??= (() => {
} }
deepFreeze(this.#m_store); deepFreeze(this.#m_store);
if (typeof obj.onSuccess === "function") if (typeof obj.onSuccess === "function") {
obj.onSuccess(responseText, responseXML); const responseText = await response.text();
obj.onSuccess(responseText);
} }
}).send(); },
(error) => {
if (typeof obj.onFailure === "function")
obj.onFailure(error);
});
} }
} }
class QbtVersionCache { class QbtVersionCache {
#m_store = ""; #m_store = "";
init() { async init() {
new Request({ return fetch("api/v2/app/version", {
url: "api/v2/app/version", method: "GET",
method: "get", cache: "no-store"
noCache: true, })
onSuccess: (responseText) => { .then(async (response) => {
if (!responseText) if (!response.ok)
return; return;
const responseText = await response.text();
this.#m_store = responseText; this.#m_store = responseText;
} });
}).send();
} }
get() { get() {

View file

@ -30,6 +30,7 @@ window.qBittorrent.Client ??= (() => {
const exports = () => { const exports = () => {
return { return {
setup: setup, setup: setup,
initializeCaches: initializeCaches,
closeWindow: closeWindow, closeWindow: closeWindow,
closeFrameWindow: closeFrameWindow, closeFrameWindow: closeFrameWindow,
getSyncMainDataInterval: getSyncMainDataInterval, getSyncMainDataInterval: getSyncMainDataInterval,
@ -45,11 +46,22 @@ window.qBittorrent.Client ??= (() => {
}; };
}; };
let cacheAllSettled;
const setup = () => { const setup = () => {
// fetch various data and store it in memory // fetch various data and store it in memory
window.qBittorrent.Cache.buildInfo.init(); cacheAllSettled = Promise.allSettled([
window.qBittorrent.Cache.preferences.init(); window.qBittorrent.Cache.buildInfo.init(),
window.qBittorrent.Cache.qbtVersion.init(); window.qBittorrent.Cache.preferences.init(),
window.qBittorrent.Cache.qbtVersion.init()
]);
};
const initializeCaches = async () => {
const results = await cacheAllSettled;
for (const [idx, result] of results.entries()) {
if (result.status === "rejected")
console.error(`Failed to initialize cache. Index: ${idx}. Reason: "${result.reason}".`);
}
}; };
const closeWindow = (windowID) => { const closeWindow = (windowID) => {
@ -1771,7 +1783,9 @@ window.addEventListener("DOMContentLoaded", () => {
}); });
}); });
window.addEventListener("load", () => { window.addEventListener("load", async () => {
await window.qBittorrent.Client.initializeCaches();
// switch to previously used tab // switch to previously used tab
const previouslyUsedTab = LocalPreferences.get("selected_window_tab", "transfers"); const previouslyUsedTab = LocalPreferences.get("selected_window_tab", "transfers");
switch (previouslyUsedTab) { switch (previouslyUsedTab) {