diff --git a/packages/app/src/System/NoteCollection.ts b/packages/app/src/System/NoteCollection.ts index 47999070..3363869b 100644 --- a/packages/app/src/System/NoteCollection.ts +++ b/packages/app/src/System/NoteCollection.ts @@ -89,10 +89,16 @@ export abstract class HookedNoteStore i } onEvent(cb: OnEventCallback): OnEventCallbackRelease { - this.#eventHooks.push(cb); + const existing = this.#eventHooks.findIndex(a => a === cb); + if (existing === -1) { + this.#eventHooks.push(cb); + return () => { + const idx = this.#eventHooks.findIndex(a => a === cb); + this.#eventHooks.splice(idx, 1); + }; + } return () => { - const idx = this.#eventHooks.findIndex(a => a === cb); - this.#eventHooks.splice(idx, 1); + //noop }; } diff --git a/packages/app/src/System/ProfileCache.ts b/packages/app/src/System/ProfileCache.ts index 29bec0af..82017722 100644 --- a/packages/app/src/System/ProfileCache.ts +++ b/packages/app/src/System/ProfileCache.ts @@ -37,6 +37,15 @@ class ProfileLoaderService { } } + async onProfileEvent(ev: Readonly>) { + for (const e of ev) { + const profile = mapEventToProfile(e); + if (profile) { + await UserCache.update(profile); + } + } + } + async #FetchMetadata() { const missingFromCache = await UserCache.buffer([...this.WantsMetadata]); @@ -50,31 +59,21 @@ class ProfileLoaderService { const sub = new RequestBuilder(`profiles`); sub - .withOptions({ - skipDiff: true, - }) .withFilter() .kinds([EventKind.SetMetadata]) .authors([...missing]); const q = System.Query(PubkeyReplaceableNoteStore, sub); // never release this callback, it will stop firing anyway after eose - q.onEvent(async ev => { - for (const e of ev) { - const profile = mapEventToProfile(e); - if (profile) { - await UserCache.update(profile); - } - } - }); + const releaseOnEvent = q.onEvent(this.onProfileEvent); const results = await new Promise>>(resolve => { let timeout: ReturnType | undefined = undefined; const release = q.hook(() => { if (!q.loading) { clearTimeout(timeout); resolve(q.getSnapshotData() ?? []); - release(); } + release(); }); timeout = setTimeout(() => { release(); @@ -82,6 +81,7 @@ class ProfileLoaderService { }, 5_000); }); + releaseOnEvent(); const couldNotFetch = [...missing].filter(a => !results.some(b => b.pubkey === a)); if (couldNotFetch.length > 0) { console.debug("No profiles: ", couldNotFetch);