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. 249
      src/webui/www/public/scripts/client.js

249
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,140 +91,146 @@ 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) { var request = new Request.JSON({
waitingTrInfo = true; url : url,
var request = new Request.JSON({ noCache : true,
url : url, method : 'get',
noCache : true, onFailure : function () {
method : 'get', $('error_div').set('html', '_(qBittorrent client is not reachable)');
onFailure : function () { loadTransferInfoTimer = loadTransferInfo.delay(4000);
$('error_div').set('html', '_(qBittorrent client is not reachable)'); },
waitingTrInfo = false; onSuccess : function (info) {
loadTransferInfo.delay(4000); if (info) {
}, $("DlInfos").set('html', "_(D: %1 - T: %2)"
onSuccess : function (info) { .replace("%1", friendlyUnit(info.dl_info_speed, true))
if (info) { .replace("%2", friendlyUnit(info.dl_info_data, false)));
$("DlInfos").set('html', "_(D: %1 - T: %2)" $("UpInfos").set('html', "_(U: %1 - T: %2)"
.replace("%1", friendlyUnit(info.dl_info_speed, true)) .replace("%1", friendlyUnit(info.up_info_speed, true))
.replace("%2", friendlyUnit(info.dl_info_data, false))); .replace("%2", friendlyUnit(info.up_info_data, false)));
$("UpInfos").set('html', "_(U: %1 - T: %2)" if(localStorage.getItem('speed_in_browser_title_bar') == 'true')
.replace("%1", 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));
.replace("%2", friendlyUnit(info.up_info_data, false))); else
if(localStorage.getItem('speed_in_browser_title_bar') == 'true') document.title = "_(qBittorrent web User Interface)";
document.title = "_(D:%1 U:%2)".replace("%1", friendlyUnit(info.dl_info_speed, true)).replace("%2", friendlyUnit(info.up_info_speed, true)); loadTransferInfoTimer = loadTransferInfo.delay(3000);
else }
document.title = "_(qBittorrent web User Interface)"; }
waitingTrInfo = false; }).send();
loadTransferInfo.delay(3000);
}
}
}).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) { var request = new Request.JSON({
waiting = true; url : url,
var request = new Request.JSON({ noCache : true,
url : url, method : 'get',
noCache : true, onFailure : function () {
method : 'get', $('error_div').set('html', '_(qBittorrent client is not reachable)');
onFailure : function () { ajaxfnTimer = ajaxfn.delay(2000);
$('error_div').set('html', '_(qBittorrent client is not reachable)'); },
waiting = false; onSuccess : function (events) {
ajaxfn.delay(2000); $('error_div').set('html', '');
}, if (events) {
onSuccess : function (events) { // Add new torrents or update them
$('error_div').set('html', ''); torrent_hashes = myTable.getRowIds();
if (events) { events_hashes = new Array();
// Add new torrents or update them pos = 0;
torrent_hashes = myTable.getRowIds(); events.each(function (event) {
events_hashes = new Array(); events_hashes[events_hashes.length] = event.hash;
pos = 0; var row = new Array();
events.each(function (event) { var data = new Array();
events_hashes[events_hashes.length] = event.hash; row.length = 10;
var row = new Array(); row[0] = stateToImg(event.state);
var data = new Array(); row[1] = event.name;
row.length = 10; row[2] = event.priority > -1 ? event.priority : null;
row[0] = stateToImg(event.state); data[2] = event.priority;
row[1] = event.name; row[3] = friendlyUnit(event.size, false);
row[2] = event.priority > -1 ? event.priority : null; data[3] = event.size;
data[2] = event.priority; row[4] = (event.progress * 100).round(1);
row[3] = friendlyUnit(event.size, false); if (row[4] == 100.0 && event.progress != 1.0)
data[3] = event.size; row[4] = 99.9;
row[4] = (event.progress * 100).round(1); data[4] = event.progress;
if (row[4] == 100.0 && event.progress != 1.0) row[5] = event.num_seeds;
row[4] = 99.9; if (event.num_complete != -1)
data[4] = event.progress; row[5] += " (" + event.num_complete + ")";
row[5] = event.num_seeds; data[5] = event.num_seeds;
if (event.num_complete != -1) row[6] = event.num_leechs;
row[5] += " (" + event.num_complete + ")"; if (event.num_incomplete != -1)
data[5] = event.num_seeds; row[6] += " (" + event.num_incomplete + ")";
row[6] = event.num_leechs; data[6] = event.num_leechs;
if (event.num_incomplete != -1) row[7] = friendlyUnit(event.dlspeed, true);
row[6] += " (" + event.num_incomplete + ")"; data[7] = event.dlspeed;
data[6] = event.num_leechs; row[8] = friendlyUnit(event.upspeed, true);
row[7] = friendlyUnit(event.dlspeed, true); data[8] = event.upspeed;
data[7] = event.dlspeed; row[9] = friendlyDuration(event.eta);
row[8] = friendlyUnit(event.upspeed, true); data[9] = event.eta;
data[8] = event.upspeed; if (event.ratio == -1)
row[9] = friendlyDuration(event.eta); row[10] = "∞";
data[9] = event.eta; else
if (event.ratio == -1) row[10] = (Math.floor(100 * event.ratio) / 100).toFixed(2); //Don't round up
row[10] = "∞"; data[10] = event.ratio;
else if (row[2] != null)
row[10] = (Math.floor(100 * event.ratio) / 100).toFixed(2); //Don't round up queueing_enabled = true;
data[10] = event.ratio; if (!torrent_hashes.contains(event.hash)) {
if (row[2] != null) // New unfinished torrent
queueing_enabled = true; torrent_hashes[torrent_hashes.length] = event.hash;
if (!torrent_hashes.contains(event.hash)) { //alert("Inserting row");
// New unfinished torrent myTable.insertRow(event.hash, row, data, event.state, pos);
torrent_hashes[torrent_hashes.length] = event.hash; } else {
//alert("Inserting row"); // Update torrent data
myTable.insertRow(event.hash, row, data, event.state, pos); myTable.updateRow(event.hash, row, data, event.state, pos);
} else { }
// Update torrent data
myTable.updateRow(event.hash, row, data, event.state, pos); pos++;
} });
// Remove deleted torrents
pos++; torrent_hashes.each(function (hash) {
}); if (!events_hashes.contains(hash)) {
// Remove deleted torrents myTable.removeRow(hash);
torrent_hashes.each(function (hash) {
if (!events_hashes.contains(hash)) {
myTable.removeRow(hash);
}
});
if (queueing_enabled) {
$('queueingButtons').removeClass('invisible');
myTable.showPriority();
} else {
$('queueingButtons').addClass('invisible');
myTable.hidePriority();
}
myTable.altRow();
} }
waiting = false; });
ajaxfn.delay(1500); if (queueing_enabled) {
$('queueingButtons').removeClass('invisible');
myTable.showPriority();
} else {
$('queueingButtons').addClass('invisible');
myTable.hidePriority();
} }
}).send();
} myTable.altRow();
}
ajaxfnTimer = ajaxfn.delay(1500);
}
}).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