Migrate to Cache for commonly used data

Previously it was abusing the `localStorage` and now it is storing data in memory (per session).
This commit is contained in:
Chocobo1 2024-02-10 15:26:16 +08:00
parent d06d5b923a
commit 963a7faab8
4 changed files with 73 additions and 54 deletions

View file

@ -34,10 +34,33 @@ if (window.qBittorrent === undefined)
window.qBittorrent.Cache = (() => { window.qBittorrent.Cache = (() => {
const exports = () => { const exports = () => {
return { return {
preferences: new PreferencesCache() buildInfo: new BuildInfoCache(),
preferences: new PreferencesCache(),
qbtVersion: new QbtVersionCache()
}; };
}; };
class BuildInfoCache {
#m_store = {};
init() {
new Request.JSON({
url: 'api/v2/app/buildInfo',
method: 'get',
noCache: true,
onSuccess: (responseJSON) => {
if (!responseJSON)
return;
this.#m_store = responseJSON;
}
}).send();
}
get() {
return structuredClone(this.#m_store);
}
}
class PreferencesCache { class PreferencesCache {
#m_store = {}; #m_store = {};
@ -106,6 +129,27 @@ window.qBittorrent.Cache = (() => {
} }
} }
class QbtVersionCache {
#m_store = '';
init() {
new Request({
url: 'api/v2/app/version',
method: 'get',
noCache: true,
onSuccess: (responseText) => {
if (!responseText)
return;
this.#m_store = responseText;
}
}).send();
}
get() {
return this.#m_store;
}
}
return exports(); return exports();
})(); })();

View file

@ -34,8 +34,7 @@ window.qBittorrent.Client = (() => {
return { return {
closeWindows: closeWindows, closeWindows: closeWindows,
genHash: genHash, genHash: genHash,
getSyncMainDataInterval: getSyncMainDataInterval, getSyncMainDataInterval: getSyncMainDataInterval
qbtVersion: qbtVersion
}; };
}; };
@ -57,10 +56,6 @@ window.qBittorrent.Client = (() => {
return customSyncMainDataInterval ? customSyncMainDataInterval : serverSyncMainDataInterval; return customSyncMainDataInterval ? customSyncMainDataInterval : serverSyncMainDataInterval;
}; };
const qbtVersion = function() {
return LocalPreferences.get('qbtVersion', '');
};
return exports(); return exports();
})(); })();
Object.freeze(window.qBittorrent.Client); Object.freeze(window.qBittorrent.Client);
@ -110,7 +105,7 @@ let selected_filter = LocalPreferences.get('selected_filter', 'all');
let setFilter = function() {}; let setFilter = function() {};
let toggleFilterDisplay = function() {}; let toggleFilterDisplay = function() {};
window.addEvent('load', function() { window.addEventListener("DOMContentLoaded", function() {
const saveColumnSizes = function() { const saveColumnSizes = function() {
const filters_width = $('Filters').getSize().x; const filters_width = $('Filters').getSize().x;
const properties_height_rel = $('propertiesPanel').getSize().y / Window.getSize().y; const properties_height_rel = $('propertiesPanel').getSize().y / Window.getSize().y;
@ -869,12 +864,15 @@ window.addEvent('load', function() {
transfer_info += " [" + window.qBittorrent.Misc.friendlyUnit(serverState.up_rate_limit, true) + "]"; transfer_info += " [" + window.qBittorrent.Misc.friendlyUnit(serverState.up_rate_limit, true) + "]";
transfer_info += " (" + window.qBittorrent.Misc.friendlyUnit(serverState.up_info_data, false) + ")"; transfer_info += " (" + window.qBittorrent.Misc.friendlyUnit(serverState.up_info_data, false) + ")";
$("UpInfos").set('html', transfer_info); $("UpInfos").set('html', transfer_info);
const qbtVersion = window.qBittorrent.Cache.qbtVersion.get();
if (speedInTitle) { if (speedInTitle) {
document.title = "QBT_TR([D: %1, U: %2] qBittorrent %3)QBT_TR[CONTEXT=MainWindow]".replace("%1", window.qBittorrent.Misc.friendlyUnit(serverState.dl_info_speed, true)).replace("%2", window.qBittorrent.Misc.friendlyUnit(serverState.up_info_speed, true)).replace("%3", window.qBittorrent.Client.qbtVersion()); document.title = "QBT_TR([D: %1, U: %2] qBittorrent %3)QBT_TR[CONTEXT=MainWindow]".replace("%1", window.qBittorrent.Misc.friendlyUnit(serverState.dl_info_speed, true)).replace("%2", window.qBittorrent.Misc.friendlyUnit(serverState.up_info_speed, true)).replace("%3", qbtVersion);
document.title += " QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]"; document.title += " QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]";
} }
else else
document.title = ("qBittorrent " + window.qBittorrent.Client.qbtVersion() + " QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]"); document.title = ("qBittorrent " + qbtVersion + " QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]");
$('freeSpaceOnDisk').set('html', 'QBT_TR(Free space: %1)QBT_TR[CONTEXT=HttpServer]'.replace("%1", window.qBittorrent.Misc.friendlyUnit(serverState.free_space_on_disk))); $('freeSpaceOnDisk').set('html', 'QBT_TR(Free space: %1)QBT_TR[CONTEXT=HttpServer]'.replace("%1", window.qBittorrent.Misc.friendlyUnit(serverState.free_space_on_disk)));
$('DHTNodes').set('html', 'QBT_TR(DHT: %1 nodes)QBT_TR[CONTEXT=StatusBar]'.replace("%1", serverState.dht_nodes)); $('DHTNodes').set('html', 'QBT_TR(DHT: %1 nodes)QBT_TR[CONTEXT=StatusBar]'.replace("%1", serverState.dht_nodes));
@ -1575,38 +1573,11 @@ window.addEvent('load', function() {
} }
} }
}).activate(); }).activate();
});
window.parent.qBittorrent.Cache.preferences.init();
window.addEventListener("load", () => {
// fetch qbt version and store it locally // fetch various data and store it in memory
new Request({ window.qBittorrent.Cache.buildInfo.init();
url: 'api/v2/app/version', window.qBittorrent.Cache.preferences.init();
method: 'get', window.qBittorrent.Cache.qbtVersion.init();
noCache: true,
onSuccess: (info) => {
if (!info)
return;
LocalPreferences.set('qbtVersion', info);
}
}).send();
// fetch build info and store it locally
new Request.JSON({
url: 'api/v2/app/buildInfo',
method: 'get',
noCache: true,
onSuccess: (info) => {
if (!info)
return;
LocalPreferences.set('buildInfo.qtVersion', info.qt);
LocalPreferences.set('buildInfo.libtorrentVersion', info.libtorrent);
LocalPreferences.set('buildInfo.boostVersion', info.boost);
LocalPreferences.set('buildInfo.opensslVersion', info.openssl);
LocalPreferences.set('buildInfo.zlibVersion', info.zlib);
LocalPreferences.set('buildInfo.bitness', info.bitness);
LocalPreferences.set('buildInfo.platform', info.platform);
}
}).send();
}); });

