34 lines
1.0 KiB
JavaScript
34 lines
1.0 KiB
JavaScript
export const createTreeWalkerCleanupAfterChildren = (treeWalker) => {
|
|
const cleanupAfterChildren = [];
|
|
const checkCleanUpAtBeginningOfIteration = () => {
|
|
for (let i = 0; i < cleanupAfterChildren.length;) {
|
|
const cleanup = cleanupAfterChildren[i];
|
|
if (!(cleanup.element === treeWalker.currentNode ||
|
|
cleanup.element.contains(treeWalker.currentNode))) {
|
|
cleanup.cleanupFn();
|
|
cleanupAfterChildren.splice(i, 1);
|
|
}
|
|
else {
|
|
i++;
|
|
}
|
|
}
|
|
};
|
|
const addCleanup = (element, cleanupFn) => {
|
|
// Last registered must be cleaned up first
|
|
cleanupAfterChildren.unshift({
|
|
element,
|
|
cleanupFn,
|
|
});
|
|
};
|
|
const cleanupInTheEndOfTheIteration = () => {
|
|
for (const cleanup of cleanupAfterChildren) {
|
|
cleanup.cleanupFn();
|
|
}
|
|
};
|
|
return {
|
|
checkCleanUpAtBeginningOfIteration,
|
|
addCleanup,
|
|
cleanupInTheEndOfTheIteration,
|
|
};
|
|
};
|