Browse Source

Add a method to return the hierarchy in which an event lives

master
buttercat1791 1 year ago
parent
commit
837b371551
  1. 22
      src/lib/data_structures/publication_tree.ts

22
src/lib/data_structures/publication_tree.ts

@ -130,6 +130,28 @@ export class PublicationTree implements AsyncIterable<NDKEvent> { @@ -130,6 +130,28 @@ export class PublicationTree implements AsyncIterable<NDKEvent> {
return event;
}
/**
* Retrieves the events in the hierarchy of the event with the given address.
* @param address The address of the event for which to retrieve the hierarchy.
* @returns Returns an array of events in the addressed event's hierarchy, beginning with the
* root and ending with the addressed event.
*/
async getHierarchy(address: string): Promise<NDKEvent[] | null> {
let node = this.#nodes.get(address);
if (!node) {
return null;
}
const hierarchy: NDKEvent[] = [this.#events.get(address)!];
while (node.parent) {
hierarchy.push(this.#events.get(node.parent.address)!);
node = node.parent;
}
return hierarchy.reverse();
}
/**
* Sets a start point for iteration over the leaves of the tree.
* @param address The address of the event to bookmark.

Loading…
Cancel
Save