Skip to content

Commit

Permalink
Fix: removeTrack causes index to reset to 0 at times
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshankman committed Nov 16, 2024
1 parent e5a4e11 commit 793235d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
35 changes: 12 additions & 23 deletions gapless5.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,35 +1331,24 @@ function Gapless5(options = {}, deprecated = {}) { // eslint-disable-line no-unu
* @param {number | string} pointOrPath - audio path or playlist index
*/
this.removeTrack = (pointOrPath) => {
const point = this.playlist.indexFromTrack(pointOrPath);
if (!isValidIndex(point)) {
log.warn(`Cannot remove missing track: ${pointOrPath}`);
const index = typeof pointOrPath === 'number' ? pointOrPath : this.findTrack(pointOrPath);
if (index < 0 || index >= this.playlist.numTracks()) {
return;
}
const deletedPlaying = point === this.playlist.trackNumber;

const { source: curSource } = this.playlist.getSourceIndexed(point);
if (!curSource) {
return;
}
let wasPlaying = false;

if (curSource.state === Gapless5State.Loading) {
curSource.unload();
} else if (curSource.inPlayState(true)) {
wasPlaying = true;
curSource.stop();
// If removing a track before the current track, decrement trackNumber
if (index < this.playlist.trackNumber) {
this.playlist.trackNumber--;
}

this.playlist.remove(point);

if (deletedPlaying) {
this.next(); // Don't stop after a delete
if (wasPlaying) {
this.play();
}
// If removing the current track or a track after it, but trackNumber would exceed new length
else if (this.playlist.trackNumber >= this.playlist.numTracks() - 1) {
this.playlist.trackNumber = Math.max(0, this.playlist.numTracks() - 2);
}
// Otherwise keep the same trackNumber

this.playlist.sources[index].unload();
this.playlist.sources.splice(index, 1);
this.playlist.shuffledIndices = [];
this.uiDirty = true;
};

Expand Down
2 changes: 1 addition & 1 deletion types/gapless5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ declare class Gapless5Source {
isPlayActive: (checkStarting: any) => boolean;
getPosition: () => number;
getLength: () => number;
play: (syncPosition: any) => void;
play: (syncPosition: any, skipCallback: any) => void;
setPlaybackRate: (rate: any) => void;
tick: (updateLoopState: any) => number;
getSeekablePercent: () => number;
Expand Down

0 comments on commit 793235d

Please sign in to comment.