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

Commit cb55b32

Browse files
committed
changed some comments
1 parent 927f355 commit cb55b32

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

src/ng/browser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ function Browser(window, document, $log, $sniffer) {
321321
*/
322322
self.defer = function(fn, delay) {
323323
var timeoutId;
324-
outstandingRequestCount++;
324+
self.$$incOutstandingRequestCount();
325325
timeoutId = setTimeout(function() {
326326
delete pendingDeferIds[timeoutId];
327327
completeOutstandingRequest(fn);

src/ngMock/angular-mocks.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ angular.mock.$Browser = function() {
4343
var outstandingRequestCallbacks = [];
4444
self.$$incOutstandingRequestCount = function() { outstandingRequestCount++; };
4545
self.$$completeOutstandingRequest = function(fn) {
46-
fn();
47-
outstandingRequestCount--;
48-
if (!outstandingRequestCount) {
49-
while (outstandingRequestCallbacks.length) {
50-
outstandingRequestCallbacks.pop()();
46+
try {
47+
fn();
48+
} finally {
49+
outstandingRequestCount--;
50+
if (!outstandingRequestCount) {
51+
while (outstandingRequestCallbacks.length) {
52+
outstandingRequestCallbacks.pop()();
53+
}
5154
}
5255
}
5356
};
@@ -82,6 +85,8 @@ angular.mock.$Browser = function() {
8285
self.deferredNextId = 0;
8386

8487
self.defer = function(fn, delay) {
88+
// Note that we do not use `$$incOutstandingRequestCount` or `$$completeOutstandingRequest`
89+
// in this mock implementation.
8590
delay = delay || 0;
8691
self.deferredFns.push({time:(self.defer.now + delay), fn:fn, id: self.deferredNextId});
8792
self.deferredFns.sort(function(a, b) { return a.time - b.time;});

src/ngRoute/route.js

+5
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,11 @@ function $RouteProvider() {
706706
$rootScope.$broadcast('$routeChangeError', nextRoute, lastRoute, error);
707707
}
708708
}).finally(function() {
709+
// Because `commitRoute()` is called from a `$rootScope.$evalAsync` block (see
710+
// `$locationWatch`), this `$$completeOutstandingRequest()` call will not cause
711+
// `outstandingRequestCount` to hit zero. This is important in case we are redirecting
712+
// to a new route which also requires some asynchronous work.
713+
709714
$browser.$$completeOutstandingRequest(noop);
710715
});
711716
}

test/ngRoute/routeSpec.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -2199,13 +2199,10 @@ describe('$route', function() {
21992199

22002200
module(function($provide, $routeProvider) {
22012201
// While normally `$browser.defer()` modifies the `outstandingRequestCount`, the mocked
2202-
// version (provided by `ngMock`) does not. This doesn't matter in most tests, but it does
2203-
// here:
2204-
// `$browser.defer()` will be indirectly called as a result of `$locationWatch`'s call to
2205-
// `$rootScope.$evalAsync()`. In the async function, the `$locationChangeSuccess` event will
2206-
// be broadcasted, which will in turn trigger `commitRoute()` in `ngRoute`.
2207-
// `outstandingRequestCount` must not reach 0 during the time between calling
2208-
// `$rootScope.$evalAsync()` and executing the async function.
2202+
// version (provided by `ngMock`) does not. This doesn't matter in most tests, but in this
2203+
// case we need the `outstandingRequestCount` logic to ensure that we don't call the
2204+
// `$$testability.whenStable()` callbacks part way through a `$rootScope.$evalAsync` block.
2205+
// See ngRoute's commitRoute()'s finally() block for details.
22092206
$provide.decorator('$browser', function($delegate) {
22102207
var oldDefer = $delegate.defer;
22112208
var newDefer = function(fn, delay) {

0 commit comments

Comments
 (0)