perf(animate): avoid unnecessary computations if animations are globally disabled

Closes #14914
This commit is contained in:
Georgios Kalpakas
2017-04-27 14:11:20 +03:00
parent c09e613aac
commit 8f563e2ecf
2 changed files with 57 additions and 8 deletions
+8 -8
View File
@@ -348,10 +348,11 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
options.to = null;
}
// there are situations where a directive issues an animation for
// a jqLite wrapper that contains only comment nodes... If this
// happens then there is no way we can perform an animation
if (!node ||
// If animations are hard-disabled for the whole application there is no need to continue.
// There are also situations where a directive issues an animation for a jqLite wrapper that
// contains only comment nodes. In this case, there is no way we can perform an animation.
if (!animationsEnabled ||
!node ||
!isAnimatableByFilter(node, event, initialOptions) ||
!isAnimatableClassName(node, options)) {
close();
@@ -362,12 +363,11 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
var documentHidden = $$isDocumentHidden();
// this is a hard disable of all animations for the application or on
// the element itself, therefore there is no need to continue further
// past this point if not enabled
// This is a hard disable of all animations the element itself, therefore there is no need to
// continue further past this point if not enabled
// Animations are also disabled if the document is currently hidden (page is not visible
// to the user), because browsers slow down or do not flush calls to requestAnimationFrame
var skipAnimations = !animationsEnabled || documentHidden || disabledElementsLookup.get(node);
var skipAnimations = documentHidden || disabledElementsLookup.get(node);
var existingAnimation = (!skipAnimations && activeAnimationsLookup.get(node)) || {};
var hasExistingAnimation = !!existingAnimation.state;
+49
View File
@@ -307,6 +307,32 @@ describe('animations', function() {
});
});
it('should not try to match the `classNameFilter` RegExp if animations are globally disabled',
function() {
var regex = /foo/;
var regexTestSpy = spyOn(regex, 'test').and.callThrough();
module(function($animateProvider) {
$animateProvider.classNameFilter(regex);
});
inject(function($animate) {
$animate.addClass(element, 'foo');
expect(regexTestSpy).toHaveBeenCalled();
regexTestSpy.calls.reset();
$animate.enabled(false);
$animate.addClass(element, 'bar');
expect(regexTestSpy).not.toHaveBeenCalled();
regexTestSpy.calls.reset();
$animate.enabled(true);
$animate.addClass(element, 'baz');
expect(regexTestSpy).toHaveBeenCalled();
});
}
);
describe('customFilter()', function() {
it('should be `null` by default', module(function($animateProvider) {
expect($animateProvider.customFilter()).toBeNull();
@@ -434,6 +460,29 @@ describe('animations', function() {
expect(element.parent()[0]).toBeUndefined();
});
});
it('should not execute the function if animations are globally disabled', function() {
var customFilterSpy = jasmine.createSpy('customFilterFn');
module(function($animateProvider) {
$animateProvider.customFilter(customFilterSpy);
});
inject(function($animate) {
$animate.addClass(element, 'foo');
expect(customFilterSpy).toHaveBeenCalled();
customFilterSpy.calls.reset();
$animate.enabled(false);
$animate.addClass(element, 'bar');
expect(customFilterSpy).not.toHaveBeenCalled();
customFilterSpy.calls.reset();
$animate.enabled(true);
$animate.addClass(element, 'baz');
expect(customFilterSpy).toHaveBeenCalled();
});
});
});
describe('enabled()', function() {