improvement(chat): realtime 刷新去抖并绕过后台全量同步

- realtime 模式拉取消息时传 source=realtime,直接从 WCDB 读取

- SSE change 事件增加 500ms debounce,减少频繁刷新/请求抖动

- 停止 realtime 时清理 debounce timer
This commit is contained in:
2977094657
2026-01-24 18:47:29 +08:00
parent 93ad7b7a1c
commit ae2d7f128d

View File

@@ -1894,6 +1894,7 @@ let realtimeSessionsRefreshQueued = false
let realtimeFullSyncFuture = null let realtimeFullSyncFuture = null
let realtimeFullSyncQueued = false let realtimeFullSyncQueued = false
let realtimeFullSyncPriority = '' let realtimeFullSyncPriority = ''
let realtimeChangeDebounceTimer = null
const allMessages = ref({}) const allMessages = ref({})
@@ -4644,9 +4645,9 @@ const loadMessages = async ({ username, reset }) => {
if (messageTypeFilter.value && messageTypeFilter.value !== 'all') { if (messageTypeFilter.value && messageTypeFilter.value !== 'all') {
params.render_types = messageTypeFilter.value params.render_types = messageTypeFilter.value
} }
if (realtimeEnabled.value) {
if (reset) { // In realtime mode, read directly from WCDB to avoid blocking on background sync.
await queueRealtimeFullSync(username) params.source = 'realtime'
} }
const resp = await api.listChatMessages(params) const resp = await api.listChatMessages(params)
@@ -4747,6 +4748,12 @@ const stopRealtimeStream = () => {
} catch {} } catch {}
realtimeEventSource = null realtimeEventSource = null
} }
if (realtimeChangeDebounceTimer) {
try {
clearTimeout(realtimeChangeDebounceTimer)
} catch {}
realtimeChangeDebounceTimer = null
}
} }
const refreshRealtimeIncremental = async () => { const refreshRealtimeIncremental = async () => {
@@ -4774,8 +4781,8 @@ const refreshRealtimeIncremental = async () => {
if (messageTypeFilter.value && messageTypeFilter.value !== 'all') { if (messageTypeFilter.value && messageTypeFilter.value !== 'all') {
params.render_types = messageTypeFilter.value params.render_types = messageTypeFilter.value
} }
params.source = 'realtime'
await queueRealtimeFullSync(username)
const resp = await api.listChatMessages(params) const resp = await api.listChatMessages(params)
if (selectedContact.value?.username !== username) return 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 = () => { const startRealtimeStream = () => {
stopRealtimeStream() stopRealtimeStream()
if (!process.client || typeof window === 'undefined') return if (!process.client || typeof window === 'undefined') return
@@ -4840,9 +4860,7 @@ const startRealtimeStream = () => {
try { try {
const data = JSON.parse(String(ev.data || '{}')) const data = JSON.parse(String(ev.data || '{}'))
if (String(data?.type || '') === 'change') { if (String(data?.type || '') === 'change') {
queueRealtimeFullSync(selectedContact.value?.username || '') queueRealtimeChange()
queueRealtimeRefresh()
queueRealtimeSessionsRefresh()
} }
} catch {} } catch {}
} }