Skip to content

Commit 9d494e8

Browse files
authored
Merge pull request #21117 from Chocobo1/webui_interval
WebUI: timer related clean ups
2 parents 69a829d + bf7e151 commit 9d494e8

File tree

11 files changed

+52
-49
lines changed

11 files changed

+52
-49
lines changed

src/webui/www/private/scripts/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ window.addEventListener("DOMContentLoaded", () => {
705705
};
706706
})();
707707

708-
let syncMainDataTimeoutID;
708+
let syncMainDataTimeoutID = -1;
709709
let syncRequestInProgress = false;
710710
const syncMainData = function() {
711711
const url = new URI("api/v2/sync/maindata");
@@ -889,6 +889,7 @@ window.addEventListener("DOMContentLoaded", () => {
889889
return;
890890

891891
clearTimeout(syncMainDataTimeoutID);
892+
syncMainDataTimeoutID = -1;
892893

893894
if (window.qBittorrent.Client.isStopped())
894895
return;

src/webui/www/private/scripts/dynamicTable.js

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -91,54 +91,43 @@ window.qBittorrent.DynamicTable ??= (() => {
9191
},
9292

9393
setupCommonEvents: function() {
94-
const scrollFn = function() {
95-
$(this.dynamicTableFixedHeaderDivId).getElements("table")[0].style.left = -$(this.dynamicTableDivId).scrollLeft + "px";
96-
}.bind(this);
94+
const tableDiv = $(this.dynamicTableDivId);
95+
const tableFixedHeaderDiv = $(this.dynamicTableFixedHeaderDivId);
9796

98-
$(this.dynamicTableDivId).addEvent("scroll", scrollFn);
97+
tableDiv.addEventListener("scroll", () => {
98+
tableFixedHeaderDiv.getElements("table")[0].style.left = `${-tableDiv.scrollLeft}px`;
99+
});
99100

100101
// if the table exists within a panel
101-
if ($(this.dynamicTableDivId).getParent(".panel")) {
102-
const resizeFn = function() {
103-
const panel = $(this.dynamicTableDivId).getParent(".panel");
104-
let h = panel.getBoundingClientRect().height - $(this.dynamicTableFixedHeaderDivId).getBoundingClientRect().height;
105-
$(this.dynamicTableDivId).style.height = h + "px";
102+
const parentPanel = tableDiv.getParent(".panel");
103+
if (parentPanel) {
104+
const resizeFn = (entries) => {
105+
const panel = entries[0].target;
106+
let h = panel.getBoundingClientRect().height - tableFixedHeaderDiv.getBoundingClientRect().height;
107+
tableDiv.style.height = `${h}px`;
106108

107109
// Workaround due to inaccurate calculation of elements heights by browser
108-
109110
let n = 2;
110111

111112
// is panel vertical scrollbar visible or does panel content not fit?
112113
while (((panel.clientWidth !== panel.offsetWidth) || (panel.clientHeight !== panel.scrollHeight)) && (n > 0)) {
113114
--n;
114115
h -= 0.5;
115-
$(this.dynamicTableDivId).style.height = h + "px";
116+
tableDiv.style.height = `${h}px`;
116117
}
118+
};
117119

118-
this.lastPanelHeight = panel.getBoundingClientRect().height;
119-
}.bind(this);
120-
121-
$(this.dynamicTableDivId).getParent(".panel").addEvent("resize", resizeFn);
122-
123-
this.lastPanelHeight = 0;
124-
125-
// Workaround. Resize event is called not always (for example it isn't called when browser window changes it's size)
126-
127-
const checkResizeFn = function() {
128-
const tableDiv = $(this.dynamicTableDivId);
129-
130-
// dynamicTableDivId is not visible on the UI
131-
if (!tableDiv)
132-
return;
133-
134-
const panel = tableDiv.getParent(".panel");
135-
if (this.lastPanelHeight !== panel.getBoundingClientRect().height) {
136-
this.lastPanelHeight = panel.getBoundingClientRect().height;
137-
panel.fireEvent("resize");
138-
}
139-
}.bind(this);
120+
this.resizeDebounceTimer = -1;
121+
const resizeDebouncer = (entries) => {
122+
clearTimeout(this.resizeDebounceTimer);
123+
this.resizeDebounceTimer = setTimeout(() => {
124+
resizeFn(entries);
125+
resizeDebounceTimer = -1;
126+
}, 100);
127+
};
140128

141-
setInterval(checkResizeFn, 500);
129+
const resizeObserver = new ResizeObserver(resizeDebouncer);
130+
resizeObserver.observe(parentPanel, { box: "border-box" });
142131
}
143132
},
144133

src/webui/www/private/scripts/piecesbar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ window.qBittorrent.PiecesBar ??= (() => {
8888
if (vals.width > 0)
8989
obj.setPieces(vals.pieces);
9090
else
91-
setTimeout(() => { checkForParent(obj.id); }, 1);
91+
setTimeout(() => { checkForParent(obj.id); });
9292

9393
return obj;
9494
}
@@ -258,7 +258,7 @@ window.qBittorrent.PiecesBar ??= (() => {
258258
if (!obj)
259259
return;
260260
if (!obj.parentNode)
261-
return setTimeout(() => { checkForParent(id); }, 1);
261+
return setTimeout(() => { checkForParent(id); }, 100);
262262

263263
obj.refresh();
264264
}

src/webui/www/private/scripts/progressbar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ window.qBittorrent.ProgressBar ??= (() => {
104104
if (vals.width)
105105
obj.setValue(vals.value);
106106
else
107-
setTimeout('ProgressBar_checkForParent("' + obj.id + '")', 1);
107+
setTimeout('ProgressBar_checkForParent("' + obj.id + '")');
108108
return obj;
109109
}
110110
});
@@ -144,7 +144,7 @@ window.qBittorrent.ProgressBar ??= (() => {
144144
if (!obj)
145145
return;
146146
if (!obj.parentNode)
147-
return setTimeout('ProgressBar_checkForParent("' + id + '")', 1);
147+
return setTimeout('ProgressBar_checkForParent("' + id + '")', 100);
148148
obj.setStyle("width", "100%");
149149
const w = obj.offsetWidth;
150150
obj.vals.dark.setStyle("width", w);

src/webui/www/private/scripts/prop-files.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ window.qBittorrent.PropFiles ??= (() => {
306306
return;
307307

308308
clearTimeout(loadTorrentFilesDataTimer);
309+
loadTorrentFilesDataTimer = -1;
310+
309311
new Request({
310312
url: "api/v2/torrents/filePrio",
311313
method: "post",
@@ -331,7 +333,7 @@ window.qBittorrent.PropFiles ??= (() => {
331333
torrentFilesTable.updateTable(false);
332334
};
333335

334-
let loadTorrentFilesDataTimer;
336+
let loadTorrentFilesDataTimer = -1;
335337
const loadTorrentFilesData = function() {
336338
if ($("prop_files").hasClass("invisible")
337339
|| $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
@@ -378,6 +380,7 @@ window.qBittorrent.PropFiles ??= (() => {
378380

379381
const updateData = function() {
380382
clearTimeout(loadTorrentFilesDataTimer);
383+
loadTorrentFilesDataTimer = -1;
381384
loadTorrentFilesData();
382385
};
383386

src/webui/www/private/scripts/prop-general.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ window.qBittorrent.PropGeneral ??= (() => {
7272
piecesBar.clear();
7373
};
7474

75-
let loadTorrentDataTimer;
75+
let loadTorrentDataTimer = -1;
7676
const loadTorrentData = function() {
7777
if ($("prop_general").hasClass("invisible")
7878
|| $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
@@ -250,6 +250,7 @@ window.qBittorrent.PropGeneral ??= (() => {
250250

251251
const updateData = function() {
252252
clearTimeout(loadTorrentDataTimer);
253+
loadTorrentDataTimer = -1;
253254
loadTorrentData();
254255
};
255256

src/webui/www/private/scripts/prop-peers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ window.qBittorrent.PropPeers ??= (() => {
3737
};
3838

3939
const torrentPeersTable = new window.qBittorrent.DynamicTable.TorrentPeersTable();
40-
let loadTorrentPeersTimer;
40+
let loadTorrentPeersTimer = -1;
4141
let syncTorrentPeersLastResponseId = 0;
4242
let show_flags = true;
4343

@@ -109,6 +109,7 @@ window.qBittorrent.PropPeers ??= (() => {
109109

110110
const updateData = function() {
111111
clearTimeout(loadTorrentPeersTimer);
112+
loadTorrentPeersTimer = -1;
112113
loadTorrentPeersData();
113114
};
114115

src/webui/www/private/scripts/prop-trackers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ window.qBittorrent.PropTrackers ??= (() => {
3939
let current_hash = "";
4040

4141
const torrentTrackersTable = new window.qBittorrent.DynamicTable.TorrentTrackersTable();
42-
let loadTrackersDataTimer;
42+
let loadTrackersDataTimer = -1;
4343

4444
const loadTrackersData = function() {
4545
if ($("prop_trackers").hasClass("invisible")
@@ -119,6 +119,7 @@ window.qBittorrent.PropTrackers ??= (() => {
119119

120120
const updateData = function() {
121121
clearTimeout(loadTrackersDataTimer);
122+
loadTrackersDataTimer = -1;
122123
loadTrackersData();
123124
};
124125

src/webui/www/private/scripts/prop-webseeds.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ window.qBittorrent.PropWebseeds ??= (() => {
8888

8989
let current_hash = "";
9090

91-
let loadWebSeedsDataTimer;
91+
let loadWebSeedsDataTimer = -1;
9292
const loadWebSeedsData = function() {
9393
if ($("prop_webseeds").hasClass("invisible")
9494
|| $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
@@ -138,6 +138,7 @@ window.qBittorrent.PropWebseeds ??= (() => {
138138

139139
const updateData = function() {
140140
clearTimeout(loadWebSeedsDataTimer);
141+
loadWebSeedsDataTimer = -1;
141142
loadWebSeedsData();
142143
};
143144

src/webui/www/private/scripts/search.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ window.qBittorrent.Search ??= (() => {
4747
};
4848

4949
const searchTabIdPrefix = "Search-";
50-
let loadSearchPluginsTimer;
50+
let loadSearchPluginsTimer = -1;
5151
const searchPlugins = [];
5252
let prevSearchPluginsResponse;
5353
let selectedCategory = "QBT_TR(All categories)QBT_TR[CONTEXT=SearchEngineWidget]";
@@ -207,7 +207,7 @@ window.qBittorrent.Search ??= (() => {
207207
rowId: 0,
208208
selectedRowIds: [],
209209
running: true,
210-
loadResultsTimer: null,
210+
loadResultsTimer: -1,
211211
sort: { column: searchResultsTable.sortedColumn, reverse: searchResultsTable.reverseSort },
212212
});
213213
updateSearchResultsData(searchId);
@@ -507,6 +507,7 @@ window.qBittorrent.Search ??= (() => {
507507
},
508508
onClose: function() {
509509
clearTimeout(loadSearchPluginsTimer);
510+
loadSearchPluginsTimer = -1;
510511
}
511512
});
512513
}
@@ -569,6 +570,7 @@ window.qBittorrent.Search ??= (() => {
569570
if (state) {
570571
state.running = false;
571572
clearTimeout(state.loadResultsTimer);
573+
state.loadResultsTimer = -1;
572574
}
573575
};
574576

src/webui/www/private/views/rss.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
const serverSyncRssDataInterval = 1500;
181181
let feedData = {};
182182
let pathByFeedId = new Map();
183-
let feedRefreshTimer;
183+
let feedRefreshTimer = -1;
184184
const rssFeedTable = new window.qBittorrent.DynamicTable.RssFeedTable();
185185
const rssArticleTable = new window.qBittorrent.DynamicTable.RssArticleTable();
186186

@@ -293,11 +293,15 @@
293293
};
294294

295295
const unload = () => {
296-
clearInterval(feedRefreshTimer);
296+
clearTimeout(feedRefreshTimer);
297+
feedRefreshTimer = -1;
297298
};
298299

299300
const load = () => {
300-
feedRefreshTimer = setInterval(updateRssFeedList, serverSyncRssDataInterval);
301+
feedRefreshTimer = setTimeout(() => {
302+
updateRssFeedList();
303+
load();
304+
}, serverSyncRssDataInterval);
301305
};
302306

303307
const addRSSFeed = () => {

0 commit comments

Comments
 (0)