Sfoglia il codice sorgente

Move function-specific constants to the top and rename debouncedFcts

Lode Hoste 11 anni fa
parent
commit
4667cb9de9
1 ha cambiato i file con 22 aggiunte e 19 eliminazioni
  1. 22 19
      gui/app.js

+ 22 - 19
gui/app.js

@@ -9,6 +9,9 @@
 
 var syncthing = angular.module('syncthing', ['pascalprecht.translate']);
 var urlbase = 'rest';
+var requestTimeout = 300000; // Retry the REST request after 5 minutes without reply
+var refreshTimeout = 5000; // Do not refresh the generic status faster than 5 seconds
+var refreshQuickTimeout = 500; // Do not refresh the quick stats faster than 500 milliseconds
 
 syncthing.config(function ($httpProvider, $translateProvider) {
     $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token';
@@ -249,16 +252,16 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
         });
     });
 
-    var debouncedFuncs = {};
+    var nextCall = {};
 
     function refreshRepo(repo) {
         var key = "refreshRepo" + repo;
-        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+        if (nextCall[key] && nextCall[key] > Date.now()) {
             return;
         }
-        debouncedFuncs[key] = Date.now() + 300000;
+        nextCall[key] = Date.now() + requestTimeout;
         $http.get(urlbase + '/model?repo=' + encodeURIComponent(repo)).success(function (data) {
-            debouncedFuncs[key] = Date.now() + 5000;
+            nextCall[key] = Date.now() + refreshTimeout;
             $scope.model[repo] = data;
             console.log("refreshRepo", repo, data);
         });
@@ -293,12 +296,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
 
     function refreshSystem() {
         var key = "refreshSystem"
-        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+        if (nextCall[key] && nextCall[key] > Date.now()) {
             return;
         }
-        debouncedFuncs[key] = Date.now() + 300000;
+        nextCall[key] = Date.now() + requestTimeout;
         $http.get(urlbase + '/system').success(function (data) {
-            debouncedFuncs[key] = Date.now() + 500;
+            nextCall[key] = Date.now() + refreshQuickTimeout;
             $scope.myID = data.myID;
             $scope.system = data;
             console.log("refreshSystem", data);
@@ -311,12 +314,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
         }
 
         var key = "refreshCompletion" + node + repo;
-        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+        if (nextCall[key] && nextCall[key] > Date.now()) {
             return;
         }
-        debouncedFuncs[key] = Date.now() + 300000;
+        nextCall[key] = Date.now() + requestTimeout;
         $http.get(urlbase + '/completion?node=' + node + '&repo=' + encodeURIComponent(repo)).success(function (data) {
-            debouncedFuncs[key] = Date.now() + 5000;
+            nextCall[key] = Date.now() + refreshTimeout;
             if (!$scope.completion[node]) {
                 $scope.completion[node] = {};
             }
@@ -339,12 +342,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
 
     function refreshConnectionStats() {
         var key = "refreshConnectionStats"
-        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+        if (nextCall[key] && nextCall[key] > Date.now()) {
             return;
         }
-        debouncedFuncs[key] = Date.now() + 300000;
+        nextCall[key] = Date.now() + requestTimeout;
         $http.get(urlbase + '/connections').success(function (data) {
-            debouncedFuncs[key] = Date.now() + 500;
+            nextCall[key] = Date.now() + refreshQuickTimeout;
             var now = Date.now(),
                 td = (now - prevDate) / 1000,
                 id;
@@ -369,12 +372,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
 
     function refreshErrors() {
         var key = "refreshErrors"
-        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+        if (nextCall[key] && nextCall[key] > Date.now()) {
             return;
         }
-        debouncedFuncs[key] = Date.now() + 300000;
+        nextCall[key] = Date.now() + requestTimeout;
         $http.get(urlbase + '/errors').success(function (data) {
-            debouncedFuncs[key] = Date.now() + 500;
+            nextCall[key] = Date.now() + refreshQuickTimeout;
             $scope.errors = data.errors;
             console.log("refreshErrors", data);
         });
@@ -393,12 +396,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
 
     var refreshNodeStats = function () {
         var key = "refreshNode";
-        if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
+        if (nextCall[key] && nextCall[key] > Date.now()) {
             return;
         }
-        debouncedFuncs[key] = Date.now() + 300000;
+        nextCall[key] = Date.now() + requestTimeout;
         $http.get(urlbase + "/stats/node").success(function (data) {
-            debouncedFuncs[key] = Date.now() + 500;
+            nextCall[key] = Date.now() + refreshTimeout;
             $scope.stats = data;
             for (var node in $scope.stats) {
                 $scope.stats[node].LastSeen = new Date($scope.stats[node].LastSeen);