Browse Source

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.
master
buttercat1791 1 year ago
parent
commit
8c9319aa64
  1. 33
      src/lib/data_structures/publication_tree.ts

33
src/lib/data_structures/publication_tree.ts

@ -7,11 +7,30 @@ interface PublicationTreeNode {
children?: PublicationTreeNode[]; children?: PublicationTreeNode[];
} }
// TODO: Add an iterator over the leaves of the tree. export class PublicationTree implements Iterable<NDKEvent> {
export class PublicationTree { /**
* The root node of the tree.
*/
private root: PublicationTreeNode; private root: PublicationTreeNode;
/**
* A map of addresses in the tree to their corresponding nodes.
*/
private nodes: Map<string, PublicationTreeNode>; private nodes: Map<string, PublicationTreeNode>;
/**
* A map of addresses in the tree to their corresponding events.
*/
private events: Map<string, NDKEvent>; private events: Map<string, NDKEvent>;
/**
* 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; private ndk: NDK;
constructor(rootEvent: NDKEvent, ndk: NDK) { constructor(rootEvent: NDKEvent, ndk: NDK) {
@ -70,6 +89,16 @@ export class PublicationTree {
return event; return event;
} }
[Symbol.iterator](): Iterator<NDKEvent> {
return this;
}
next(): IteratorResult<NDKEvent> {
// TODO: Implement iteration from the bookmark over subsequent leaves.
return { done: true, value: null };
}
// #region Private Methods // #region Private Methods
/** /**

Loading…
Cancel
Save