Skip to content

Commit e08dd2b

Browse files
committed
fix($http): properly synchronize $browser outstandingRequestCount
The issue is that using $http doesn't update $browser.outstandingRequestCount synchronously so that $browser.notifyWhenNoOutstandingRequests waits accordingly. Configuring this synchronization with the promise chain updates the outstandingRequestCount correctly. Closes angular#13782
1 parent f0f6da3 commit e08dd2b

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/ng/http.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ function $HttpProvider() {
375375
**/
376376
var interceptorFactories = this.interceptors = [];
377377

378-
this.$get = ['$httpBackend', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector',
379-
function($httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector) {
378+
this.$get = ['$httpBackend', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector', '$browser',
379+
function($httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector, $browser) {
380380

381381
var defaultCache = $cacheFactory('$http');
382382

@@ -968,13 +968,19 @@ function $HttpProvider() {
968968
}
969969
});
970970

971+
$browser.$$incOutstandingRequestCount();
972+
971973
while (chain.length) {
972974
var thenFn = chain.shift();
973975
var rejectFn = chain.shift();
974976

975977
promise = promise.then(thenFn, rejectFn);
976978
}
977979

980+
promise.finally(function() {
981+
$browser.$$completeOutstandingRequest(noop);
982+
});
983+
978984
if (useLegacyPromise) {
979985
promise.success = function(fn) {
980986
assertArgFn(fn, 'fn');

src/ng/httpBackend.js

-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ function $HttpBackendProvider() {
5555
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
5656
// TODO(vojta): fix the signature
5757
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
58-
$browser.$$incOutstandingRequestCount();
5958
url = url || $browser.url();
6059

6160
if (lowercase(method) == 'jsonp') {
@@ -158,7 +157,6 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
158157
jsonpDone = xhr = null;
159158

160159
callback(status, response, headersString, statusText);
161-
$browser.$$completeOutstandingRequest(noop);
162160
}
163161
};
164162

src/ngMock/angular-mocks.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ angular.mock.$Browser = function() {
3737
self.pollFns = [];
3838

3939
// TODO(vojta): remove this temporary api
40-
self.$$completeOutstandingRequest = angular.noop;
41-
self.$$incOutstandingRequestCount = angular.noop;
40+
self.$$outstandingRequestCount = 0;
41+
self.$$completeOutstandingRequest = function() { self.$$outstandingRequestCount--; };
42+
self.$$incOutstandingRequestCount = function() { self.$$outstandingRequestCount++; };
4243

4344

4445
// register url polling fn

test/ng/httpSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,24 @@ describe('$http', function() {
18661866
expect(paramSerializer({foo: 'foo', bar: ['bar', 'baz']})).toEqual('bar=bar&bar=baz&foo=foo');
18671867
});
18681868
});
1869+
1870+
describe('$browser outstandingRequests', function() {
1871+
var $browser;
1872+
1873+
beforeEach(inject(['$browser', function(browser) {
1874+
$browser = browser;
1875+
}]));
1876+
1877+
it('should update $brower outstandingRequestCount', function() {
1878+
$httpBackend.when('GET').respond(200);
1879+
1880+
expect($browser.$$outstandingRequestCount).toBe(0);
1881+
$http({method: 'GET', url: '/some'});
1882+
expect($browser.$$outstandingRequestCount).toBe(1);
1883+
$httpBackend.flush();
1884+
expect($browser.$$outstandingRequestCount).toBe(0);
1885+
});
1886+
});
18691887
});
18701888

18711889

0 commit comments

Comments
 (0)