From 8c9319aa64bef82ce6d508b8ad1ca06b7c169643 Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Thu, 6 Mar 2025 09:26:01 -0600 Subject: [PATCH] Stub out methods to make the tree both iterable and its own iterator Consumers should be able to invoke `.next()` on an instance of the tree to retrieve events one at a time. --- src/lib/data_structures/publication_tree.ts | 33 +++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/lib/data_structures/publication_tree.ts b/src/lib/data_structures/publication_tree.ts index a9076ea..c6e7802 100644 --- a/src/lib/data_structures/publication_tree.ts +++ b/src/lib/data_structures/publication_tree.ts @@ -7,11 +7,30 @@ interface PublicationTreeNode { children?: PublicationTreeNode[]; } -// TODO: Add an iterator over the leaves of the tree. -export class PublicationTree { +export class PublicationTree implements Iterable { + /** + * The root node of the tree. + */ private root: PublicationTreeNode; + + /** + * A map of addresses in the tree to their corresponding nodes. + */ private nodes: Map; + + /** + * A map of addresses in the tree to their corresponding events. + */ private events: Map; + + /** + * The address of the last-visited node. Used for iteration and progressive retrieval. + */ + private bookmark?: string; + + /** + * The NDK instance used to fetch events. + */ private ndk: NDK; constructor(rootEvent: NDKEvent, ndk: NDK) { @@ -70,6 +89,16 @@ export class PublicationTree { return event; } + [Symbol.iterator](): Iterator { + return this; + } + + next(): IteratorResult { + // TODO: Implement iteration from the bookmark over subsequent leaves. + + return { done: true, value: null }; + } + // #region Private Methods /**