impr: untrack dependency changes in virtual list to avoid recursion

This commit is contained in:
James Xylight committed 2025-06-26 10:56:01 -07:00
1 parent dd8e055cef
commit de3ca92544
2 files changed
+9 -8

No files matched your search

@@ -189,7 +189,6 @@
class="divide-y -mx-4 sm:-mx-6 divide-slate-200 dark:divide-zinc-900"
items={posts}
{initialOffset}
overscan={500}
bind:restore={postFeeds.value[feedId].clientData}
bind:this={listComp}
>
+9 -7
View File
@@ -20,7 +20,7 @@
let {
items,
estimatedHeight = 100,
overscan = 5,
overscan = 3,
item: itemSnippet,
initialOffset = 0,
restore = $bindable(),
@@ -91,7 +91,7 @@
function updateVisibleItems() {
if (!virtualListEl) return []
viewportHeight = innerHeight?.current ?? 1000
const scrollTop = (scrollY.current ?? 0) - initialOffset
@@ -100,11 +100,10 @@
const firstIndex = findFirstVisibleIndex(scrollTop, cumulativeItemHeights)
let i = firstIndex
let offset = i == 0 ? 0 : cumulativeItemHeights[i - 1]
let offset = i === 0 ? 0 : cumulativeItemHeights[i - 1]
while (
i < items.length &&
i < firstIndex + overscan &&
offset < scrollTop + viewportHeight + overscan * estimatedHeight
) {
newVisibleItems.push({ index: i, offset: offset })
@@ -113,7 +112,7 @@
i++
}
return newVisibleItems ?? []
return newVisibleItems
}
function resizeObserver(node: HTMLElement) {
@@ -167,12 +166,15 @@
}
})
// Scroll position changes (only every few px)
let oldScroll = $state(0)
$effect(() => {
const currentScrollY = scrollY.current ?? 0
if (Math.abs(currentScrollY - oldScroll) > estimatedHeight) {
visibleItems = updateVisibleItems()
oldScroll = currentScrollY
untrack(() => {
visibleItems = updateVisibleItems()
oldScroll = currentScrollY
})
}
})
</script>