Skip to content

Commit

Permalink
fixed bugs on remove dupes and now you can add multiple tracks to for…
Browse files Browse the repository at this point in the history
…ceplay
  • Loading branch information
achiragaming committed Dec 31, 2023
1 parent c05df47 commit 48142d8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "damonjs",
"version": "1.5.0",
"version": "1.5.1",
"preview": false,
"description": "A modified Shoukaku wrapper with enhanced queue support.",
"main": "dist/Index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export * from './DamonJs';
export { DamonJsTrack, DamonJsQueue, DamonJsPlayer, Plugins };
export * from './Modules/Interfaces';

export const version = '1.5.0';
export const version = '1.5.1';
8 changes: 4 additions & 4 deletions src/Managers/DamonJsPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ export class DamonJsPlayer {
* @param {PlayOptions} options Play options
* @returns {Promise<DamonJsPlayer>}
*/
public async play(track?: DamonJsTrack, options?: PlayOptions): Promise<DamonJsPlayer> {
public async play(tracks?: DamonJsTrack[], options?: PlayOptions): Promise<DamonJsPlayer> {
if (this.state === PlayerState.DESTROYED) throw new DamonJsError(1, 'Player is already destroyed');

if (!track && !this.queue.totalSize) throw new DamonJsError(1, 'No track is available to play');
if (!tracks && !this.queue.totalSize) throw new DamonJsError(1, 'No track is available to play');

if (!options) options = { replaceCurrent: false };

if (track) {
this.queue.splice(this.queue.currentId, options.replaceCurrent && this.queue.current ? 1 : 0, track);
if (tracks) {
this.queue.splice(this.queue.currentId, options.replaceCurrent && this.queue.current ? 1 : 0, ...tracks);
}

if (this.playable) {
Expand Down
22 changes: 20 additions & 2 deletions src/Managers/Supports/DamonJsQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,26 @@ export class DamonJsQueue extends Array<DamonJsTrack> {
return this;
}
public removeDupes(): DamonJsQueue {
const newQueue = [...new Set(this)];
this.splice(0, this.length, ...newQueue);
const trackUris = new Set();
const playedTracks = this.slice(0, this.currentId);
const unplayedTracks = this.slice(this.currentId + 1);
const currentTrack = this[this.currentId];
trackUris.add(currentTrack.uri);

const newPlayedTracks = playedTracks.filter((track) => {
if (trackUris.has(track.uri)) return false;
trackUris.add(track.uri);
return true;
});

const newUnplayedTracks = unplayedTracks.filter((track) => {
if (trackUris.has(track.uri)) return false;
trackUris.add(track.uri);
return true;
});

this.currentId = newPlayedTracks.length;
this.splice(0, this.length, ...newPlayedTracks, currentTrack, ...newUnplayedTracks);
this.player.emit(Events.InitQueue, this.player);
return this;
}
Expand Down

0 comments on commit 48142d8

Please sign in to comment.