Add queue output, various bug fixes

This commit is contained in:
Max Isom 2020-03-18 22:29:43 -05:00
parent 0357373123
commit 7f39642c49
8 changed files with 123 additions and 24 deletions

View file

@ -100,7 +100,7 @@ export default class {
case 'playlist': {
const uri = parsed as spotifyURI.Playlist;
let [{body: playlistResponse}, {body: tracksResponse}] = await Promise.all([this.spotify.getPlaylist(uri.id), this.spotify.getPlaylistTracks(uri.id, {limit: 1})]);
let [{body: playlistResponse}, {body: tracksResponse}] = await Promise.all([this.spotify.getPlaylist(uri.id), this.spotify.getPlaylistTracks(uri.id, {limit: 50})]);
playlist = {title: playlistResponse.name, source: playlistResponse.href};
@ -109,7 +109,7 @@ export default class {
while (tracksResponse.next) {
// eslint-disable-next-line no-await-in-loop
({body: tracksResponse} = await this.spotify.getPlaylistTracks(uri.id, {
limit: parseInt(new URL(tracksResponse.next).searchParams.get('limit') ?? '1', 10),
limit: parseInt(new URL(tracksResponse.next).searchParams.get('limit') ?? '50', 10),
offset: parseInt(new URL(tracksResponse.next).searchParams.get('offset') ?? '0', 10)
}));

View file

@ -39,7 +39,7 @@ export default class {
await player.connect(channel);
}
const isPlaying = queue.getCurrent();
const isPlaying = queue.getCurrent() !== null;
let oldPosition = 0;
queue.add({title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false});
@ -54,12 +54,11 @@ export default class {
return new Promise((resolve, reject) => {
try {
setTimeout(async () => {
if (isPlaying) {
queue.back();
queue.removeCurrent();
if (isPlaying) {
await player.seek(oldPosition);
} else {
queue.forward();
player.disconnect();
}

View file

@ -21,6 +21,7 @@ export default class {
private dispatcher: StreamDispatcher | null = null;
private nowPlaying: QueuedSong | null = null;
private playPositionInterval: NodeJS.Timeout | undefined;
private lastSongURL = '';
private positionInSeconds = 0;
@ -100,6 +101,7 @@ export default class {
if (this.dispatcher) {
this.dispatcher.resume();
this.status = STATUS.PLAYING;
this.startTrackingPosition();
return;
}
@ -115,7 +117,13 @@ export default class {
this.status = STATUS.PLAYING;
this.nowPlaying = currentSong;
this.startTrackingPosition();
if (currentSong.url === this.lastSongURL) {
this.startTrackingPosition();
} else {
// Reset position counter
this.startTrackingPosition(0);
this.lastSongURL = currentSong.url;
}
}
pause(): void {
@ -235,7 +243,7 @@ export default class {
}
private startTrackingPosition(initalPosition?: number): void {
if (initalPosition) {
if (initalPosition !== undefined) {
this.positionInSeconds = initalPosition;
}
@ -270,9 +278,16 @@ export default class {
this.dispatcher.on('speaking', async isSpeaking => {
// Automatically advance queued song at end
if (!isSpeaking && this.status === STATUS.PLAYING) {
if (this.queue.get().length > 0) {
if (this.queue.size() >= 0) {
this.queue.forward();
await this.play();
this.positionInSeconds = 0;
if (this.queue.getCurrent()) {
await this.play();
} else {
this.status = STATUS.PAUSED;
}
}
}
});

View file

@ -52,7 +52,7 @@ export default class {
this.queue.push(song);
} else {
// Not from playlist, add immediately
let insertAt = 0;
let insertAt = this.position;
// Loop until playlist song
this.queue.some(song => {
@ -83,6 +83,14 @@ export default class {
this.queue = newQueue;
}
removeCurrent(): void {
this.queue = [...this.queue.slice(0, this.position), ...this.queue.slice(this.position + 1)];
if (this.position !== 0) {
this.position--;
}
}
size(): number {
return this.get().length;
}