aboutsummaryrefslogtreecommitdiffstats
path: root/src/ng
diff options
context:
space:
mode:
authorDavid Bennett2013-04-27 11:22:03 -0400
committerIgor Minar2013-05-20 14:15:04 -0700
commit9f4f5937112655a9881d3281da8e72035bc8b180 (patch)
treef6c9b19448ea5ddde11198b0e6d7a516e0b83d7b /src/ng
parent27a8824b50aa78e9a082b4377ca09250382a8655 (diff)
downloadangular.js-9f4f5937112655a9881d3281da8e72035bc8b180.tar.bz2
feat($http): add support for aborting via timeout promises
If the timeout argument is a promise, abort the request when it is resolved. Implemented by adding support to $httpBackend service and $httpBackend mock service. This api can also be used to explicitly abort requests while keeping the communication between the deffered and promise unidirectional. Closes #1159
Diffstat (limited to 'src/ng')
-rw-r--r--src/ng/http.js5
-rw-r--r--src/ng/httpBackend.js17
2 files changed, 14 insertions, 8 deletions
diff --git a/src/ng/http.js b/src/ng/http.js
index 708a0650..fbdc89a5 100644
--- a/src/ng/http.js
+++ b/src/ng/http.js
@@ -548,7 +548,8 @@ function $HttpProvider() {
* GET request, otherwise if a cache instance built with
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
* caching.
- * - **timeout** – `{number}` – timeout in milliseconds.
+ * - **timeout** – `{number|Promise}` – timeout in milliseconds, or {@link ng.$q promise}
+ * that should abort the request when resolved.
* - **withCredentials** - `{boolean}` - whether to to set the `withCredentials` flag on the
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
* requests with credentials} for more information.
@@ -927,7 +928,7 @@ function $HttpProvider() {
}
resolvePromise(response, status, headersString);
- $rootScope.$apply();
+ if (!$rootScope.$$phase) $rootScope.$apply();
}
diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js
index 69ac5976..ed8404f9 100644
--- a/src/ng/httpBackend.js
+++ b/src/ng/httpBackend.js
@@ -107,20 +107,25 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
}
if (timeout > 0) {
- var timeoutId = $browserDefer(function() {
- status = -1;
- jsonpDone && jsonpDone();
- xhr && xhr.abort();
- }, timeout);
+ var timeoutId = $browserDefer(timeoutRequest, timeout);
+ } else if (timeout && timeout.then) {
+ timeout.then(timeoutRequest);
}
+ function timeoutRequest() {
+ status = -1;
+ jsonpDone && jsonpDone();
+ xhr && xhr.abort();
+ }
+
function completeRequest(callback, status, response, headersString) {
// URL_MATCH is defined in src/service/location.js
var protocol = (url.match(SERVER_MATCH) || ['', locationProtocol])[1];
- // cancel timeout
+ // cancel timeout and subsequent timeout promise resolution
timeoutId && $browserDefer.cancel(timeoutId);
+ jsonpDone = xhr = null;
// fix status code for file protocol (it's always 0)
status = (protocol == 'file') ? (response ? 200 : 404) : status;