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

@ -6,57 +6,66 @@
*/ */
//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. //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 PING_TIMEOUT = 1000;
var USE_PING_TIMEOUT=true; //will be disabled on unsupported browsers 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)) {
USE_PING_TIMEOUT=false; //IE11 doesn't support XHR timeout
USE_PING_TIMEOUT = false;
} }
function ping(url,result){ function ping(url, result) {
var xhr=new XMLHttpRequest(); var xhr = new XMLHttpRequest();
var t=new Date().getTime(); var t = new Date().getTime();
xhr.onload=function(){ xhr.onload = function() {
if(xhr.responseText.length==0){ //we expect an empty response if (xhr.responseText.length == 0) {
var instspd=new Date().getTime()-t; //rough timing estimate //we expect an empty response
try{ var instspd = new Date().getTime() - t; //rough timing estimate
try {
//try to get more accurate timing using performance API //try to get more accurate timing using performance API
var p=performance.getEntries(); var p = performance.getEntries();
p=p[p.length-1]; p = p[p.length - 1];
var d=p.responseStart-p.requestStart; var d = p.responseStart - p.requestStart;
if(d<=0) d=p.duration; if (d <= 0) d = p.duration;
if(d>0&&d<instspd) instspd=d; if (d > 0 && d < instspd) instspd = d;
}catch(e){ } catch (e) {}
}
result(instspd); result(instspd);
}else result(-1); } else result(-1);
}.bind(this); }.bind(this);
xhr.onerror=function(){ xhr.onerror = function() {
result(-1); result(-1);
}.bind(this); }.bind(this);
xhr.open("GET",url); xhr.open("GET", url);
if(USE_PING_TIMEOUT){ if (USE_PING_TIMEOUT) {
try{ try {
xhr.timeout=PING_TIMEOUT; xhr.timeout = PING_TIMEOUT;
xhr.ontimeout=xhr.onerror; xhr.ontimeout = xhr.onerror;
}catch(e){} } catch (e) {}
} }
xhr.send(); xhr.send();
} }
//this function repeatedly pings a server to get a good estimate of the ping. When it's done, it calls the done function without parameters. At the end of the execution, the server will have a new parameter called pingT, which is either the best ping we got from the server or -1 if something went wrong. //this function repeatedly pings a server to get a good estimate of the ping. When it's done, it calls the done function without parameters. At the end of the execution, the server will have a new parameter called pingT, which is either the best ping we got from the server or -1 if something went wrong.
var PINGS=3, //up to 3 pings are performed, unless the server is down... var PINGS = 3, //up to 3 pings are performed, unless the server is down...
SLOW_THRESHOLD=500; //...or one of the pings is above this threshold SLOW_THRESHOLD = 500; //...or one of the pings is above this threshold
function checkServer(server,done){ function checkServer(server, done) {
var i=0; var i = 0;
server.pingT=-1; server.pingT = -1;
if(server.server.indexOf(location.protocol)==-1) done(); else{ if (server.server.indexOf(location.protocol) == -1) done();
var nextPing=function(){ else {
if(i++==PINGS){done(); return;} var nextPing = function() {
ping(server.server+server.pingURL,function(t){ if (i++ == PINGS) {
if(t>=0){ done();
if(t<server.pingT||server.pingT==-1) server.pingT=t; return;
if(t<SLOW_THRESHOLD) nextPing(); else done(); }
}else done(); ping(
}.bind(this)); 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();
} else done();
}.bind(this)
);
}.bind(this); }.bind(this);
nextPing(); nextPing();
} }
@ -73,18 +82,21 @@ function checkServer(server,done){
} }
For each server, the ping is measured, then the server with the function result is called with the best server, or null if all the servers were down. For each server, the ping is measured, then the server with the function result is called with the best server, or null if all the servers were down.
*/ */
function selectServer(serverList,result){ function selectServer(serverList, result) {
var i=0; var i = 0;
var done=function(){ var done = function() {
var bestServer=null; var bestServer = null;
for(var i=0;i<serverList.length;i++){ for (var i = 0; i < serverList.length; i++) {
if(serverList[i].pingT!=-1&&(bestServer==null||serverList[i].pingT<bestServer.pingT)) bestServer=serverList[i]; if (serverList[i].pingT != -1 && (bestServer == null || serverList[i].pingT < bestServer.pingT)) bestServer = serverList[i];
} }
result(bestServer); result(bestServer);
}.bind(this); }.bind(this);
var nextServer=function(){ var nextServer = function() {
if(i==serverList.length){done(); return;} if (i == serverList.length) {
checkServer(serverList[i++],nextServer); done();
return;
}
checkServer(serverList[i++], nextServer);
}.bind(this); }.bind(this);
nextServer(); nextServer();
} }
@ -92,24 +104,27 @@ function selectServer(serverList,result){
/* /*
this function is a faster version of selectServer that tests multiple servers concurrently. Useful for large server lists this function is a faster version of selectServer that tests multiple servers concurrently. Useful for large server lists
*/ */
var CONCURRENCY=4; //4 seems to be the safest value var CONCURRENCY = 4; //4 seems to be the safest value
function fastSelectServer(serverList,result){ function fastSelectServer(serverList, result) {
var serverLists=[]; var serverLists = [];
for(var i=0;i<CONCURRENCY;i++){ for (var i = 0; i < CONCURRENCY; i++) {
serverLists[i]=[]; serverLists[i] = [];
} }
for(var i=0;i<serverList.length;i++){ for (var i = 0; i < serverList.length; i++) {
serverLists[i%CONCURRENCY].push(serverList[i]); serverLists[i % CONCURRENCY].push(serverList[i]);
} }
var completed=0; var completed = 0;
var bestServer=null; var bestServer = null;
for(var i=0;i<CONCURRENCY;i++){ for (var i = 0; i < CONCURRENCY; i++) {
selectServer(serverLists[i],function(server){ selectServer(
if(server!=null){ serverLists[i],
if(bestServer==null||server.pingT<bestServer.pingT) bestServer=server; function(server) {
} if (server != null) {
completed++; if (bestServer == null || server.pingT < bestServer.pingT) bestServer = server;
if(completed==CONCURRENCY) result(bestServer); }
}.bind(this)); completed++;
if (completed == CONCURRENCY) result(bestServer);
}.bind(this)
);
} }
} }

File diff suppressed because it is too large Load diff