Skip to content

Commit 76e9c5b

Browse files
committed
fix: review issues
1 parent 26906d8 commit 76e9c5b

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/renderer/src/composables/message/useMessageScroll.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref, reactive, readonly, onUnmounted, nextTick, type Ref } from 'vue'
1+
import { ref, reactive, readonly, onBeforeUnmount, nextTick, type Ref } from 'vue'
22
import { useDebounceFn } from '@vueuse/core'
33
import type { ScrollInfo } from './types'
44
import type { DynamicScroller } from 'vue-virtual-scroller'
@@ -30,6 +30,8 @@ export function useMessageScroll(options?: UseMessageScrollOptions) {
3030
let intersectionObserver: IntersectionObserver | null = null
3131
let scrollRetryTimer: number | null = null
3232
let scrollRetryToken = 0
33+
let bottomScrollRetryTimer: number | null = null
34+
let bottomScrollCancelToken = 0
3335
let pendingScrollTargetId: string | null = null
3436

3537
const updateScrollInfoImmediate = () => {
@@ -63,6 +65,12 @@ export function useMessageScroll(options?: UseMessageScrollOptions) {
6365
* Schedule scroll to bottom with retry mechanism for virtual scroller
6466
*/
6567
const scheduleScrollToBottom = (force = false) => {
68+
if (bottomScrollRetryTimer) {
69+
clearTimeout(bottomScrollRetryTimer)
70+
bottomScrollRetryTimer = null
71+
}
72+
const currentBottomToken = ++bottomScrollCancelToken
73+
6674
nextTick(() => {
6775
const shouldAutoFollow = options?.shouldAutoFollow
6876
if (force && shouldAutoFollow) {
@@ -83,10 +91,14 @@ export function useMessageScroll(options?: UseMessageScrollOptions) {
8391
let lastScrollHeight = 0
8492

8593
const attemptScrollToBottom = () => {
94+
if (currentBottomToken !== bottomScrollCancelToken) return
8695
scroller.scrollToBottom()
8796

8897
nextTick(() => {
89-
setTimeout(() => {
98+
bottomScrollRetryTimer = window.setTimeout(() => {
99+
bottomScrollRetryTimer = null
100+
if (currentBottomToken !== bottomScrollCancelToken) return
101+
90102
const container = messagesContainer.value
91103
if (!container) {
92104
updateScrollInfo()
@@ -268,7 +280,7 @@ export function useMessageScroll(options?: UseMessageScrollOptions) {
268280
updateScrollInfoImmediate()
269281
}
270282

271-
onUnmounted(() => {
283+
onBeforeUnmount(() => {
272284
if (intersectionObserver) {
273285
intersectionObserver.disconnect()
274286
intersectionObserver = null
@@ -279,6 +291,12 @@ export function useMessageScroll(options?: UseMessageScrollOptions) {
279291
scrollRetryTimer = null
280292
}
281293

294+
if (bottomScrollRetryTimer) {
295+
clearTimeout(bottomScrollRetryTimer)
296+
bottomScrollRetryTimer = null
297+
}
298+
bottomScrollCancelToken++
299+
282300
pendingScrollTargetId = null
283301
})
284302

src/renderer/src/stores/chat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,14 +1125,14 @@ export const useChatStore = defineStore('chat', () => {
11251125
if (mainMessage) {
11261126
const enrichedMainMessage = await enrichMessageWithExtra(mainMessage)
11271127
// 如果是当前激活的会话,更新显示
1128-
if (getActiveThreadId() === getActiveThreadId()) {
1128+
if (getActiveThreadId() === cached.threadId) {
11291129
cacheMessageForView(enrichedMainMessage as AssistantMessage | UserMessage)
11301130
ensureMessageId(enrichedMainMessage.id)
11311131
}
11321132
}
11331133
} else {
11341134
// 如果是当前激活的会话,更新显示
1135-
if (getActiveThreadId() === getActiveThreadId()) {
1135+
if (getActiveThreadId() === cached.threadId) {
11361136
cacheMessageForView(enrichedMessage as AssistantMessage | UserMessage)
11371137
ensureMessageId(enrichedMessage.id)
11381138
}

0 commit comments

Comments
 (0)