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
@@ -1,30 +1,22 @@
|
||||
import { discoverPeers, searchMessages } from "$lib/api/endpoints";
|
||||
import type { DiscoverItem, SearchHit } from "$lib/api/types";
|
||||
import { chats } from "$lib/stores/chats.svelte";
|
||||
import { discoverPeers, listChats, searchMessages } from "$lib/api/endpoints";
|
||||
import type { Chat, DiscoverItem, SearchHit } from "$lib/api/types";
|
||||
|
||||
const DEBOUNCE_MS = 250;
|
||||
const REMOTE_DEBOUNCE_MS = 700;
|
||||
const MIN_LENGTH = 1;
|
||||
const CHAT_LIMIT = 30;
|
||||
|
||||
function createSearch() {
|
||||
let active = $state(false);
|
||||
let query = $state("");
|
||||
let messageHits = $state<SearchHit[]>([]);
|
||||
let chatHits = $state<Chat[]>([]);
|
||||
let peerResults = $state<DiscoverItem[]>([]);
|
||||
let loading = $state(false);
|
||||
let timers: ReturnType<typeof setTimeout>[] = [];
|
||||
let seq = 0;
|
||||
|
||||
const trimmed = $derived(query.trim());
|
||||
const chatHits = $derived.by(() => {
|
||||
const needle = trimmed.toLowerCase();
|
||||
if (needle.length < MIN_LENGTH) {
|
||||
return [];
|
||||
}
|
||||
return chats.list.filter((chat) =>
|
||||
(chat.title ?? "").toLowerCase().includes(needle)
|
||||
);
|
||||
});
|
||||
const peerHits = $derived.by(() => {
|
||||
const shown = new Set(chatHits.map((chat) => chat.chat_id));
|
||||
return peerResults.filter((item) => !shown.has(item.chat_id));
|
||||
@@ -47,6 +39,19 @@ function createSearch() {
|
||||
}
|
||||
}
|
||||
|
||||
async function runChats(value: string, current: number) {
|
||||
try {
|
||||
const found = await listChats({ limit: CHAT_LIMIT, search: value });
|
||||
if (current === seq) {
|
||||
chatHits = found;
|
||||
}
|
||||
} catch {
|
||||
if (current === seq) {
|
||||
chatHits = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function runPeers(value: string, current: number, remote: boolean) {
|
||||
try {
|
||||
const items = await discoverPeers(value, remote);
|
||||
@@ -73,6 +78,7 @@ function createSearch() {
|
||||
const current = ++seq;
|
||||
if (value.length < MIN_LENGTH) {
|
||||
messageHits = [];
|
||||
chatHits = [];
|
||||
peerResults = [];
|
||||
loading = false;
|
||||
return;
|
||||
@@ -81,6 +87,7 @@ function createSearch() {
|
||||
timers.push(
|
||||
setTimeout(() => {
|
||||
runMessages(value, current).catch(() => undefined);
|
||||
runChats(value, current).catch(() => undefined);
|
||||
runPeers(value, current, false).catch(() => undefined);
|
||||
}, DEBOUNCE_MS),
|
||||
setTimeout(() => {
|
||||
@@ -122,6 +129,7 @@ function createSearch() {
|
||||
active = false;
|
||||
query = "";
|
||||
messageHits = [];
|
||||
chatHits = [];
|
||||
peerResults = [];
|
||||
loading = false;
|
||||
seq++;
|
||||
|
||||
Reference in new issue
Block a user