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

Commit 7e73232

Browse files
committed
fixup! feat(*): implement more granular pending task tracking
1 parent 8397d3c commit 7e73232

File tree

5 files changed

+231
-73
lines changed

5 files changed

+231
-73
lines changed

src/ng/browser.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ function Browser(window, document, $log, $sniffer) {
7575

7676
function decOutstandingRequestCount(taskType) {
7777
taskType = taskType || DEFAULT_TASK_TYPE;
78-
if (outstandingRequestCounts[taskType]) outstandingRequestCounts[taskType]--;
79-
if (outstandingRequestCounts[ALL_TASKS_TYPE]) outstandingRequestCounts[ALL_TASKS_TYPE]--;
78+
if (outstandingRequestCounts[taskType]) {
79+
outstandingRequestCounts[taskType]--;
80+
outstandingRequestCounts[ALL_TASKS_TYPE]--;
81+
}
8082
}
8183

8284
function incOutstandingRequestCount(taskType) {

src/ng/testability.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,20 @@ function $$TestabilityProvider() {
104104
* @name $$testability#whenStable
105105
*
106106
* @description
107-
* Calls the callback when all pending requests (such as `$timeout` and `$http`) are completed.
108-
* You can optionally pass a `taskType`, in which case the callback will be called when all
109-
* pending requests of that type are completed (even if there are tasks of different types still
110-
* pending).
107+
* Calls the callback when all pending tasks are completed.
111108
*
112-
* Available task types:
113-
* - `$timeout`: Pending timeouts (via {@link $timeout}).
114-
* - `$http`: Pending HTTP requests (via {@link $http}).
115-
* - `$route`: A route transition is in progress (via {@link $route}).
116-
* - `$applyAsync`: Pending tasks scheduled via {@link $rootScope#$applyAsync}.
117-
* - `$evalAsync`: Pending tasks scheduled via {@link $rootScope#$evalAsync}.
109+
* Types of tasks waited for include:
110+
* - Pending timeouts (via {@link $timeout}).
111+
* - Pending HTTP requests (via {@link $http}).
112+
* - In-progress route transitions (via {@link $route}).
113+
* - Pending tasks scheduled via {@link $rootScope#$applyAsync}.
114+
* - Pending tasks scheduled via {@link $rootScope#$evalAsync}.
118115
* These include tasks scheduled via `$evalAsync()` indirectly (such as {@link $q} promises).
119116
*
120117
* @param {function} callback
121-
* @param {string=} taskType
122118
*/
123-
testability.whenStable = function(callback, taskType) {
124-
$browser.notifyWhenNoOutstandingRequests(callback, taskType);
119+
testability.whenStable = function(callback) {
120+
$browser.notifyWhenNoOutstandingRequests(callback);
125121
};
126122

127123
return testability;

src/ngMock/angular-mocks.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ angular.mock.$Browser = function() {
5252

5353
function decOutstandingRequestCount(taskType) {
5454
taskType = taskType || DEFAULT_TASK_TYPE;
55-
if (outstandingRequestCounts[taskType]) outstandingRequestCounts[taskType]--;
56-
if (outstandingRequestCounts[ALL_TASKS_TYPE]) outstandingRequestCounts[ALL_TASKS_TYPE]--;
55+
if (outstandingRequestCounts[taskType]) {
56+
outstandingRequestCounts[taskType]--;
57+
outstandingRequestCounts[ALL_TASKS_TYPE]--;
58+
}
5759
}
5860
function incOutstandingRequestCount(taskType) {
5961
taskType = taskType || DEFAULT_TASK_TYPE;
@@ -184,14 +186,12 @@ angular.mock.$Browser = function() {
184186
if (angular.isDefined(delay)) {
185187
// A delay was passed so compute the next time
186188
nextTime = self.defer.now + delay;
189+
} else if (self.deferredFns.length) {
190+
// No delay was passed so set the next time so that it clears the deferred queue
191+
nextTime = self.deferredFns[self.deferredFns.length - 1].time;
187192
} else {
188-
if (self.deferredFns.length) {
189-
// No delay was passed so set the next time so that it clears the deferred queue
190-
nextTime = self.deferredFns[self.deferredFns.length - 1].time;
191-
} else {
192-
// No delay passed, but there are no deferred tasks so flush - indicates an error!
193-
throw new Error('No deferred tasks to be flushed');
194-
}
193+
// No delay passed, but there are no deferred tasks so flush - indicates an error!
194+
throw new Error('No deferred tasks to be flushed');
195195
}
196196

197197
while (self.deferredFns.length && self.deferredFns[0].time <= nextTime) {
@@ -2263,6 +2263,9 @@ angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function($delegate, $
22632263
* @param {number=} delay maximum timeout amount to flush up until
22642264
*/
22652265
$delegate.flush = function(delay) {
2266+
// For historical reasons, `$timeout.flush()` flushes all types of pending tasks.
2267+
// Keep the same behavior for backwards compatibility (and because it doesn't make sense to
2268+
// selectively flush scheduled events out of order).
22662269
$browser.defer.flush(delay);
22672270
};
22682271

@@ -2274,6 +2277,8 @@ angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function($delegate, $
22742277
* Verifies that there are no pending tasks that need to be flushed.
22752278
*/
22762279
$delegate.verifyNoPendingTasks = function() {
2280+
// For historical reasons, `$timeout.verifyNoPendingTasks()` takes all types of pending tasks
2281+
// into account. Keep the same behavior for backwards compatibility.
22772282
$browser.defer.verifyNoPendingTasks();
22782283
};
22792284

test/ng/testabilitySpec.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,7 @@ describe('$$testability', function() {
201201
var callback = noop;
202202

203203
$$testability.whenStable(callback);
204-
expect(spy).toHaveBeenCalledWith(callback, undefined);
205-
206-
spy.calls.reset();
207-
$$testability.whenStable(callback, 'taskType');
208-
expect(spy).toHaveBeenCalledWith(callback, 'taskType');
204+
expect(spy).toHaveBeenCalledWith(callback);
209205
}));
210206
});
211207
});

0 commit comments

Comments
 (0)