Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($http): properly synchronize $browser outstandingRequestCount #13861

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ function $HttpProvider() {
**/
var interceptorFactories = this.interceptors = [];

this.$get = ['$httpBackend', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector',
function($httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector) {
this.$get = ['$httpBackend', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector', '$browser',
function($httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector, $browser) {

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

Expand Down Expand Up @@ -968,13 +968,19 @@ function $HttpProvider() {
}
});

$browser.$$incOutstandingRequestCount();

while (chain.length) {
var thenFn = chain.shift();
var rejectFn = chain.shift();

promise = promise.then(thenFn, rejectFn);
}

promise.finally(function() {
$browser.$$completeOutstandingRequest(noop);
});

if (useLegacyPromise) {
promise.success = function(fn) {
assertArgFn(fn, 'fn');
Expand Down
2 changes: 0 additions & 2 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ function $HttpBackendProvider() {
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
// TODO(vojta): fix the signature
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
$browser.$$incOutstandingRequestCount();
url = url || $browser.url();

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

callback(status, response, headersString, statusText);
$browser.$$completeOutstandingRequest(noop);
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ angular.mock.$Browser = function() {
self.pollFns = [];

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


// register url polling fn
Expand Down
18 changes: 18 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,24 @@ describe('$http', function() {
expect(paramSerializer({foo: 'foo', bar: ['bar', 'baz']})).toEqual('bar=bar&bar=baz&foo=foo');
});
});

describe('$browser outstandingRequests', function() {
var $browser;

beforeEach(inject(['$browser', function(browser) {
$browser = browser;
}]));

it('should update $brower outstandingRequestCount', function() {
$httpBackend.when('GET').respond(200);

expect($browser.$$outstandingRequestCount).toBe(0);
$http({method: 'GET', url: '/some'});
expect($browser.$$outstandingRequestCount).toBe(1);
$httpBackend.flush();
expect($browser.$$outstandingRequestCount).toBe(0);
});
});
});


Expand Down