Refactor $browser's lazy start polling

+ unit tests
This commit is contained in:
Vojta Jina 2011-06-01 17:38:25 +02:00 committed by Igor Minar
parent 50076b571d
commit dad2603752
3 changed files with 29 additions and 9 deletions

View file

@ -12,12 +12,6 @@ angularService('$browser', function($log){
if (!browserSingleton) { if (!browserSingleton) {
browserSingleton = new Browser(window, jqLite(window.document), jqLite(window.document.body), browserSingleton = new Browser(window, jqLite(window.document), jqLite(window.document.body),
XHR, $log); XHR, $log);
var addPollFn = browserSingleton.addPollFn;
browserSingleton.addPollFn = function(){
browserSingleton.addPollFn = addPollFn;
browserSingleton.startPoller(100, function(delay, fn){setTimeout(delay,fn);});
return addPollFn.apply(browserSingleton, arguments);
};
browserSingleton.bind(); browserSingleton.bind();
} }
return browserSingleton; return browserSingleton;

View file

@ -141,7 +141,8 @@ function Browser(window, document, body, XHR, $log) {
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
// Poll Watcher API // Poll Watcher API
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
var pollFns = []; var pollFns = [],
pollTimeout;
/** /**
* @workInProgress * @workInProgress
@ -162,11 +163,13 @@ function Browser(window, document, body, XHR, $log) {
* @param {function()} fn Poll function to add * @param {function()} fn Poll function to add
* *
* @description * @description
* Adds a function to the list of functions that poller periodically executes * Adds a function to the list of functions that poller periodically executes,
* and starts polling if not started yet.
* *
* @returns {function()} the added function * @returns {function()} the added function
*/ */
self.addPollFn = function(fn) { self.addPollFn = function(fn) {
if (!pollTimeout) self.startPoller(100, setTimeout);
pollFns.push(fn); pollFns.push(fn);
return fn; return fn;
}; };
@ -187,7 +190,7 @@ function Browser(window, document, body, XHR, $log) {
self.startPoller = function(interval, setTimeout) { self.startPoller = function(interval, setTimeout) {
(function check(){ (function check(){
self.poll(); self.poll();
setTimeout(check, interval); pollTimeout = setTimeout(check, interval);
})(); })();
}; };

View file

@ -4,6 +4,7 @@ describe('browser', function(){
function fakeSetTimeout(fn) { function fakeSetTimeout(fn) {
setTimeoutQueue.push(fn); setTimeoutQueue.push(fn);
return Math.random();
} }
fakeSetTimeout.flush = function() { fakeSetTimeout.flush = function() {
@ -384,6 +385,10 @@ describe('browser', function(){
}); });
describe('poller', function(){ describe('poller', function(){
beforeEach(function() {
spyOn(browser, 'startPoller');
});
it('should call all fns on poll', function(){ it('should call all fns on poll', function(){
var log = ''; var log = '';
browser.addPollFn(function(){log+='a';}); browser.addPollFn(function(){log+='a';});
@ -399,6 +404,7 @@ describe('browser', function(){
var log = ''; var log = '';
var setTimeoutSpy = jasmine.createSpy('setTimeout'); var setTimeoutSpy = jasmine.createSpy('setTimeout');
browser.addPollFn(function(){log+='.';}); browser.addPollFn(function(){log+='.';});
browser.startPoller.andCallThrough();
browser.startPoller(50, setTimeoutSpy); browser.startPoller(50, setTimeoutSpy);
expect(log).toEqual('.'); expect(log).toEqual('.');
expect(setTimeoutSpy.mostRecentCall.args[1]).toEqual(50); expect(setTimeoutSpy.mostRecentCall.args[1]).toEqual(50);
@ -411,6 +417,22 @@ describe('browser', function(){
var returnedFn = browser.addPollFn(fn); var returnedFn = browser.addPollFn(fn);
expect(returnedFn).toBe(fn); expect(returnedFn).toBe(fn);
}); });
it('should auto start poller when first fn registered', function() {
browser.addPollFn(function() {});
expect(browser.startPoller).toHaveBeenCalled();
expect(browser.startPoller.callCount).toBe(1);
});
it('should auto start poller only when first fn registered', function() {
browser.startPoller.andCallThrough();
browser.addPollFn(function() {});
browser.addPollFn(function() {});
browser.addPollFn(function() {});
expect(browser.startPoller.callCount).toBe(1);
});
}); });
@ -424,6 +446,7 @@ describe('browser', function(){
}; };
browser = new Browser(fakeWindow, {}, {}); browser = new Browser(fakeWindow, {}, {});
browser.startPoller = function() {};
var events = []; var events = [];