Skip to content

Commit d745151

Browse files
committed
🎈 perf: 优化歌词并发限制
1 parent 0aae10e commit d745151

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

components.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,15 @@ declare module 'vue' {
6161
NDrawerContent: typeof import('naive-ui')['NDrawerContent']
6262
NDropdown: typeof import('naive-ui')['NDropdown']
6363
NDynamicTags: typeof import('naive-ui')['NDynamicTags']
64-
NEllipsis: typeof import('naive-ui')['NEllipsis']
6564
NEmpty: typeof import('naive-ui')['NEmpty']
6665
NFlex: typeof import('naive-ui')['NFlex']
67-
NFloatButton: typeof import('naive-ui')['NFloatButton']
68-
NFloatButtonGroup: typeof import('naive-ui')['NFloatButtonGroup']
6966
NForm: typeof import('naive-ui')['NForm']
7067
NFormItem: typeof import('naive-ui')['NFormItem']
7168
NFormItemGi: typeof import('naive-ui')['NFormItemGi']
7269
NGi: typeof import('naive-ui')['NGi']
7370
NGlobalStyle: typeof import('naive-ui')['NGlobalStyle']
7471
NGrid: typeof import('naive-ui')['NGrid']
7572
NH1: typeof import('naive-ui')['NH1']
76-
NH2: typeof import('naive-ui')['NH2']
7773
NH3: typeof import('naive-ui')['NH3']
7874
NIcon: typeof import('naive-ui')['NIcon']
7975
NImage: typeof import('naive-ui')['NImage']
@@ -104,7 +100,6 @@ declare module 'vue' {
104100
NSelect: typeof import('naive-ui')['NSelect']
105101
NSkeleton: typeof import('naive-ui')['NSkeleton']
106102
NSlider: typeof import('naive-ui')['NSlider']
107-
NSpin: typeof import('naive-ui')['NSpin']
108103
NSwitch: typeof import('naive-ui')['NSwitch']
109104
NTabPane: typeof import('naive-ui')['NTabPane']
110105
NTabs: typeof import('naive-ui')['NTabs']

src/components/Player/FullPlayer.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ onBeforeUnmount(() => {
268268
}
269269
}
270270
}
271+
.content-right {
272+
opacity: 0;
273+
}
271274
}
272275
// 无歌词
273276
&.no-lrc {

src/components/Player/MainLyric.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ onBeforeUnmount(() => {
383383
top: 0;
384384
transform: none;
385385
will-change: -webkit-mask-position-x, transform, opacity;
386+
// padding: 2px 8px;
387+
// margin: -2px -8px;
386388
mask-image: linear-gradient(
387389
to right,
388390
rgb(0, 0, 0) 45.4545454545%,
@@ -399,7 +401,7 @@ onBeforeUnmount(() => {
399401
-webkit-mask-repeat: no-repeat;
400402
transition:
401403
opacity 0.3s,
402-
filter 0.5s,
404+
filter 0.3s,
403405
margin 0.3s,
404406
padding 0.3s !important;
405407
}

src/utils/player-utils/lyric.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { parseTTML } from "@applemusic-like-lyrics/lyric";
55
import { LyricLine } from "@applemusic-like-lyrics/core";
66
import { LyricType } from "@/types/main";
77

8+
// 歌词加载并发保护:始终只允许最新一次请求写入
9+
let lyricSessionCounter = 0;
10+
const newLyricSession = () => ++lyricSessionCounter;
11+
const isStale = (sid: number) => sid !== lyricSessionCounter;
12+
813
/**
914
* 获取歌词
1015
* @param id 歌曲id
@@ -15,6 +20,8 @@ export const getLyricData = async (id: number) => {
1520
const statusStore = useStatusStore();
1621
// 切歌或重新获取时,先标记为加载中
1722
statusStore.lyricLoading = true;
23+
// 为本次歌词请求创建会话 ID,用于防止旧请求覆盖新结果
24+
const currentSessionId = newLyricSession();
1825

1926
if (!id) {
2027
statusStore.usingTTMLLyric = false;
@@ -31,6 +38,8 @@ export const getLyricData = async (id: number) => {
3138
const ttmlPromise = settingStore.enableTTMLLyric ? getLyric("ttml", songLyricTTML) : null;
3239

3340
const { lyric: lyricRes, isLocal: lyricLocal } = await lrcPromise;
41+
// 如果已经发起了新的歌词请求,直接放弃旧结果
42+
if (isStale(currentSessionId)) return;
3443
parsedLyricsData(lyricRes, lyricLocal && !settingStore.enableExcludeLocalLyrics);
3544
// LRC 到达后即可认为加载完成
3645
statusStore.lyricLoading = false;
@@ -40,11 +49,15 @@ export const getLyricData = async (id: number) => {
4049
statusStore.usingTTMLLyric = false;
4150
void ttmlPromise
4251
.then(({ lyric: ttmlContent, isLocal: ttmlLocal }) => {
52+
// 如果已经发起了新的歌词请求,直接放弃旧结果
53+
if (isStale(currentSessionId)) return;
4354
if (!ttmlContent) {
4455
statusStore.usingTTMLLyric = false;
4556
return;
4657
}
4758
const parsedResult = parseTTML(ttmlContent);
59+
// 如果已经发起了新的歌词请求,直接放弃旧结果
60+
if (isStale(currentSessionId)) return;
4861
if (!parsedResult?.lines?.length) {
4962
statusStore.usingTTMLLyric = false;
5063
return;
@@ -65,6 +78,7 @@ export const getLyricData = async (id: number) => {
6578
updates.yrcData = ttmlYrcLyric;
6679
console.log("✅ TTML Yrc lyrics success");
6780
}
81+
if (isStale(currentSessionId)) return;
6882
if (Object.keys(updates).length) {
6983
musicStore.setSongLyric(updates);
7084
statusStore.usingTTMLLyric = true;
@@ -74,18 +88,25 @@ export const getLyricData = async (id: number) => {
7488
})
7589
.catch((err) => {
7690
console.error("❌ Error loading TTML lyrics:", err);
77-
statusStore.usingTTMLLyric = false;
91+
// 旧请求错误不影响当前状态
92+
if (!isStale(currentSessionId)) {
93+
statusStore.usingTTMLLyric = false;
94+
}
7895
});
7996
} else {
8097
statusStore.usingTTMLLyric = false;
8198
}
8299

83-
console.log("Lyrics: ", musicStore.songLyric);
100+
// 如果已经发起了新的歌词请求,跳过日志输出以避免混淆
101+
if (!isStale(currentSessionId)) console.log("Lyrics: ", musicStore.songLyric);
84102
} catch (error) {
85103
console.error("❌ Error loading lyrics:", error);
86-
statusStore.usingTTMLLyric = false;
87-
resetSongLyric();
88-
statusStore.lyricLoading = false;
104+
// 旧请求错误不影响当前最新状态
105+
if (!isStale(currentSessionId)) {
106+
statusStore.usingTTMLLyric = false;
107+
resetSongLyric();
108+
statusStore.lyricLoading = false;
109+
}
89110
}
90111
};
91112

0 commit comments

Comments
 (0)