diff --git a/src/lib/components/Publication.svelte b/src/lib/components/Publication.svelte index 7d53987..58f29db 100644 --- a/src/lib/components/Publication.svelte +++ b/src/lib/components/Publication.svelte @@ -39,7 +39,7 @@ for (let i = 0; i < count; i++) { const nextItem = await publicationTree.next(); - if (leaves.includes(nextItem.value) || nextItem.done) { + if (leaves.includes(nextItem.value) || (nextItem.done && nextItem.value === null)) { isLoading = false; return; } diff --git a/src/lib/data_structures/publication_tree.ts b/src/lib/data_structures/publication_tree.ts index 7f6c968..466e676 100644 --- a/src/lib/data_structures/publication_tree.ts +++ b/src/lib/data_structures/publication_tree.ts @@ -4,7 +4,6 @@ import { Lazy } from "./lazy.ts"; import { findIndexAsync as _findIndexAsync } from '../utils.ts'; enum PublicationTreeNodeType { - Root, Branch, Leaf, } @@ -50,7 +49,7 @@ export class PublicationTree implements AsyncIterable { constructor(rootEvent: NDKEvent, ndk: NDK) { const rootAddress = rootEvent.tagAddress(); this.#root = { - type: PublicationTreeNodeType.Root, + type: this.#getNodeType(rootEvent), address: rootAddress, children: [], }; @@ -286,9 +285,15 @@ export class PublicationTree implements AsyncIterable { continue; } - if (this.#cursor.target?.type === PublicationTreeNodeType.Root) { + const isRoot = this.#cursor.target?.address === this.#root.address; + + if (isRoot && this.#cursor.target?.type === PublicationTreeNodeType.Branch) { return { done: true, value: null }; } + + if (isRoot && this.#cursor.target?.type === PublicationTreeNodeType.Leaf) { + return { done: true, value: this.#events.get(this.#cursor.target!.address)! }; + } } while (this.#cursor.target?.type !== PublicationTreeNodeType.Leaf); const event = await this.getEvent(this.#cursor.target!.address); @@ -414,11 +419,7 @@ export class PublicationTree implements AsyncIterable { return node; } - async #getNodeType(event: NDKEvent): Promise { - if (event.tagAddress() === this.#root.address) { - return PublicationTreeNodeType.Root; - } - + #getNodeType(event: NDKEvent): PublicationTreeNodeType { if (event.kind === 30040 && event.tags.some(tag => tag[0] === 'a')) { return PublicationTreeNodeType.Branch; }