perf: use a virtual list

This commit is contained in:
Xylight committed 2024-07-04 16:13:56 -07:00
1 parent 25b1ab8d8e
commit 58b6c12a8b
5 files changed
+130 -186

No files matched your search

BIN
View File
Binary file not shown.
+12 -12
View File
@@ -1,17 +1,6 @@
{
"name": "photon-lemmy",
"version": "1.30.4",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"test": "vitest",
"preview": "vite preview",
"start": "node build/index.js",
"bunstart": "bun build/index.js",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
},
"devDependencies": {
"@catdadcode/svelte-adapter-bun": "^0.5.7",
"@dicebear/core": "^8.0.1",
@@ -22,6 +11,7 @@
"@sveltejs/kit": "latest",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@tailwindcss/container-queries": "^0.1.1",
"@tanstack/svelte-virtual": "^3.8.1",
"@types/markdown-it": "^12.2.3",
"@types/markdown-it-container": "^2.0.6",
"@types/nprogress": "^0.2.0",
@@ -41,7 +31,6 @@
"svelte-check": "^3.0.1",
"svelte-hero-icons": "^5.0.0",
"svelte-infinite-scroll": "^2.0.1",
"svelte-tiny-virtual-list": "^2.0.5",
"sveltekit-i18n": "^2.4.2",
"tailwindcss": "^3.3.3",
"tslib": "^2.4.1",
@@ -51,5 +40,16 @@
"vitest": "^1.0.0",
"workbox-window": "^7.0.0"
},
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"test": "vitest",
"preview": "vite preview",
"start": "node build/index.js",
"bunstart": "bun build/index.js",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
},
"type": "module"
}
+2 -1
View File
@@ -70,6 +70,7 @@
on:click={(e) => {
onClick(e)
}}
style={$$props.style ?? ''}
>
<PostMeta
community={hideCommunity ? undefined : post.community}
@@ -108,7 +109,7 @@
<PostMediaCompact
{view}
bind:post={post.post}
bind:type={type}
bind:type
class="{$userSettings.leftAlign
? 'mr-2'
: 'ml-2'} flex-shrink no-list-margin"
+116 -71
View File
@@ -7,6 +7,9 @@
import { ArchiveBox, Icon, Minus, Plus } from 'svelte-hero-icons'
import { expoOut } from 'svelte/easing'
import { fly, slide } from 'svelte/transition'
import { browser } from '$app/environment'
import { afterUpdate, type SvelteComponent } from 'svelte'
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
type PostViewWithCrossposts = PostView & {
withCrossposts: true
@@ -69,6 +72,36 @@
$: combinedPosts = combineCrossposts(posts)
let viewPost: number = -1
let list: any
function calculateHeight(index: number): number {
if (browser) {
const el = document.querySelector(`[data-index="${index}"]`)
return el?.clientHeight ?? 150
} else return 150
return 150
}
afterUpdate(() => {
setTimeout(() => {
list?.recomputeSizes?.(0)
})
})
const virtualizer = createWindowVirtualizer({
count: posts.length,
estimateSize: () => 150,
})
let virtualItemEls: HTMLElement[] = []
$: items = $virtualizer.getVirtualItems()
$: {
if (virtualItemEls.length)
virtualItemEls.forEach((el) => $virtualizer.measureElement(el))
}
</script>
<ul
@@ -98,82 +131,94 @@
</Placeholder>
</div>
{:else}
{#each combinedPosts as post, index}
{#if !($userSettings.hidePosts.deleted && post.post.deleted) && !($userSettings.hidePosts.removed && post.post.removed)}
<li
in:fly|global={{
y: -8,
duration: index < 4 ? 500 : 0,
opacity: 0,
delay: index < 4 ? index * 100 : 0,
}}
class="relative"
>
<Post
hideCommunity={community}
view={(post.post.featured_community || post.post.featured_local) &&
$userSettings.posts.compactFeatured
? 'compact'
: $userSettings.view}
{post}
class="transition-all duration-250 {post.withCrossposts &&
viewPost != post.post.id
? ''
: ''}"
on:hide={() => {
combinedPosts = combinedPosts.toSpliced(index, 1)
<!-- {#each combinedPosts as post, index} -->
<!-- {#if !($userSettings.hidePosts.deleted && post.post.deleted) && !($userSettings.hidePosts.removed && post.post.removed)} -->
<div
style="position:relative; height: {$virtualizer.getTotalSize()}px; width: 100%;"
>
<div
style="position: absolute; top: 0; left: 0; width: 100%; transform: translateY({items[0]
? items[0].start
: 0}px);"
>
{#each items as row, index (row.index)}
<li
in:fly|global={{
y: -8,
duration: 500,
opacity: 0,
delay: 200,
}}
bind:this={virtualItemEls[index]}
data-index={row.index}
class="relative"
>
<svelte:fragment slot="badges">
{#if post.withCrossposts}
<button
on:click={() => {
if (viewPost == post.post.id) viewPost = -1
else viewPost = post.post.id
}}
>
<Badge
class="z-10 backdrop-blur-xl hover:brightness-110 cursor-pointer transition-all"
color="gray-subtle"
>
{#if viewPost == post.post.id}
<Icon mini src={Minus} size="14" />
{:else}
<Icon mini src={Plus} size="14" />
{/if}
{post.crossposts.length} crosspost{post.crossposts.length ==
1
? ''
: 's'}
</Badge>
</button>{/if}
</svelte:fragment>
</Post>
{#if post.withCrossposts && viewPost == post.post.id}
<div
transition:slide|global={{
axis: 'y',
duration: 500,
easing: expoOut,
<Post
hideCommunity={community}
view={(posts[row.index].post.featured_community ||
posts[row.index].post.featured_local) &&
$userSettings.posts.compactFeatured
? 'compact'
: $userSettings.view}
post={posts[row.index]}
class="transition-all duration-250"
on:hide={() => {
posts = posts.toSpliced(row.index, 1)
}}
>
<span
class="text-sm flex flex-row gap-2 items-center"
class:my-4={$userSettings.view == 'card'}
<svelte:fragment slot="badges">
<!-- {#if posts[index].withCrossposts}
<button
on:click={() => {
if (viewPost == posts[index].post.id) viewPost = -1
else viewPost = posts[index].post.id
}}
>
<Badge
class="z-10 backdrop-blur-xl hover:brightness-110 cursor-pointer transition-all"
color="gray-subtle"
>
{#if viewPost == posts[index].post.id}
<Icon mini src={Minus} size="14" />
{:else}
<Icon mini src={Plus} size="14" />
{/if}
{posts[index].crossposts.length} crosspost{posts[index]
.crossposts.length == 1
? ''
: 's'}
</Badge>
</button>{/if} -->
</svelte:fragment>
</Post>
<!-- {#if posts[index].withCrossposts && viewPost == posts[index].post.id}
<div
transition:slide|global={{
axis: 'y',
duration: 500,
easing: expoOut,
}}
>
Crossposts <hr class="w-full dark:border-zinc-800" />
{post.crossposts.length}
</span>
{#each post.crossposts as crosspost, index}
<div class="w-full transition-all mb-4">
<Post post={crosspost} view={$userSettings.view} />
</div>
{/each}
</div>
{/if}
</li>
{/if}
{/each}
<span
class="text-sm flex flex-row gap-2 items-center"
class:my-4={$userSettings.view == 'card'}
>
Crossposts <hr class="w-full dark:border-zinc-800" />
{posts[index].crossposts.length}
</span>
{#each posts[index].crossposts as crosspost, index}
<div class="w-full transition-all mb-4">
<Post post={crosspost} view={$userSettings.view} />
</div>
{/each}
</div>
{/if} -->
</li>
{/each}
</div>
</div>
<!-- {/if} -->
<!-- {/each} -->
{/if}
<slot />
</ul>
-102
View File
@@ -1,102 +0,0 @@
<script lang="ts">
import { onMount, afterUpdate } from 'svelte'
import type { SvelteComponent } from 'svelte'
interface VisibleItem<T> {
item: T
index: number
top: number
}
export let items: any[] = []
let containerHeight = 0
let scrollTop = 0
let itemHeights: number[] = []
let visibleItems: VisibleItem<any>[] = []
let totalHeight = 0
let container: HTMLDivElement
$: {
let currentOffset = 0
let startIndex = 0
let endIndex = items.length - 1
// Find the start index
for (let i = 0; i < items.length; i++) {
if (currentOffset + itemHeights[i] > scrollTop) {
startIndex = Math.max(0, i)
break
}
currentOffset += itemHeights[i]
}
// Find the end index
currentOffset = 0
for (let i = 0; i < items.length; i++) {
if (currentOffset > scrollTop + containerHeight) {
endIndex = Math.min(items.length - 1, i)
break
}
currentOffset += itemHeights[i]
}
visibleItems = items.slice(startIndex, endIndex + 1).map((item, index) => ({
item,
index: startIndex + index,
top: itemHeights.slice(0, startIndex + index).reduce((a, b) => a + b, 0),
}))
console.log(visibleItems.length == items.length)
totalHeight = itemHeights.reduce((a, b) => a + b, 0)
}
function handleScroll(event: Event): void {
const target = event.target as HTMLDivElement
scrollTop = target.scrollTop
}
function updateItemHeight(index: number, height: number): void {
if (itemHeights[index] !== height) {
itemHeights[index] = height
itemHeights = itemHeights
}
}
onMount(() => {
containerHeight = container.clientHeight
// Force an update
items = items
})
afterUpdate(() => {
// Measure item heights after render
visibleItems.forEach(({ index }) => {
const element = container.querySelector(
`[data-index="${index}"]`
) as HTMLDivElement
if (element) {
updateItemHeight(index, element.offsetHeight)
}
})
})
</script>
<svelte:window on:scroll={handleScroll} />
<div
bind:this={container}
style="height: 100%; width: 100%; overflow-x: hidden; overflow-y: scroll;"
>
<div style="height: {totalHeight}px; position: relative;">
{#each visibleItems as { item, index, top }}
<div
data-index={index}
style="position: absolute; top: {top}px; width: 100%;"
>
<slot {item} {index} />
</div>
{/each}
</div>
</div>