@@ -397,14 +397,23 @@ function updatePlaylistList(
397397 context . currentPlaylistList = playlists ;
398398}
399399
400+ function updateTrackList (
401+ collection : ITrackCollection ,
402+ context : IClientContext ,
403+ ) {
404+ context . currentTrackList = collection ;
405+ context . lastTrackStartIndex = 0 ;
406+ context . lastTrackEndIndex = collection . getTrackCount ( ) ;
407+
408+ context . currentTrackList = collection ;
409+ }
410+
400411async function updateTrackListAndPrint (
401412 collection : ITrackCollection ,
402413 clientContext : IClientContext ,
403414) {
404415 await printTrackNames ( collection , clientContext ) ;
405- clientContext . currentTrackList = collection ;
406- clientContext . lastTrackStartIndex = 0 ;
407- clientContext . lastTrackEndIndex = collection . getTrackCount ( ) ;
416+ updateTrackList ( collection , clientContext ) ;
408417}
409418
410419export async function getClientContext (
@@ -506,30 +515,62 @@ export async function searchForPlaylists(
506515async function playTrackCollection (
507516 trackCollection : ITrackCollection ,
508517 clientContext : IClientContext ,
509- trackIndex = 0 ,
518+ trackIndex : number | undefined = undefined ,
510519) {
511520 const deviceId = await ensureSelectedDeviceId ( clientContext ) ;
512521 const tracks = trackCollection . getTracks ( ) ;
513522 const playContext = trackCollection . getContext ( ) ;
514- const singleTrackCollection = new TrackCollection ( [ tracks [ trackIndex ] ] ) ;
515- const actionResult = await htmlTrackNames (
516- singleTrackCollection ,
517- "Now playing" ,
518- ) ;
519- if ( playContext === undefined ) {
520- const singleTracks = singleTrackCollection . getTracks ( ) ;
521- const uris = singleTracks . map ( ( track ) => track . uri ) ;
522- await play ( clientContext . service , deviceId , uris ) ;
523- } else {
524- await play (
525- clientContext . service ,
526- deviceId ,
527- undefined ,
528- playContext ,
529- trackIndex ,
523+ if (
524+ trackIndex !== undefined &&
525+ ( trackIndex < 0 || trackIndex >= tracks . length )
526+ ) {
527+ return createErrorActionResult (
528+ `Track index ${ trackIndex } out of range for track collection of size ${ tracks . length } ` ,
530529 ) ;
530+ } else {
531+ if ( trackIndex !== undefined ) {
532+ if ( playContext === undefined ) {
533+ const singleTrackCollection = new TrackCollection ( [
534+ tracks [ trackIndex ] ,
535+ ] ) ;
536+ const actionResult = await htmlTrackNames (
537+ singleTrackCollection ,
538+ "Now playing" ,
539+ ) ;
540+ const singleTracks = singleTrackCollection . getTracks ( ) ;
541+ const uris = singleTracks . map ( ( track ) => track . uri ) ;
542+ await play ( clientContext . service , deviceId , uris ) ;
543+ return actionResult ;
544+ } else {
545+ const trackCollectionFromTrackNumber = new TrackCollection (
546+ tracks . slice ( trackIndex ) ,
547+ playContext ,
548+ ) ;
549+ const actionResult = await htmlTrackNames (
550+ trackCollectionFromTrackNumber ,
551+ "Now playing" ,
552+ ) ;
553+ updateTrackList ( trackCollectionFromTrackNumber , clientContext ) ;
554+ await play (
555+ clientContext . service ,
556+ deviceId ,
557+ undefined ,
558+ playContext ,
559+ trackIndex ,
560+ ) ;
561+ return actionResult ;
562+ }
563+ } else {
564+ const actionResult = await htmlTrackNames (
565+ trackCollection ,
566+ "Now playing" ,
567+ ) ;
568+ updateTrackList ( trackCollection , clientContext ) ;
569+ const uris = tracks . map ( ( track ) => track . uri ) ;
570+ await play ( clientContext . service , deviceId , uris , playContext ) ;
571+ return actionResult ;
572+ }
531573 }
532- return actionResult ;
533574}
534575
535576function randomShuffle < T > ( array : T [ ] ) {
@@ -551,8 +592,12 @@ async function playRandomAction(
551592 if ( quantity > 0 ) {
552593 savedTracks . splice ( quantity ) ;
553594 }
554-
555- const tracks = randomShuffle ( savedTracks . map ( ( track ) => track . track ) ) ;
595+ const rawTracks = savedTracks . map ( ( track ) => track . track ) ;
596+ const uniqueTracks = new Map < string , SpotifyApi . TrackObjectFull > ( ) ;
597+ for ( const track of rawTracks ) {
598+ uniqueTracks . set ( track . id , track ) ;
599+ }
600+ const tracks = randomShuffle ( Array . from ( uniqueTracks . values ( ) ) ) ;
556601 const collection = new TrackCollection ( tracks ) ;
557602 return playTrackCollection ( collection , clientContext ) ;
558603 }
0 commit comments