Improved JS formatting

This commit is contained in:
adolfintel 2019-01-25 09:20:13 +01:00
parent 58970adcb8
commit 697b4c85a6
2 changed files with 756 additions and 590 deletions

View file

@ -8,14 +8,16 @@
//pings the specified URL, then calls the function result. Result will receive a parameter which is either the time it took to ping the URL, or -1 if something went wrong.
var PING_TIMEOUT = 1000;
var USE_PING_TIMEOUT = true; //will be disabled on unsupported browsers
if(/MSIE.(\d+\.\d+)/i.test(navigator.userAgent)){ //IE11 doesn't support XHR timeout
if (/MSIE.(\d+\.\d+)/i.test(navigator.userAgent)) {
//IE11 doesn't support XHR timeout
USE_PING_TIMEOUT = false;
}
function ping(url, result) {
var xhr = new XMLHttpRequest();
var t = new Date().getTime();
xhr.onload = function() {
if(xhr.responseText.length==0){ //we expect an empty response
if (xhr.responseText.length == 0) {
//we expect an empty response
var instspd = new Date().getTime() - t; //rough timing estimate
try {
//try to get more accurate timing using performance API
@ -24,8 +26,7 @@ function ping(url,result){
var d = p.responseStart - p.requestStart;
if (d <= 0) d = p.duration;
if (d > 0 && d < instspd) instspd = d;
}catch(e){
}
} catch (e) {}
result(instspd);
} else result(-1);
}.bind(this);
@ -48,15 +49,23 @@ var PINGS=3, //up to 3 pings are performed, unless the server is down...
function checkServer(server, done) {
var i = 0;
server.pingT = -1;
if(server.server.indexOf(location.protocol)==-1) done(); else{
if (server.server.indexOf(location.protocol) == -1) done();
else {
var nextPing = function() {
if(i++==PINGS){done(); return;}
ping(server.server+server.pingURL,function(t){
if (i++ == PINGS) {
done();
return;
}
ping(
server.server + server.pingURL,
function(t) {
if (t >= 0) {
if (t < server.pingT || server.pingT == -1) server.pingT = t;
if(t<SLOW_THRESHOLD) nextPing(); else done();
if (t < SLOW_THRESHOLD) nextPing();
else done();
} else done();
}.bind(this));
}.bind(this)
);
}.bind(this);
nextPing();
}
@ -83,7 +92,10 @@ function selectServer(serverList,result){
result(bestServer);
}.bind(this);
var nextServer = function() {
if(i==serverList.length){done(); return;}
if (i == serverList.length) {
done();
return;
}
checkServer(serverList[i++], nextServer);
}.bind(this);
nextServer();
@ -104,12 +116,15 @@ function fastSelectServer(serverList,result){
var completed = 0;
var bestServer = null;
for (var i = 0; i < CONCURRENCY; i++) {
selectServer(serverLists[i],function(server){
selectServer(
serverLists[i],
function(server) {
if (server != null) {
if (bestServer == null || server.pingT < bestServer.pingT) bestServer = server;
}
completed++;
if (completed == CONCURRENCY) result(bestServer);
}.bind(this));
}.bind(this)
);
}
}

File diff suppressed because it is too large Load diff