From da2cae365fe767644e0c583645564069db6f015d Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Tue, 6 May 2025 23:33:59 -0500 Subject: [PATCH] Detect when publication loading is done in component --- src/lib/components/Publication.svelte | 44 +++++++++++++++++++++------ 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/lib/components/Publication.svelte b/src/lib/components/Publication.svelte index eaf3559..adc1489 100644 --- a/src/lib/components/Publication.svelte +++ b/src/lib/components/Publication.svelte @@ -32,6 +32,7 @@ let leaves = $state([]); let loadedAddresses = $state>(new Set()); let isLoading = $state(false); + let isDone = $state(false); let lastElementRef = $state(null); let observer: IntersectionObserver; @@ -40,9 +41,18 @@ isLoading = true; for (let i = 0; i < count; i++) { - const nextItem = await publicationTree.next(); + const iterResult = await publicationTree.next(); + const { done, value } = iterResult; - const nextAddress = nextItem.value?.tagAddress(); + console.debug('Iterator result:', iterResult, 'done type:', typeof done); + + if (done) { + console.debug('Done condition met, setting isDone to true'); + isDone = true; + break; + } + + const nextAddress = value?.tagAddress(); if (nextAddress && loadedAddresses.has(nextAddress)) { continue; } @@ -50,13 +60,18 @@ if (nextAddress && !loadedAddresses.has(nextAddress)) { loadedAddresses.add(nextAddress); } - - if (leaves.includes(nextItem.value) || (nextItem.done && nextItem.value === null)) { + + if (value == null) { + isLoading = false; + break; + } + + if (leaves.includes(value)) { isLoading = false; - return; + break; } - leaves.push(nextItem.value); + leaves.push(value); } isLoading = false; @@ -73,8 +88,17 @@ return; } - observer.observe(lastElementRef!); - return () => observer.unobserve(lastElementRef!); + if (isDone) { + observer?.unobserve(lastElementRef!); + return; + } + + observer?.observe(lastElementRef!); + return () => observer?.unobserve(lastElementRef!); + }); + + $effect(() => { + console.debug('isDone changed to:', isDone); }); // #endregion @@ -149,7 +173,7 @@ // Set up the intersection observer. observer = new IntersectionObserver((entries) => { entries.forEach((entry) => { - if (entry.isIntersecting && !isLoading) { + if (entry.isIntersecting && !isLoading && !isDone) { loadMore(1); } }); @@ -221,7 +245,7 @@ - {:else} + {:else if !isDone}