You've already forked bilibili-subtitle
优化性能
This commit is contained in:
@@ -113,19 +113,22 @@ const useSubtitleService = () => {
|
|||||||
|
|
||||||
// 更新当前位置
|
// 更新当前位置
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let curIdx
|
let newCurIdx
|
||||||
if (((data?.body) != null) && currentTime) {
|
if (((data?.body) != null) && currentTime) {
|
||||||
for (let i=0; i<data.body.length; i++) {
|
for (let i=0; i<data.body.length; i++) {
|
||||||
const item = data.body[i]
|
const item = data.body[i]
|
||||||
if (item.from && currentTime < item.from) {
|
if (item.from && currentTime < item.from) {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
curIdx = i
|
newCurIdx = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dispatch(setCurIdx(curIdx))
|
// 只有当索引发生变化时才更新状态
|
||||||
}, [currentTime, data?.body, dispatch])
|
if (newCurIdx !== curIdx) {
|
||||||
|
dispatch(setCurIdx(newCurIdx))
|
||||||
|
}
|
||||||
|
}, [currentTime, data?.body, dispatch, curIdx])
|
||||||
|
|
||||||
// 需要滚动 => segment自动展开
|
// 需要滚动 => segment自动展开
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -193,7 +196,10 @@ const useSubtitleService = () => {
|
|||||||
// 每0.5秒更新当前视频时间
|
// 每0.5秒更新当前视频时间
|
||||||
useInterval(() => {
|
useInterval(() => {
|
||||||
sendInject(null, 'GET_VIDEO_STATUS', {}).then(status => {
|
sendInject(null, 'GET_VIDEO_STATUS', {}).then(status => {
|
||||||
|
// 只有当时间发生显著变化时才更新状态(差异大于0.1秒),避免不必要的重新渲染
|
||||||
|
if (currentTime == null || Math.abs(status.currentTime - currentTime) > 0.1) {
|
||||||
dispatch(setCurrentTime(status.currentTime))
|
dispatch(setCurrentTime(status.currentTime))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}, 500)
|
}, 500)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user