-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
handle moving nodes #382
base: main
Are you sure you want to change the base?
handle moving nodes #382
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -460,7 +460,6 @@ function insertExpression(parent, value, current, marker, unwrapArray) { | |
if (value === current) return current; | ||
const t = typeof value, | ||
multi = marker !== undefined; | ||
parent = (multi && current[0] && current[0].parentNode) || parent; | ||
|
||
if (t === "string" || t === "number") { | ||
if (hydrating) return current; | ||
|
@@ -570,7 +569,14 @@ function appendNodes(parent, array, marker = null) { | |
for (let i = 0, len = array.length; i < len; i++) parent.insertBefore(array[i], marker); | ||
} | ||
|
||
let moved; | ||
|
||
function cleanChildren(parent, current, marker, replacement) { | ||
if (!moved) { | ||
moved = new WeakSet(); | ||
queueMicrotask(() => (moved = null)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest considering a strong referenced array to be cleared in the microtask as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking if weakset was faster than array to check for element existence. Any form of book-keeping works really. We could also keep it in the nodes but I don't know the performance side of this. Essentially we need to know if a node has changed position in the tree. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Last time I measured this, for a fairly low amount of items there was no significant difference between At first sight sounds like keeping the info in the node will complicate the logic, either a microtask for each node or some sort of counter. Probably worth considering, as a microtask could introduce desync, not sure if solidjs/signals will use microtasks, but everything becomes a race when used. This is why I do not like reactivity in microtasks Maybe make of the |
||
|
||
if (marker === undefined) return (parent.textContent = ""); | ||
const node = replacement || document.createTextNode(""); | ||
if (current.length) { | ||
|
@@ -579,9 +585,12 @@ function cleanChildren(parent, current, marker, replacement) { | |
const el = current[i]; | ||
if (node !== el) { | ||
const isParent = el.parentNode === parent; | ||
if (!inserted && !i) | ||
isParent ? parent.replaceChild(node, el) : parent.insertBefore(node, marker); | ||
else isParent && el.remove(); | ||
if (!inserted && !i) { | ||
node.parentNode && moved.add(node); | ||
isParent && !moved.has(el) | ||
? parent.replaceChild(node, el) | ||
: parent.insertBefore(node, marker); | ||
} else isParent && el.remove(); | ||
} else inserted = true; | ||
} | ||
} else parent.insertBefore(node, marker); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what might break by this removal but this for sure breaks the parent comparison in
cleanChildren
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ryan made a comment about this code in a related issue here #307 (comment)
I will have to study it again to refresh my mind, I'm not sure I actually ever understood that part
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried to track this down and, oh well 😅, no wonder even Ryan can't remember this. This check existed since the beginning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah someone made a codesandbox that was sorting nodes around randomly on an interval and it would occasionally break. I added that way back. But I think its need predates the "modern" way we handle fragments I changed to back in Feb 2019.
In any case I'm moving forward with this not there right now in 2.0 stuff.