Added a simple cache for xhr objects in the xhr transport.

This commit is contained in:
jaubourg 2011-01-09 04:03:00 +01:00
parent 368f2441f6
commit 98c6c4b2b0

View file

@ -6,6 +6,9 @@ var // Next fake timer id
// Callbacks hashtable // Callbacks hashtable
xhrs = {}, xhrs = {},
// XHR pool
xhrPool = [],
// #5280: see end of file // #5280: see end of file
xhrUnloadAbortMarker = []; xhrUnloadAbortMarker = [];
@ -21,7 +24,7 @@ jQuery.ajax.transport( function( s , determineDataType ) {
send: function(headers, complete) { send: function(headers, complete) {
var xhr = s.xhr(), var xhr = xhrPool.pop() || s.xhr(),
handle; handle;
// Open the socket // Open the socket
@ -53,6 +56,8 @@ jQuery.ajax.transport( function( s , determineDataType ) {
try { try {
xhr.send( ( s.hasContent && s.data ) || null ); xhr.send( ( s.hasContent && s.data ) || null );
} catch(e) { } catch(e) {
// Store back in pool
xhrPool.push( xhr );
complete(0, "error", "" + e); complete(0, "error", "" + e);
return; return;
} }
@ -64,10 +69,12 @@ jQuery.ajax.transport( function( s , determineDataType ) {
if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) { if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) {
// Do not listen anymore // Do not listen anymore
// and Store back in pool
if (handle) { if (handle) {
xhr.onreadystatechange = jQuery.noop; xhr.onreadystatechange = jQuery.noop;
delete xhrs[ handle ]; delete xhrs[ handle ];
handle = undefined; handle = undefined;
xhrPool.push( xhr );
} }
callback = 0; callback = 0;