Skip to content

Commit 21bec01

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

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/ngAnimate/animate.js

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

12001200
//but it may not need to cancel out the existing timeout
12011201
//if the timestamp is less than the previous one
1202-
var futureTimestamp = Date.now() + (totalTime * 1000);
1202+
var futureTimestamp = Date.now() + totalTime;
12031203
if(futureTimestamp <= closingTimestamp) {
12041204
return;
12051205
}

test/ngAnimate/animateSpec.js

+29-7
Original file line numberDiff line numberDiff line change
@@ -1390,24 +1390,44 @@ describe("ngAnimate", function() {
13901390
}));
13911391

13921392
it("should intelligently cancel former timeouts and close off a series of elements a final timeout", function() {
1393+
var currentTimestamp = Date.now();
1394+
spyOn(Date,'now').andCallFake(function() {
1395+
return currentTimestamp;
1396+
});
1397+
13931398
var cancellations = 0;
13941399
module(function($provide) {
1400+
13951401
$provide.decorator('$timeout', function($delegate) {
13961402
var _cancel = $delegate.cancel;
1397-
$delegate.cancel = function() {
1398-
cancellations++;
1399-
return _cancel.apply($delegate, arguments);
1403+
$delegate.cancel = function(timer) {
1404+
if(timer) {
1405+
cancellations++;
1406+
return _cancel.apply($delegate, arguments);
1407+
}
14001408
};
14011409
return $delegate;
14021410
});
14031411
})
14041412
inject(function($animate, $rootScope, $compile, $sniffer, $timeout) {
14051413
if (!$sniffer.transitions) return;
14061414

1407-
ss.addRule('.animate-me', '-webkit-transition:1s linear all;' +
1415+
ss.addRule('.animate-me div', '-webkit-transition:1s linear all;' +
14081416
'transition:1s linear all;');
14091417

1410-
element = $compile(html('<div><div class="animate-me" ng-repeat="item in items"></div></div>'))($rootScope);
1418+
ss.addRule('.animate-me-longer div', '-webkit-transition:1.5s linear all;' +
1419+
'transition:1.5s linear all;');
1420+
1421+
element = $compile(html('<div class="animate-me-longer">' +
1422+
'<div ng-repeat="item in items"></div>' +
1423+
'</div>'))($rootScope);
1424+
$rootScope.items = [0];
1425+
$rootScope.$digest();
1426+
$animate.triggerReflow();
1427+
1428+
currentTimestamp += 2250; //1.5 * 1500 = 2250
1429+
1430+
element[0].className = 'animate-me';
14111431

14121432
$rootScope.items = [1,2,3,4,5,6,7,8,9,10];
14131433
var totalOperations = $rootScope.items.length;
@@ -1416,9 +1436,11 @@ describe("ngAnimate", function() {
14161436

14171437
$rootScope.items = [0];
14181438
$animate.triggerReflow();
1439+
1440+
currentTimestamp += 1500; //1.5 * 1000 = 1500
14191441
$timeout.flush(1500);
14201442

1421-
expect(cancellations).toBeLessThan(totalOperations);
1443+
expect(cancellations).toBe(1);
14221444
expect(element.children().length).toBe(10);
14231445
cancellations = 0;
14241446

@@ -1428,7 +1450,7 @@ describe("ngAnimate", function() {
14281450
$animate.triggerReflow();
14291451
$timeout.flush(1500);
14301452
expect(element.children().length).toBe(1);
1431-
expect(cancellations).toBeLessThan(totalOperations);
1453+
expect(cancellations).toBe(1);
14321454
});
14331455
});
14341456

0 commit comments

Comments
 (0)