Skip to content

Commit ae2d7f1

Browse files
committed
improvement(chat): realtime 刷新去抖并绕过后台全量同步
- realtime 模式拉取消息时传 source=realtime,直接从 WCDB 读取 - SSE change 事件增加 500ms debounce,减少频繁刷新/请求抖动 - 停止 realtime 时清理 debounce timer
1 parent 93ad7b7 commit ae2d7f1

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

frontend/pages/chat/[[username]].vue

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,7 @@ let realtimeSessionsRefreshQueued = false
18941894
let realtimeFullSyncFuture = null
18951895
let realtimeFullSyncQueued = false
18961896
let realtimeFullSyncPriority = ''
1897+
let realtimeChangeDebounceTimer = null
18971898

18981899
const allMessages = ref({})
18991900

@@ -4644,9 +4645,9 @@ const loadMessages = async ({ username, reset }) => {
46444645
if (messageTypeFilter.value && messageTypeFilter.value !== 'all') {
46454646
params.render_types = messageTypeFilter.value
46464647
}
4647-
4648-
if (reset) {
4649-
await queueRealtimeFullSync(username)
4648+
if (realtimeEnabled.value) {
4649+
// In realtime mode, read directly from WCDB to avoid blocking on background sync.
4650+
params.source = 'realtime'
46504651
}
46514652
const resp = await api.listChatMessages(params)
46524653

@@ -4747,6 +4748,12 @@ const stopRealtimeStream = () => {
47474748
} catch {}
47484749
realtimeEventSource = null
47494750
}
4751+
if (realtimeChangeDebounceTimer) {
4752+
try {
4753+
clearTimeout(realtimeChangeDebounceTimer)
4754+
} catch {}
4755+
realtimeChangeDebounceTimer = null
4756+
}
47504757
}
47514758

47524759
const refreshRealtimeIncremental = async () => {
@@ -4774,8 +4781,8 @@ const refreshRealtimeIncremental = async () => {
47744781
if (messageTypeFilter.value && messageTypeFilter.value !== 'all') {
47754782
params.render_types = messageTypeFilter.value
47764783
}
4784+
params.source = 'realtime'
47774785

4778-
await queueRealtimeFullSync(username)
47794786
const resp = await api.listChatMessages(params)
47804787
if (selectedContact.value?.username !== username) return
47814788

@@ -4820,6 +4827,19 @@ const queueRealtimeRefresh = () => {
48204827
})
48214828
}
48224829

4830+
const queueRealtimeChange = () => {
4831+
if (!process.client || typeof window === 'undefined') return
4832+
if (!realtimeEnabled.value) return
4833+
if (realtimeChangeDebounceTimer) return
4834+
4835+
// Debounce noisy db_storage change events to avoid hammering the backend.
4836+
realtimeChangeDebounceTimer = setTimeout(() => {
4837+
realtimeChangeDebounceTimer = null
4838+
queueRealtimeRefresh()
4839+
queueRealtimeSessionsRefresh()
4840+
}, 500)
4841+
}
4842+
48234843
const startRealtimeStream = () => {
48244844
stopRealtimeStream()
48254845
if (!process.client || typeof window === 'undefined') return
@@ -4840,9 +4860,7 @@ const startRealtimeStream = () => {
48404860
try {
48414861
const data = JSON.parse(String(ev.data || '{}'))
48424862
if (String(data?.type || '') === 'change') {
4843-
queueRealtimeFullSync(selectedContact.value?.username || '')
4844-
queueRealtimeRefresh()
4845-
queueRealtimeSessionsRefresh()
4863+
queueRealtimeChange()
48464864
}
48474865
} catch {}
48484866
}

0 commit comments

Comments
 (0)