From 700dec88752c2772eca383ea5f60373f4f62eaac Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Sat, 28 Jun 2025 18:46:03 -0500 Subject: [PATCH] Proxy bookmark observers through `SveltePublicationTree` --- .../svelte_publication_tree.svelte.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/lib/components/publications/svelte_publication_tree.svelte.ts b/src/lib/components/publications/svelte_publication_tree.svelte.ts index 9969ed7..0c1eefc 100644 --- a/src/lib/components/publications/svelte_publication_tree.svelte.ts +++ b/src/lib/components/publications/svelte_publication_tree.svelte.ts @@ -7,11 +7,13 @@ export class SveltePublicationTree { #publicationTree: PublicationTree; #nodeResolvedObservers: Array<(address: string) => void> = []; + #bookmarkMovedObservers: Array<(address: string) => void> = []; constructor(rootEvent: NDKEvent, ndk: NDK) { this.#publicationTree = new PublicationTree(rootEvent, ndk); this.#publicationTree.onNodeResolved(this.#handleNodeResolved); + this.#publicationTree.onBookmarkMoved(this.#handleBookmarkMoved); } // #region Proxied Public Methods @@ -48,6 +50,14 @@ export class SveltePublicationTree { this.#nodeResolvedObservers.push(observer); } + /** + * Registers an observer function that is invoked whenever the bookmark is moved. + * @param observer The observer function. + */ + onBookmarkMoved(observer: (address: string) => void) { + this.#bookmarkMovedObservers.push(observer); + } + // #endregion // #region Proxied Async Iterator Methods @@ -83,5 +93,19 @@ export class SveltePublicationTree { } } + /** + * Observer function that is invoked whenever the bookmark is moved on the publication tree. + * + * @param address The address of the new bookmark. + * + * This member is declared as an arrow function to ensure that the correct `this` context is + * used when the function is invoked in this class's constructor. + */ + #handleBookmarkMoved = (address: string) => { + for (const observer of this.#bookmarkMovedObservers) { + observer(address); + } + } + // #endregion } \ No newline at end of file