[ Index ] |
|
Code source de Seagull 0.6.1 |
1 /** 2 * Various processing queues, use when you want to control how multiple requests are made 3 * @category HTML 4 * @package AJAX 5 * @author Joshua Eichorn <josh@bluga.net> 6 * @copyright 2005 Joshua Eichorn 7 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 8 */ 9 10 // Single Buffer queue with interval 11 // works by attempting to send a request every x miliseconds 12 // if an item is currently in the queue when a new item is added it will be replaced 13 // simple queue, just processes the request immediately 14 // the first request starts the interval timer 15 function HTML_AJAX_Queue_Interval_SingleBuffer(interval,singleOutstandingRequest) { 16 this.interval = interval; 17 if (singleOutstandingRequest) { 18 this.singleOutstandingRequest = true; 19 } 20 } 21 HTML_AJAX_Queue_Interval_SingleBuffer.prototype = { 22 request: false, 23 _intervalId: false, 24 singleOutstandingRequest: false, 25 client: false, 26 addRequest: function(request) { 27 this.request = request; 28 }, 29 processRequest: function() { 30 if (!this._intervalId) { 31 this.runInterval(); 32 this.start(); 33 } 34 }, 35 start: function() { 36 var self = this; 37 this._intervalId = setInterval(function() { self.runInterval() },this.interval); 38 }, 39 stop: function() { 40 clearInterval(this._intervalId); 41 }, 42 runInterval: function() { 43 if (this.request) { 44 if (this.singleOutstandingRequest && this.client) { 45 this.client.abort(); 46 } 47 this.client = HTML_AJAX.httpClient(); 48 this.client.request = this.request; 49 this.request = false; 50 this.client.makeRequest(); 51 } 52 } 53 } 54 55 // Requests return in the same order they were called 56 // this helps handle high latency situations 57 function HTML_AJAX_Queue_Ordered() { } 58 HTML_AJAX_Queue_Ordered.prototype = { 59 request: false, 60 order: 0, 61 current: 0, 62 callbacks: {}, 63 interned: {}, 64 addRequest: function(request) { 65 request.order = this.order; 66 this.request = request; 67 this.callbacks[this.order] = this.request.callback; 68 var self = this; 69 this.request.callback = function(result) { 70 self.processCallback(result,request.order); 71 } 72 }, 73 processRequest: function() { 74 var client = HTML_AJAX.httpClient(); 75 client.request = this.request; 76 client.makeRequest(); 77 this.order++; 78 }, 79 requestComplete: function(request,e) { 80 // something when wrong with the request lets stop waiting for it 81 if (e) { 82 this.current++; 83 } 84 }, 85 processCallback: function(result,order) { 86 if (order == this.current) { 87 this.callbacks[order](result); 88 this.current++; 89 } 90 else { 91 this.interned[order] = result; 92 if (this.interned[this.current]) { 93 this.callbacks[this.current](this.interned[this.current]); 94 this.current++; 95 } 96 } 97 } 98 } 99 100 // Make a single request at once, canceling and currently outstanding requests when a new one is made 101 function HTML_AJAX_Queue_Single() { 102 } 103 HTML_AJAX_Queue_Single.prototype = { 104 request: false, 105 client: false, 106 addRequest: function(request) { 107 this.request = request; 108 }, 109 processRequest: function() { 110 if (this.request) { 111 if (this.client) { 112 this.client.abort(); 113 } 114 this.client = HTML_AJAX.httpClient(); 115 this.client.request = this.request; 116 this.request = false; 117 this.client.makeRequest(); 118 } 119 } 120 } 121 122 /** 123 * Priority queue 124 * 125 * @author Arpad Ray <arpad@php.net> 126 */ 127 function HTML_AJAX_Queue_Priority_Item(item, time) { 128 this.item = item; 129 this.time = time; 130 } 131 HTML_AJAX_Queue_Priority_Item.prototype = { 132 compareTo: function (other) { 133 var ret = this.item.compareTo(other.item); 134 if (ret == 0) { 135 ret = this.time - other.time; 136 } 137 return ret; 138 } 139 } 140 141 function HTML_AJAX_Queue_Priority_Simple(interval) { 142 this.interval = interval; 143 this.idleMax = 10; // keep the interval going with an empty queue for 10 intervals 144 this.requestTimeout = 5; // retry uncompleted requests after 5 seconds 145 this.checkRetryChance = 0.1; // check for uncompleted requests to retry on 10% of intervals 146 this._intervalId = 0; 147 this._requests = []; 148 this._removed = []; 149 this._len = 0; 150 this._removedLen = 0; 151 this._idle = 0; 152 } 153 HTML_AJAX_Queue_Priority_Simple.prototype = { 154 isEmpty: function () { 155 return this._len == 0; 156 }, 157 addRequest: function (request) { 158 request = new HTML_AJAX_Queue_Priority_Item(request, new Date().getTime()); 159 ++this._len; 160 if (this.isEmpty()) { 161 this._requests[0] = request; 162 return; 163 } 164 for (i = 0; i < this._len - 1; i++) { 165 if (request.compareTo(this._requests[i]) < 0) { 166 this._requests.splice(i, 1, request, this._requests[i]); 167 return; 168 } 169 } 170 this._requests.push(request); 171 }, 172 peek: function () { 173 return (this.isEmpty() ? false : this._requests[0]); 174 }, 175 requestComplete: function (request) { 176 for (i = 0; i < this._removedLen; i++) { 177 if (this._removed[i].item == request) { 178 this._removed.splice(i, 1); 179 --this._removedLen; 180 out('removed from _removed'); 181 return true; 182 } 183 } 184 return false; 185 }, 186 processRequest: function() { 187 if (!this._intervalId) { 188 this._runInterval(); 189 this._start(); 190 } 191 this._idle = 0; 192 }, 193 _runInterval: function() { 194 if (Math.random() < this.checkRetryChance) { 195 this._doRetries(); 196 } 197 if (this.isEmpty()) { 198 if (++this._idle > this.idleMax) { 199 this._stop(); 200 } 201 return; 202 } 203 var client = HTML_AJAX.httpClient(); 204 if (!client) { 205 return; 206 } 207 var request = this.peek(); 208 if (!request) { 209 this._requests.splice(0, 1); 210 return; 211 } 212 client.request = request.item; 213 client.makeRequest(); 214 this._requests.splice(0, 1); 215 --this._len; 216 this._removed[this._removedLen++] = new HTML_AJAX_Queue_Priority_Item(request, new Date().getTime()); 217 }, 218 _doRetries: function () { 219 for (i = 0; i < this._removedLen; i++) { 220 if (this._removed[i].time + this._requestTimeout < new Date().getTime()) { 221 this.addRequest(request.item); 222 this._removed.splice(i, 1); 223 --this._removedLen; 224 return true; 225 } 226 } 227 }, 228 _start: function() { 229 var self = this; 230 this._intervalId = setInterval(function() { self._runInterval() }, this.interval); 231 }, 232 _stop: function() { 233 clearInterval(this._intervalId); 234 this._intervalId = 0; 235 } 236 };
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 01:27:52 2007 | par Balluche grâce à PHPXref 0.7 |