From f1bdb20e21d41ba81b4f10b75b8f42c78e636cfb Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Fri, 28 Mar 2025 23:32:18 -0500 Subject: [PATCH] Add `getChildAddresses` function to publication tree --- src/lib/data_structures/publication_tree.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lib/data_structures/publication_tree.ts b/src/lib/data_structures/publication_tree.ts index 11fbf31..e7e0e41 100644 --- a/src/lib/data_structures/publication_tree.ts +++ b/src/lib/data_structures/publication_tree.ts @@ -129,6 +129,23 @@ export class PublicationTree implements AsyncIterable { return event; } + /** + * Retrieves the addresses of the loaded children, if any, of the node with the given address. + * @param address The address of the parent node. + * @returns An array of addresses of any loaded child nodes. + */ + async getChildAddresses(address: string): Promise { + const node = await this.#nodes.get(address)?.value(); + if (!node) { + throw new Error(`PublicationTree: Node with address ${address} not found.`); + } + + return Promise.all( + node.children?.map(async child => + (await child.value()).address + ) ?? [] + ); + } /** * 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.