Skip to content

Commit 34e8d52

Browse files
committed
feat: 优化数据加载逻辑,添加延迟执行以提高触发成功率 #190
1 parent d566b13 commit 34e8d52

6 files changed

Lines changed: 77 additions & 18 deletions

File tree

src/contentScripts/views/App.vue

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const handleThrottledPageRefresh = useThrottleFn(() => {
9090
setTimeout(checkScrollComplete, 100)
9191
}
9292
}, 500)
93-
const handleThrottledReachBottom = useThrottleFn(() => handleReachBottom.value?.(), 500)
93+
const handleThrottledReachBottom = useThrottleFn(() => handleReachBottom.value?.(), 200)
9494
const handleThrottledBackToTop = useThrottleFn(() => handleBackToTop(), 500)
9595
const handleThrottledPageUnRefresh = useThrottleFn(() => handleUndoRefresh.value?.(), 500)
9696
const handleThrottledPageForwardRefresh = useThrottleFn(() => handleForwardRefresh.value?.(), 500)
@@ -359,6 +359,9 @@ function handleBackToTop(targetScrollTop = 0 as number) {
359359
iframePageRef.value?.handleBackToTop()
360360
}
361361
362+
// 添加滚动结束检测
363+
let scrollEndTimer: ReturnType<typeof setTimeout> | null = null
364+
362365
function handleOsScroll() {
363366
emitter.emit(OVERLAY_SCROLL_BAR_SCROLL)
364367
@@ -373,8 +376,32 @@ function handleOsScroll() {
373376
reachTop.value = false
374377
}
375378
376-
if (clientHeight + scrollTop >= scrollHeight - 300)
379+
// 优化滚动检测:降低阈值,提高敏感度
380+
const threshold = Math.min(200, clientHeight * 0.2) // 动态阈值,最大200px或20%屏幕高度
381+
if (clientHeight + scrollTop >= scrollHeight - threshold) {
377382
handleThrottledReachBottom()
383+
}
384+
385+
// 清除之前的滚动结束定时器
386+
if (scrollEndTimer) {
387+
clearTimeout(scrollEndTimer)
388+
}
389+
390+
// 设置滚动结束检测,在滚动停止150ms后再检查一次
391+
scrollEndTimer = setTimeout(() => {
392+
const osInstance = scrollbarRef.value?.osInstance()
393+
if (!osInstance)
394+
return
395+
396+
const { viewport } = osInstance.elements()
397+
const { scrollTop, scrollHeight, clientHeight } = viewport
398+
const threshold = Math.min(200, clientHeight * 0.2)
399+
400+
// 滚动结束后的最终检测,使用更大的阈值确保不遗漏
401+
if (clientHeight + scrollTop >= scrollHeight - threshold * 1.5) {
402+
handleReachBottom.value?.()
403+
}
404+
}, 150)
378405
379406
if (isHomePage())
380407
topBarRef.value?.handleScroll()

src/contentScripts/views/Favorites/Favorites.vue

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,18 @@ function initPageAction() {
7878
if (noMoreContent.value)
7979
return
8080
81-
if (searchScope.value === 'all') {
82-
const firstCategoryId = favoriteCategories.length > 0 ? favoriteCategories[0].id : 0
83-
await getFavoriteResources(firstCategoryId, ++currentPageNum.value, keyword.value, 1)
84-
}
85-
else {
86-
await getFavoriteResources(selectedCategory.value!.id, ++currentPageNum.value, keyword.value, 0)
87-
}
81+
// 优化:添加延迟执行提高触发成功率
82+
setTimeout(() => {
83+
if (!isLoading.value && !noMoreContent.value) {
84+
if (searchScope.value === 'all') {
85+
const firstCategoryId = favoriteCategories.length > 0 ? favoriteCategories[0].id : 0
86+
getFavoriteResources(firstCategoryId, ++currentPageNum.value, keyword.value, 1)
87+
}
88+
else {
89+
getFavoriteResources(selectedCategory.value!.id, ++currentPageNum.value, keyword.value, 0)
90+
}
91+
}
92+
}, 50)
8893
}
8994
9095
handlePageRefresh.value = () => {

src/contentScripts/views/History/History.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ function initPageAction() {
4040
if (noMoreContent.value)
4141
return
4242
43-
if (keyword.value)
44-
searchHistoryList()
45-
else
46-
getHistoryList()
43+
// 优化:添加延迟执行提高触发成功率
44+
setTimeout(() => {
45+
if (!isLoading.value && !noMoreContent.value) {
46+
if (keyword.value)
47+
searchHistoryList()
48+
else
49+
getHistoryList()
50+
}
51+
}, 50)
4752
}
4853
4954
handlePageRefresh.value = () => {

src/contentScripts/views/Home/components/Following.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ function initPageAction() {
105105
if (noMoreContent.value)
106106
return
107107
108-
getData()
108+
// 优化:添加延迟执行而不是直接阻止
109+
setTimeout(() => {
110+
if (!isLoading.value && !noMoreContent.value) {
111+
getData()
112+
}
113+
}, 50) // 短暂延迟确保滚动结束
109114
}
110115
handlePageRefresh.value = async () => {
111116
if (isLoading.value)

src/contentScripts/views/Home/components/ForYou.vue

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ const hasBackState = ref<boolean>(false)
132132
const hasForwardState = ref<boolean>(false)
133133
134134
// 添加请求限制相关的变量
135-
const requestThrottleTime = 300 // 请求间隔时间(毫秒)
135+
const requestThrottleTime = 150 // 请求间隔时间(毫秒) - 从300ms减少到150ms
136136
const lastRequestTime = ref<number>(0)
137137
const PAGE_SIZE = 30
138138
@@ -270,9 +270,17 @@ function initPageAction() {
270270
if (isFilterBlocked.value)
271271
return
272272
273-
// 检查频率限制
273+
// 优化频率限制:允许滚动结束后的立即检查
274274
const now = Date.now()
275-
if (now - lastRequestTime.value < requestThrottleTime) {
275+
const timeSinceLastRequest = now - lastRequestTime.value
276+
if (timeSinceLastRequest < requestThrottleTime) {
277+
// 如果距离上次请求时间很短,延迟执行而不是直接返回
278+
const remainingTime = requestThrottleTime - timeSinceLastRequest
279+
setTimeout(() => {
280+
if (!isLoading.value && !noMoreContent.value && !isFilterBlocked.value) {
281+
getData()
282+
}
283+
}, remainingTime)
276284
return
277285
}
278286

src/contentScripts/views/WatchLater/WatchLater.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,16 @@ function initPageAction() {
4949
}
5050
5151
handleReachBottom.value = async () => {
52-
getData()
52+
if (isLoading.value || noMoreContent.value) {
53+
return
54+
}
55+
56+
// 优化:添加延迟执行提高触发成功率
57+
setTimeout(() => {
58+
if (!isLoading.value && !noMoreContent.value) {
59+
getData()
60+
}
61+
}, 50)
5362
}
5463
}
5564

0 commit comments

Comments
 (0)