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

refactor($interval): do not use notify to trigger the callback #13261

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/ng/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


function $IntervalProvider() {
this.$get = ['$rootScope', '$window', '$q', '$$q',
function($rootScope, $window, $q, $$q) {
this.$get = ['$rootScope', '$window', '$q', '$$q', '$browser',
function($rootScope, $window, $q, $$q, $browser) {
var intervals = {};


Expand Down Expand Up @@ -144,11 +144,12 @@ function $IntervalProvider() {

count = isDefined(count) ? count : 0;

promise.then(null, null, (!hasParams) ? fn : function() {
fn.apply(null, args);
});

promise.$$intervalId = setInterval(function tick() {
if (skipApply) {
$browser.defer(callback);
} else {
$rootScope.$evalAsync(callback);
}
deferred.notify(iteration++);

if (count > 0 && iteration >= count) {
Expand All @@ -164,6 +165,14 @@ function $IntervalProvider() {
intervals[promise.$$intervalId] = deferred;

return promise;

function callback() {
if (!hasParams) {
fn(iteration);
} else {
fn.apply(null, args);
}
}
}


Expand Down
29 changes: 29 additions & 0 deletions test/ng/intervalSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@ describe('$interval', function() {
}));


it('should not depend on `notify` to trigger the callback call', function() {
module(function($provide) {
$provide.decorator('$q', function($delegate) {
function replacement() {}
replacement.defer = function() {
var result = $delegate.defer();
result.notify = noop;
return result;
};
return replacement;
});
});

inject(function($interval, $window) {
var counter = 0;
$interval(function() { counter++; }, 1000);

expect(counter).toBe(0);

$window.flush(1000);
expect(counter).toBe(1);

$window.flush(1000);

expect(counter).toBe(2);
});
});


it('should allow you to specify the delay time', inject(function($interval, $window) {
var counter = 0;
$interval(function() { counter++; }, 123);
Expand Down