Barebone setup@4 > fix: public.js.lib.api.js (XHR garbage collector)

This commit is contained in:
xdrm-brackets 2017-11-23 18:22:47 +01:00
parent 2ff880a571
commit bd1248b8da
1 changed files with 44 additions and 36 deletions

View File

@ -54,45 +54,67 @@ API.prototype = {
/* (3) Manage xhr stack /* (3) Create form data
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Pop ended requests */ /* (1) Create virtual form */
for( var i = 0, il = this.xhr.length ; i < il ; i++ ) var lForm = new FormData();
if( this.xhr[i].readyState == 4 ) // if over
this.xhr.splice(i, 1); /* (2) Add attributes */
for( var key in pArgs ){
// {2.1} If a file -> send as it //
if( pArgs[key] instanceof File )
lForm.append(key, pArgs[key]);
// {2.2} Else -> JSON stringify //
else
lForm.append(key, JSON.stringify(pArgs[key]));
}
/* (4) Create XHR request /* (4) Create XHR request
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Push a new entry -> fetch its index */ /* (1) Clean ended requests */
this.xhr.push(null); for( var i = this.xhr.length-1 ; i >= 0 ; i-- ){
i = this.xhr.length-1;
/* (2) Create XHR object */ if( this.xhr[i] != null )
break;
this.xhr.pop();
}
/* (2) Push a new entry -> fetch its index */
i = this.xhr.push(null) - 1;
/* (3) Create XHR object */
this.xhr[i] = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttpRequest'); this.xhr[i] = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttpRequest');
/* (5) Bind response event /* (5) Bind response event
---------------------------------------------------------*/ ---------------------------------------------------------*/
var self = this; // to access the buffer var self = this; // to access the buffer
this.xhr[i].onreadystatechange = function(){ this.xhr[i].onreadystatechange = function(i){
/* (1) If request over */ /* (1) If request over */
if( this.readyState == 4 ){ if( this.xhr[i].readyState == 4 ){
/* (2) Update buffer (for debug) */ /* (2) Update buffer (for debug) */
self.buffer = this.responseText; self.buffer = this.xhr[i].responseText;
/* (3) If request success */ /* (3) If request success */
if( [0, 200, 417].indexOf(this.status) > -1 ){ if( [0, 200, 417].indexOf(this.xhr[i].status) > -1 ){
/* (3.1) Create default response (if JSON error) */ /* (3.1) Create default response (if JSON error) */
var response = {error:-3, ErrorDescription: self.error['-3']}; var response = {error:-3, ErrorDescription: self.error['-3']};
/* (3.2) Try to parse JSON */ /* (3.2) Try to parse JSON */
try{ response = JSON.parse(this.responseText); }catch(e){} try{ response = JSON.parse(this.xhr[i].responseText); }catch(e){}
/* (3.3) Launch @pHandler with response */ /* (3.3) Launch @pHandler with response */
pHandler(response); pHandler(response);
@ -101,31 +123,15 @@ API.prototype = {
}else }else
pHandler({ error:-2, ErrorDescription: self.error['-2'] }); pHandler({ error:-2, ErrorDescription: self.error['-2'] });
} /* (5) Notify current xhr instance is done */
this.xhr[i] = null;
}.bind(this.xhr[i]);
/* (6) Create form data
---------------------------------------------------------*/
/* (1) Create virtual form */
var form = new FormData();
/* (2) Add attributes */
for( var key in pArgs ){
// {2.1} If a file -> send as it //
if( pArgs[key] instanceof File )
form.append(key, pArgs[key]);
// {2.2} Else -> JSON stringify //
else
form.append(key, JSON.stringify(pArgs[key]));
} }
}.bind(this, i);
/* (7) Finish & send request
/* (6) Finish & send request
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Open the XHR */ /* (1) Open the XHR */
this.xhr[i].open(lHttpMethod, this.target+lUri, true); this.xhr[i].open(lHttpMethod, this.target+lUri, true);
@ -138,7 +144,9 @@ API.prototype = {
this.xhr[i].setRequestHeader('X-Requested-With', 'XMLHttpRequest'); this.xhr[i].setRequestHeader('X-Requested-With', 'XMLHttpRequest');
/* (4) Make the call */ /* (4) Make the call */
this.xhr[i].send( form ); this.xhr[i].send( lForm );
return true;
} }
}; };