11import { Player } from './Player' ;
22
33import { State } from '../../src/constants/State' ;
4- import type { Track } from '../../src/types ' ;
4+ import type { Track } from '../../src/features ' ;
55import { RepeatMode } from './RepeatMode' ;
66
77export class PlaylistPlayer extends Player {
@@ -11,31 +11,31 @@ export class PlaylistPlayer extends Player {
1111 protected _currentIndex ?: number ;
1212 protected repeatMode : RepeatMode = RepeatMode . Off ;
1313
14- protected async onStateUpdate ( state : Exclude < State , State . Error > ) {
14+ protected onStateUpdate ( state : Exclude < State , State . Error > ) {
1515 super . onStateUpdate ( state ) ;
1616
1717 if ( state === State . Ended ) {
18- await this . onTrackEnded ( ) ;
18+ this . onTrackEnded ( ) ;
1919 }
2020 }
2121
22- protected async onTrackEnded ( ) {
22+ protected onTrackEnded ( ) {
2323 switch ( this . repeatMode ) {
2424 case RepeatMode . Track :
2525 if ( this . currentIndex !== undefined ) {
26- await this . goToIndex ( this . currentIndex ) ;
26+ this . goToIndex ( this . currentIndex ) ;
2727 }
2828 break ;
2929 case RepeatMode . Playlist :
3030 if ( this . currentIndex === this . playlist . length - 1 ) {
31- await this . goToIndex ( 0 ) ;
31+ this . goToIndex ( 0 ) ;
3232 } else {
33- await this . skipToNext ( ) ;
33+ this . skipToNext ( ) ;
3434 }
3535 break ;
3636 default :
3737 try {
38- await this . skipToNext ( ) ;
38+ this . skipToNext ( ) ;
3939 } catch ( err ) {
4040 if ( ( err as Error ) . message !== 'playlist_exhausted' ) {
4141 throw err ;
@@ -59,61 +59,65 @@ export class PlaylistPlayer extends Player {
5959 this . _currentIndex = current ;
6060 }
6161
62- protected async goToIndex ( index : number , initialPosition ?: number ) {
62+ protected goToIndex ( index : number , initialPosition ?: number ) {
6363 const track = this . playlist [ index ] ;
6464
6565 if ( ! track ) {
6666 throw new Error ( 'playlist_exhausted' ) ;
6767 }
6868
69- if ( this . currentIndex !== index ) {
70- this . currentIndex = index ;
71- await this . load ( track ) ;
72- }
69+ const onCompletedLoading = ( ) => {
70+ if ( initialPosition ) {
71+ this . seekTo ( initialPosition ) ;
72+ }
7373
74- if ( initialPosition ) {
75- this . seekTo ( initialPosition ) ;
76- }
74+ if ( this . playWhenReady ) {
75+ this . play ( ) ;
76+ }
77+ } ;
7778
78- if ( this . playWhenReady ) {
79- await this . play ( ) ;
79+ if ( this . currentIndex !== index ) {
80+ this . currentIndex = index ;
81+ this . load ( track , onCompletedLoading ) ;
82+ } else {
83+ onCompletedLoading ( ) ;
8084 }
8185 }
8286
83- public async add ( tracks : Track [ ] , insertBeforeIndex ?: number ) {
87+ public add ( tracks : Track [ ] , insertBeforeIndex ?: number ) {
8488 if ( insertBeforeIndex !== - 1 && insertBeforeIndex !== undefined ) {
8589 this . playlist . splice ( insertBeforeIndex , 0 , ...tracks ) ;
8690 } else {
8791 this . playlist . push ( ...tracks ) ;
8892 }
8993
9094 if ( this . currentIndex === undefined ) {
91- await this . goToIndex ( 0 ) ;
95+ this . goToIndex ( 0 ) ;
9296 }
9397 }
9498
95- public async skip ( index : number , initialPosition ?: number ) {
99+ public skip ( index : number , initialPosition ?: number ) {
96100 const track = this . playlist [ index ] ;
97101
98102 if ( track === undefined ) {
99103 throw new Error ( 'index out of bounds' ) ;
100104 }
101105
102- await this . goToIndex ( index , initialPosition ) ;
106+ this . goToIndex ( index , initialPosition ) ;
103107 }
104108
105- public async skipToNext ( initialPosition ?: number ) {
109+ public skipToNext ( initialPosition ?: number ) {
106110 if ( this . currentIndex === undefined ) return ;
107111
108112 const index = this . currentIndex + 1 ;
109- await this . goToIndex ( index , initialPosition ) ;
113+ this . goToIndex ( index , initialPosition ) ;
110114 }
111115
112- public async skipToPrevious ( initialPosition ?: number ) {
116+ public skipToPrevious ( initialPosition ?: number ) {
113117 if ( this . currentIndex === undefined ) return ;
114118
115119 const index = this . currentIndex - 1 ;
116- await this . goToIndex ( index , initialPosition ) ;
120+ this . goToIndex ( index , initialPosition ) ;
117121 }
118122
119123 public getTrack ( index : number ) : Track | null {
@@ -129,7 +133,7 @@ export class PlaylistPlayer extends Player {
129133 return this . repeatMode ;
130134 }
131135
132- public async remove ( indexes : number [ ] ) {
136+ public remove ( indexes : number [ ] ) {
133137 const idxMap = indexes . reduce < Record < number , boolean > > ( ( acc , elem ) => {
134138 acc [ elem ] = true ;
135139 return acc ;
@@ -151,28 +155,31 @@ export class PlaylistPlayer extends Player {
151155
152156 const hasItems = this . playlist . length > 0 ;
153157 if ( isCurrentRemoved && hasItems ) {
154- await this . goToIndex ( this . currentIndex % this . playlist . length ) ;
158+ this . goToIndex ( this . currentIndex % this . playlist . length ) ;
155159 } else if ( isCurrentRemoved ) {
156- await this . stop ( ) ;
160+ this . stop ( ) ;
157161 }
158162 }
159163
160- public async stop ( ) {
161- await super . stop ( ) ;
162- this . currentIndex = undefined ;
164+ public stop ( onComplete ?: ( ) => void ) {
165+ super . stop ( ( ) => {
166+ this . currentIndex = undefined ;
167+ onComplete ?.( ) ;
168+ } ) ;
163169 }
164170
165- public async reset ( ) {
166- await this . stop ( ) ;
167- this . playlist = [ ] ;
171+ public reset ( ) {
172+ this . stop ( ( ) => {
173+ this . playlist = [ ] ;
174+ } ) ;
168175 }
169176
170- public async removeUpcomingTracks ( ) {
177+ public removeUpcomingTracks ( ) {
171178 if ( this . currentIndex === undefined ) return ;
172179 this . playlist = this . playlist . slice ( 0 , this . currentIndex + 1 ) ;
173180 }
174181
175- public async move ( fromIndex : number , toIndex : number ) : Promise < void > {
182+ public move ( fromIndex : number , toIndex : number ) : void {
176183 if ( ! this . playlist [ fromIndex ] ) {
177184 throw new Error ( 'index out of bounds' ) ;
178185 }
0 commit comments