perf(api,frontend): index hot queries, paginate chats, fix realtime
This commit is contained in:
1 parent
9d767d2531
commit
525ce024bc
27 files changed
+831
-268
No files matched your search
@@ -6,6 +6,8 @@ const RETRY_DELAY = 2500;
|
||||
|
||||
export type AvatarKind = "peer" | "chat";
|
||||
|
||||
const MAX_CACHED = 240;
|
||||
|
||||
const ready = new Map<string, string>();
|
||||
const missing = new Set<string>();
|
||||
const inflight = new Map<string, Promise<string | null>>();
|
||||
@@ -14,6 +16,21 @@ function cacheKey(account: number, kind: AvatarKind, id: number): string {
|
||||
return `${account}:${kind}:${id}`;
|
||||
}
|
||||
|
||||
function remember(key: string, url: string) {
|
||||
ready.set(key, url);
|
||||
while (ready.size > MAX_CACHED) {
|
||||
const oldest = ready.keys().next();
|
||||
if (oldest.done) {
|
||||
return;
|
||||
}
|
||||
const stale = ready.get(oldest.value);
|
||||
ready.delete(oldest.value);
|
||||
if (stale) {
|
||||
URL.revokeObjectURL(stale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function authHeaders(): Record<string, string> {
|
||||
return auth.token ? { Authorization: `Bearer ${auth.token}` } : {};
|
||||
}
|
||||
@@ -35,7 +52,7 @@ async function fetchAvatar(
|
||||
const response = await fetch(url, { headers: authHeaders() });
|
||||
if (response.ok) {
|
||||
const objectUrl = URL.createObjectURL(await response.blob());
|
||||
ready.set(key, objectUrl);
|
||||
remember(key, objectUrl);
|
||||
return objectUrl;
|
||||
}
|
||||
if (response.status === 409 && retry) {
|
||||
@@ -85,7 +102,7 @@ async function fetchVariant(
|
||||
const response = await fetch(url, { headers: authHeaders() });
|
||||
if (response.ok) {
|
||||
const objectUrl = URL.createObjectURL(await response.blob());
|
||||
ready.set(key, objectUrl);
|
||||
remember(key, objectUrl);
|
||||
return objectUrl;
|
||||
}
|
||||
if (response.status === 409 && retry) {
|
||||
|
||||
@@ -99,10 +99,19 @@ export function logoutAccount(accountId: number): Promise<void> {
|
||||
return request<void>(`/accounts/${accountId}`, { method: "DELETE" });
|
||||
}
|
||||
|
||||
export function listChats(page: Page = {}): Promise<Chat[]> {
|
||||
interface ChatPage extends Page {
|
||||
folder_id?: number;
|
||||
search?: string;
|
||||
}
|
||||
|
||||
export function listChats(page: ChatPage = {}): Promise<Chat[]> {
|
||||
return request<Chat[]>("/chats", { account: true, query: { ...page } });
|
||||
}
|
||||
|
||||
export function getChat(chatId: number): Promise<Chat | null> {
|
||||
return request<Chat | null>(`/chats/${chatId}`, { account: true });
|
||||
}
|
||||
|
||||
export function listFolders(): Promise<Folder[]> {
|
||||
return request<Folder[]>("/folders", { account: true });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user