View file

@ -843,11 +843,14 @@
<script> <script>
'use strict'; 'use strict';
$('qbittorrentVersion').innerText = `qBittorrent ${window.qBittorrent.Client.qbtVersion()} QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]`; const qbtVersion = window.parent.qBittorrent.Cache.qbtVersion.get();
$('qtVersion').textContent = LocalPreferences.get('buildInfo.qtVersion'); const buildInfo = window.parent.qBittorrent.Cache.buildInfo.get();
$('libtorrentVersion').textContent = LocalPreferences.get('buildInfo.libtorrentVersion');
$('boostVersion').textContent = LocalPreferences.get('buildInfo.boostVersion'); $('qbittorrentVersion').innerText = `qBittorrent ${qbtVersion} QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]`;
$('opensslVersion').textContent = LocalPreferences.get('buildInfo.opensslVersion'); $('qtVersion').textContent = buildInfo.qt;
$('zlibVersion').textContent = LocalPreferences.get('buildInfo.zlibVersion'); $('libtorrentVersion').textContent = buildInfo.libtorrent;
$('qbittorrentVersion').textContent += ` (${LocalPreferences.get('buildInfo.bitness')}-bit)`; $('boostVersion').textContent = buildInfo.boost;
$('opensslVersion').textContent = buildInfo.openssl;
$('zlibVersion').textContent = buildInfo.zlib;
$('qbittorrentVersion').textContent += ` (${buildInfo.bitness}-bit)`;
</script> </script>

View file

@ -1575,7 +1575,9 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
}; };
// hide entries // hide entries
const libtorrentVersion = window.qBittorrent.Misc.parseVersion(LocalPreferences.get('buildInfo.libtorrentVersion', '')); const buildInfo = window.qBittorrent.Cache.buildInfo.get();
const libtorrentVersion = window.qBittorrent.Misc.parseVersion(buildInfo.libtorrent);
if (libtorrentVersion.valid) { if (libtorrentVersion.valid) {
if (libtorrentVersion.major >= 2) { if (libtorrentVersion.major >= 2) {
$('rowDiskCache').style.display = 'none'; $('rowDiskCache').style.display = 'none';
@ -1597,8 +1599,7 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
$('diskIOWriteModeWriteThrough').style.display = 'none'; $('diskIOWriteModeWriteThrough').style.display = 'none';
} }
const serverPlatform = LocalPreferences.get('buildInfo.platform', ''); if ((buildInfo.platform !== 'macos') && (buildInfo.platform !== 'windows'))
if ((serverPlatform !== 'macos') && (serverPlatform !== 'windows'))
$('rowMarkOfTheWeb').style.display = 'none'; $('rowMarkOfTheWeb').style.display = 'none';
// Behavior tab // Behavior tab