@@ -40,6 +40,8 @@ function Browser(window, document, body, XHR, $log) {
40
40
rawDocument = document [ 0 ] ,
41
41
location = window . location ,
42
42
setTimeout = window . setTimeout ,
43
+ clearTimeout = window . clearTimeout ,
44
+ pendingDeferIds = { } ,
43
45
lastLocationUrl ;
44
46
45
47
self . isMock = false ;
@@ -163,15 +165,12 @@ function Browser(window, document, body, XHR, $log) {
163
165
* @returns {function() } the added function
164
166
*/
165
167
self . addPollFn = function ( fn ) {
166
- if ( ! pollTimeout ) startPoller ( 100 , setTimeout ) ;
168
+ if ( isUndefined ( pollTimeout ) ) startPoller ( 100 , setTimeout ) ;
167
169
pollFns . push ( fn ) ;
168
170
return fn ;
169
171
} ;
170
172
171
173
/**
172
- * @name angular.service.$browser#startPoller
173
- * @methodOf angular.service.$browser
174
- *
175
174
* @param {number } interval How often should browser call poll functions (ms)
176
175
* @param {function() } setTimeout Reference to a real or fake `setTimeout` function.
177
176
*
@@ -339,20 +338,49 @@ function Browser(window, document, body, XHR, $log) {
339
338
* @methodOf angular.service.$browser
340
339
* @param {function() } fn A function, who's execution should be defered.
341
340
* @param {number= } [delay=0] of milliseconds to defer the function execution.
341
+ * @returns {* } DeferId that can be used to cancel the task via `$browser.defer.cancel()`.
342
342
*
343
343
* @description
344
344
* Executes a fn asynchroniously via `setTimeout(fn, delay)`.
345
345
*
346
346
* Unlike when calling `setTimeout` directly, in test this function is mocked and instead of using
347
- * `setTimeout` in tests, the fns are queued in an array, which can be programmatically flushed via
348
- * `$browser.defer.flush()`.
347
+ * `setTimeout` in tests, the fns are queued in an array, which can be programmatically flushed
348
+ * via `$browser.defer.flush()`.
349
349
*
350
350
*/
351
351
self . defer = function ( fn , delay ) {
352
+ var timeoutId ;
352
353
outstandingRequestCount ++ ;
353
- setTimeout ( function ( ) { completeOutstandingRequest ( fn ) ; } , delay || 0 ) ;
354
+ timeoutId = setTimeout ( function ( ) {
355
+ delete pendingDeferIds [ timeoutId ] ;
356
+ completeOutstandingRequest ( fn ) ;
357
+ } , delay || 0 ) ;
358
+ pendingDeferIds [ timeoutId ] = true ;
359
+ return timeoutId ;
354
360
} ;
355
361
362
+
363
+ /**
364
+ * @workInProgress
365
+ * @ngdoc method
366
+ * @name angular.service.$browser.defer#cancel
367
+ * @methodOf angular.service.$browser.defer
368
+ * @returns {boolean } Returns `true` if the task hasn't executed yet and was successfuly canceled.
369
+ *
370
+ * @description
371
+ * Cancels a defered task identified with `deferId`.
372
+ */
373
+
374
+ self . defer . cancel = function ( deferId ) {
375
+ if ( pendingDeferIds [ deferId ] ) {
376
+ delete pendingDeferIds [ deferId ] ;
377
+ clearTimeout ( deferId ) ;
378
+ completeOutstandingRequest ( noop ) ;
379
+ return true ;
380
+ }
381
+ } ;
382
+
383
+
356
384
//////////////////////////////////////////////////////////////
357
385
// Misc API
358
386
//////////////////////////////////////////////////////////////
0 commit comments