From 7974e7eb5f3821b77691819683f9aa37f3c37473 Mon Sep 17 00:00:00 2001 From: DiPeng Date: Fri, 24 Jun 2011 12:26:44 -0700 Subject: refactor($browser): hide startPoll and poll methods Breaks $browser.poll() method is moved inline to $browser.startpoll() Breaks $browser.startpoll() method is made private Refactor tests to reflect updated browser API Closes #387 --- src/Browser.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'src/Browser.js') diff --git a/src/Browser.js b/src/Browser.js index 4340ac4e..61396bd1 100644 --- a/src/Browser.js +++ b/src/Browser.js @@ -10,8 +10,9 @@ var XHR = window.XMLHttpRequest || function () { /** - * @private - * @name Browser + * @ngdoc service + * @name angular.service.$browser + * @requires $log * * @description * Constructor for the object exposed as $browser service. @@ -21,6 +22,11 @@ var XHR = window.XMLHttpRequest || function () { * - hide all the global state in the browser caused by the window object * - abstract away all the browser specific features and inconsistencies * + * For tests we provide {@link angular.mock.service.$browser mock implementation} of the `$browser` + * service, which can be used for convenient testing of the application without the interaction with + * the real browser apis. + */ +/** * @param {object} window The global window object. * @param {object} document jQuery wrapped document. * @param {object} body jQuery wrapped document.body. @@ -121,6 +127,11 @@ function Browser(window, document, body, XHR, $log) { * @param {function()} callback Function that will be called when no outstanding request */ self.notifyWhenNoOutstandingRequests = function(callback) { + // force browser to execute all pollFns - this is needed so that cookies and other pollers fire + // at some deterministic time in respect to the test runner's actions. Leaving things up to the + // regular poller would result in flaky tests. + forEach(pollFns, function(pollFn){ pollFn(); }); + if (outstandingRequestCount === 0) { callback(); } else { @@ -134,16 +145,6 @@ function Browser(window, document, body, XHR, $log) { var pollFns = [], pollTimeout; - /** - * @workInProgress - * @ngdoc method - * @name angular.service.$browser#poll - * @methodOf angular.service.$browser - */ - self.poll = function() { - forEach(pollFns, function(pollFn){ pollFn(); }); - }; - /** * @workInProgress * @ngdoc method @@ -159,14 +160,12 @@ function Browser(window, document, body, XHR, $log) { * @returns {function()} the added function */ self.addPollFn = function(fn) { - if (!pollTimeout) self.startPoller(100, setTimeout); + if (!pollTimeout) startPoller(100, setTimeout); pollFns.push(fn); return fn; }; /** - * @workInProgress - * @ngdoc method * @name angular.service.$browser#startPoller * @methodOf angular.service.$browser * @@ -177,9 +176,9 @@ function Browser(window, document, body, XHR, $log) { * Configures the poller to run in the specified intervals, using the specified * setTimeout fn and kicks it off. */ - self.startPoller = function(interval, setTimeout) { - (function check(){ - self.poll(); + function startPoller(interval, setTimeout) { + (function check() { + forEach(pollFns, function(pollFn){ pollFn(); }); pollTimeout = setTimeout(check, interval); })(); }; -- cgit v1.2.3