From 233731b80b3aa1c90ad8e5281a20cc9fa8ad9bff Mon Sep 17 00:00:00 2001 From: 2977094657 <2977094657@qq.com> Date: Sat, 24 Jan 2026 18:47:29 +0800 Subject: [PATCH] =?UTF-8?q?improvement(chat):=20realtime=20=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E5=8E=BB=E6=8A=96=E5=B9=B6=E7=BB=95=E8=BF=87=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E5=85=A8=E9=87=8F=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - realtime 模式拉取消息时传 source=realtime,直接从 WCDB 读取 - SSE change 事件增加 500ms debounce,减少频繁刷新/请求抖动 - 停止 realtime 时清理 debounce timer --- frontend/pages/chat/[[username]].vue | 32 ++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/frontend/pages/chat/[[username]].vue b/frontend/pages/chat/[[username]].vue index 48ed479..b2348ab 100644 --- a/frontend/pages/chat/[[username]].vue +++ b/frontend/pages/chat/[[username]].vue @@ -1894,6 +1894,7 @@ let realtimeSessionsRefreshQueued = false let realtimeFullSyncFuture = null let realtimeFullSyncQueued = false let realtimeFullSyncPriority = '' +let realtimeChangeDebounceTimer = null const allMessages = ref({}) @@ -4644,9 +4645,9 @@ const loadMessages = async ({ username, reset }) => { if (messageTypeFilter.value && messageTypeFilter.value !== 'all') { params.render_types = messageTypeFilter.value } - - if (reset) { - await queueRealtimeFullSync(username) + if (realtimeEnabled.value) { + // In realtime mode, read directly from WCDB to avoid blocking on background sync. + params.source = 'realtime' } const resp = await api.listChatMessages(params) @@ -4747,6 +4748,12 @@ const stopRealtimeStream = () => { } catch {} realtimeEventSource = null } + if (realtimeChangeDebounceTimer) { + try { + clearTimeout(realtimeChangeDebounceTimer) + } catch {} + realtimeChangeDebounceTimer = null + } } const refreshRealtimeIncremental = async () => { @@ -4774,8 +4781,8 @@ const refreshRealtimeIncremental = async () => { if (messageTypeFilter.value && messageTypeFilter.value !== 'all') { params.render_types = messageTypeFilter.value } + params.source = 'realtime' - await queueRealtimeFullSync(username) const resp = await api.listChatMessages(params) if (selectedContact.value?.username !== username) return @@ -4820,6 +4827,19 @@ const queueRealtimeRefresh = () => { }) } +const queueRealtimeChange = () => { + if (!process.client || typeof window === 'undefined') return + if (!realtimeEnabled.value) return + if (realtimeChangeDebounceTimer) return + + // Debounce noisy db_storage change events to avoid hammering the backend. + realtimeChangeDebounceTimer = setTimeout(() => { + realtimeChangeDebounceTimer = null + queueRealtimeRefresh() + queueRealtimeSessionsRefresh() + }, 500) +} + const startRealtimeStream = () => { stopRealtimeStream() if (!process.client || typeof window === 'undefined') return @@ -4840,9 +4860,7 @@ const startRealtimeStream = () => { try { const data = JSON.parse(String(ev.data || '{}')) if (String(data?.type || '') === 'change') { - queueRealtimeFullSync(selectedContact.value?.username || '') - queueRealtimeRefresh() - queueRealtimeSessionsRefresh() + queueRealtimeChange() } } catch {} }