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
@@ -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'}
|
||||
|
||||
Reference in new issue
Block a user