impr: optimize virtualizer resizeobserver (#688)

* impr: use one resize observer that observes all elements 1984 style instead of 1 agent per element

* design: tweak VirtualFeed.svelte to have a more modern retry window
This commit is contained in:
xylight authored and GitHub committed 2025-11-19 01:33:43 +00:00
1 parent 2d2a9a5671
commit 568bb3330a
8 files changed
+53 -52

No files matched your search

+25 -24
View File
@@ -139,38 +139,38 @@
} }
function resizeObserver(node: HTMLElement) { function resizeObserver(node: HTMLElement) {
const debouncedUpdate = debounce((entries: ResizeObserverEntry[]) => {
for (const entry of entries) {
const indexAttr = node.getAttribute('data-index')
if (indexAttr === null) continue
const index = Number(indexAttr)
if (isNaN(index)) continue
const newHeight = entry.contentRect.height
if (itemHeights[index] !== newHeight) {
itemHeights[index] = newHeight
if (!initialRender) visibleItems = updateVisibleItems()
}
}
}, debounceResize)
const observer = new ResizeObserver((entries) => {
debouncedUpdate(entries)
})
observer.observe(node) observer.observe(node)
onDestroy(() => {
observer.disconnect()
})
return { return {
destroy() { destroy() {
observer.disconnect() observer.unobserve(node)
}, },
} }
} }
const debouncedUpdate = debounce((entries: ResizeObserverEntry[]) => {
for (const entry of entries) {
const indexAttr = entry.target.getAttribute('data-index')
if (indexAttr === null) continue
const index = Number(indexAttr)
if (isNaN(index)) continue
const newHeight = entry.contentRect.height
if (itemHeights[index] !== newHeight) {
itemHeights[index] = newHeight
if (!initialRender) visibleItems = updateVisibleItems()
}
}
}, debounceResize)
const observer = new ResizeObserver((entries) => {
debouncedUpdate(entries)
})
onDestroy(() => {
observer.disconnect()
})
// Scroll position changes (only every few px) // Scroll position changes (only every few px)
let oldScroll = $state(0) let oldScroll = $state(0)
$effect(() => { $effect(() => {
@@ -196,6 +196,7 @@
itemHeights[index] = newHeight itemHeights[index] = newHeight
} }
}) })
untrack(() => { untrack(() => {
visibleItems = updateVisibleItems() visibleItems = updateVisibleItems()
initialRender = false initialRender = false
+18 -21
View File
@@ -2,12 +2,13 @@
import { browser } from '$app/environment' import { browser } from '$app/environment'
import { client } from '$lib/api/client.svelte' import { client } from '$lib/api/client.svelte'
import type { GetPosts, PostView } from '$lib/api/types' import type { GetPosts, PostView } from '$lib/api/types'
import { errorMessage } from '$lib/app/error'
import { t } from '$lib/app/i18n' import { t } from '$lib/app/i18n'
import VirtualList from '$lib/app/render/VirtualList.svelte' import VirtualList from '$lib/app/render/VirtualList.svelte'
import { settings } from '$lib/app/settings.svelte' import { settings } from '$lib/app/settings.svelte'
import Placeholder from '$lib/ui/info/Placeholder.svelte' import Placeholder from '$lib/ui/info/Placeholder.svelte'
import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte' import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte'
import { Button } from 'mono-svelte' import { Button, Material, Spinner } from 'mono-svelte'
import { onDestroy, onMount, untrack } from 'svelte' import { onDestroy, onMount, untrack } from 'svelte'
import { import {
ArchiveBox, ArchiveBox,
@@ -213,15 +214,16 @@
{#if settings.infiniteScroll && browser && posts.length > 0} {#if settings.infiniteScroll && browser && posts.length > 0}
{#if error} {#if error}
<div <Material color="error" class="flex flex-col gap-4">
class="flex flex-col justify-center items-center <div>
rounded-xl gap-2 py-8 mt-6 <Icon
border border-b! border-red-500! bg-red-500/5 px-4" src={ExclamationTriangle}
> size="20"
<div class="bg-red-500/30 rounded-full p-3 text-red-500"> micro
<Icon src={ExclamationTriangle} size="24" solid></Icon> class="inline-block rounded-lg clear-both float-left mr-2"
/>
{errorMessage(error)}
</div> </div>
<pre class="py-0.5">{error}</pre>
<Button <Button
color="primary" color="primary"
{loading} {loading}
@@ -230,23 +232,18 @@
> >
{$t('message.retry')} {$t('message.retry')}
</Button> </Button>
</div> </Material>
{:else if hasMore} {:else if hasMore}
<div class="w-full flex flex-col gap-2 animate-pulse pt-6"> <div class="w-full h-32 grid place-items-center">
<div <Spinner width={24} />
class="w-96 max-w-full h-8 bg-slate-100 dark:bg-zinc-800 rounded-md"
></div>
<div class="w-full h-48 bg-slate-100 dark:bg-zinc-800 rounded-md"></div>
<div class="bg-transparent! h-8 flex justify-between">
<div class="w-48 h-8 bg-slate-100 dark:bg-zinc-800 rounded-md"></div>
<div class="w-24 h-8 bg-slate-100 dark:bg-zinc-800 rounded-md"></div>
</div>
</div> </div>
{:else} {:else}
<div style="border-top-width: 0"> <div style="border-top-width: 0">
<EndPlaceholder> <EndPlaceholder>
{$t('routes.frontpage.endFeed', { {$t('routes.frontpage.endFeed', {
community_name: params.community_name ?? 'undefined', community_name:
params.community_name ??
'Lemmy. There are no more posts. You saw them all.',
})} })}
{#snippet action()} {#snippet action()}
<Button color="tertiary" icon={ChevronDoubleUp}> <Button color="tertiary" icon={ChevronDoubleUp}>
@@ -256,7 +253,7 @@
</EndPlaceholder> </EndPlaceholder>
</div> </div>
{/if} {/if}
<InfiniteScroll window threshold={600} on:loadMore={loadMore} /> <InfiniteScroll window threshold={300} on:loadMore={loadMore} />
{/if} {/if}
{@render children?.()} {@render children?.()}
</ul> </ul>
+4 -2
View File
@@ -16,7 +16,7 @@
</script> </script>
<script lang="ts"> <script lang="ts">
import { onDestroy } from 'svelte' import { onDestroy, type Snippet } from 'svelte'
import { ExclamationTriangle, Icon } from 'svelte-hero-icons/dist' import { ExclamationTriangle, Icon } from 'svelte-hero-icons/dist'
import { expoOut } from 'svelte/easing' import { expoOut } from 'svelte/easing'
import { fly, slide } from 'svelte/transition' import { fly, slide } from 'svelte/transition'
@@ -28,9 +28,10 @@
scope?: string | undefined | null scope?: string | undefined | null
message?: string message?: string
class?: string class?: string
children?: Snippet
} }
let { scope, message, class: clazz = '' }: Props = $props() let { scope, message, children, class: clazz = '' }: Props = $props()
let scopedErrors = $derived( let scopedErrors = $derived(
errors.filter((e) => e.scope == scope || e.scope == 'global'), errors.filter((e) => e.scope == scope || e.scope == 'global'),
@@ -63,6 +64,7 @@
{#if message} {#if message}
{message} {message}
{/if} {/if}
{@render children?.()}
{#each scopedErrors as error} {#each scopedErrors as error}
<p>{error.message}</p> <p>{error.message}</p>
{/each} {/each}
+1
View File
@@ -7,5 +7,6 @@ export async function load({ url }) {
if (link) { if (link) {
const localized = photonify(link) const localized = photonify(link)
if (localized) redirect(302, localized) if (localized) redirect(302, localized)
else redirect(302, link)
} }
} }
+1 -1
View File
@@ -1,4 +1,4 @@
[{ [{
"relation": ["delegate_permission/common.handle_all_urls"], "relation": ["delegate_permission/common.handle_all_urls"],
"target": { "target": {
"namespace": "android_app", "namespace": "android_app",
+4 -4
View File
@@ -1,6 +1,6 @@
{ {
"short_name": "Photon", "short_name": "Photon",
"name": "Photon for Lemmy", "name": "Photon",
"start_url": "/", "start_url": "/",
"icons": [ "icons": [
{ {
@@ -54,9 +54,9 @@
"action": "/go", "action": "/go",
"method": "GET", "method": "GET",
"params": { "params": {
"post_title": "name", "title": "post_name",
"post_body": "description", "text": "post_body",
"localize": "link" "url": "localize"
} }
}, },
"categories": ["entertainment", "news", "social"] "categories": ["entertainment", "news", "social"]
Binary file not shown.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 KiB