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

Commit 2bae1ad

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 manually fire change if not watcher is present
1 parent 7a650bc commit 2bae1ad

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

src/components/tooltip/tooltip.js

Lines changed: 35 additions & 6 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});
122+
123+
if (attr.hasOwnProperty('mdVisible')) // build watcher only if mdVisible is being used
124+
scope.visibleWatcher = scope.$watch('visible', onVisibleChanged );
106125

107-
scope.$watch('direction', updatePosition );
126+
}
127+
else { // MutationObserver not supported
128+
scope.visibleWatcher = scope.$watch('visible', onVisibleChanged );
129+
scope.$watch('direction', updatePosition );
130+
}
108131
}
109132

110133
function addAriaLabel () {
@@ -194,9 +217,15 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe
194217
$timeout(function() {
195218
scope.visible = setVisible.value;
196219
setVisible.queued = false;
220+
if (!scope.visibleWatcher)
221+
onVisibleChanged(scope.visible);
197222
}, scope.delay);
198223
} else {
199-
$mdUtil.nextTick(function() { scope.visible = false; });
224+
$mdUtil.nextTick(function() {
225+
scope.visible = false;
226+
if (!scope.visibleWatcher)
227+
onVisibleChanged(false);
228+
});
200229
}
201230
}
202231
}

0 commit comments

Comments
 (0)