bug: profile cache multiple hooks

This commit is contained in:
Kieran 2023-04-04 19:44:45 +01:00
parent 03446aa50d
commit 1a444a8418
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 21 additions and 15 deletions

View File

@ -89,10 +89,16 @@ export abstract class HookedNoteStore<TSnapshot extends NoteStoreSnapshotData> 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
};
}

View File

@ -37,6 +37,15 @@ class ProfileLoaderService {
}
}
async onProfileEvent(ev: Readonly<Array<TaggedRawEvent>>) {
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>(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<Readonly<Array<TaggedRawEvent>>>(resolve => {
let timeout: ReturnType<typeof setTimeout> | 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);