Files
rustpad/static/js/dom-reconcile.js
T
2026-10-14 22:03:06 +02:00

21 lines
708 B
JavaScript

/*
* Copyright (C) 2026 Mateusz Gruszczynski @linuxiarz.pl
* Source-Available Code / Dual-Licensed.
* See LICENSE.md for terms.
*/
// Preserve existing nodes. Remove stale entries first: otherwise deleting a
// user near the start of a 1000-person list would move every following node.
// `desired` must contain each node at most once.
export function reconcileChildren(container, desired) {
const retained = new Set(desired);
for (const node of [...container.childNodes]) {
if (!retained.has(node)) node.remove();
}
let cursor = container.firstChild;
for (const node of desired) {
if (node === cursor) cursor = cursor.nextSibling;
else container.insertBefore(node, cursor);
}
}