Skip to content

Commit 9dd7f45

Browse files
committed
fix(chat): avoid voice ref render recursion
1 parent b1c2660 commit 9dd7f45

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

frontend/composables/chat/useChatMessages.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,21 @@ export const useChatMessages = ({
4747
})
4848
}
4949

50+
const summarizeRenderTypes = (list) => {
51+
const counts = {}
52+
for (const item of Array.isArray(list) ? list : []) {
53+
const key = String(item?.renderType || 'unknown').trim() || 'unknown'
54+
counts[key] = Number(counts[key] || 0) + 1
55+
}
56+
return counts
57+
}
58+
5059
const previewImageUrl = ref(null)
5160
const previewVideoUrl = ref(null)
5261
const previewVideoPosterUrl = ref('')
5362
const previewVideoError = ref('')
5463

55-
const voiceRefs = ref({})
64+
const voiceRefs = new Map()
5665
const currentPlayingVoice = ref(null)
5766
const playingVoiceId = ref(null)
5867

@@ -230,18 +239,16 @@ export const useChatMessages = ({
230239
const key = String(id || '').trim()
231240
if (!key) return
232241
if (element) {
233-
voiceRefs.value = { ...voiceRefs.value, [key]: element }
234-
} else if (voiceRefs.value[key]) {
235-
const next = { ...voiceRefs.value }
236-
delete next[key]
237-
voiceRefs.value = next
242+
voiceRefs.set(key, element)
243+
} else {
244+
voiceRefs.delete(key)
238245
}
239246
}
240247

241248
const playVoiceById = async (voiceId) => {
242249
const key = String(voiceId || '').trim()
243250
if (!key) return
244-
const audio = voiceRefs.value[key]
251+
const audio = voiceRefs.get(key)
245252
if (!audio) return
246253

247254
try {
@@ -421,7 +428,8 @@ export const useChatMessages = ({
421428
const mapped = dedupeMessagesById(raw.map(normalizeMessage))
422429
logMessagePhase('loadMessages:normalize:end', {
423430
username,
424-
mappedCount: mapped.length
431+
mappedCount: mapped.length,
432+
renderTypeCounts: summarizeRenderTypes(mapped)
425433
})
426434

427435
if (activeMessagesFor.value !== username) {
@@ -598,7 +606,18 @@ export const useChatMessages = ({
598606
} catch {}
599607
}
600608

609+
const clearVoicePlaybackState = () => {
610+
try {
611+
currentPlayingVoice.value?.pause?.()
612+
if (currentPlayingVoice.value) currentPlayingVoice.value.currentTime = 0
613+
} catch {}
614+
currentPlayingVoice.value = null
615+
playingVoiceId.value = null
616+
voiceRefs.clear()
617+
}
618+
601619
const resetMessageState = () => {
620+
clearVoicePlaybackState()
602621
allMessages.value = {}
603622
messagesMeta.value = {}
604623
messagesError.value = ''
@@ -807,6 +826,7 @@ export const useChatMessages = ({
807826
if (highlightTimer) clearTimeout(highlightTimer)
808827
highlightTimer = null
809828
clearContactProfileHoverHideTimer()
829+
clearVoicePlaybackState()
810830
})
811831

812832
return {

0 commit comments

Comments
 (0)