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

Commit 6b69480

Browse files
committed
feat($timeout): overload service API.
Provide an overload for $timeout that doesn't take a function. Closes #9176
1 parent 89c57a8 commit 6b69480

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/ng/timeout.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
function $TimeoutProvider() {
55
this.$get = ['$rootScope', '$browser', '$q', '$$q', '$exceptionHandler',
66
function($rootScope, $browser, $q, $$q, $exceptionHandler) {
7-
var deferreds = {};
7+
8+
var deferreds = {};
89

910

1011
/**
@@ -19,20 +20,35 @@ function $TimeoutProvider() {
1920
* The return value of registering a timeout function is a promise, which will be resolved when
2021
* the timeout is reached and the timeout function is executed.
2122
*
23+
* Additionally, the wrapper function can also be used without a target 'fn' function in order
24+
* to generate a delayed promise.
25+
*
2226
* To cancel a timeout request, call `$timeout.cancel(promise)`.
2327
*
2428
* In tests you can use {@link ngMock.$timeout `$timeout.flush()`} to
2529
* synchronously flush the queue of deferred functions.
2630
*
27-
* @param {function()} fn A function, whose execution should be delayed.
31+
* @param {function()} fn A function, whose execution should be delayed. This argument is
32+
* optional and can be skipped if required.
2833
* @param {number=} [delay=0] Delay in milliseconds.
2934
* @param {boolean=} [invokeApply=true] If set to `false` skips model dirty checking, otherwise
3035
* will invoke `fn` within the {@link ng.$rootScope.Scope#$apply $apply} block.
3136
* @returns {Promise} Promise that will be resolved when the timeout is reached. The value this
3237
* promise will be resolved with is the return value of the `fn` function.
3338
*
3439
*/
35-
function timeout(fn, delay, invokeApply) {
40+
function timeout() {
41+
var fn, delay, invokeApply;
42+
if (isFunction(arguments[0])) {
43+
fn = arguments[0],
44+
delay = arguments[1],
45+
invokeApply = arguments[2];
46+
} else {
47+
fn = noop,
48+
delay = arguments[0],
49+
invokeApply = false;
50+
}
51+
3652
var skipApply = (isDefined(invokeApply) && !invokeApply),
3753
deferred = (skipApply ? $$q : $q).defer(),
3854
promise = deferred.promise,

test/ng/timeoutSpec.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ describe('$timeout', function() {
4848
}));
4949

5050

51+
it('should NOT call $apply if no callback function is used', inject(function($timeout, $rootScope) {
52+
var applySpy = spyOn($rootScope, '$apply').andCallThrough();
53+
54+
$timeout().then(function() {});
55+
expect(applySpy).not.toHaveBeenCalled();
56+
57+
$timeout.flush();
58+
expect(applySpy).not.toHaveBeenCalled();
59+
}));
60+
61+
5162
it('should NOT call $evalAsync or $digest if invokeApply is set to false',
5263
inject(function($timeout, $rootScope) {
5364
var evalAsyncSpy = spyOn($rootScope, '$evalAsync').andCallThrough();
@@ -69,6 +80,10 @@ describe('$timeout', function() {
6980
$timeout(noop, 123);
7081
expect(defer.callCount).toEqual(1);
7182
expect(defer.mostRecentCall.args[1]).toEqual(123);
83+
84+
$timeout(456);
85+
expect(defer.callCount).toEqual(2);
86+
expect(defer.mostRecentCall.args[1]).toEqual(456);
7287
}));
7388

7489

@@ -81,6 +96,14 @@ describe('$timeout', function() {
8196

8297
$timeout.flush();
8398
expect(log).toEqual(['timeout', 'promise success: buba']);
99+
100+
var promise = $timeout();
101+
102+
promise.then(function(value) { log('promise success'); }, log.fn('promise error'));
103+
expect(log).toEqual(['timeout', 'promise success: buba']);
104+
105+
$timeout.flush();
106+
expect(log).toEqual(['timeout', 'promise success: buba', 'promise success']);
84107
}));
85108

86109

@@ -165,19 +188,24 @@ describe('$timeout', function() {
165188
var task1 = jasmine.createSpy('task1'),
166189
task2 = jasmine.createSpy('task2'),
167190
task3 = jasmine.createSpy('task3'),
168-
promise1, promise3;
191+
task4 = jasmine.createSpy('task4'),
192+
promise1, promise3, promise4;
169193

170194
promise1 = $timeout(task1);
171195
$timeout(task2);
172196
promise3 = $timeout(task3, 333);
197+
promise4 = $timeout(333);
198+
promise3.then(task4);
173199

174-
$timeout.cancel(promise3);
175200
$timeout.cancel(promise1);
201+
$timeout.cancel(promise3);
202+
$timeout.cancel(promise4);
176203
$timeout.flush();
177204

178205
expect(task1).not.toHaveBeenCalled();
179206
expect(task2).toHaveBeenCalledOnce();
180207
expect(task3).not.toHaveBeenCalled();
208+
expect(task4).not.toHaveBeenCalled();
181209
}));
182210

183211

0 commit comments

Comments
 (0)