Move function-specific constants to the top and rename debouncedFcts

This commit is contained in:
Lode Hoste 2014-09-20 17:12:39 +02:00
parent f4b6704aad
commit 4667cb9de9

View File

@ -9,6 +9,9 @@
var syncthing = angular.module('syncthing', ['pascalprecht.translate']); var syncthing = angular.module('syncthing', ['pascalprecht.translate']);
var urlbase = 'rest'; 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) { syncthing.config(function ($httpProvider, $translateProvider) {
$httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token'; $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) { function refreshRepo(repo) {
var key = "refreshRepo" + repo; var key = "refreshRepo" + repo;
if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) { if (nextCall[key] && nextCall[key] > Date.now()) {
return; return;
} }
debouncedFuncs[key] = Date.now() + 300000; nextCall[key] = Date.now() + requestTimeout;
$http.get(urlbase + '/model?repo=' + encodeURIComponent(repo)).success(function (data) { $http.get(urlbase + '/model?repo=' + encodeURIComponent(repo)).success(function (data) {
debouncedFuncs[key] = Date.now() + 5000; nextCall[key] = Date.now() + refreshTimeout;
$scope.model[repo] = data; $scope.model[repo] = data;
console.log("refreshRepo", repo, data); console.log("refreshRepo", repo, data);
}); });
@ -293,12 +296,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
function refreshSystem() { function refreshSystem() {
var key = "refreshSystem" var key = "refreshSystem"
if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) { if (nextCall[key] && nextCall[key] > Date.now()) {
return; return;
} }
debouncedFuncs[key] = Date.now() + 300000; nextCall[key] = Date.now() + requestTimeout;
$http.get(urlbase + '/system').success(function (data) { $http.get(urlbase + '/system').success(function (data) {
debouncedFuncs[key] = Date.now() + 500; nextCall[key] = Date.now() + refreshQuickTimeout;
$scope.myID = data.myID; $scope.myID = data.myID;
$scope.system = data; $scope.system = data;
console.log("refreshSystem", data); console.log("refreshSystem", data);
@ -311,12 +314,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
} }
var key = "refreshCompletion" + node + repo; var key = "refreshCompletion" + node + repo;
if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) { if (nextCall[key] && nextCall[key] > Date.now()) {
return; return;
} }
debouncedFuncs[key] = Date.now() + 300000; nextCall[key] = Date.now() + requestTimeout;
$http.get(urlbase + '/completion?node=' + node + '&repo=' + encodeURIComponent(repo)).success(function (data) { $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]) { if (!$scope.completion[node]) {
$scope.completion[node] = {}; $scope.completion[node] = {};
} }
@ -339,12 +342,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
function refreshConnectionStats() { function refreshConnectionStats() {
var key = "refreshConnectionStats" var key = "refreshConnectionStats"
if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) { if (nextCall[key] && nextCall[key] > Date.now()) {
return; return;
} }
debouncedFuncs[key] = Date.now() + 300000; nextCall[key] = Date.now() + requestTimeout;
$http.get(urlbase + '/connections').success(function (data) { $http.get(urlbase + '/connections').success(function (data) {
debouncedFuncs[key] = Date.now() + 500; nextCall[key] = Date.now() + refreshQuickTimeout;
var now = Date.now(), var now = Date.now(),
td = (now - prevDate) / 1000, td = (now - prevDate) / 1000,
id; id;
@ -369,12 +372,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
function refreshErrors() { function refreshErrors() {
var key = "refreshErrors" var key = "refreshErrors"
if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) { if (nextCall[key] && nextCall[key] > Date.now()) {
return; return;
} }
debouncedFuncs[key] = Date.now() + 300000; nextCall[key] = Date.now() + requestTimeout;
$http.get(urlbase + '/errors').success(function (data) { $http.get(urlbase + '/errors').success(function (data) {
debouncedFuncs[key] = Date.now() + 500; nextCall[key] = Date.now() + refreshQuickTimeout;
$scope.errors = data.errors; $scope.errors = data.errors;
console.log("refreshErrors", data); console.log("refreshErrors", data);
}); });
@ -393,12 +396,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
var refreshNodeStats = function () { var refreshNodeStats = function () {
var key = "refreshNode"; var key = "refreshNode";
if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) { if (nextCall[key] && nextCall[key] > Date.now()) {
return; return;
} }
debouncedFuncs[key] = Date.now() + 300000; nextCall[key] = Date.now() + requestTimeout;
$http.get(urlbase + "/stats/node").success(function (data) { $http.get(urlbase + "/stats/node").success(function (data) {
debouncedFuncs[key] = Date.now() + 500; nextCall[key] = Date.now() + refreshTimeout;
$scope.stats = data; $scope.stats = data;
for (var node in $scope.stats) { for (var node in $scope.stats) {
$scope.stats[node].LastSeen = new Date($scope.stats[node].LastSeen); $scope.stats[node].LastSeen = new Date($scope.stats[node].LastSeen);