[WebUI] use setTimeout instead of setInterval

When server is busy or the request is slow for big file list, WebUI still
requests for new update blindly. "Connection lost" is often triggerd.

Change to only ask for update 2s after reponse (either success or error)

Signed-off-by: Xuefer H <xuefer@gmail.com>
Closes: https://github.com/deluge-torrent/deluge/pull/416
This commit is contained in:
Xuefer H 2023-04-11 16:16:37 +08:00 committed by Calum Lind
parent 39bd97f03e
commit 71b634e968
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
2 changed files with 42 additions and 10 deletions

View File

@ -162,13 +162,23 @@ Deluge.ConnectionManager = Ext.extend(Ext.Window, {
}, },
update: function () { update: function () {
this.updating = setTimeout(this.update, 2000);
this.list.getStore().each(function (r) { this.list.getStore().each(function (r) {
deluge.client.web.get_host_status(r.id, { deluge.client.web.get_host_status(r.id, {
success: this.onGetHostStatus, success: this.onUpdate,
scope: this, scope: this,
}); });
}, this); }, this);
}, },
onUpdate: function (host) {
if (!this.isVisible()) return;
this.onGetHostStatus(host);
if (this.updating) {
clearTimeout(this.updating);
}
this.updating = setTimeout(this.update, 2000);
},
/** /**
* Updates the buttons in the Connection Manager UI according to the * Updates the buttons in the Connection Manager UI according to the
@ -312,7 +322,10 @@ Deluge.ConnectionManager = Ext.extend(Ext.Window, {
// private // private
onHide: function () { onHide: function () {
if (this.running) window.clearInterval(this.running); if (this.updating) {
window.clearTimeout(this.updating);
this.updating = undefined;
}
}, },
// private // private
@ -396,8 +409,8 @@ Deluge.ConnectionManager = Ext.extend(Ext.Window, {
this.stopHostButton = bbar.items.get('cm-stop'); this.stopHostButton = bbar.items.get('cm-stop');
} }
this.loadHosts(); this.loadHosts();
if (this.running) return; if (this.updating) return;
this.running = window.setInterval(this.update, 2000, this); this.updating = window.setTimeout(this.update, 2000);
}, },
// private // private

View File

@ -134,14 +134,24 @@ deluge.ui = {
deluge.details.update(); deluge.details.update();
}, },
onConnectionError: function (error) {}, onConnectionError: function (error) {
if (this.checking) {
clearTimeout(this.checking);
}
this.checking = setTimeout(this.checkConnection, 2000);
},
onConnectionSuccess: function (result) { onConnectionSuccess: function (result) {
if (this.checking) {
clearTimeout(this.checking);
this.checking = undefined;
}
this.running = setTimeout(this.update, 2000);
this.update();
deluge.statusbar.setStatus({ deluge.statusbar.setStatus({
iconCls: 'x-deluge-statusbar icon-ok', iconCls: 'x-deluge-statusbar icon-ok',
text: _('Connection restored'), text: _('Connection restored'),
}); });
clearInterval(this.checking);
if (!result) { if (!result) {
deluge.connectionManager.show(); deluge.connectionManager.show();
} }
@ -159,9 +169,13 @@ deluge.ui = {
deluge.statusbar.setStatus({ deluge.statusbar.setStatus({
text: _('Lost connection to webserver'), text: _('Lost connection to webserver'),
}); });
this.checking = setInterval(this.checkConnection, 2000); this.checking = setTimeout(this.checkConnection, 2000);
} }
this.errorCount++; this.errorCount++;
if (this.running) {
clearTimeout(this.running);
this.running = undefined;
}
}, },
/** /**
@ -170,10 +184,15 @@ deluge.ui = {
* Updates the various components in the interface. * Updates the various components in the interface.
*/ */
onUpdate: function (data) { onUpdate: function (data) {
if (this.running) {
clearTimeout(this.running);
this.running = undefined;
}
if (!data['connected']) { if (!data['connected']) {
deluge.connectionManager.disconnect(true); deluge.connectionManager.disconnect(true);
return; return;
} }
this.running = setTimeout(this.update, 2000);
if (deluge.config.show_session_speed) { if (deluge.config.show_session_speed) {
document.title = document.title =
@ -201,7 +220,7 @@ deluge.ui = {
*/ */
onConnect: function () { onConnect: function () {
if (!this.running) { if (!this.running) {
this.running = setInterval(this.update, 2000); this.running = setTimeout(this.update, 2000);
this.update(); this.update();
} }
deluge.client.web.get_plugins({ deluge.client.web.get_plugins({
@ -280,8 +299,8 @@ deluge.ui = {
*/ */
stop: function () { stop: function () {
if (this.running) { if (this.running) {
clearInterval(this.running); clearTimeout(this.running);
this.running = false; this.running = undefined;
deluge.torrents.getStore().removeAll(); deluge.torrents.getStore().removeAll();
} }
}, },