Improve debounce functionality of REST requests

This commit is contained in:
Lode Hoste 2014-09-19 22:28:45 +02:00
parent b1a31d3b30
commit f4b6704aad

View File

@ -253,15 +253,15 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
function refreshRepo(repo) { function refreshRepo(repo) {
var key = "refreshRepo" + repo; var key = "refreshRepo" + repo;
if (!debouncedFuncs[key]) { if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
debouncedFuncs[key] = debounce(function () { return;
}
debouncedFuncs[key] = Date.now() + 300000;
$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;
$scope.model[repo] = data; $scope.model[repo] = data;
console.log("refreshRepo", repo, data); console.log("refreshRepo", repo, data);
}); });
}, 1000, true);
}
debouncedFuncs[key]();
} }
function updateLocalConfig(config) { function updateLocalConfig(config) {
@ -292,7 +292,13 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
} }
function refreshSystem() { 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) { $http.get(urlbase + '/system').success(function (data) {
debouncedFuncs[key] = Date.now() + 500;
$scope.myID = data.myID; $scope.myID = data.myID;
$scope.system = data; $scope.system = data;
console.log("refreshSystem", data); console.log("refreshSystem", data);
@ -305,9 +311,12 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
} }
var key = "refreshCompletion" + node + repo; var key = "refreshCompletion" + node + repo;
if (!debouncedFuncs[key]) { if (debouncedFuncs[key] && debouncedFuncs[key] > Date.now()) {
debouncedFuncs[key] = debounce(function () { return;
}
debouncedFuncs[key] = Date.now() + 300000;
$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;
if (!$scope.completion[node]) { if (!$scope.completion[node]) {
$scope.completion[node] = {}; $scope.completion[node] = {};
} }
@ -326,13 +335,16 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
console.log("refreshCompletion", node, repo, $scope.completion[node]); console.log("refreshCompletion", node, repo, $scope.completion[node]);
}); });
}, 1000, true);
}
debouncedFuncs[key]();
} }
function refreshConnectionStats() { 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) { $http.get(urlbase + '/connections').success(function (data) {
debouncedFuncs[key] = Date.now() + 500;
var now = Date.now(), var now = Date.now(),
td = (now - prevDate) / 1000, td = (now - prevDate) / 1000,
id; id;
@ -356,7 +368,13 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
} }
function refreshErrors() { 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) { $http.get(urlbase + '/errors').success(function (data) {
debouncedFuncs[key] = Date.now() + 500;
$scope.errors = data.errors; $scope.errors = data.errors;
console.log("refreshErrors", data); 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) { $http.get(urlbase + "/stats/node").success(function (data) {
debouncedFuncs[key] = Date.now() + 500;
$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);
@ -382,7 +406,7 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca
} }
console.log("refreshNodeStats", data); console.log("refreshNodeStats", data);
}); });
}, 500); }
$scope.init = function () { $scope.init = function () {
refreshSystem(); refreshSystem();
@ -995,40 +1019,6 @@ function isEmptyObject(obj) {
return true; 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 () { syncthing.filter('natural', function () {
return function (input, valid) { return function (input, valid) {
return input.toFixed(decimals(input, valid)); return input.toFixed(decimals(input, valid));