impr: improve virtual list performance and reliability (#595)
* fix: sometimes missing posts when navigating backl * impr: debounce updates for resize operations * fix: sentient comments
This commit is contained in:
1 parent
2fb3c2675f
commit
a8b65750ca
5 files changed
+38
-26
No files matched your search
@@ -27,6 +27,7 @@ export default ts.config(
|
|||||||
'@typescript-eslint/no-unsafe-function-type': 'warn',
|
'@typescript-eslint/no-unsafe-function-type': 'warn',
|
||||||
'svelte/require-each-key': 'off',
|
'svelte/require-each-key': 'off',
|
||||||
'svelte/prefer-writable-derived': 'warn',
|
'svelte/prefer-writable-derived': 'warn',
|
||||||
|
'no-console': 'warn',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,11 +38,11 @@
|
|||||||
actions = true,
|
actions = true,
|
||||||
meta = true,
|
meta = true,
|
||||||
replying = $bindable(false),
|
replying = $bindable(false),
|
||||||
|
open = $bindable(),
|
||||||
contentClass = '',
|
contentClass = '',
|
||||||
class: clazz = '',
|
class: clazz = '',
|
||||||
metaSuffix,
|
metaSuffix,
|
||||||
children,
|
children,
|
||||||
open = $bindable(true),
|
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
|
|
||||||
let editing = $state(false)
|
let editing = $state(false)
|
||||||
@@ -74,16 +74,6 @@
|
|||||||
|
|
||||||
editingLoad = false
|
editingLoad = false
|
||||||
}
|
}
|
||||||
|
|
||||||
$effect(() => {
|
|
||||||
if (!open) {
|
|
||||||
// const
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
open = true
|
|
||||||
}, 500)
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if editing}
|
{#if editing}
|
||||||
|
|||||||
@@ -42,7 +42,8 @@
|
|||||||
{#if offsetEl}
|
{#if offsetEl}
|
||||||
<VirtualList
|
<VirtualList
|
||||||
class="divide-y divide-slate-200 dark:divide-zinc-800 w-full"
|
class="divide-y divide-slate-200 dark:divide-zinc-800 w-full"
|
||||||
overscan={500}
|
overscan={50}
|
||||||
|
estimatedHeight={100}
|
||||||
items={nodes}
|
items={nodes}
|
||||||
{initialOffset}
|
{initialOffset}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -111,7 +111,7 @@
|
|||||||
node.comment_view.counts.child_count > 0
|
node.comment_view.counts.child_count > 0
|
||||||
? 'border-l'
|
? 'border-l'
|
||||||
: ''} ml-2.5 border-slate-200 dark:border-zinc-800 pl-2.5"
|
: ''} ml-2.5 border-slate-200 dark:border-zinc-800 pl-2.5"
|
||||||
bind:open={() => node.expanded, value => (node.expanded = value)}
|
bind:open={nodes[index].expanded}
|
||||||
>
|
>
|
||||||
{#if node.children?.length > 0}
|
{#if node.children?.length > 0}
|
||||||
<Comments {post} nodes={nodes[index].children} isParent={false} />
|
<Comments {post} nodes={nodes[index].children} isParent={false} />
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
<script lang="ts" generics="T">
|
<script lang="ts" generics="T">
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
|
import { debounce } from 'mono-svelte/util/time'
|
||||||
import { onDestroy, untrack, type Snippet } from 'svelte'
|
import { onDestroy, untrack, type Snippet } from 'svelte'
|
||||||
import type { HTMLAttributes } from 'svelte/elements'
|
import type { HTMLAttributes } from 'svelte/elements'
|
||||||
import { innerHeight } from 'svelte/reactivity/window'
|
import { innerHeight, scrollY } from 'svelte/reactivity/window'
|
||||||
|
|
||||||
interface Props extends HTMLAttributes<HTMLDivElement> {
|
interface Props extends HTMLAttributes<HTMLDivElement> {
|
||||||
items: T[]
|
items: T[]
|
||||||
@@ -13,6 +14,7 @@
|
|||||||
itemHeights: (number | null)[]
|
itemHeights: (number | null)[]
|
||||||
}
|
}
|
||||||
initialOffset?: number
|
initialOffset?: number
|
||||||
|
debounceResize?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
let {
|
let {
|
||||||
@@ -22,13 +24,16 @@
|
|||||||
item: itemSnippet,
|
item: itemSnippet,
|
||||||
initialOffset = 0,
|
initialOffset = 0,
|
||||||
restore = $bindable(),
|
restore = $bindable(),
|
||||||
|
debounceResize = 100,
|
||||||
...rest
|
...rest
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
|
|
||||||
export function scrollToIndex(index: number, useWindow: boolean = false) {
|
export function scrollToIndex(index: number, useWindow: boolean = false) {
|
||||||
scrollPosition = cumulativeItemHeights[index] - initialOffset || 0
|
|
||||||
if (useWindow && browser)
|
if (useWindow && browser)
|
||||||
window.scrollTo({ behavior: 'instant', top: scrollPosition })
|
window.scrollTo({
|
||||||
|
behavior: 'instant',
|
||||||
|
top: cumulativeItemHeights[index] - initialOffset || 0,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
@@ -55,7 +60,6 @@
|
|||||||
return cumulation
|
return cumulation
|
||||||
})
|
})
|
||||||
|
|
||||||
let scrollPosition = $state(0)
|
|
||||||
let viewportHeight = $state(0)
|
let viewportHeight = $state(0)
|
||||||
let visibleItems = $state<{ index: number; offset: number }[]>([])
|
let visibleItems = $state<{ index: number; offset: number }[]>([])
|
||||||
|
|
||||||
@@ -96,7 +100,7 @@
|
|||||||
if (!virtualListEl) return []
|
if (!virtualListEl) return []
|
||||||
|
|
||||||
viewportHeight = innerHeight?.current ?? 1000
|
viewportHeight = innerHeight?.current ?? 1000
|
||||||
const scrollTop = scrollPosition - initialOffset
|
const scrollTop = (scrollY.current ?? 0) - initialOffset
|
||||||
|
|
||||||
let newVisibleItems: { index: number; offset: number }[] = []
|
let newVisibleItems: { index: number; offset: number }[] = []
|
||||||
|
|
||||||
@@ -105,7 +109,19 @@
|
|||||||
let i = firstIndex
|
let i = firstIndex
|
||||||
let offset = i == 0 ? 0 : cumulativeItemHeights[i - 1]
|
let offset = i == 0 ? 0 : cumulativeItemHeights[i - 1]
|
||||||
|
|
||||||
while (i < items.length && offset < scrollTop + viewportHeight + overscan) {
|
// while (i < items.length && offset < scrollTop + viewportHeight + overscan) {
|
||||||
|
// newVisibleItems.push({ index: i, offset: offset })
|
||||||
|
// const height = itemHeights[i] || estimatedHeight
|
||||||
|
// offset += height
|
||||||
|
// i++
|
||||||
|
// }
|
||||||
|
|
||||||
|
// above but with overscan for item count, rather than pixel offset (using cumulativeHeights instead of estimatedHeight)
|
||||||
|
while (
|
||||||
|
i < items.length &&
|
||||||
|
i < firstIndex + overscan &&
|
||||||
|
offset < scrollTop + viewportHeight + overscan * estimatedHeight
|
||||||
|
) {
|
||||||
newVisibleItems.push({ index: i, offset: offset })
|
newVisibleItems.push({ index: i, offset: offset })
|
||||||
const height = itemHeights[i] || estimatedHeight
|
const height = itemHeights[i] || estimatedHeight
|
||||||
offset += height
|
offset += height
|
||||||
@@ -116,7 +132,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function resizeObserver(node: HTMLElement) {
|
function resizeObserver(node: HTMLElement) {
|
||||||
const observer = new ResizeObserver(entries => {
|
const debouncedUpdate = debounce((entries: ResizeObserverEntry[]) => {
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
const indexAttr = node.getAttribute('data-index')
|
const indexAttr = node.getAttribute('data-index')
|
||||||
if (indexAttr === null) continue
|
if (indexAttr === null) continue
|
||||||
@@ -124,11 +140,15 @@
|
|||||||
if (isNaN(index)) continue
|
if (isNaN(index)) continue
|
||||||
|
|
||||||
const newHeight = entry.contentRect.height
|
const newHeight = entry.contentRect.height
|
||||||
if (itemHeights[index] !== newHeight) {
|
if (itemHeights[index] !== newHeight && newHeight !== estimatedHeight) {
|
||||||
itemHeights[index] = newHeight
|
itemHeights[index] = newHeight
|
||||||
visibleItems = updateVisibleItems()
|
visibleItems = updateVisibleItems()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}, debounceResize)
|
||||||
|
|
||||||
|
const observer = new ResizeObserver(entries => {
|
||||||
|
debouncedUpdate(entries)
|
||||||
})
|
})
|
||||||
|
|
||||||
observer.observe(node)
|
observer.observe(node)
|
||||||
@@ -146,15 +166,15 @@
|
|||||||
|
|
||||||
let oldScroll = $state(0)
|
let oldScroll = $state(0)
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (Math.abs(scrollPosition - oldScroll) > estimatedHeight) {
|
if (scrollY.current) {
|
||||||
visibleItems = updateVisibleItems()
|
if (Math.abs(scrollY.current - oldScroll) > estimatedHeight) {
|
||||||
oldScroll = scrollPosition
|
visibleItems = updateVisibleItems()
|
||||||
|
oldScroll = scrollY.current
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:window bind:scrollY={scrollPosition} />
|
|
||||||
|
|
||||||
<div
|
<div
|
||||||
bind:this={virtualListEl}
|
bind:this={virtualListEl}
|
||||||
style="position: relative; height: {cumulativeItemHeights[
|
style="position: relative; height: {cumulativeItemHeights[
|
||||||
|
|||||||
Reference in new issue
Block a user