bug: profile cache multiple hooks
This commit is contained in:
parent
03446aa50d
commit
1a444a8418
@ -89,10 +89,16 @@ export abstract class HookedNoteStore<TSnapshot extends NoteStoreSnapshotData> i
|
|||||||
}
|
}
|
||||||
|
|
||||||
onEvent(cb: OnEventCallback): OnEventCallbackRelease {
|
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 () => {
|
return () => {
|
||||||
const idx = this.#eventHooks.findIndex(a => a === cb);
|
//noop
|
||||||
this.#eventHooks.splice(idx, 1);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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() {
|
async #FetchMetadata() {
|
||||||
const missingFromCache = await UserCache.buffer([...this.WantsMetadata]);
|
const missingFromCache = await UserCache.buffer([...this.WantsMetadata]);
|
||||||
|
|
||||||
@ -50,31 +59,21 @@ class ProfileLoaderService {
|
|||||||
|
|
||||||
const sub = new RequestBuilder(`profiles`);
|
const sub = new RequestBuilder(`profiles`);
|
||||||
sub
|
sub
|
||||||
.withOptions({
|
|
||||||
skipDiff: true,
|
|
||||||
})
|
|
||||||
.withFilter()
|
.withFilter()
|
||||||
.kinds([EventKind.SetMetadata])
|
.kinds([EventKind.SetMetadata])
|
||||||
.authors([...missing]);
|
.authors([...missing]);
|
||||||
|
|
||||||
const q = System.Query<PubkeyReplaceableNoteStore>(PubkeyReplaceableNoteStore, sub);
|
const q = System.Query<PubkeyReplaceableNoteStore>(PubkeyReplaceableNoteStore, sub);
|
||||||
// never release this callback, it will stop firing anyway after eose
|
// never release this callback, it will stop firing anyway after eose
|
||||||
q.onEvent(async ev => {
|
const releaseOnEvent = q.onEvent(this.onProfileEvent);
|
||||||
for (const e of ev) {
|
|
||||||
const profile = mapEventToProfile(e);
|
|
||||||
if (profile) {
|
|
||||||
await UserCache.update(profile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const results = await new Promise<Readonly<Array<TaggedRawEvent>>>(resolve => {
|
const results = await new Promise<Readonly<Array<TaggedRawEvent>>>(resolve => {
|
||||||
let timeout: ReturnType<typeof setTimeout> | undefined = undefined;
|
let timeout: ReturnType<typeof setTimeout> | undefined = undefined;
|
||||||
const release = q.hook(() => {
|
const release = q.hook(() => {
|
||||||
if (!q.loading) {
|
if (!q.loading) {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
resolve(q.getSnapshotData() ?? []);
|
resolve(q.getSnapshotData() ?? []);
|
||||||
release();
|
|
||||||
}
|
}
|
||||||
|
release();
|
||||||
});
|
});
|
||||||
timeout = setTimeout(() => {
|
timeout = setTimeout(() => {
|
||||||
release();
|
release();
|
||||||
@ -82,6 +81,7 @@ class ProfileLoaderService {
|
|||||||
}, 5_000);
|
}, 5_000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
releaseOnEvent();
|
||||||
const couldNotFetch = [...missing].filter(a => !results.some(b => b.pubkey === a));
|
const couldNotFetch = [...missing].filter(a => !results.some(b => b.pubkey === a));
|
||||||
if (couldNotFetch.length > 0) {
|
if (couldNotFetch.length > 0) {
|
||||||
console.debug("No profiles: ", couldNotFetch);
|
console.debug("No profiles: ", couldNotFetch);
|
||||||
|
Loading…
Reference in New Issue
Block a user