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:
@@ -169,8 +169,26 @@ class PlayerStore {
|
||||
}
|
||||
|
||||
addAll(tracks: Track[]) {
|
||||
// Batch: collect new tracks, push all at once
|
||||
const newTracks: Track[] = [];
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user