From 963a7faab8a6015ae0e885fa9397df4498676b96 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 10 Feb 2024 15:26:16 +0800 Subject: [PATCH] Migrate to Cache for commonly used data Previously it was abusing the `localStorage` and now it is storing data in memory (per session). --- src/webui/www/private/scripts/cache.js | 46 +++++++++++++++- src/webui/www/private/scripts/client.js | 57 +++++--------------- src/webui/www/private/views/about.html | 17 +++--- src/webui/www/private/views/preferences.html | 7 +-- 4 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/webui/www/private/scripts/cache.js b/src/webui/www/private/scripts/cache.js index bf5a35609..aab1433be 100644 --- a/src/webui/www/private/scripts/cache.js +++ b/src/webui/www/private/scripts/cache.js @@ -34,10 +34,33 @@ if (window.qBittorrent === undefined) window.qBittorrent.Cache = (() => { const exports = () => { 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 { #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(); })(); diff --git a/src/webui/www/private/scripts/client.js b/src/webui/www/private/scripts/client.js index 9aeec493b..05847c73b 100644 --- a/src/webui/www/private/scripts/client.js +++ b/src/webui/www/private/scripts/client.js @@ -34,8 +34,7 @@ window.qBittorrent.Client = (() => { return { closeWindows: closeWindows, genHash: genHash, - getSyncMainDataInterval: getSyncMainDataInterval, - qbtVersion: qbtVersion + getSyncMainDataInterval: getSyncMainDataInterval }; }; @@ -57,10 +56,6 @@ window.qBittorrent.Client = (() => { return customSyncMainDataInterval ? customSyncMainDataInterval : serverSyncMainDataInterval; }; - const qbtVersion = function() { - return LocalPreferences.get('qbtVersion', ''); - }; - return exports(); })(); Object.freeze(window.qBittorrent.Client); @@ -110,7 +105,7 @@ let selected_filter = LocalPreferences.get('selected_filter', 'all'); let setFilter = function() {}; let toggleFilterDisplay = function() {}; -window.addEvent('load', function() { +window.addEventListener("DOMContentLoaded", function() { const saveColumnSizes = function() { const filters_width = $('Filters').getSize().x; 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_info_data, false) + ")"; $("UpInfos").set('html', transfer_info); + + const qbtVersion = window.qBittorrent.Cache.qbtVersion.get(); + 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]"; } 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))); $('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(); - - window.parent.qBittorrent.Cache.preferences.init(); - - // fetch qbt version and store it locally - new Request({ - url: 'api/v2/app/version', - method: 'get', - 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(); +}); + +window.addEventListener("load", () => { + // fetch various data and store it in memory + window.qBittorrent.Cache.buildInfo.init(); + window.qBittorrent.Cache.preferences.init(); + window.qBittorrent.Cache.qbtVersion.init(); }); diff --git a/src/webui/www/private/views/about.html b/src/webui/www/private/views/about.html index c6276a0a5..e228e5c91 100644 --- a/src/webui/www/private/views/about.html +++ b/src/webui/www/private/views/about.html @@ -843,11 +843,14 @@ diff --git a/src/webui/www/private/views/preferences.html b/src/webui/www/private/views/preferences.html index 3ff1d1072..00b7212f4 100644 --- a/src/webui/www/private/views/preferences.html +++ b/src/webui/www/private/views/preferences.html @@ -1575,7 +1575,9 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD }; // 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.major >= 2) { $('rowDiskCache').style.display = 'none'; @@ -1597,8 +1599,7 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD $('diskIOWriteModeWriteThrough').style.display = 'none'; } - const serverPlatform = LocalPreferences.get('buildInfo.platform', ''); - if ((serverPlatform !== 'macos') && (serverPlatform !== 'windows')) + if ((buildInfo.platform !== 'macos') && (buildInfo.platform !== 'windows')) $('rowMarkOfTheWeb').style.display = 'none'; // Behavior tab