fix: various

This commit is contained in:
2024-04-23 15:43:07 +01:00
parent 9ddd8fc6c2
commit a3299ab29a
8 changed files with 59 additions and 104 deletions

View File

@ -6,6 +6,7 @@ export abstract class BackgroundLoader<T extends { loaded: number; created: numb
#system: SystemInterface;
readonly cache: CachedTable<T>;
#log = debug(this.name());
#blacklist = new Set();
/**
* List of pubkeys to fetch metadata for
@ -43,11 +44,6 @@ export abstract class BackgroundLoader<T extends { loaded: number; created: numb
*/
protected abstract buildSub(missing: Array<string>): RequestBuilder;
/**
* Create a placeholder value when no data can be found
*/
protected abstract makePlaceholder(key: string): T | undefined;
/**
* Start requesting a set of keys to be loaded
*/
@ -92,7 +88,7 @@ export abstract class BackgroundLoader<T extends { loaded: number; created: numb
}
async #FetchMetadata() {
const loading = [...this.#wantsKeys];
const loading = [...this.#wantsKeys].filter(a => !this.#blacklist.has(a));
await this.cache.buffer(loading);
const missing = loading.filter(a => (this.cache.getFromCache(a)?.loaded ?? 0) < this.getExpireCutoff());
@ -100,12 +96,9 @@ export abstract class BackgroundLoader<T extends { loaded: number; created: numb
this.#log("Fetching keys: %O", missing);
try {
const found = await this.#loadData(missing);
const noResult = removeUndefined(
missing.filter(a => !found.some(b => a === this.cache.key(b))).map(a => this.makePlaceholder(a)),
);
const noResult = removeUndefined(missing.filter(a => !found.some(b => a === this.cache.key(b))));
if (noResult.length > 0) {
this.#log("Adding placeholders for %O", noResult);
await Promise.all(noResult.map(a => this.cache.update(a)));
noResult.forEach(a => this.#blacklist.add(a));
}
} catch (e) {
this.#log("Error: %O", e);
@ -122,19 +115,11 @@ export abstract class BackgroundLoader<T extends { loaded: number; created: numb
await Promise.all(results.map(a => this.cache.update(a)));
return results;
} else {
const hookHandled = new Set<string>();
const v = await this.#system.Fetch(this.buildSub(missing), async e => {
this.#log("Callback handled %o", e);
for (const pe of e) {
const m = this.onEvent(pe);
if (m) {
await this.cache.update(m);
hookHandled.add(pe.id);
}
}
});
const v = await this.#system.Fetch(this.buildSub(missing));
this.#log("Got data", v);
return removeUndefined(v.map(this.onEvent));
const results = removeUndefined(v.map(this.onEvent));
await Promise.all(results.map(a => this.cache.update(a)));
return results;
}
}
}