Skip to content

Commit 0f802a1

Browse files
committed
fix($animate): prevent cancellation timestamp from being too far in the future
Closes angular#6748
1 parent 3f2d756 commit 0f802a1

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/ngAnimate/animate.js

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

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

test/ngAnimate/animateSpec.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -1301,24 +1301,42 @@ describe("ngAnimate", function() {
13011301
}));
13021302

13031303
it("should intelligently cancel former timeouts and close off a series of elements a final timeout", function() {
1304-
var cancellations = 0;
1304+
var _now, currentTimestamp, cancellations = 0;
13051305
module(function($provide) {
1306+
_now = Date.now;
1307+
currentTimestamp = _now();
1308+
Date.now = function() {
1309+
return currentTimestamp;
1310+
};
1311+
13061312
$provide.decorator('$timeout', function($delegate) {
13071313
var _cancel = $delegate.cancel;
1308-
$delegate.cancel = function() {
1309-
cancellations++;
1310-
return _cancel.apply($delegate, arguments);
1314+
$delegate.cancel = function(timer) {
1315+
if(timer) {
1316+
cancellations++;
1317+
return _cancel.apply($delegate, arguments);
1318+
}
13111319
};
13121320
return $delegate;
13131321
});
13141322
})
13151323
inject(function($animate, $rootScope, $compile, $sniffer, $timeout) {
13161324
if (!$sniffer.transitions) return;
13171325

1318-
ss.addRule('.animate-me', '-webkit-transition:1s linear all;' +
1326+
ss.addRule('.animate-me div', '-webkit-transition:1s linear all;' +
13191327
'transition:1s linear all;');
1328+
1329+
ss.addRule('.animate-me-longer div', '-webkit-transition:1.5s linear all;' +
1330+
'transition:1.5s linear all;');
13201331

1321-
element = $compile(html('<div><div class="animate-me" ng-repeat="item in items"></div></div>'))($rootScope);
1332+
element = $compile(html('<div class="animate-me-longer"><div ng-repeat="item in items"></div></div>'))($rootScope);
1333+
$rootScope.items = [0];
1334+
$rootScope.$digest();
1335+
$animate.triggerReflow();
1336+
1337+
currentTimestamp += 2250; //1.5 * 1500 = 2250
1338+
1339+
element[0].className = 'animate-me';
13221340

13231341
$rootScope.items = [1,2,3,4,5,6,7,8,9,10];
13241342
var totalOperations = $rootScope.items.length;
@@ -1327,9 +1345,11 @@ describe("ngAnimate", function() {
13271345

13281346
$rootScope.items = [0];
13291347
$animate.triggerReflow();
1348+
1349+
currentTimestamp += 1500; //1.5 * 1000 = 1500
13301350
$timeout.flush(1500);
13311351

1332-
expect(cancellations).toBeLessThan(totalOperations);
1352+
expect(cancellations).toBe(1);
13331353
expect(element.children().length).toBe(10);
13341354
cancellations = 0;
13351355

@@ -1339,7 +1359,9 @@ describe("ngAnimate", function() {
13391359
$animate.triggerReflow();
13401360
$timeout.flush(1500);
13411361
expect(element.children().length).toBe(1);
1342-
expect(cancellations).toBeLessThan(totalOperations);
1362+
expect(cancellations).toBe(1);
1363+
1364+
Date.now = _now;
13431365
});
13441366
});
13451367

0 commit comments

Comments
 (0)