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
@@ -0,0 +1,42 @@
|
||||
import { listChats } from "$lib/api/endpoints";
|
||||
import type { Chat } from "$lib/api/types";
|
||||
|
||||
const DEBOUNCE_MS = 250;
|
||||
const LIMIT = 40;
|
||||
|
||||
export function createChatPicker() {
|
||||
let results = $state<Chat[]>([]);
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
let seq = 0;
|
||||
|
||||
async function run(query: string, current: number) {
|
||||
try {
|
||||
const found = await listChats({
|
||||
limit: LIMIT,
|
||||
search: query || undefined,
|
||||
});
|
||||
if (current === seq) {
|
||||
results = found;
|
||||
}
|
||||
} catch {
|
||||
if (current === seq) {
|
||||
results = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
get results(): Chat[] {
|
||||
return results;
|
||||
},
|
||||
search(query: string) {
|
||||
const current = ++seq;
|
||||
if (timer !== null) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer = setTimeout(() => {
|
||||
run(query.trim(), current).catch(() => undefined);
|
||||
}, DEBOUNCE_MS);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user