Simple Mutationobserver Version Of Domnoderemovedfromdocument
I attach some functionality to DOM elements and want to be able to clear all references when the element is removed from the DOM so it can be garbage collected, My initial version
Solution 1:
Actually... yes, there is a more elegant solution :).
What you added looks good and seems to be well optimized. However there is an easier way to know if the node is attached to the DOM or not.
functiononRemove(element, onDetachCallback) {
const observer = newMutationObserver(function () {
functionisDetached(el) {
if (el.parentNode === document) {
returnfalse;
} elseif (el.parentNode === null) {
returntrue;
} else {
returnisDetached(el.parentNode);
}
}
if (isDetached(element)) {
observer.disconnect();
onDetachCallback();
}
})
observer.observe(document, {
childList: true,
subtree: true
});
}
Post a Comment for "Simple Mutationobserver Version Of Domnoderemovedfromdocument"