-
-
Save extralam/ef68e6d9bd83a68b82b3 to your computer and use it in GitHub Desktop.
Revisions
-
extralam revised this gist
Oct 14, 2015 . 1 changed file with 73 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ /* Allows for ajax requests to be run synchronously in a queue with pool , without cancel Usage:: var queue = new $.AjaxQueue(); // add with tag name or not , both ok queue.add('test',{ url: 'url', complete: function() { console.log('ajax completed'); }, _run: function(req) { //special pre-processor to alter the request just before it is finally executed in the queue req.url = 'changed_url' } }); */ $.AjaxQueue = function() { this.MAX_POOL_SIZE = 3; this.CURR_POOL_SIZE = 0; this.reqs = []; }; $.AjaxQueue.prototype = { add: function(req) { //console.log("Add Request : "); this.reqs.push({n : '' , r: req }); this.next(); }, add: function(name , req) { console.log("Add Request : " + name); this.reqs.push( {n : name , r: req }); this.next(); }, remove: function (name){ // TODO: NOT IMPLEMENT }, next: function() { console.log(' Q Size ' + this.reqs.length); if (this.reqs.length == 0) { return; } if(this.CURR_POOL_SIZE >= this.MAX_POOL_SIZE){ return; } var request = this.reqs.splice(0, 1)[0]; var req = request['r']; var complete = req.complete; var self = this; self.current_reqs_tag = request['n']; if (req._run) { req._run(req); } req.complete = function() { if (complete) { complete.apply(this, arguments); } self.CURR_POOL_SIZE--; self.next(); } self.CURR_POOL_SIZE++; self.current_req = $.ajax(req); } }; -
extralam revised this gist
Oct 13, 2015 . 1 changed file with 81 additions and 43 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,53 +1,91 @@ /* :: Enhanced Version @extralam :: Allows for ajax requests to be run synchronously in a queue , remove queue Usage:: var queue = new $.AjaxQueue(); // add with tag name or not , both ok queue.add('test',{ url: 'url', complete: function() { console.log('ajax completed'); }, _run: function(req) { //special pre-processor to alter the request just before it is finally executed in the queue req.url = 'changed_url' } }); // Cancel Request queue.remove('test'); */ $.AjaxQueue = function() { this.reqs = []; this.requesting = false; this.current_reqs_tag = null; this.current_req = null; }; $.AjaxQueue.prototype = { add: function(req) { //console.log("Add Request : "); this.reqs.push({n : '' , r: req }); this.next(); }, add: function(name , req) { console.log("Add Request : " + name); this.reqs.push( {n : name , r: req }); this.next(); }, remove: function (name){ if(this.current_reqs_tag == name){ console.log("Cancel Request : " + name); this.current_req.abort(); this.current_req = null; this.current_reqs_tag = null; this.requesting = false; this.next(); }else{ for (var i = 0; i < this.reqs.length; i++) { if(this.reqs[i]['n'] == name){ this.reqs.splice(i, 1); break; } } } }, next: function() { console.log(' Q Size ' + this.reqs.length); if (this.reqs.length == 0) { return; } if (this.requesting == true) { return; } var request = this.reqs.splice(0, 1)[0]; var req = request['r']; var complete = req.complete; var self = this; self.current_reqs_tag = request['n']; if (req._run) { req._run(req); } req.complete = function() { if (complete) { complete.apply(this, arguments); } self.requesting = false; self.next(); } self.requesting = true; self.current_req = $.ajax(req); } }; -
guybedford revised this gist
Apr 23, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,14 +38,14 @@ $.AjaxQueue.prototype = { var req = this.reqs.splice(0, 1)[0]; var complete = req.complete; var self = this; if (req._run) req._run(req); req.complete = function() { if (complete) complete.apply(this, arguments); self.requesting = false; self.next(); } this.requesting = true; $.ajax(req); -
guybedford revised this gist
Apr 23, 2012 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,7 +39,8 @@ $.AjaxQueue.prototype = { var complete = req.complete; var self = this; req.complete = function() { if (complete) complete.apply(this, arguments); self.requesting = false; self.next(); } -
guybedford revised this gist
Apr 23, 2012 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -43,6 +43,8 @@ $.AjaxQueue.prototype = { self.requesting = false; self.next(); } if (req._run) req._run(req); this.requesting = true; $.ajax(req); -
guybedford created this gist
Apr 23, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ /* Allows for ajax requests to be run synchronously in a queue Usage:: var queue = new $.AjaxQueue(); queue.add({ url: 'url', complete: function() { console.log('ajax completed'); }, _run: function(req) { //special pre-processor to alter the request just before it is finally executed in the queue req.url = 'changed_url' } }); */ $.AjaxQueue = function() { this.reqs = []; this.requesting = false; }; $.AjaxQueue.prototype = { add: function(req) { this.reqs.push(req); this.next(); }, next: function() { if (this.reqs.length == 0) return; if (this.requesting == true) return; var req = this.reqs.splice(0, 1)[0]; var complete = req.complete; var self = this; req.complete = function() { complete.apply(this, arguments); self.requesting = false; self.next(); } this.requesting = true; $.ajax(req); } };