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 1 commit
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
36 changes: 13 additions & 23 deletions gapless5.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,35 +1331,25 @@ 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()) {
log.warn(`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;

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 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