]> granicus.if.org Git - transmission/commitdiff
Replace setInterval() with setTimeout() for web UI refresh (patch by WGH)
authorMike Gelfand <mikedld@mikedld.com>
Sun, 15 Jan 2017 08:16:50 +0000 (11:16 +0300)
committerMike Gelfand <mikedld@mikedld.com>
Sun, 15 Jan 2017 08:17:31 +0000 (11:17 +0300)
Fixes: TRAC-6031
web/javascript/inspector.js
web/javascript/transmission.js

index 0ad78872baea65f7bf116ca191f9f001407ddc5a..14164dfcdf5793b52822009f8a01502e79bdb35f 100644 (file)
@@ -23,7 +23,7 @@ function Inspector(controller) {
             return false;
         },
 
-        refreshTorrents = function () {
+        refreshTorrents = function (callback) {
             var fields,
                 ids = $.map(data.torrents.slice(0), function (t) {
                     return t.getId();
@@ -36,7 +36,7 @@ function Inspector(controller) {
                     $.merge(fields, Torrent.Fields.InfoExtra);
                 }
 
-                data.controller.updateTorrents(ids, fields);
+                data.controller.updateTorrents(ids, fields, callback);
             }
         },
 
@@ -851,7 +851,8 @@ function Inspector(controller) {
      ****/
 
     this.setTorrents = function (torrents) {
-        var d = data;
+        var d = data,
+            that = this;
 
         // update the inspector when a selected torrent's data changes.
         $(d.torrents).unbind('dataChanged.inspector');
@@ -859,8 +860,17 @@ function Inspector(controller) {
         d.torrents = torrents;
 
         // periodically ask for updates to the inspector's torrents
-        clearInterval(d.refreshInterval);
-        d.refreshInterval = setInterval($.proxy(refreshTorrents, this), 2000);
+        clearTimeout(d.refreshTimeout);
+
+        function callback() {
+            refreshTorrents(rescheduleTimeout);
+        }
+
+        function rescheduleTimeout() {
+            d.refreshTimeout = setTimeout(callback, 2000);
+        }
+
+        rescheduleTimeout();
         refreshTorrents();
 
         // refresh the inspector's UI
index 748cebf3872920288dd05ccb0ff9fc68a28dde40..6502182a593d7b1fb314aa3ddd26bda8f5a1b54b 100644 (file)
@@ -113,12 +113,16 @@ Transmission.prototype = {
         this.updateButtonsSoon();
     },
 
-    loadDaemonPrefs: function (async) {
+    loadDaemonPrefs: function (async, callback) {
         this.remote.loadDaemonPrefs(function (data) {
             var o = data['arguments'];
             Prefs.getClutchPrefs(o);
             this.updateGuiFromSession(o);
             this.sessionProperties = o;
+
+            if (callback) {
+                callback();
+            }
         }, this, async);
     },
 
@@ -632,14 +636,23 @@ Transmission.prototype = {
 
     // turn the periodic ajax session refresh on & off
     togglePeriodicSessionRefresh: function (enabled) {
-        clearInterval(this.sessionInterval);
-        delete this.sessionInterval;
-        if (enabled) {
-            var callback = $.proxy(this.loadDaemonPrefs, this);
-            var msec = 8000;
+        var that = this,
+            msec = 8000;
 
-            this.sessionInterval = setInterval(callback, msec);
-        };
+        function callback() {
+            that.loadDaemonPrefs(undefined, rescheduleTimeout);
+        }
+
+        function rescheduleTimeout() {
+            that.sessionTimeout = setTimeout(callback, msec);
+        }
+
+        clearTimeout(this.sessionTimeout);
+        delete this.sessionTimeout;
+
+        if (enabled) {
+            rescheduleTimeout();
+        }
     },
 
     toggleTurtleClicked: function () {
@@ -829,8 +842,18 @@ Transmission.prototype = {
         }
     },
 
-    updateTorrents: function (ids, fields) {
-        this.remote.updateTorrents(ids, fields, this.updateFromTorrentGet, this);
+    updateTorrents: function (ids, fields, callback) {
+        var that = this;
+
+        function f(updates, removedIds) {
+            if (callback) {
+                callback();
+            }
+
+            that.updateFromTorrentGet(updates, removedIds);
+        }
+
+        this.remote.updateTorrents(ids, fields, f);
     },
 
     refreshTorrents: function () {
@@ -1676,20 +1699,32 @@ Transmission.prototype = {
 
     // turn the periodic ajax stats refresh on & off
     togglePeriodicStatsRefresh: function (enabled) {
-        clearInterval(this.statsInterval);
-        delete this.statsInterval;
+        var that = this,
+            msec = 5000;
 
-        if (enabled) {
-            var callback = $.proxy(this.loadDaemonStats, this);
-            var msec = 5000;
+        function callback() {
+            that.loadDaemonStats(undefined, rescheduleTimeout);
+        }
 
-            this.statsInterval = setInterval(callback, msec);
-        };
+        function rescheduleTimeout() {
+            that.statsTimeout = setTimeout(callback, msec);
+        }
+
+        clearTimeout(this.statsTimeout);
+        delete this.statsTimeout;
+
+        if (enabled) {
+            rescheduleTimeout();
+        }
     },
 
-    loadDaemonStats: function (async) {
+    loadDaemonStats: function (async, callback) {
         this.remote.loadDaemonStats(function (data) {
             this.updateStats(data['arguments']);
+
+            if (callback) {
+                callback();
+            }
         }, this, async);
     },