From 2bae1adccb1121ffd120c2121683baccea133e18 Mon Sep 17 00:00:00 2001 From: Carlos Lopez Jr Date: Thu, 31 Mar 2016 13:54:04 -0400 Subject: [PATCH] perf(md-tooltip) use MutationObserver instead of watcher if available use watcher on mdVisible only if being used braces on same line manually fire change if not watcher is present --- src/components/tooltip/tooltip.js | 41 ++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/components/tooltip/tooltip.js b/src/components/tooltip/tooltip.js index f4119df19d..869ada1495 100644 --- a/src/components/tooltip/tooltip.js +++ b/src/components/tooltip/tooltip.js @@ -92,6 +92,11 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe content.css('transform-origin', origin); } + function onVisibleChanged (isVisible) { + if (isVisible) showTooltip(); + else hideTooltip(); + } + function configureWatchers () { scope.$on('$destroy', function() { scope.visible = false; @@ -99,12 +104,30 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe angular.element($window).off('resize', debouncedOnResize); }); - scope.$watch('visible', function (isVisible) { - if (isVisible) showTooltip(); - else hideTooltip(); - }); + if (element[0] && 'MutationObserver' in $window) { + var attributeObserver = new MutationObserver(function(mutations) { + mutations + .forEach(function (mutation) { + if (mutation.attributeName === 'md-visible') { + if (!scope.visibleWatcher) + scope.visibleWatcher = scope.$watch('visible', onVisibleChanged ); + } + if (mutation.attributeName === 'md-direction') { + updatePosition(scope.direction); + } + }); + }); + + attributeObserver.observe(element[0], { attributes: true}); + + if (attr.hasOwnProperty('mdVisible')) // build watcher only if mdVisible is being used + scope.visibleWatcher = scope.$watch('visible', onVisibleChanged ); - scope.$watch('direction', updatePosition ); + } + else { // MutationObserver not supported + scope.visibleWatcher = scope.$watch('visible', onVisibleChanged ); + scope.$watch('direction', updatePosition ); + } } function addAriaLabel () { @@ -194,9 +217,15 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe $timeout(function() { scope.visible = setVisible.value; setVisible.queued = false; + if (!scope.visibleWatcher) + onVisibleChanged(scope.visible); }, scope.delay); } else { - $mdUtil.nextTick(function() { scope.visible = false; }); + $mdUtil.nextTick(function() { + scope.visible = false; + if (!scope.visibleWatcher) + onVisibleChanged(false); + }); } } }