From bd1248b8da2596a33a9c13eb0d0ad0eab922ffb5 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 23 Nov 2017 18:22:47 +0100 Subject: [PATCH] Barebone setup@4 > fix: public.js.lib.api.js (XHR garbage collector) --- public_html/js/lib/api.js | 80 +++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/public_html/js/lib/api.js b/public_html/js/lib/api.js index 69f1f43..5dc4fb0 100755 --- a/public_html/js/lib/api.js +++ b/public_html/js/lib/api.js @@ -54,45 +54,67 @@ API.prototype = { - /* (3) Manage xhr stack + /* (3) Create form data ---------------------------------------------------------*/ - /* (1) Pop ended requests */ - for( var i = 0, il = this.xhr.length ; i < il ; i++ ) - if( this.xhr[i].readyState == 4 ) // if over - this.xhr.splice(i, 1); + /* (1) Create virtual form */ + var lForm = new FormData(); + + /* (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 ---------------------------------------------------------*/ - /* (1) Push a new entry -> fetch its index */ - this.xhr.push(null); - i = this.xhr.length-1; + /* (1) Clean ended requests */ + for( var i = this.xhr.length-1 ; i >= 0 ; i-- ){ - /* (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'); + /* (5) Bind response event ---------------------------------------------------------*/ var self = this; // to access the buffer - this.xhr[i].onreadystatechange = function(){ + this.xhr[i].onreadystatechange = function(i){ /* (1) If request over */ - if( this.readyState == 4 ){ + if( this.xhr[i].readyState == 4 ){ /* (2) Update buffer (for debug) */ - self.buffer = this.responseText; + self.buffer = this.xhr[i].responseText; /* (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) */ var response = {error:-3, ErrorDescription: self.error['-3']}; /* (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 */ pHandler(response); @@ -101,31 +123,15 @@ API.prototype = { }else pHandler({ error:-2, ErrorDescription: self.error['-2'] }); + /* (5) Notify current xhr instance is done */ + this.xhr[i] = null; + } - }.bind(this.xhr[i]); + }.bind(this, 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])); - - } - - - /* (7) Finish & send request + /* (6) Finish & send request ---------------------------------------------------------*/ /* (1) Open the XHR */ this.xhr[i].open(lHttpMethod, this.target+lUri, true); @@ -138,7 +144,9 @@ API.prototype = { this.xhr[i].setRequestHeader('X-Requested-With', 'XMLHttpRequest'); /* (4) Make the call */ - this.xhr[i].send( form ); + this.xhr[i].send( lForm ); + + return true; } }; \ No newline at end of file