Re: [whatwg/dom] Add convenient, ergonomic, performant API for sorting siblings (#586)

Responding to just the reordering primitive use case here:

Preserving focus and not reloading resources is absolutely great, however we get there.

> However, with the new (performant) `reorderChildren()` primitive, diffing libraries can use a combination of `remove()`, `appendChild()`, and `reorderChildren()` to optimally reconstruct the children of an Element.

`reorderChildren()` does sound like a nice improvement, but thinking how I'd use this to reimplement our list updating, I would personally rather have one call to move, add, and remove children, and limit the operation to a sublist of the child list.

Something like `parentNode.updateChildren(children: Iteratable<Node>, from?: Node, to? Node)`

Because

- Our looping primitives can be used in the middle of a child list. Right now they only need local knowledge of their contents, but without start and end node support they'll have to gather all the children on the parent to call `reorderChildren()` correctly.
- We'd have to track added, removed, and moved nodes separately, and added nodes would have to keep track of their new reference node to be able to call `insertBefore()`. This isn't _terrible_, but it might be more complicated than the individual remove and insert calls we use now. In some ways a `moveBefore()` that only took an existing child would be simpler to adopt. `swapChildren(a, b)` would be a very interesting option to given the current implementation of some of the list diffs. Otherwise, it seems likely simpler to build one list of the new node order and apply that in one call.
- If there are other perf benefits around skipping or batching integrity checks and mutation events, could that extend to adding and removing nodes too?

----

Sorting child nodes as in the other use case is something I've had to deal with less often, and usually via the looping mechanisms we have for the reorder use case, but I have seen it with tables. I think there it'd be very useful to be able to sort a sublist too, via before and after reference nodes.


-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/586#issuecomment-1442703121
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/586/1442703121@github.com>

Received on Friday, 24 February 2023 02:19:54 UTC