Browse Source

Fix the tree traversal algorithm

The loader's `next()` method now correctly indicates when it has reached the root node in the tree.  From this, the publication component can known when to stop loading and hide the "Load More" button.
master
buttercat1791 10 months ago
parent
commit
fb7e2539e6
  1. 9
      src/lib/components/Publication.svelte
  2. 32
      src/lib/data_structures/publication_tree.ts

9
src/lib/components/Publication.svelte

@ -43,11 +43,8 @@ @@ -43,11 +43,8 @@
for (let i = 0; i < count; i++) {
const iterResult = await publicationTree.next();
const { done, value } = iterResult;
console.debug('Iterator result:', iterResult, 'done type:', typeof done);
if (done) {
console.debug('Done condition met, setting isDone to true');
isDone = true;
break;
}
@ -97,10 +94,6 @@ @@ -97,10 +94,6 @@
return () => observer?.unobserve(lastElementRef!);
});
$effect(() => {
console.debug('isDone changed to:', isDone);
});
// #endregion
// #region ToC
@ -191,7 +184,6 @@ @@ -191,7 +184,6 @@
</script>
<!-- TODO: Keep track of already-loaded leaves. -->
<!-- TODO: Determine when we've reached the end of the document. -->
<!-- TODO: Handle entering mid-document and scrolling up. -->
{#if showTocButton && !showToc}
@ -239,7 +231,6 @@ @@ -239,7 +231,6 @@
/>
{/if}
{/each}
<!-- TODO: Hide the button if there are no more leaves to load. -->
<div class="flex justify-center my-4">
{#if isLoading}
<Button disabled color="primary">

32
src/lib/data_structures/publication_tree.ts

@ -288,32 +288,22 @@ export class PublicationTree implements AsyncIterable<NDKEvent | null> { @@ -288,32 +288,22 @@ export class PublicationTree implements AsyncIterable<NDKEvent | null> {
await this.#cursor.tryMoveTo(this.#bookmark);
}
// Based on Raymond Chen's tree traversal algorithm.
// https://devblogs.microsoft.com/oldnewthing/20200106-00/?p=103300
do {
if (await this.#cursor.tryMoveToFirstChild()) {
continue;
}
if (await this.#cursor.tryMoveToNextSibling()) {
continue;
}
if (this.#cursor.tryMoveToParent()) {
continue;
}
const isRoot = this.#cursor.target?.address === this.#root.address;
if (isRoot && this.#cursor.target?.type === PublicationTreeNodeType.Branch) {
return { done: true, value: null };
}
while (await this.#cursor.tryMoveToFirstChild()) {
continue;
}
if (isRoot && this.#cursor.target?.type === PublicationTreeNodeType.Leaf) {
return { done: true, value: this.#events.get(this.#cursor.target!.address)! };
const event = await this.getEvent(this.#cursor.target!.address);
return { done: false, value: event };
}
} while (this.#cursor.target?.type !== PublicationTreeNodeType.Leaf);
this.#cursor.tryMoveToParent();
} while (this.#cursor.tryMoveToParent());
const event = await this.getEvent(this.#cursor.target!.address);
return { done: false, value: event };
// If we get to this point, we're at the root node (can't move up any more).
return { done: true, value: null };
}
// #endregion

Loading…
Cancel
Save