perf: virtual list was rendering every single item and then destroying
This commit is contained in:
4 files changed
+77
-49
No files matched your search
@@ -20,6 +20,7 @@
|
||||
useWindow?: boolean
|
||||
height?: number
|
||||
itemContainer?: string
|
||||
initialScrollIndex?: number
|
||||
}
|
||||
|
||||
let {
|
||||
@@ -33,6 +34,7 @@
|
||||
useWindow = true,
|
||||
height = 0,
|
||||
itemContainer = 'div',
|
||||
initialScrollIndex = 0,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
|
||||
@@ -80,7 +82,23 @@
|
||||
return cumulation
|
||||
})
|
||||
|
||||
let scrollY = $state(0)
|
||||
let isRestoring = untrack(() => initialScrollIndex > 0)
|
||||
let initialScrollY = untrack(() => {
|
||||
if (isRestoring && initialScrollIndex < itemHeights.length) {
|
||||
let sum = 0
|
||||
for (let i = 0; i < initialScrollIndex; i++) {
|
||||
sum += itemHeights[i] || estimatedHeight
|
||||
}
|
||||
|
||||
const targetPx = sum - (initialOffset || 0)
|
||||
if (targetPx >= 0) {
|
||||
return targetPx
|
||||
}
|
||||
}
|
||||
return 0
|
||||
})
|
||||
|
||||
let scrollY = $state(initialScrollY)
|
||||
let viewportHeight = $state(0)
|
||||
let visibleItems = $state<{ index: number; offset: number }[]>([])
|
||||
|
||||
@@ -206,6 +224,10 @@
|
||||
})
|
||||
|
||||
untrack(() => {
|
||||
if (initialScrollIndex && initialScrollIndex > 0) {
|
||||
scrollToIndex(initialScrollIndex, useWindow)
|
||||
isRestoring = false
|
||||
}
|
||||
visibleItems = updateVisibleItems()
|
||||
initialRender = false
|
||||
})
|
||||
@@ -217,7 +239,13 @@
|
||||
bind:scrollY={
|
||||
() => (useWindow ? scrollY : 0),
|
||||
(v) => {
|
||||
if (useWindow) scrollY = v
|
||||
if (useWindow) {
|
||||
if (isRestoring) {
|
||||
if (v === 0) return
|
||||
isRestoring = false
|
||||
}
|
||||
scrollY = v
|
||||
}
|
||||
}
|
||||
}
|
||||
/>
|
||||
@@ -225,7 +253,8 @@
|
||||
<div
|
||||
bind:this={virtualListEl}
|
||||
style="position: relative; height: {height ||
|
||||
cumulativeItemHeights[visibleItems?.[visibleItems.length - 1]?.index]}px;"
|
||||
cumulativeItemHeights[items.length - 1] ||
|
||||
0}px;"
|
||||
{...rest}
|
||||
id="feed"
|
||||
onscroll={() => {
|
||||
@@ -246,6 +275,11 @@
|
||||
{@render itemSnippet(item.index)}
|
||||
</svelte:element>
|
||||
{/each}
|
||||
<div
|
||||
style="height: {(cumulativeItemHeights[items.length - 1] || 0) -
|
||||
(cumulativeItemHeights[visibleItems?.[visibleItems.length - 1]?.index] ||
|
||||
0)}px; border: 0 !important;"
|
||||
></div>
|
||||
</div>
|
||||
{#if settings.debugInfo}
|
||||
<Expandable>
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
import Placeholder from '$lib/ui/info/Placeholder.svelte'
|
||||
import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte'
|
||||
import { Button, Material, Spinner } from 'mono-svelte'
|
||||
import { onDestroy, onMount, untrack } from 'svelte'
|
||||
import { onDestroy, untrack } from 'svelte'
|
||||
import type { Attachment } from 'svelte/attachments'
|
||||
import {
|
||||
ArchiveBox,
|
||||
ArrowsPointingOut,
|
||||
@@ -106,55 +107,35 @@
|
||||
}
|
||||
}
|
||||
|
||||
const callback: IntersectionObserverCallback = (entries, observer) => {
|
||||
entries.forEach((entry) => {
|
||||
if (!entry.isIntersecting) return
|
||||
const observer = browser
|
||||
? new IntersectionObserver(
|
||||
(entries) => {
|
||||
entries.forEach((entry) => {
|
||||
if (!entry.isIntersecting) return
|
||||
|
||||
const element = entry.target as HTMLElement
|
||||
const id = element.getAttribute('data-index')
|
||||
const element = entry.target as HTMLElement
|
||||
const id = element.getAttribute('data-index')
|
||||
|
||||
if (!id) return
|
||||
if (!id) return
|
||||
|
||||
lastSeen = Number(id)
|
||||
lastSeen = Number(id)
|
||||
|
||||
observer.unobserve(element)
|
||||
})
|
||||
observer?.unobserve(element)
|
||||
})
|
||||
},
|
||||
{
|
||||
threshold: 0.5,
|
||||
},
|
||||
)
|
||||
: null
|
||||
|
||||
const intersectionAttach: Attachment = (node) => {
|
||||
observer?.observe(node)
|
||||
return () => {
|
||||
observer?.unobserve(node)
|
||||
}
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
const observer = new IntersectionObserver(callback, {
|
||||
threshold: 0.5,
|
||||
})
|
||||
|
||||
const observePost = (node: Node) => {
|
||||
if (
|
||||
node instanceof HTMLElement &&
|
||||
node.classList.contains('post-container')
|
||||
)
|
||||
observer.observe(node)
|
||||
}
|
||||
|
||||
const unobservePost = (node: Node) => {
|
||||
if (
|
||||
node instanceof HTMLElement &&
|
||||
node.classList.contains('post-container')
|
||||
)
|
||||
observer.unobserve(node)
|
||||
}
|
||||
|
||||
document.querySelectorAll('.post-container').forEach(observePost)
|
||||
|
||||
const feed = document.getElementById('feed')
|
||||
if (!feed) return
|
||||
|
||||
new MutationObserver((mutations) => {
|
||||
mutations.forEach(({ addedNodes, removedNodes }) => {
|
||||
addedNodes.forEach(observePost)
|
||||
removedNodes.forEach(unobservePost)
|
||||
})
|
||||
}).observe(feed, { childList: true, subtree: false })
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
if (listComp) {
|
||||
untrack(() => {
|
||||
@@ -169,6 +150,7 @@
|
||||
|
||||
onDestroy(() => {
|
||||
abortLoad?.abort()
|
||||
observer?.disconnect()
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -201,6 +183,7 @@
|
||||
estimatedHeight={settings.view == 'cozy' ? 500 : 150}
|
||||
bind:restore={virtualList}
|
||||
bind:this={listComp}
|
||||
initialScrollIndex={lastSeen}
|
||||
>
|
||||
{#snippet item(row)}
|
||||
<!--god svelte is gonna make me lose it-->
|
||||
@@ -215,6 +198,7 @@
|
||||
filter.value.action == 'hide' && 'hidden',
|
||||
row < 7 && '',
|
||||
]}
|
||||
{@attach intersectionAttach}
|
||||
>
|
||||
<!--TODO make my component isolation not abysmal-->
|
||||
{#if filter.value.action == 'none'}
|
||||
|
||||
@@ -104,7 +104,12 @@
|
||||
bind:posts
|
||||
bind:lastSeen={() => client.lastSeen ?? 0, (v) => (client.lastSeen = v)}
|
||||
bind:params={getParams}
|
||||
virtualList={{ itemHeights: client?.itemHeights ?? [] }}
|
||||
bind:virtualList={
|
||||
() => ({ itemHeights: client.itemHeights ?? [] }),
|
||||
(v) => {
|
||||
if (v) client.itemHeights = v.itemHeights
|
||||
}
|
||||
}
|
||||
/>
|
||||
<svelte:element
|
||||
this={settings.infiniteScroll && !settings.posts.noVirtualize
|
||||
|
||||
@@ -86,7 +86,12 @@
|
||||
() => feed.client.lastSeen ?? 0, (v) => (feed.client.lastSeen = v)
|
||||
}
|
||||
bind:params={feed.params}
|
||||
virtualList={{ itemHeights: feed.client?.itemHeights ?? [] }}
|
||||
bind:virtualList={
|
||||
() => ({ itemHeights: feed.client.itemHeights ?? [] }),
|
||||
(v) => {
|
||||
if (v) feed.client.itemHeights = v.itemHeights
|
||||
}
|
||||
}
|
||||
/>
|
||||
<svelte:element
|
||||
this={settings.infiniteScroll && !settings.posts.noVirtualize
|
||||
|
||||
Reference in new issue
Block a user