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

Commit e527559

Browse files
committed
refactor($interval): do not use notify to trigger the callback
Do not use `$q.notify` to trigger the callback. This allows `$q` to be replaced with another Promise/A+ compilant library Closes: #13261
1 parent 78297d2 commit e527559

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/ng/interval.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
function $IntervalProvider() {
5-
this.$get = ['$rootScope', '$window', '$q', '$$q',
6-
function($rootScope, $window, $q, $$q) {
5+
this.$get = ['$rootScope', '$window', '$q', '$$q', '$browser',
6+
function($rootScope, $window, $q, $$q, $browser) {
77
var intervals = {};
88

99

@@ -144,11 +144,12 @@ function $IntervalProvider() {
144144

145145
count = isDefined(count) ? count : 0;
146146

147-
promise.then(null, null, (!hasParams) ? fn : function() {
148-
fn.apply(null, args);
149-
});
150-
151147
promise.$$intervalId = setInterval(function tick() {
148+
if (skipApply) {
149+
$browser.defer(callback);
150+
} else {
151+
$rootScope.$evalAsync(callback);
152+
}
152153
deferred.notify(iteration++);
153154

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

166167
return promise;
168+
169+
function callback() {
170+
if (!hasParams) {
171+
fn(iteration);
172+
} else {
173+
fn.apply(null, args);
174+
}
175+
}
167176
}
168177

169178

test/ng/intervalSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,35 @@ describe('$interval', function() {
115115
}));
116116

117117

118+
it('should not depend on `notify` to trigger the callback call', function() {
119+
module(function($provide) {
120+
$provide.decorator('$q', function($delegate) {
121+
function replacement() {}
122+
replacement.defer = function() {
123+
var result = $delegate.defer();
124+
result.notify = noop;
125+
return result;
126+
};
127+
return replacement;
128+
});
129+
});
130+
131+
inject(function($interval, $window) {
132+
var counter = 0;
133+
$interval(function() { counter++; }, 1000);
134+
135+
expect(counter).toBe(0);
136+
137+
$window.flush(1000);
138+
expect(counter).toBe(1);
139+
140+
$window.flush(1000);
141+
142+
expect(counter).toBe(2);
143+
});
144+
});
145+
146+
118147
it('should allow you to specify the delay time', inject(function($interval, $window) {
119148
var counter = 0;
120149
$interval(function() { counter++; }, 123);

0 commit comments

Comments
 (0)