Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch: Fix Index Resetting on Removal #59

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions gapless5.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,36 +1331,39 @@ 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 (!isValidIndex(index)) {
log.error(`Cannot remove missing track at index: ${index}`);
return;
}
const deletedPlaying = point === this.playlist.trackNumber;

const { source: curSource } = this.playlist.getSourceIndexed(point);
if (!curSource) {
return;
}
let wasPlaying = false;
const isCurrentTrack = index === this.playlist.trackNumber;
const wasPlayingCurrentTrack = isCurrentTrack && this.isPlaying();

if (curSource.state === Gapless5State.Loading) {
curSource.unload();
} else if (curSource.inPlayState(true)) {
wasPlaying = true;
curSource.stop();
// If it's the current track, pause playback
if (wasPlayingCurrentTrack) {
this.pause();
}

this.playlist.remove(point);

if (deletedPlaying) {
this.next(); // Don't stop after a delete
if (wasPlaying) {
this.play();
}
// If removing a track before the current track, decrement trackNumber
if (index < this.playlist.trackNumber) {
this.playlist.trackNumber--;
}
// 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;

// If we were playing and there are tracks remaining, play the next available track
if (wasPlayingCurrentTrack && this.playlist.numTracks() > 0) {
this.play();
}
};

/**
Expand Down
67 changes: 67 additions & 0 deletions gapless5.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,71 @@ describe('Gapless-5 object with tracklist', () => {
player.stop();
expect(player.onstop).toHaveBeenCalledWith(TRACKS[0]);
});

it('regression: maintains correct index when removing tracks that come after the currently playing track', () => {
const testTracks = ['track1.mp3', 'track2.mp3', 'track3.mp3'];
const player = new Gapless5({
...INIT_OPTIONS,
tracks: testTracks,
});

// Verify initial state
expect(player.getIndex()).toBe(0);
expect(player.getTracks()).toStrictEqual(testTracks);

// Move to track 1
player.gotoTrack(1);
expect(player.getIndex()).toBe(1);

// Remove track at index 2
player.removeTrack(2);

// Check index is still 1 and track list is updated
expect(player.getIndex()).toBe(1);
expect(player.getTracks()).toStrictEqual(['track1.mp3', 'track2.mp3']);
});

it('regression: maintains correct index when removing the currently playing track', () => {
const testTracks = ['track1.mp3', 'track2.mp3', 'track3.mp3'];
const player = new Gapless5({
...INIT_OPTIONS,
tracks: testTracks,
});

// Verify initial state
expect(player.getIndex()).toBe(0);
expect(player.getTracks()).toStrictEqual(testTracks);

// Move to track 1
player.gotoTrack(1);
expect(player.getIndex()).toBe(1);
player.play();

// Remove the currently playing track
player.removeTrack(1);

// Check index is still 1 and track list is updated, therefore we move to the next available track
expect(player.getIndex()).toBe(1);
expect(player.getTracks()).toStrictEqual(['track1.mp3', 'track3.mp3']);

player.play();

// Remove the currently playing track
player.removeTrack(1);

// Check index is still 0 and track list is updated, therefore we move to the next available track
expect(player.getIndex()).toBe(0);
expect(player.getTracks()).toStrictEqual(['track1.mp3']);

player.play();

// Remove the only remaining track
player.removeTrack(0);

// Check index is still 0 and track list is updated
expect(player.getIndex()).toBe(0);
expect(player.getTracks()).toStrictEqual([]);
});
});

describe('Gapless-5 object with load limit', () => {
Expand Down Expand Up @@ -179,3 +244,5 @@ describe('Gapless-5 object with load limit', () => {
expect(loadedTracks.size).toBe(0);
});
});


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@regosen/gapless-5",
"version": "1.5.5",
"version": "1.5.6",
"description": "A gapless JavaScript audio player for HTML5",
"main": "gapless5.js",
"style": "gapless5.css",
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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I reran the type generator and this just appeared.

setPlaybackRate: (rate: any) => void;
tick: (updateLoopState: any) => number;
getSeekablePercent: () => number;
Expand Down