mirror of
https://github.com/octoleo/syncthing.git
synced 2025-02-08 23:08:27 +00:00
Improve debounce functionality of REST requests
This commit is contained in:
parent
b1a31d3b30
commit
f4b6704aad
124
gui/app.js
124
gui/app.js
@ -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;
|
||||||
$http.get(urlbase + '/model?repo=' + encodeURIComponent(repo)).success(function (data) {
|
|
||||||
$scope.model[repo] = data;
|
|
||||||
console.log("refreshRepo", repo, data);
|
|
||||||
});
|
|
||||||
}, 1000, true);
|
|
||||||
}
|
}
|
||||||
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) {
|
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,34 +311,40 @@ 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;
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
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() {
|
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));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user