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.
This commit is contained in:
Gabriele 2014-11-26 21:26:29 +01:00
parent aab2c34257
commit 2226f166ac

View file

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