From 5487bdb3d1c905fb9453644f7e290c75dcee14c1 Mon Sep 17 00:00:00 2001
From: Vojta Jina
Date: Thu, 18 Aug 2011 23:48:01 +0200
Subject: feat($browser.xhr): add timeout option to abort request
Timeouted request responds internal status code -1, which should be normalized
into 0 by $xhr.
---
 src/service/browser.js | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
(limited to 'src/service/browser.js')
diff --git a/src/service/browser.js b/src/service/browser.js
index b38c9211..74bea44c 100644
--- a/src/service/browser.js
+++ b/src/service/browser.js
@@ -95,6 +95,7 @@ function Browser(window, document, body, XHR, $log, $sniffer) {
    *     
X-Requested-With: XMLHttpRequest
    *   
    *
+   * @param {number=} timeout Timeout in ms, when the request will be aborted
    * @returns {XMLHttpRequest|undefined} Raw XMLHttpRequest object or undefined when JSONP method
    *
    * @description
@@ -102,7 +103,7 @@ function Browser(window, document, body, XHR, $log, $sniffer) {
    *
    * TODO(vojta): change signature of this method to (method, url, data, headers, callback)
    */
-  self.xhr = function(method, url, post, callback, headers) {
+  self.xhr = function(method, url, post, callback, headers, timeout) {
     outstandingRequestCount ++;
     if (lowercase(method) == 'jsonp') {
       var callbackId = ("angular_" + Math.random() + '_' + (idCounter++)).replace(/\d\./, '');
@@ -126,21 +127,30 @@ function Browser(window, document, body, XHR, $log, $sniffer) {
           if (value) xhr.setRequestHeader(key, value);
       });
 
+      var status;
       xhr.send(post || '');
 
       // IE6, IE7 bug - does sync when serving from cache
       if (xhr.readyState == 4) {
         setTimeout(function() {
-          completeOutstandingRequest(callback, fixStatus(xhr.status), xhr.responseText);
+          completeOutstandingRequest(callback, fixStatus(status || xhr.status), xhr.responseText);
         }, 0);
       } else {
         xhr.onreadystatechange = function() {
           if (xhr.readyState == 4) {
-            completeOutstandingRequest(callback, fixStatus(xhr.status), xhr.responseText);
+            completeOutstandingRequest(callback, fixStatus(status || xhr.status),
+                                       xhr.responseText);
           }
         };
       }
 
+      if (timeout > 0) {
+        setTimeout(function() {
+          status = -1;
+          xhr.abort();
+        }, timeout);
+      }
+
       return xhr;
     }
   };
-- 
cgit v1.2.3