From 34942c50466627811f60214090d5ff6cafafa878 Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Mon, 2 Jun 2025 08:51:36 -0500 Subject: [PATCH] Make adding a node observable --- src/lib/data_structures/publication_tree.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib/data_structures/publication_tree.ts b/src/lib/data_structures/publication_tree.ts index 7f4c677..8c57729 100644 --- a/src/lib/data_structures/publication_tree.ts +++ b/src/lib/data_structures/publication_tree.ts @@ -52,6 +52,8 @@ export class PublicationTree implements AsyncIterable { */ #ndk: NDK; + #onNodeAddedCallbacks: Array<(address: string) => void> = []; + #onNodeResolvedCallbacks: Array<(address: string) => void> = []; constructor(rootEvent: NDKEvent, ndk: NDK) { @@ -187,6 +189,10 @@ export class PublicationTree implements AsyncIterable { this.#cursor.tryMoveTo(address); } + onNodeAdded(observer: (address: string) => void) { + this.#onNodeAddedCallbacks.push(observer); + } + /** * Registers an observer function that is invoked whenever a new node is resolved. Nodes are * added lazily. @@ -479,6 +485,8 @@ export class PublicationTree implements AsyncIterable { const lazyNode = new Lazy(() => this.#resolveNode(address, parentNode)); parentNode.children!.push(lazyNode); this.#nodes.set(address, lazyNode); + + this.#onNodeAddedCallbacks.forEach(observer => observer(address)); } /**