diff options
| author | Vojta Jina | 2011-10-17 22:52:21 -0700 |
|---|---|---|
| committer | Igor Minar | 2011-11-30 11:17:22 -0500 |
| commit | fdcc2dbfd37d14ca5f3c830b589c091611ab54bd (patch) | |
| tree | 4e2c9dfd9c4c9aa57d9e137e3feb5869709f58e8 | |
| parent | 5ad0c7d0e4a2aff071d3afb181fa618c982ce991 (diff) | |
| download | angular.js-fdcc2dbfd37d14ca5f3c830b589c091611ab54bd.tar.bz2 | |
feat($http): expose pendingRequests and configuration object
- $http.pendingRequests is now an array of pending requests
- each request (its future object) has public property configuration
| -rw-r--r-- | src/service/http.js | 41 | ||||
| -rw-r--r-- | test/service/httpSpec.js | 30 |
2 files changed, 34 insertions, 37 deletions
diff --git a/src/service/http.js b/src/service/http.js index 087c3809..f06b88fd 100644 --- a/src/service/http.js +++ b/src/service/http.js @@ -56,6 +56,8 @@ function transform(data, fns, param) { * @requires $exceptionHandler * @requires $cacheFactory * + * @property {Array.<XhrFuture>} pendingRequests Array of pending requests. + * * @description */ function $HttpProvider() { @@ -89,28 +91,14 @@ function $HttpProvider() { this.$get = ['$httpBackend', '$browser', '$exceptionHandler', '$cacheFactory', '$rootScope', function($httpBackend, $browser, $exceptionHandler, $cacheFactory, $rootScope) { - var cache = $cacheFactory('$http'), - pendingRequestsCount = 0; + var cache = $cacheFactory('$http'); // the actual service function $http(config) { return new XhrFuture().retry(config); } - /** - * @workInProgress - * @ngdoc method - * @name angular.service.$http#pendingCount - * @methodOf angular.service.$http - * - * @description - * Return number of pending requests - * - * @returns {number} Number of pending requests - */ - $http.pendingCount = function() { - return pendingRequestsCount; - }; + $http.pendingRequests = []; /** * @ngdoc method @@ -236,12 +224,14 @@ function $HttpProvider() { /** * Represents Request object, returned by $http() * - * !!! ACCESS CLOSURE VARS: $httpBackend, $browser, $config, $log, $rootScope, cache, pendingRequestsCount + * !!! ACCESS CLOSURE VARS: + * $httpBackend, $browser, $config, $log, $rootScope, cache, $http.pendingRequests */ function XhrFuture() { - var rawRequest, cfg = {}, callbacks = [], + var rawRequest, parsedHeaders, + cfg = {}, callbacks = [], defHeaders = $config.headers, - parsedHeaders; + self = this; /** * Callback registered to $httpBackend(): @@ -281,9 +271,11 @@ function $HttpProvider() { response = transform(response, cfg.transformResponse || $config.transformResponse, rawRequest); var regexp = statusToRegexp(status), - pattern, callback; + pattern, callback, idx; - pendingRequestsCount--; + // remove from pending requests + if ((idx = indexOf($http.pendingRequests, self)) !== -1) + $http.pendingRequests.splice(idx, 1); // normalize internal statuses to 0 status = Math.max(status, 0); @@ -372,7 +364,7 @@ function $HttpProvider() { rawRequest = $httpBackend(cfg.method, cfg.url, data, done, headers, cfg.timeout); } - pendingRequestsCount++; + $http.pendingRequests.push(self); return this; }; @@ -423,6 +415,11 @@ function $HttpProvider() { return this; }; + + /** + * Configuration object of the request + */ + this.config = cfg; } }]; } diff --git a/test/service/httpSpec.js b/test/service/httpSpec.js index 75e85359..ad83bdf8 100644 --- a/test/service/httpSpec.js +++ b/test/service/httpSpec.js @@ -862,54 +862,54 @@ describe('$http', function() { }); - describe('pendingCount', function() { + describe('pendingRequests', function() { - it('should return number of pending requests', function() { + it('should be an array of pending requests', function() { $httpBackend.when('GET').then(200); - expect($http.pendingCount()).toBe(0); + expect($http.pendingRequests.length).toBe(0); $http({method: 'get', url: '/some'}); - expect($http.pendingCount()).toBe(1); + expect($http.pendingRequests.length).toBe(1); $httpBackend.flush(); - expect($http.pendingCount()).toBe(0); + expect($http.pendingRequests.length).toBe(0); }); - it('should decrement the counter when request aborted', function() { + it('should remove the request when aborted', function() { $httpBackend.when('GET').then(0); future = $http({method: 'get', url: '/x'}); - expect($http.pendingCount()).toBe(1); + expect($http.pendingRequests.length).toBe(1); future.abort(); $httpBackend.flush(); - expect($http.pendingCount()).toBe(0); + expect($http.pendingRequests.length).toBe(0); }); - it('should decrement the counter when served from cache', function() { + it('should remove the request when served from cache', function() { $httpBackend.when('GET').then(200); $http({method: 'get', url: '/cached', cache: true}); $httpBackend.flush(); - expect($http.pendingCount()).toBe(0); + expect($http.pendingRequests.length).toBe(0); $http({method: 'get', url: '/cached', cache: true}); - expect($http.pendingCount()).toBe(1); + expect($http.pendingRequests.length).toBe(1); $browser.defer.flush(); - expect($http.pendingCount()).toBe(0); + expect($http.pendingRequests.length).toBe(0); }); - it('should decrement the counter before firing callbacks', function() { + it('should remove the request before firing callbacks', function() { $httpBackend.when('GET').then(200); $http({method: 'get', url: '/url'}).on('xxx', function() { - expect($http.pendingCount()).toBe(0); + expect($http.pendingRequests.length).toBe(0); }); - expect($http.pendingCount()).toBe(1); + expect($http.pendingRequests.length).toBe(1); $httpBackend.flush(); }); }); |
