Browse Source

WebUI: Fix increasing frequency of XHR requests

ajaxfn() is called every time the torrent list is filtered. This function
calls itself after a predefined amount of time by setting a timer. If
the function is called manually without destroying the timer, the
frequency of the XHR requests will increase.

Fix this by saving a reference to each timer that is created so that
they can be later destroyed.

Add also two helper functions to request updated data safely.
adaptive-webui-19844
Gabriele 10 years ago
parent
commit
2226f166ac
  1. 47
      src/webui/www/public/scripts/client.js

47
src/webui/www/public/scripts/client.js

@ -23,7 +23,6 @@
*/ */
myTable = new dynamicTable(); myTable = new dynamicTable();
ajaxfn = function () {};
window.addEvent('load', function () { window.addEvent('load', function () {
@ -77,8 +76,6 @@ window.addEvent('load', function () {
}); });
initializeWindows(); initializeWindows();
var r = 0; var r = 0;
var waiting = false;
var waitingTrInfo = false;
var stateToImg = function (state) { var stateToImg = function (state) {
if (state == "pausedUP" || state == "pausedDL") { if (state == "pausedUP" || state == "pausedDL") {
@ -94,18 +91,17 @@ window.addEvent('load', function () {
} }
return 'images/skin/' + state + '.png'; return 'images/skin/' + state + '.png';
}; };
var loadTransferInfoTimer;
var loadTransferInfo = function () { var loadTransferInfo = function () {
var url = 'json/transferInfo'; var url = 'json/transferInfo';
if (!waitingTrInfo) {
waitingTrInfo = true;
var request = new Request.JSON({ var request = new Request.JSON({
url : url, url : url,
noCache : true, noCache : true,
method : 'get', method : 'get',
onFailure : function () { onFailure : function () {
$('error_div').set('html', '_(qBittorrent client is not reachable)'); $('error_div').set('html', '_(qBittorrent client is not reachable)');
waitingTrInfo = false; loadTransferInfoTimer = loadTransferInfo.delay(4000);
loadTransferInfo.delay(4000);
}, },
onSuccess : function (info) { onSuccess : function (info) {
if (info) { if (info) {
@ -119,32 +115,37 @@ window.addEvent('load', function () {
document.title = "_(D:%1 U:%2)".replace("%1", friendlyUnit(info.dl_info_speed, true)).replace("%2", friendlyUnit(info.up_info_speed, true)); document.title = "_(D:%1 U:%2)".replace("%1", friendlyUnit(info.dl_info_speed, true)).replace("%2", friendlyUnit(info.up_info_speed, true));
else else
document.title = "_(qBittorrent web User Interface)"; document.title = "_(qBittorrent web User Interface)";
waitingTrInfo = false; loadTransferInfoTimer = loadTransferInfo.delay(3000);
loadTransferInfo.delay(3000);
} }
} }
}).send(); }).send();
}
}; };
var updateTransferInfo = function() {
clearTimeout(loadTransferInfoTimer);
loadTransferInfoTimer = loadTransferInfo();
}
// Start fetching data now
loadTransferInfo();
$('DlInfos').addEvent('click', globalDownloadLimitFN); $('DlInfos').addEvent('click', globalDownloadLimitFN);
$('UpInfos').addEvent('click', globalUploadLimitFN); $('UpInfos').addEvent('click', globalUploadLimitFN);
var ajaxfnTimer;
var ajaxfn = function () { var ajaxfn = function () {
var queueing_enabled = false; var queueing_enabled = false;
var url = new URI('json/torrents'); var url = new URI('json/torrents');
url.setData('filter', filter); url.setData('filter', filter);
url.setData('sort', myTable.table.sortedColumn); url.setData('sort', myTable.table.sortedColumn);
url.setData('reverse', myTable.table.reverseSort); url.setData('reverse', myTable.table.reverseSort);
if (!waiting) {
waiting = true;
var request = new Request.JSON({ var request = new Request.JSON({
url : url, url : url,
noCache : true, noCache : true,
method : 'get', method : 'get',
onFailure : function () { onFailure : function () {
$('error_div').set('html', '_(qBittorrent client is not reachable)'); $('error_div').set('html', '_(qBittorrent client is not reachable)');
waiting = false; ajaxfnTimer = ajaxfn.delay(2000);
ajaxfn.delay(2000);
}, },
onSuccess : function (events) { onSuccess : function (events) {
$('error_div').set('html', ''); $('error_div').set('html', '');
@ -217,17 +218,19 @@ window.addEvent('load', function () {
myTable.altRow(); myTable.altRow();
} }
waiting = false; ajaxfnTimer = ajaxfn.delay(1500);
ajaxfn.delay(1500);
} }
}).send(); }).send();
}
}; };
var updateTransferList = function() {
clearTimeout(ajaxfnTimer);
ajaxfnTimer = ajaxfn();
}
setSortedColumn = function (column) { setSortedColumn = function (column) {
myTable.setSortedColumn(column); myTable.setSortedColumn(column);
// reload torrents updateTransferList();
ajaxfn();
}; };
new MochaUI.Panel({ new MochaUI.Panel({
@ -243,7 +246,7 @@ window.addEvent('load', function () {
loadMethod : 'xhr', loadMethod : 'xhr',
contentURL : 'transferlist.html', contentURL : 'transferlist.html',
onContentLoaded : function () { onContentLoaded : function () {
ajaxfn(); updateTransferList();
}, },
column : 'mainColumn', column : 'mainColumn',
onResize : saveColumnSizes, onResize : saveColumnSizes,
@ -272,8 +275,6 @@ window.addEvent('load', function () {
column : 'mainColumn', column : 'mainColumn',
height : prop_h height : prop_h
}); });
//ajaxfn();
loadTransferInfo();
setFilter = function (f) { setFilter = function (f) {
// Visually Select the right filter // Visually Select the right filter
@ -287,7 +288,7 @@ window.addEvent('load', function () {
filter = f; filter = f;
localStorage.setItem('selected_filter', f); localStorage.setItem('selected_filter', f);
// Reload torrents // Reload torrents
ajaxfn(); updateTransferList();
} }
}); });

Loading…
Cancel
Save