perf(*): bound list queries, batch fetches, harden media downloads

This commit is contained in:
hh committed 2026-09-01 21:38:22 +02:00
1 parent 9c12628304
commit 1fa4f18a9b
35 files changed
+473 -138

No files matched your search

+23 -2
View File
@@ -102,9 +102,30 @@ export function visualKind(kind: string): VisualKind {
return "other";
}
const MAX_CACHED = 200;
const ready = new Map<string, InlineMedia>();
const inflight = new Map<string, Promise<InlineMedia>>();
function remember(
cache: Map<string, InlineMedia>,
key: string,
item: InlineMedia
) {
cache.set(key, item);
while (cache.size > MAX_CACHED) {
const oldest = cache.keys().next();
if (oldest.done) {
return;
}
const stale = cache.get(oldest.value);
cache.delete(oldest.value);
if (stale?.state === "ready") {
URL.revokeObjectURL(stale.url);
}
}
}
function cacheKey(account: number, chatId: number, messageId: number): string {
return `${account}:${chatId}:${messageId}`;
}
@@ -183,7 +204,7 @@ export function loadMediaItem(media: MediaRef): Promise<InlineMedia> {
const promise = resolveById(media)
.then((result) => {
if (result.state === "ready") {
byId.set(key, result);
remember(byId, key, result);
}
return result;
})
@@ -251,7 +272,7 @@ export function loadInlineMedia(
const promise = resolve(chatId, messageId)
.then((result) => {
if (result.state === "ready") {
ready.set(key, result);
remember(ready, key, result);
}
return result;
})