mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-20 21:33:27 -07:00
Revise cache internals
Now cache initialization and `get()` is less costly to use and it shifts the weight to `set()`. PR #20430.
This commit is contained in:
parent
63c9b6388e
commit
bb8a012b1c
1 changed files with 21 additions and 3 deletions
|
@ -40,6 +40,18 @@ window.qBittorrent.Cache = (() => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const deepFreeze = (obj) => {
|
||||||
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#examples
|
||||||
|
|
||||||
|
const keys = Reflect.ownKeys(obj);
|
||||||
|
for (const key of keys) {
|
||||||
|
const value = obj[key];
|
||||||
|
if ((value && (typeof value === 'object')) || (typeof value === 'function'))
|
||||||
|
deepFreeze(value);
|
||||||
|
}
|
||||||
|
Object.freeze(obj);
|
||||||
|
};
|
||||||
|
|
||||||
class BuildInfoCache {
|
class BuildInfoCache {
|
||||||
#m_store = {};
|
#m_store = {};
|
||||||
|
|
||||||
|
@ -51,13 +63,15 @@ window.qBittorrent.Cache = (() => {
|
||||||
onSuccess: (responseJSON) => {
|
onSuccess: (responseJSON) => {
|
||||||
if (!responseJSON)
|
if (!responseJSON)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
deepFreeze(responseJSON);
|
||||||
this.#m_store = responseJSON;
|
this.#m_store = responseJSON;
|
||||||
}
|
}
|
||||||
}).send();
|
}).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
get() {
|
get() {
|
||||||
return structuredClone(this.#m_store);
|
return this.#m_store;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +94,9 @@ window.qBittorrent.Cache = (() => {
|
||||||
onSuccess: (responseJSON, responseText) => {
|
onSuccess: (responseJSON, responseText) => {
|
||||||
if (!responseJSON)
|
if (!responseJSON)
|
||||||
return;
|
return;
|
||||||
this.#m_store = structuredClone(responseJSON);
|
|
||||||
|
deepFreeze(responseJSON);
|
||||||
|
this.#m_store = responseJSON;
|
||||||
|
|
||||||
if (typeof obj.onSuccess === 'function')
|
if (typeof obj.onSuccess === 'function')
|
||||||
obj.onSuccess(responseJSON, responseText);
|
obj.onSuccess(responseJSON, responseText);
|
||||||
|
@ -89,7 +105,7 @@ window.qBittorrent.Cache = (() => {
|
||||||
}
|
}
|
||||||
|
|
||||||
get() {
|
get() {
|
||||||
return structuredClone(this.#m_store);
|
return this.#m_store;
|
||||||
}
|
}
|
||||||
|
|
||||||
// obj: {
|
// obj: {
|
||||||
|
@ -114,6 +130,7 @@ window.qBittorrent.Cache = (() => {
|
||||||
obj.onFailure(xhr);
|
obj.onFailure(xhr);
|
||||||
},
|
},
|
||||||
onSuccess: (responseText, responseXML) => {
|
onSuccess: (responseText, responseXML) => {
|
||||||
|
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))
|
||||||
continue;
|
continue;
|
||||||
|
@ -121,6 +138,7 @@ window.qBittorrent.Cache = (() => {
|
||||||
const value = obj.data[key];
|
const value = obj.data[key];
|
||||||
this.#m_store[key] = value;
|
this.#m_store[key] = value;
|
||||||
}
|
}
|
||||||
|
deepFreeze(this.#m_store);
|
||||||
|
|
||||||
if (typeof obj.onSuccess === 'function')
|
if (typeof obj.onSuccess === 'function')
|
||||||
obj.onSuccess(responseText, responseXML);
|
obj.onSuccess(responseText, responseXML);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue