diff options
Diffstat (limited to 'src/Browser.js')
| -rw-r--r-- | src/Browser.js | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/src/Browser.js b/src/Browser.js index 55b65471..58ad22c8 100644 --- a/src/Browser.js +++ b/src/Browser.js @@ -40,6 +40,8 @@ function Browser(window, document, body, XHR, $log) { rawDocument = document[0], location = window.location, setTimeout = window.setTimeout, + clearTimeout = window.clearTimeout, + pendingDeferIds = {}, lastLocationUrl; self.isMock = false; @@ -163,15 +165,12 @@ function Browser(window, document, body, XHR, $log) { * @returns {function()} the added function */ self.addPollFn = function(fn) { - if (!pollTimeout) startPoller(100, setTimeout); + if (isUndefined(pollTimeout)) startPoller(100, setTimeout); pollFns.push(fn); return fn; }; /** - * @name angular.service.$browser#startPoller - * @methodOf angular.service.$browser - * * @param {number} interval How often should browser call poll functions (ms) * @param {function()} setTimeout Reference to a real or fake `setTimeout` function. * @@ -339,20 +338,49 @@ function Browser(window, document, body, XHR, $log) { * @methodOf angular.service.$browser * @param {function()} fn A function, who's execution should be defered. * @param {number=} [delay=0] of milliseconds to defer the function execution. + * @returns {*} DeferId that can be used to cancel the task via `$browser.defer.cancel()`. * * @description * Executes a fn asynchroniously via `setTimeout(fn, delay)`. * * Unlike when calling `setTimeout` directly, in test this function is mocked and instead of using - * `setTimeout` in tests, the fns are queued in an array, which can be programmatically flushed via - * `$browser.defer.flush()`. + * `setTimeout` in tests, the fns are queued in an array, which can be programmatically flushed + * via `$browser.defer.flush()`. * */ self.defer = function(fn, delay) { + var timeoutId; outstandingRequestCount++; - setTimeout(function() { completeOutstandingRequest(fn); }, delay || 0); + timeoutId = setTimeout(function() { + delete pendingDeferIds[timeoutId]; + completeOutstandingRequest(fn); + }, delay || 0); + pendingDeferIds[timeoutId] = true; + return timeoutId; }; + + /** + * @workInProgress + * @ngdoc method + * @name angular.service.$browser.defer#cancel + * @methodOf angular.service.$browser.defer + * @returns {boolean} Returns `true` if the task hasn't executed yet and was successfuly canceled. + * + * @description + * Cancels a defered task identified with `deferId`. + */ + + self.defer.cancel = function(deferId) { + if (pendingDeferIds[deferId]) { + delete pendingDeferIds[deferId]; + clearTimeout(deferId); + completeOutstandingRequest(noop); + return true; + } + }; + + ////////////////////////////////////////////////////////////// // Misc API ////////////////////////////////////////////////////////////// |
