@@ -5,6 +5,11 @@ import { parseTTML } from "@applemusic-like-lyrics/lyric";
55import { LyricLine } from "@applemusic-like-lyrics/core" ;
66import { 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