Browse Source

Support loading single content events

Handles the case where the root of a publication tree is identical to its one and only leaf, i.e., the publication consists of a single event.
master
buttercat1791 11 months ago
parent
commit
035ef934d5
  1. 2
      src/lib/components/Publication.svelte
  2. 17
      src/lib/data_structures/publication_tree.ts

2
src/lib/components/Publication.svelte

@ -39,7 +39,7 @@ @@ -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;
}

17
src/lib/data_structures/publication_tree.ts

@ -4,7 +4,6 @@ import { Lazy } from "./lazy.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<NDKEvent> { @@ -50,7 +49,7 @@ export class PublicationTree implements AsyncIterable<NDKEvent> {
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<NDKEvent> { @@ -286,9 +285,15 @@ export class PublicationTree implements AsyncIterable<NDKEvent> {
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<NDKEvent> { @@ -414,11 +419,7 @@ export class PublicationTree implements AsyncIterable<NDKEvent> {
return node;
}
async #getNodeType(event: NDKEvent): Promise<PublicationTreeNodeType> {
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;
}

Loading…
Cancel
Save