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 @@
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
const nextItem = await publicationTree.next(); const nextItem = await publicationTree.next();
if (leaves.includes(nextItem.value) || nextItem.done) { if (leaves.includes(nextItem.value) || (nextItem.done && nextItem.value === null)) {
isLoading = false; isLoading = false;
return; return;
} }

17
src/lib/data_structures/publication_tree.ts

@ -4,7 +4,6 @@ import { Lazy } from "./lazy.ts";
import { findIndexAsync as _findIndexAsync } from '../utils.ts'; import { findIndexAsync as _findIndexAsync } from '../utils.ts';
enum PublicationTreeNodeType { enum PublicationTreeNodeType {
Root,
Branch, Branch,
Leaf, Leaf,
} }
@ -50,7 +49,7 @@ export class PublicationTree implements AsyncIterable<NDKEvent> {
constructor(rootEvent: NDKEvent, ndk: NDK) { constructor(rootEvent: NDKEvent, ndk: NDK) {
const rootAddress = rootEvent.tagAddress(); const rootAddress = rootEvent.tagAddress();
this.#root = { this.#root = {
type: PublicationTreeNodeType.Root, type: this.#getNodeType(rootEvent),
address: rootAddress, address: rootAddress,
children: [], children: [],
}; };
@ -286,9 +285,15 @@ export class PublicationTree implements AsyncIterable<NDKEvent> {
continue; 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 }; 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); } while (this.#cursor.target?.type !== PublicationTreeNodeType.Leaf);
const event = await this.getEvent(this.#cursor.target!.address); const event = await this.getEvent(this.#cursor.target!.address);
@ -414,11 +419,7 @@ export class PublicationTree implements AsyncIterable<NDKEvent> {
return node; return node;
} }
async #getNodeType(event: NDKEvent): Promise<PublicationTreeNodeType> { #getNodeType(event: NDKEvent): PublicationTreeNodeType {
if (event.tagAddress() === this.#root.address) {
return PublicationTreeNodeType.Root;
}
if (event.kind === 30040 && event.tags.some(tag => tag[0] === 'a')) { if (event.kind === 30040 && event.tags.some(tag => tag[0] === 'a')) {
return PublicationTreeNodeType.Branch; return PublicationTreeNodeType.Branch;
} }

Loading…
Cancel
Save