X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fajax%2Fxhr.js;h=614bd3a0c80ee57f69ab8183bb693f074d0a2f41;hb=57956152d8a9d23ae97ba576b8ec4b63306c8758;hp=a2ec4a4c5b500d0e76e2bac35e1b9da4f3a69a84;hpb=d515068ee807efef29b6c8406171be4725d7154f;p=jquery.git diff --git a/src/ajax/xhr.js b/src/ajax/xhr.js index a2ec4a4..614bd3a 100644 --- a/src/ajax/xhr.js +++ b/src/ajax/xhr.js @@ -1,16 +1,13 @@ (function( jQuery ) { -var // Next fake timer id - xhrPollingId = jQuery.now(), +var // Next active xhr id + xhrId = jQuery.now(), - // Callbacks hashtable + // active xhrs xhrs = {}, - // XHR pool - xhrPool = [], - - // #5280: see end of file - xhrUnloadAbortMarker = []; + // #5280: see below + xhrUnloadAbortInstalled; jQuery.ajax.transport( function( s , determineDataType ) { @@ -24,7 +21,25 @@ jQuery.ajax.transport( function( s , determineDataType ) { send: function(headers, complete) { - var xhr = xhrPool.pop() || s.xhr(), + // #5280: we need to abort on unload or IE will keep connections alive + if ( ! xhrUnloadAbortInstalled ) { + + xhrUnloadAbortInstalled = 1; + + jQuery(window).bind( "unload" , function() { + + // Abort all pending requests + jQuery.each(xhrs, function(_, xhr) { + if ( xhr.onreadystatechange ) { + xhr.onreadystatechange( 1 ); + } + }); + + }); + } + + // Get a new xhr + var xhr = s.xhr(), handle; // Open the socket @@ -38,7 +53,7 @@ jQuery.ajax.transport( function( s , determineDataType ) { // Requested-With header // Not set for crossDomain requests with no content // (see why at http://trac.dojotoolkit.org/ticket/9486) - // Won't change header if already provided in beforeSend + // Won't change header if already provided if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) { headers["x-requested-with"] = "XMLHttpRequest"; } @@ -56,49 +71,40 @@ jQuery.ajax.transport( function( s , determineDataType ) { try { xhr.send( ( s.hasContent && s.data ) || null ); } catch(e) { - // Store back in pool - xhrPool.push( xhr ); complete(0, "error", "" + e); return; } // Listener - callback = function ( abortStatusText ) { + callback = function( _ , isAbort ) { // Was never called and is aborted or complete - if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) { + if ( callback && ( isAbort || xhr.readyState === 4 ) ) { + + // Only called once + callback = 0; - // Do not listen anymore - // and Store back in pool + // Do not keep as active anymore + // and store back into pool if (handle) { xhr.onreadystatechange = jQuery.noop; delete xhrs[ handle ]; - handle = undefined; - xhrPool.push( xhr ); } - callback = 0; - - // Get info - var status, statusText, response, responseHeaders; - - if ( abortStatusText ) { + // If it's an abort + if ( isAbort ) { + // Abort it manually if needed if ( xhr.readyState !== 4 ) { xhr.abort(); } - - // Stop here if unloadAbort - if ( abortStatusText === xhrUnloadAbortMarker ) { - return; - } - - status = 0; - statusText = abortStatusText; - } else { - status = xhr.status; + // Get info + var status = xhr.status, + statusText, + response, + responseHeaders = xhr.getAllResponseHeaders(); try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests @@ -110,8 +116,6 @@ jQuery.ajax.transport( function( s , determineDataType ) { } - responseHeaders = xhr.getAllResponseHeaders(); - // Filter status for non standard behaviours // (so many they seem to be the actual "standard") status = @@ -144,10 +148,10 @@ jQuery.ajax.transport( function( s , determineDataType ) { xhr.getResponseHeader("content-type"), xhr.responseText, xhr.responseXML ); - } - // Call complete - complete(status,statusText,response,responseHeaders); + // Call complete + complete(status,statusText,response,responseHeaders); + } } }; @@ -160,37 +164,20 @@ jQuery.ajax.transport( function( s , determineDataType ) { } else { - // Listener is externalized to handle abort on unload - handle = xhrPollingId++; + // Add to list of active xhrs + handle = xhrId++; xhrs[ handle ] = xhr; - xhr.onreadystatechange = function() { - callback(); - }; + xhr.onreadystatechange = callback; } }, - abort: function(statusText) { + abort: function() { if ( callback ) { - callback(statusText); + callback(0,1); } } }; } }); -// #5280: we need to abort on unload or IE will keep connections alive -jQuery(window).bind( "unload" , function() { - - // Abort all pending requests - jQuery.each(xhrs, function(_, xhr) { - if ( xhr.onreadystatechange ) { - xhr.onreadystatechange( xhrUnloadAbortMarker ); - } - }); - - // Resest polling structure to be safe - xhrs = {}; - -}); - })( jQuery );