Skip to content

Commit 5205d6e

Browse files
committed
✨ feat: 优化播放列表管理,避免直接修改状态并确保唯一性
1 parent ed13243 commit 5205d6e

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/stores/data.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,18 @@ export const useDataStore = defineStore("data", {
246246
await musicDB.setItem("playList", cloneDeep(this.playList));
247247
return 0;
248248
}
249+
// 避免直接修改 state
250+
const newList = [...this.playList];
249251
// 在当前播放位置之后插入歌曲
250252
const indexAdd = index + 1;
251-
this.playList.splice(indexAdd, 0, song);
253+
newList.splice(indexAdd, 0, song);
252254
// 移除重复的歌曲(如果存在)
253-
const playList = this.playList.filter((item, idx) => idx === indexAdd || item.id !== song.id);
255+
const finalList = newList.filter((item, idx) => idx === indexAdd || item.id !== song.id);
254256
// 更新本地存储
255-
this.playList = markRaw(playList);
256-
await musicDB.setItem("playList", cloneDeep(playList));
257+
this.playList = markRaw(finalList);
258+
await musicDB.setItem("playList", cloneDeep(finalList));
257259
// 返回刚刚插入的歌曲索引
258-
return playList.indexOf(song);
260+
return finalList.indexOf(song);
259261
},
260262
/**
261263
* 设置播放历史

0 commit comments

Comments
 (0)