perf(player): batch addAll/playAllNext to avoid cascading reactivity

addAll() now collects all new tracks and pushes them in a single
array operation instead of calling add() in a loop, which triggered
per-item reactive updates and O(n²) existence checks.
This commit is contained in:
2026-02-11 22:56:00 -08:00
parent 7e3a22f14b
commit da3ab81ae6

View File

@@ -169,8 +169,26 @@ class PlayerStore {
} }
addAll(tracks: Track[]) { addAll(tracks: Track[]) {
// Batch: collect new tracks, push all at once
const newTracks: Track[] = [];
for (const track of tracks) { for (const track of tracks) {
this.add(track); // Check existence inline to avoid O(n) per-track via add()
if (!this.queue.some((t) => t.id === track.id)) {
newTracks.push(track);
}
}
if (newTracks.length === 0) return;
const startIdx = this.queue.length;
this.queue.push(...newTracks);
if (this.isShuffled) {
const newIndices = newTracks.map((_, i) => startIdx + i);
this.shuffledIndices.push(...newIndices);
}
if (startIdx === 0 && !this.currentId) {
this.currentId = newTracks[0].id;
} }
} }