Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fdcc2db

Browse files
vojtajinaIgorMinar
authored andcommitted
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
1 parent 5ad0c7d commit fdcc2db

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

src/service/http.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ function transform(data, fns, param) {
5656
* @requires $exceptionHandler
5757
* @requires $cacheFactory
5858
*
59+
* @property {Array.<XhrFuture>} pendingRequests Array of pending requests.
60+
*
5961
* @description
6062
*/
6163
function $HttpProvider() {
@@ -89,28 +91,14 @@ function $HttpProvider() {
8991
this.$get = ['$httpBackend', '$browser', '$exceptionHandler', '$cacheFactory', '$rootScope',
9092
function($httpBackend, $browser, $exceptionHandler, $cacheFactory, $rootScope) {
9193

92-
var cache = $cacheFactory('$http'),
93-
pendingRequestsCount = 0;
94+
var cache = $cacheFactory('$http');
9495

9596
// the actual service
9697
function $http(config) {
9798
return new XhrFuture().retry(config);
9899
}
99100

100-
/**
101-
* @workInProgress
102-
* @ngdoc method
103-
* @name angular.service.$http#pendingCount
104-
* @methodOf angular.service.$http
105-
*
106-
* @description
107-
* Return number of pending requests
108-
*
109-
* @returns {number} Number of pending requests
110-
*/
111-
$http.pendingCount = function() {
112-
return pendingRequestsCount;
113-
};
101+
$http.pendingRequests = [];
114102

115103
/**
116104
* @ngdoc method
@@ -236,12 +224,14 @@ function $HttpProvider() {
236224
/**
237225
* Represents Request object, returned by $http()
238226
*
239-
* !!! ACCESS CLOSURE VARS: $httpBackend, $browser, $config, $log, $rootScope, cache, pendingRequestsCount
227+
* !!! ACCESS CLOSURE VARS:
228+
* $httpBackend, $browser, $config, $log, $rootScope, cache, $http.pendingRequests
240229
*/
241230
function XhrFuture() {
242-
var rawRequest, cfg = {}, callbacks = [],
231+
var rawRequest, parsedHeaders,
232+
cfg = {}, callbacks = [],
243233
defHeaders = $config.headers,
244-
parsedHeaders;
234+
self = this;
245235

246236
/**
247237
* Callback registered to $httpBackend():
@@ -281,9 +271,11 @@ function $HttpProvider() {
281271
response = transform(response, cfg.transformResponse || $config.transformResponse, rawRequest);
282272

283273
var regexp = statusToRegexp(status),
284-
pattern, callback;
274+
pattern, callback, idx;
285275

286-
pendingRequestsCount--;
276+
// remove from pending requests
277+
if ((idx = indexOf($http.pendingRequests, self)) !== -1)
278+
$http.pendingRequests.splice(idx, 1);
287279

288280
// normalize internal statuses to 0
289281
status = Math.max(status, 0);
@@ -372,7 +364,7 @@ function $HttpProvider() {
372364
rawRequest = $httpBackend(cfg.method, cfg.url, data, done, headers, cfg.timeout);
373365
}
374366

375-
pendingRequestsCount++;
367+
$http.pendingRequests.push(self);
376368
return this;
377369
};
378370

@@ -423,6 +415,11 @@ function $HttpProvider() {
423415

424416
return this;
425417
};
418+
419+
/**
420+
* Configuration object of the request
421+
*/
422+
this.config = cfg;
426423
}
427424
}];
428425
}

test/service/httpSpec.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -862,54 +862,54 @@ describe('$http', function() {
862862
});
863863

864864

865-
describe('pendingCount', function() {
865+
describe('pendingRequests', function() {
866866

867-
it('should return number of pending requests', function() {
867+
it('should be an array of pending requests', function() {
868868
$httpBackend.when('GET').then(200);
869-
expect($http.pendingCount()).toBe(0);
869+
expect($http.pendingRequests.length).toBe(0);
870870

871871
$http({method: 'get', url: '/some'});
872-
expect($http.pendingCount()).toBe(1);
872+
expect($http.pendingRequests.length).toBe(1);
873873

874874
$httpBackend.flush();
875-
expect($http.pendingCount()).toBe(0);
875+
expect($http.pendingRequests.length).toBe(0);
876876
});
877877

878878

879-
it('should decrement the counter when request aborted', function() {
879+
it('should remove the request when aborted', function() {
880880
$httpBackend.when('GET').then(0);
881881
future = $http({method: 'get', url: '/x'});
882-
expect($http.pendingCount()).toBe(1);
882+
expect($http.pendingRequests.length).toBe(1);
883883

884884
future.abort();
885885
$httpBackend.flush();
886886

887-
expect($http.pendingCount()).toBe(0);
887+
expect($http.pendingRequests.length).toBe(0);
888888
});
889889

890890

891-
it('should decrement the counter when served from cache', function() {
891+
it('should remove the request when served from cache', function() {
892892
$httpBackend.when('GET').then(200);
893893

894894
$http({method: 'get', url: '/cached', cache: true});
895895
$httpBackend.flush();
896-
expect($http.pendingCount()).toBe(0);
896+
expect($http.pendingRequests.length).toBe(0);
897897

898898
$http({method: 'get', url: '/cached', cache: true});
899-
expect($http.pendingCount()).toBe(1);
899+
expect($http.pendingRequests.length).toBe(1);
900900

901901
$browser.defer.flush();
902-
expect($http.pendingCount()).toBe(0);
902+
expect($http.pendingRequests.length).toBe(0);
903903
});
904904

905905

906-
it('should decrement the counter before firing callbacks', function() {
906+
it('should remove the request before firing callbacks', function() {
907907
$httpBackend.when('GET').then(200);
908908
$http({method: 'get', url: '/url'}).on('xxx', function() {
909-
expect($http.pendingCount()).toBe(0);
909+
expect($http.pendingRequests.length).toBe(0);
910910
});
911911

912-
expect($http.pendingCount()).toBe(1);
912+
expect($http.pendingRequests.length).toBe(1);
913913
$httpBackend.flush();
914914
});
915915
});

0 commit comments

Comments
 (0)