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

Commit dad2603

Browse files
vojtajinaIgorMinar
authored andcommitted
Refactor $browser's lazy start polling
+ unit tests
1 parent 50076b5 commit dad2603

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/AngularPublic.js

-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ angularService('$browser', function($log){
1212
if (!browserSingleton) {
1313
browserSingleton = new Browser(window, jqLite(window.document), jqLite(window.document.body),
1414
XHR, $log);
15-
var addPollFn = browserSingleton.addPollFn;
16-
browserSingleton.addPollFn = function(){
17-
browserSingleton.addPollFn = addPollFn;
18-
browserSingleton.startPoller(100, function(delay, fn){setTimeout(delay,fn);});
19-
return addPollFn.apply(browserSingleton, arguments);
20-
};
2115
browserSingleton.bind();
2216
}
2317
return browserSingleton;

src/Browser.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ function Browser(window, document, body, XHR, $log) {
141141
//////////////////////////////////////////////////////////////
142142
// Poll Watcher API
143143
//////////////////////////////////////////////////////////////
144-
var pollFns = [];
144+
var pollFns = [],
145+
pollTimeout;
145146

146147
/**
147148
* @workInProgress
@@ -162,11 +163,13 @@ function Browser(window, document, body, XHR, $log) {
162163
* @param {function()} fn Poll function to add
163164
*
164165
* @description
165-
* Adds a function to the list of functions that poller periodically executes
166+
* Adds a function to the list of functions that poller periodically executes,
167+
* and starts polling if not started yet.
166168
*
167169
* @returns {function()} the added function
168170
*/
169171
self.addPollFn = function(fn) {
172+
if (!pollTimeout) self.startPoller(100, setTimeout);
170173
pollFns.push(fn);
171174
return fn;
172175
};
@@ -187,7 +190,7 @@ function Browser(window, document, body, XHR, $log) {
187190
self.startPoller = function(interval, setTimeout) {
188191
(function check(){
189192
self.poll();
190-
setTimeout(check, interval);
193+
pollTimeout = setTimeout(check, interval);
191194
})();
192195
};
193196

test/BrowserSpecs.js

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ describe('browser', function(){
44

55
function fakeSetTimeout(fn) {
66
setTimeoutQueue.push(fn);
7+
return Math.random();
78
}
89

910
fakeSetTimeout.flush = function() {
@@ -384,6 +385,10 @@ describe('browser', function(){
384385
});
385386

386387
describe('poller', function(){
388+
beforeEach(function() {
389+
spyOn(browser, 'startPoller');
390+
});
391+
387392
it('should call all fns on poll', function(){
388393
var log = '';
389394
browser.addPollFn(function(){log+='a';});
@@ -399,6 +404,7 @@ describe('browser', function(){
399404
var log = '';
400405
var setTimeoutSpy = jasmine.createSpy('setTimeout');
401406
browser.addPollFn(function(){log+='.';});
407+
browser.startPoller.andCallThrough();
402408
browser.startPoller(50, setTimeoutSpy);
403409
expect(log).toEqual('.');
404410
expect(setTimeoutSpy.mostRecentCall.args[1]).toEqual(50);
@@ -411,6 +417,22 @@ describe('browser', function(){
411417
var returnedFn = browser.addPollFn(fn);
412418
expect(returnedFn).toBe(fn);
413419
});
420+
421+
it('should auto start poller when first fn registered', function() {
422+
browser.addPollFn(function() {});
423+
424+
expect(browser.startPoller).toHaveBeenCalled();
425+
expect(browser.startPoller.callCount).toBe(1);
426+
});
427+
428+
it('should auto start poller only when first fn registered', function() {
429+
browser.startPoller.andCallThrough();
430+
browser.addPollFn(function() {});
431+
browser.addPollFn(function() {});
432+
browser.addPollFn(function() {});
433+
434+
expect(browser.startPoller.callCount).toBe(1);
435+
});
414436
});
415437

416438

@@ -424,6 +446,7 @@ describe('browser', function(){
424446
};
425447

426448
browser = new Browser(fakeWindow, {}, {});
449+
browser.startPoller = function() {};
427450

428451
var events = [];
429452

0 commit comments

Comments
 (0)