Skip to content

Commit dc5090e

Browse files
committed
fix(perf): Fix event listeners/ DOM nodes memory leak and throttle resize events
Includes: - Unbind 'resize' event handlers on $destroy (as per issue 720kb#143). Not only this reduces the overall number of listeners being bound, but also allows DOM nodes to be GC'ed. - To alleviate some CPU churn, we now throttle the resize event handler. By doing so, we enable users to resize their browsers in a much smoother fashion.
1 parent fa1aa17 commit dc5090e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/angular-tooltips.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
window.console.log('Skipped!');
2222
}
2323
}
24+
, resizeTimeout
2425
, resize = function resize() {
25-
26-
window.requestAnimationFrame(runCallbacks);
26+
window.clearTimeout(resizeTimeout);
27+
resizeTimeout = window.setTimeout(function onResizeTimeout() {
28+
window.requestAnimationFrame(runCallbacks);
29+
}, 500);
2730
}
2831
, addCallback = function addCallback(callback) {
2932

@@ -41,6 +44,12 @@
4144
window.addEventListener('resize', resize);
4245
}
4346
addCallback(callback);
47+
},
48+
'remove': function remove() {
49+
if (!callbacks.length) {
50+
window.clearTimeout(resizeTimeout);
51+
window.removeEventListener('resize', resize);
52+
}
4453
}
4554
};
4655
}())
@@ -767,6 +776,7 @@
767776
unregisterOnTooltipSizeChange();
768777
unregisterOnTooltipSpeedChange();
769778
unregisterTipContentChangeWatcher();
779+
resizeObserver.remove();
770780
element.off($attrs.tooltipShowTrigger + ' ' + $attrs.tooltipHideTrigger);
771781
});
772782
});

0 commit comments

Comments
 (0)