Skip to content

Commit

Permalink
fix: prevent Play Next restart after manual play
Browse files Browse the repository at this point in the history
  • Loading branch information
Bellisario committed Aug 9, 2023
1 parent 5f844da commit 9276f23
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/components/PlayNextView/PlayNextController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,37 @@ import { wantPlay } from '$lib/wantPlay';

import { get } from 'svelte/store';

let playNextListEmpty = true;

playNextList.subscribe((list) => {
if (playNextListEmpty) loadNext(true);

playNextListEmpty = list.length === 0;
});

ended.subscribe((isEnded) => {
if (!isEnded) return;

loadNext();
});

function loadNext(manual = false) {
const wasPlayingID = get(currentID);

// find the index of the current playing song
const index = get(playNextList).findIndex((item) => {
return item.id === wasPlayingID;
});

// if index is -1, the song is not in the playNextList
if (index === -1 && !manual) return;

// if the index is the last index, it means the song is the last song
if (index === get(playNextList).length - 1) return;

// play the next song
wantPlay(get(playNextList)[index + 1]);
});
}

if ('mediaSession' in navigator) {
navigator.mediaSession.setActionHandler('previoustrack', () => {
Expand Down

0 comments on commit 9276f23

Please sign in to comment.