Skip to content

Commit

Permalink
fix($animate): prevent cancellation timestamp from being too far in t…
Browse files Browse the repository at this point in the history
…he future

Closes angular#6748
  • Loading branch information
matsko committed Mar 21, 2014
1 parent 3f2d756 commit ec4984f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/ngAnimate/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ angular.module('ngAnimate', ['ng'])

//but it may not need to cancel out the existing timeout
//if the timestamp is less than the previous one
var futureTimestamp = Date.now() + (totalTime * 1000);
var futureTimestamp = Date.now() + totalTime;
if(futureTimestamp <= closingTimestamp) {
return;
}
Expand Down
39 changes: 31 additions & 8 deletions test/ngAnimate/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1301,24 +1301,43 @@ describe("ngAnimate", function() {
}));

it("should intelligently cancel former timeouts and close off a series of elements a final timeout", function() {
var cancellations = 0;
var _now, currentTimestamp, cancellations = 0;
module(function($provide) {
_now = Date.now;
currentTimestamp = _now();
Date.now = function() {
return currentTimestamp;
};

$provide.decorator('$timeout', function($delegate) {
var _cancel = $delegate.cancel;
$delegate.cancel = function() {
cancellations++;
return _cancel.apply($delegate, arguments);
$delegate.cancel = function(timer) {
if(timer) {
cancellations++;
return _cancel.apply($delegate, arguments);
}
};
return $delegate;
});
})
inject(function($animate, $rootScope, $compile, $sniffer, $timeout) {
if (!$sniffer.transitions) return;

ss.addRule('.animate-me', '-webkit-transition:1s linear all;' +

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

ss.addRule('.animate-me-longer div', '-webkit-transition:1.5s linear all;' +
'transition:1.5s linear all;');

element = $compile(html('<div class="animate-me-longer"><div ng-repeat="item in items"></div></div>'))($rootScope);
$rootScope.items = [0];
$rootScope.$digest();
$animate.triggerReflow();

currentTimestamp += 2250; //1.5 * 1500 = 2250

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

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

$rootScope.items = [0];
$animate.triggerReflow();

currentTimestamp += 1500; //1.5 * 1000 = 1500
$timeout.flush(1500);

expect(cancellations).toBeLessThan(totalOperations);
expect(cancellations).toBe(1);
expect(element.children().length).toBe(10);
cancellations = 0;

Expand All @@ -1339,7 +1360,9 @@ describe("ngAnimate", function() {
$animate.triggerReflow();
$timeout.flush(1500);
expect(element.children().length).toBe(1);
expect(cancellations).toBeLessThan(totalOperations);
expect(cancellations).toBe(1);

Date.now = _now;
});
});

Expand Down

0 comments on commit ec4984f

Please sign in to comment.