Skip to content

Commit

Permalink
fix: prevent multiple streams (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
miksuh-dev authored Apr 8, 2023
1 parent 60d3ab4 commit f211068
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions server/common/playlist/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const removeSongFromQueue = (song: Song) => {
});
};

export const onSongEnd = async (song: Song) => {
const onSongEnd = async (song: Song) => {
await prisma.song.update({
where: {
id: song.id,
Expand All @@ -83,8 +83,6 @@ export const onSongEnd = async (song: Song) => {

ee.emit(`onUpdate`, { song: { remove: [song.id] } });

client.voiceConnection.stopStream();

removeSongFromQueue(song);

const nextSong = await getNextSong();
Expand All @@ -95,9 +93,7 @@ export const onSongEnd = async (song: Song) => {

async function onSongError(this: ProcessQueueItem, error: string) {
try {
if (stream) {
stream.destroy();
}
stopStream();

if (this.retryCount <= MAX_RETRIES) {
sendErrorMessage(
Expand Down Expand Up @@ -127,6 +123,9 @@ async function onSongError(this: ProcessQueueItem, error: string) {
}

const createStream = async (song: Song) => {
// Make sure we stop any previous streams
stopStream();

if (song.type === SourceType.SONG) {
return createYoutubeStream(song);
}
Expand Down Expand Up @@ -195,16 +194,8 @@ async function onPlayStart(this: ProcessQueueItem) {
}
}

export async function playSong(this: ProcessQueueItem) {
async function playSong(this: ProcessQueueItem) {
try {
if (endTimeout) {
clearTimeout(endTimeout);
}

if (stream) {
stream.destroy();
}

stream = await createStream(this.song);

let started = false;
Expand Down Expand Up @@ -246,13 +237,21 @@ export const getCurrentSong = (): PlayingSong | undefined => {
return item.song;
};

export const stopCurrentSong = () => {
const stopStream = () => {
client.voiceConnection.stopStream();

if (endTimeout) {
clearTimeout(endTimeout);
}

if (stream) {
stream.destroy();
stream = undefined;
}
};

client.voiceConnection.stopStream();
clearTimeout(endTimeout);
export const stopCurrentSong = () => {
stopStream();

ee.emit(`onUpdate`, {
song: { setPlaying: undefined },
Expand Down

0 comments on commit f211068

Please sign in to comment.