Browse Source

Improve debounce functionality of REST requests

Lode Hoste 11 năm trước cách đây
mục cha
commit
f4b6704aad
1 tập tin đã thay đổi với 57 bổ sung67 xóa
  1. 57 67
      gui/app.js

+ 57 - 67
gui/app.js

@@ -253,15 +253,15 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
 
     function refreshRepo(repo) {
         var key = "refreshRepo" + repo;
-        if (!debouncedFuncs[key]) {
-            debouncedFuncs[key] = debounce(function () {
-                $http.get(urlbase + '/model?repo=' + encodeURIComponent(repo)).success(function (data) {
-                    $scope.model[repo] = data;
-                    console.log("refreshRepo", repo, data);
-                });
-            }, 1000, true);
+        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+            return;
         }
-        debouncedFuncs[key]();
+        debouncedFuncs[key] = Date.now() + 300000;
+        $http.get(urlbase + '/model?repo=' + encodeURIComponent(repo)).success(function (data) {
+            debouncedFuncs[key] = Date.now() + 5000;
+            $scope.model[repo] = data;
+            console.log("refreshRepo", repo, data);
+        });
     }
 
     function updateLocalConfig(config) {
@@ -292,7 +292,13 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
     }
 
     function refreshSystem() {
+        var key = "refreshSystem"
+        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+            return;
+        }
+        debouncedFuncs[key] = Date.now() + 300000;
         $http.get(urlbase + '/system').success(function (data) {
+            debouncedFuncs[key] = Date.now() + 500;
             $scope.myID = data.myID;
             $scope.system = data;
             console.log("refreshSystem", data);
@@ -305,34 +311,40 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
         }
 
         var key = "refreshCompletion" + node + repo;
-        if (!debouncedFuncs[key]) {
-            debouncedFuncs[key] = debounce(function () {
-                $http.get(urlbase + '/completion?node=' + node + '&repo=' + encodeURIComponent(repo)).success(function (data) {
-                    if (!$scope.completion[node]) {
-                        $scope.completion[node] = {};
-                    }
-                    $scope.completion[node][repo] = data.completion;
-
-                    var tot = 0,
-                        cnt = 0;
-                    for (var cmp in $scope.completion[node]) {
-                        if (cmp === "_total") {
-                            continue;
-                        }
-                        tot += $scope.completion[node][cmp];
-                        cnt += 1;
-                    }
-                    $scope.completion[node]._total = tot / cnt;
-
-                    console.log("refreshCompletion", node, repo, $scope.completion[node]);
-                });
-            }, 1000, true);
+        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+            return;
         }
-        debouncedFuncs[key]();
+        debouncedFuncs[key] = Date.now() + 300000;
+        $http.get(urlbase + '/completion?node=' + node + '&repo=' + encodeURIComponent(repo)).success(function (data) {
+            debouncedFuncs[key] = Date.now() + 5000;
+            if (!$scope.completion[node]) {
+                $scope.completion[node] = {};
+            }
+            $scope.completion[node][repo] = data.completion;
+
+            var tot = 0,
+                cnt = 0;
+            for (var cmp in $scope.completion[node]) {
+                if (cmp === "_total") {
+                    continue;
+                }
+                tot += $scope.completion[node][cmp];
+                cnt += 1;
+            }
+            $scope.completion[node]._total = tot / cnt;
+
+            console.log("refreshCompletion", node, repo, $scope.completion[node]);
+        });
     }
 
     function refreshConnectionStats() {
+        var key = "refreshConnectionStats"
+        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+            return;
+        }
+        debouncedFuncs[key] = Date.now() + 300000;
         $http.get(urlbase + '/connections').success(function (data) {
+            debouncedFuncs[key] = Date.now() + 500;
             var now = Date.now(),
                 td = (now - prevDate) / 1000,
                 id;
@@ -356,7 +368,13 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
     }
 
     function refreshErrors() {
+        var key = "refreshErrors"
+        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+            return;
+        }
+        debouncedFuncs[key] = Date.now() + 300000;
         $http.get(urlbase + '/errors').success(function (data) {
+            debouncedFuncs[key] = Date.now() + 500;
             $scope.errors = data.errors;
             console.log("refreshErrors", data);
         });
@@ -373,8 +391,14 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
         });
     }
 
-    var refreshNodeStats = debounce(function () {
+    var refreshNodeStats = function () {
+        var key = "refreshNode";
+        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+            return;
+        }
+        debouncedFuncs[key] = Date.now() + 300000;
         $http.get(urlbase + "/stats/node").success(function (data) {
+            debouncedFuncs[key] = Date.now() + 500;
             $scope.stats = data;
             for (var node in $scope.stats) {
                 $scope.stats[node].LastSeen = new Date($scope.stats[node].LastSeen);
@@ -382,7 +406,7 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
             }
             console.log("refreshNodeStats", data);
         });
-    }, 500);
+    }
 
     $scope.init = function () {
         refreshSystem();
@@ -995,40 +1019,6 @@ function isEmptyObject(obj) {
     return true;
 }
 
-function debounce(func, wait) {
-    var timeout, args, context, timestamp, result, again;
-
-    var later = function () {
-        var last = Date.now() - timestamp;
-        if (last < wait) {
-            timeout = setTimeout(later, wait - last);
-        } else {
-            timeout = null;
-            if (again) {
-                again = false;
-                result = func.apply(context, args);
-                context = args = null;
-            }
-        }
-    };
-
-    return function () {
-        context = this;
-        args = arguments;
-        timestamp = Date.now();
-        var callNow = !timeout;
-        if (!timeout) {
-            timeout = setTimeout(later, wait);
-            result = func.apply(context, args);
-            context = args = null;
-        } else {
-            again = true;
-        }
-
-        return result;
-    };
-}
-
 syncthing.filter('natural', function () {
     return function (input, valid) {
         return input.toFixed(decimals(input, valid));