From 8498884cb4205ae0a29586c10d6ade72ae7552da Mon Sep 17 00:00:00 2001 From: yml2213 Date: Mon, 29 Jun 2026 17:26:54 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20SSE=20=E5=AE=9E=E6=97=B6=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E9=A9=B1=E5=8A=A8=E6=9C=AA=E8=AF=BB=E6=95=B0=E9=87=8F?= =?UTF-8?q?=E5=88=B7=E6=96=B0=EF=BC=8C=E8=BD=AE=E8=AF=A2=E9=97=B4=E9=9A=94?= =?UTF-8?q?=E4=BB=8E=2030s=20=E9=99=8D=E8=87=B3=205min?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chats/composables/useChatUnreadCount.ts | 18 +++++++++++++++--- frontend/src/features/chats/views/ChatView.vue | 1 + .../src/features/chats/views/MessagesView.vue | 1 + .../features/chats/views/MobileChatView.vue | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/frontend/src/features/chats/composables/useChatUnreadCount.ts b/frontend/src/features/chats/composables/useChatUnreadCount.ts index fb5fa11..fa4c8d1 100644 --- a/frontend/src/features/chats/composables/useChatUnreadCount.ts +++ b/frontend/src/features/chats/composables/useChatUnreadCount.ts @@ -4,6 +4,9 @@ import type { RouteLocationNormalizedLoaded } from 'vue-router' import { fetchUnreadChatCount } from '@/features/chats/api/chats' import { useSessionStore } from '@/stores/session' +const pollingInterval = 300_000 +const unreadChangedEvent = 'chat-unread-changed' + export function useChatUnreadCount(route: RouteLocationNormalizedLoaded) { const session = useSessionStore() const unreadCount = ref(0) @@ -32,14 +35,19 @@ export function useChatUnreadCount(route: RouteLocationNormalizedLoaded) { unreadCount.value = count } } catch { - // 底部导航不弹错误提示,保持上一次未读数即可。 + // 不弹错误提示,保持上一次未读数即可。 } } + function onUnreadChanged() { + void loadUnreadCount() + } + function startPolling() { stopPolling() void loadUnreadCount() - timer = window.setInterval(loadUnreadCount, 30_000) + timer = window.setInterval(loadUnreadCount, pollingInterval) + window.addEventListener(unreadChangedEvent, onUnreadChanged) } onMounted(() => { @@ -48,7 +56,10 @@ export function useChatUnreadCount(route: RouteLocationNormalizedLoaded) { } }) - onBeforeUnmount(stopPolling) + onBeforeUnmount(() => { + stopPolling() + window.removeEventListener(unreadChangedEvent, onUnreadChanged) + }) watch( () => session.isLoggedIn, @@ -58,6 +69,7 @@ export function useChatUnreadCount(route: RouteLocationNormalizedLoaded) { return } stopPolling() + window.removeEventListener(unreadChangedEvent, onUnreadChanged) unreadCount.value = 0 } ) diff --git a/frontend/src/features/chats/views/ChatView.vue b/frontend/src/features/chats/views/ChatView.vue index 3f3e46a..f795876 100644 --- a/frontend/src/features/chats/views/ChatView.vue +++ b/frontend/src/features/chats/views/ChatView.vue @@ -48,6 +48,7 @@ const desktopNotification = useDesktopNotification('user') function handleSSEEvent(event: ChatEvent) { if (event.type === 'new_message') { + window.dispatchEvent(new Event('chat-unread-changed')) const msg = event.message if (msg) { const isSelf = msg.sender_type === 'user' && msg.sender_id === currentUserId diff --git a/frontend/src/features/chats/views/MessagesView.vue b/frontend/src/features/chats/views/MessagesView.vue index a7479a4..9f0aa6f 100644 --- a/frontend/src/features/chats/views/MessagesView.vue +++ b/frontend/src/features/chats/views/MessagesView.vue @@ -81,6 +81,7 @@ async function loadChats(isRefresh = false, showLoading = true) { function handleSSEEvent(event: ChatEvent) { if (event.type === 'new_message' || event.type === 'conversation_updated') { loadChats(true, false) + window.dispatchEvent(new Event('chat-unread-changed')) } if (event.type === 'new_message' && event.message) { const isSelf = event.message.sender_type === 'user' && event.message.sender_id === currentUserId diff --git a/frontend/src/features/chats/views/MobileChatView.vue b/frontend/src/features/chats/views/MobileChatView.vue index 8bb5854..038358c 100644 --- a/frontend/src/features/chats/views/MobileChatView.vue +++ b/frontend/src/features/chats/views/MobileChatView.vue @@ -44,6 +44,7 @@ const desktopNotification = useDesktopNotification('user') function handleSSEEvent(event: ChatEvent) { if (event.type === 'new_message') { + window.dispatchEvent(new Event('chat-unread-changed')) const msg = event.message if (msg) { const isSelf = msg.sender_type === 'user' && msg.sender_id === currentUserId