Skip to content

Commit 5d39063

Browse files
author
Marvin Tam
committed
Pass invokeApply == false into $interval
This prevents excessive $digest cycles whenever the $interval callback fires. The invokeApply param is effectively ignored in 1.2.x so this patch doesn't do anything there. See angular/angular.js#7103 for more details on the fix that landed in 1.3.x.
1 parent de998b8 commit 5d39063

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/angular-idle.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
stopKeepalive();
142142
state.countdown = options.warningDuration;
143143
countdown();
144-
state.warning = $interval(countdown, 1000, options.warningDuration);
144+
state.warning = $interval(countdown, 1000, options.warningDuration, false);
145145
} else {
146146
startKeepalive();
147147
}
@@ -178,7 +178,7 @@
178178

179179
state.running = true;
180180

181-
state.idle = $interval(toggleState, options.idleDuration * 1000);
181+
state.idle = $interval(toggleState, options.idleDuration * 1000, 0, false);
182182
},
183183
unwatch: function() {
184184
$interval.cancel(state.idle);

src/angular-idle.spec.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('ngIdle', function() {
1111
});
1212

1313
describe('idle', function() {
14-
var $idleProvider, $interval, $rootScope, $log, $document, $keepalive;
14+
var $idleProvider, $interval, $rootScope, $log, $document, $keepalive, $timeout;
1515
var DEFAULTIDLEDURATION = 20*60*1000, DEFAULTWARNINGDURATION = 30 * 1000;
1616

1717
beforeEach(module('ngIdle.idle'));
@@ -26,11 +26,12 @@ describe('ngIdle', function() {
2626

2727
module('app');
2828

29-
inject(function(_$interval_, _$log_, _$rootScope_, _$document_) {
29+
inject(function(_$interval_, _$log_, _$rootScope_, _$document_, _$timeout_) {
3030
$rootScope = _$rootScope_;
3131
$interval = _$interval_;
3232
$log = _$log_;
3333
$document = _$document_;
34+
$timeout = _$timeout_;
3435
});
3536

3637
$keepalive = {
@@ -164,6 +165,7 @@ describe('ngIdle', function() {
164165
$idle.watch();
165166

166167
$interval.flush(DEFAULTIDLEDURATION);
168+
$timeout.flush();
167169

168170
expect($rootScope.$broadcast).toHaveBeenCalledWith('$idleStart');
169171
expect($keepalive.stop).toHaveBeenCalled();
@@ -175,6 +177,7 @@ describe('ngIdle', function() {
175177
$idle.watch();
176178

177179
$interval.flush(DEFAULTIDLEDURATION);
180+
$timeout.flush();
178181

179182
$idle.watch();
180183

@@ -189,15 +192,24 @@ describe('ngIdle', function() {
189192
$idle.watch();
190193

191194
$interval.flush(DEFAULTIDLEDURATION);
195+
$timeout.flush();
192196

193197
expect($rootScope.$broadcast).toHaveBeenCalledWith('$idleStart');
194198
expect($rootScope.$broadcast).toHaveBeenCalledWith('$idleWarn', 3);
199+
195200
$interval.flush(1000);
201+
$timeout.flush();
202+
196203
expect($rootScope.$broadcast).toHaveBeenCalledWith('$idleWarn', 2);
204+
197205
$interval.flush(1000);
206+
$timeout.flush();
207+
198208
expect($rootScope.$broadcast).toHaveBeenCalledWith('$idleWarn', 1);
199209

200210
$interval.flush(1000);
211+
$timeout.flush();
212+
201213
expect($rootScope.$broadcast).toHaveBeenCalledWith('$idleTimeout');
202214

203215
// ensure idle interval doesn't keep executing after $idleStart
@@ -214,6 +226,7 @@ describe('ngIdle', function() {
214226
$interval.flush(DEFAULTIDLEDURATION);
215227

216228
$interval.flush(1000);
229+
$timeout.flush();
217230

218231
expect($idle.idling()).toBe(true);
219232

0 commit comments

Comments
 (0)