Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 1e0d5f6

Browse files
committed
perf(md-tooltip) use MutationObserver instead of watcher if available
use watcher on mdVisible only if being used braces on same line
1 parent 7a650bc commit 1e0d5f6

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/components/tooltip/tooltip.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,42 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe
9292
content.css('transform-origin', origin);
9393
}
9494

95+
function onVisibleChanged (isVisible) {
96+
if (isVisible) showTooltip();
97+
else hideTooltip();
98+
}
99+
95100
function configureWatchers () {
96101
scope.$on('$destroy', function() {
97102
scope.visible = false;
98103
element.remove();
99104
angular.element($window).off('resize', debouncedOnResize);
100105
});
101106

102-
scope.$watch('visible', function (isVisible) {
103-
if (isVisible) showTooltip();
104-
else hideTooltip();
105-
});
107+
if (element[0] && 'MutationObserver' in $window) {
108+
var attributeObserver = new MutationObserver(function(mutations) {
109+
mutations
110+
.forEach(function (mutation) {
111+
if (mutation.attributeName === 'md-visible') {
112+
if (!scope.visibleWatcher)
113+
scope.visibleWatcher = scope.$watch('visible', onVisibleChanged );
114+
}
115+
if (mutation.attributeName === 'md-direction') {
116+
updatePosition(scope.direction);
117+
}
118+
});
119+
});
120+
121+
attributeObserver.observe(element[0], { attributes: true});
106122

107-
scope.$watch('direction', updatePosition );
123+
if (attr.hasOwnProperty('mdVisible')) // build watcher only if mdVisible is being used
124+
scope.visibleWatcher = scope.$watch('visible', onVisibleChanged );
125+
126+
}
127+
else { // MutationObserver not supported
128+
scope.visibleWatcher = scope.$watch('visible', onVisibleChanged );
129+
scope.$watch('direction', updatePosition );
130+
}
108131
}
109132

110133
function addAriaLabel () {

0 commit comments

Comments
 (0)