Skip to content

Commit ec4984f

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

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-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

+31-8
Original file line numberDiff line numberDiff line change
@@ -1301,24 +1301,43 @@ 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+
1327+
ss.addRule('.animate-me div', '-webkit-transition:1s linear all;' +
13191328
'transition:1s linear all;');
1329+
1330+
ss.addRule('.animate-me-longer div', '-webkit-transition:1.5s linear all;' +
1331+
'transition:1.5s linear all;');
1332+
1333+
element = $compile(html('<div class="animate-me-longer"><div ng-repeat="item in items"></div></div>'))($rootScope);
1334+
$rootScope.items = [0];
1335+
$rootScope.$digest();
1336+
$animate.triggerReflow();
1337+
1338+
currentTimestamp += 2250; //1.5 * 1500 = 2250
13201339

1321-
element = $compile(html('<div><div class="animate-me" ng-repeat="item in items"></div></div>'))($rootScope);
1340+
element[0].className = 'animate-me';
13221341

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

13281347
$rootScope.items = [0];
13291348
$animate.triggerReflow();
1349+
1350+
currentTimestamp += 1500; //1.5 * 1000 = 1500
13301351
$timeout.flush(1500);
13311352

1332-
expect(cancellations).toBeLessThan(totalOperations);
1353+
expect(cancellations).toBe(1);
13331354
expect(element.children().length).toBe(10);
13341355
cancellations = 0;
13351356

@@ -1339,7 +1360,9 @@ describe("ngAnimate", function() {
13391360
$animate.triggerReflow();
13401361
$timeout.flush(1500);
13411362
expect(element.children().length).toBe(1);
1342-
expect(cancellations).toBeLessThan(totalOperations);
1363+
expect(cancellations).toBe(1);
1364+
1365+
Date.now = _now;
13431366
});
13441367
});
13451368

0 commit comments

Comments
 (0)