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

Commit 35d635c

Browse files
committed
fix($animate): prevent cancellation timestamp from being too far in the future
Closes #6748
1 parent db2a4c0 commit 35d635c

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/ngAnimate/animate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ angular.module('ngAnimate', ['ng'])
11121112

11131113
//but it may not need to cancel out the existing timeout
11141114
//if the timestamp is less than the previous one
1115-
var futureTimestamp = Date.now() + (totalTime * 1000);
1115+
var futureTimestamp = Date.now() + totalTime;
11161116
if(futureTimestamp <= closingTimestamp) {
11171117
return;
11181118
}

test/ngAnimate/animateSpec.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -1332,24 +1332,43 @@ describe("ngAnimate", function() {
13321332
}));
13331333

13341334
it("should intelligently cancel former timeouts and close off a series of elements a final timeout", function() {
1335+
var currentTimestamp = Date.now();
1336+
spyOn(Date,'now').andCallFake(function() {
1337+
return currentTimestamp;
1338+
});
1339+
13351340
var cancellations = 0;
13361341
module(function($provide) {
13371342
$provide.decorator('$timeout', function($delegate) {
13381343
var _cancel = $delegate.cancel;
1339-
$delegate.cancel = function() {
1340-
cancellations++;
1341-
return _cancel.apply($delegate, arguments);
1344+
$delegate.cancel = function(timer) {
1345+
if(timer) {
1346+
cancellations++;
1347+
return _cancel.apply($delegate, arguments);
1348+
}
13421349
};
13431350
return $delegate;
13441351
});
13451352
})
13461353
inject(function($animate, $rootScope, $compile, $sniffer, $timeout) {
13471354
if (!$sniffer.transitions) return;
13481355

1349-
ss.addRule('.animate-me', '-webkit-transition:1s linear all;' +
1356+
ss.addRule('.animate-me div', '-webkit-transition:1s linear all;' +
13501357
'transition:1s linear all;');
13511358

1352-
element = $compile(html('<div><div class="animate-me" ng-repeat="item in items"></div></div>'))($rootScope);
1359+
ss.addRule('.animate-me-longer div', '-webkit-transition:1.5s linear all;' +
1360+
'transition:1.5s linear all;');
1361+
1362+
element = $compile(html('<div class="animate-me-longer">' +
1363+
' <div ng-repeat="item in items"></div>' +
1364+
'</div>'))($rootScope);
1365+
$rootScope.items = [0];
1366+
$rootScope.$digest();
1367+
$animate.triggerReflow();
1368+
1369+
currentTimestamp += 2250; //1.5 * 1500 = 2250
1370+
1371+
element[0].className = 'animate-me';
13531372

13541373
$rootScope.items = [1,2,3,4,5,6,7,8,9,10];
13551374
var totalOperations = $rootScope.items.length;
@@ -1358,9 +1377,11 @@ describe("ngAnimate", function() {
13581377

13591378
$rootScope.items = [0];
13601379
$animate.triggerReflow();
1380+
1381+
currentTimestamp += 1500; //1.5 * 1000 = 1500
13611382
$timeout.flush(1500);
13621383

1363-
expect(cancellations).toBeLessThan(totalOperations);
1384+
expect(cancellations).toBe(1);
13641385
expect(element.children().length).toBe(10);
13651386
cancellations = 0;
13661387

@@ -1370,7 +1391,7 @@ describe("ngAnimate", function() {
13701391
$animate.triggerReflow();
13711392
$timeout.flush(1500);
13721393
expect(element.children().length).toBe(1);
1373-
expect(cancellations).toBeLessThan(totalOperations);
1394+
expect(cancellations).toBe(1);
13741395
});
13751396
});
13761397

0 commit comments

Comments
 (0)