@@ -36,6 +36,7 @@ import {parseBlob, parseWebStream} from 'music-metadata';
3636import './scrollIntoViewIfNeeded-polyfill.js' ;
3737import { get , set , del } from 'idb-keyval' ;
3838import * as yaml from 'js-yaml' ;
39+ import PQueue from 'p-queue' ;
3940
4041import Sortable , { MultiDrag } from 'sortablejs' ;
4142Sortable . mount ( new MultiDrag ( ) ) ;
@@ -909,6 +910,9 @@ const getCurrentSettings = _ => ({
909910 weighting : getControlValue ( elWeighting )
910911} ) ;
911912
913+ // Limit the number of parallel metadata requests
914+ const metadataRetrievalQueue = new PQueue ( { concurrency : MAX_METADATA_REQUESTS } ) ;
915+
912916// get the array index for a preset key, or validate a given index; if invalid or not found returns -1
913917const getPresetIndex = key => {
914918 const index = ( + key == key ) ? key : presets . findIndex ( item => item . key == key ) ;
@@ -1205,7 +1209,7 @@ async function addSongToPlayQueue( fileObject, content ) {
12051209 if ( FILE_EXT_AUDIO . includes ( extension ) || ! extension ) {
12061210 // disable retrieving metadata of video files for now - https://github.com/Borewit/music-metadata-browser/issues/950
12071211 trackData . retrieve = 1 ; // flag this item as needing metadata
1208- await retrieveMetadata ( ) ;
1212+ retrieveMetadataForQueueItem ( newEl ) ; // ToDo improve handling promise
12091213 }
12101214
12111215 if ( queueLength ( ) === 1 && ! isPlaying ( ) ) {
@@ -1221,22 +1225,18 @@ async function addSongToPlayQueue( fileObject, content ) {
12211225/**
12221226 * Add a song or playlist to the play queue
12231227 */
1224- function addToPlayQueue ( fileObject , autoplay = false ) {
1225-
1226- let ret ;
1228+ async function addToPlayQueue ( fileObject , autoplay = false ) {
12271229
1230+ let n ;
12281231 if ( FILE_EXT_PLIST . includes ( parsePath ( fileObject . file ) . extension ) )
1229- ret = loadPlaylist ( fileObject ) ;
1232+ n = await loadPlaylist ( fileObject ) ;
12301233 else
1231- ret = addSongToPlayQueue ( fileObject ) ;
1234+ n = await addSongToPlayQueue ( fileObject ) ;
12321235
12331236 // when promise resolved, if autoplay requested start playing the first added song
1234- ret . then ( n => {
1235- if ( autoplay && ! isPlaying ( ) && n > 0 )
1236- playSong ( queueLength ( ) - n ) ;
1237- } ) ;
12381237
1239- return ret ;
1238+ if ( autoplay && ! isPlaying ( ) && n > 0 )
1239+ playSong ( queueLength ( ) - n ) ;
12401240}
12411241
12421242/**
@@ -1245,7 +1245,7 @@ function addToPlayQueue( fileObject, autoplay = false ) {
12451245function changeFsHeight ( incr ) {
12461246 const val = + elFsHeight . value ;
12471247
1248- if ( incr == 1 && val < + elFsHeight . max || incr == - 1 && val > + elFsHeight . min ) {
1248+ if ( incr === 1 && val < + elFsHeight . max || incr = == - 1 && val > + elFsHeight . min ) {
12491249 elFsHeight . value = val + elFsHeight . step * incr ;
12501250 setProperty ( elFsHeight ) ;
12511251 }
@@ -3225,28 +3225,22 @@ async function retrieveBackgrounds() {
32253225 catch ( e ) { } // needs permission to access local device
32263226 }
32273227
3228- if ( bgLocation != BGFOLDER_NONE ) {
3228+ if ( bgLocation !== BGFOLDER_NONE ) {
32293229 const imageCount = bgImages . length ,
32303230 videoCount = bgVideos . length ;
32313231
3232- consoleLog ( 'Found ' + ( imageCount + videoCount == 0 ? 'no media' : imageCount + ' image files and ' + videoCount + ' video' ) + ' files in the backgrounds folder' ) ;
3232+ consoleLog ( 'Found ' + ( imageCount + videoCount === 0 ? 'no media' : imageCount + ' image files and ' + videoCount + ' video' ) + ' files in the backgrounds folder' ) ;
32333233 }
32343234
32353235 populateBackgrounds ( ) ;
32363236}
32373237
32383238/**
3239- * Retrieve metadata for the first MAX_METADATA_REQUESTS files in the play queue,
3240- * which have no metadata assigned yet
3239+ * Retrieve metadata for element queueItem
32413240 */
3242- async function retrieveMetadata ( ) {
3243-
3244- // Process in sequential order
3245- for ( const queueItem of elPlayqueue . children ) {
3246-
3247- if ( ! queueItem . dataset . retrieve ) continue ;
3248- delete queueItem . dataset . retrieve ;
3241+ function retrieveMetadataForQueueItem ( queueItem ) {
32493242
3243+ return metadataRetrievalQueue . add ( async ( ) => {
32503244 let metadata ;
32513245 let file ;
32523246
@@ -3299,7 +3293,7 @@ async function retrieveMetadata() {
32993293 }
33003294
33013295 syncMetadataToAudioElements ( queueItem ) ;
3302- }
3296+ } ) ;
33033297}
33043298
33053299/**
0 commit comments