@@ -36,6 +36,7 @@ import {parseBlob, parseWebStream} from 'music-metadata';
36
36
import './scrollIntoViewIfNeeded-polyfill.js' ;
37
37
import { get , set , del } from 'idb-keyval' ;
38
38
import * as yaml from 'js-yaml' ;
39
+ import PQueue from 'p-queue' ;
39
40
40
41
import Sortable , { MultiDrag } from 'sortablejs' ;
41
42
Sortable . mount ( new MultiDrag ( ) ) ;
@@ -958,6 +959,9 @@ const getCurrentSettings = _ => ({
958
959
weighting : getControlValue ( elWeighting )
959
960
} ) ;
960
961
962
+ // Limit the number of parallel metadata requests
963
+ const metadataRetrievalQueue = new PQueue ( { concurrency : MAX_METADATA_REQUESTS } ) ;
964
+
961
965
// get the array index for a preset key, or validate a given index; if invalid or not found returns -1
962
966
const getPresetIndex = key => {
963
967
const index = ( + key == key ) ? key : presets . findIndex ( item => item . key == key ) ;
@@ -1251,7 +1255,7 @@ async function addSongToPlayQueue( fileObject, content ) {
1251
1255
if ( FILE_EXT_AUDIO . includes ( extension ) || ! extension ) {
1252
1256
// disable retrieving metadata of video files for now - https://github.com/Borewit/music-metadata-browser/issues/950
1253
1257
trackData . retrieve = 1 ; // flag this item as needing metadata
1254
- await retrieveMetadata ( ) ;
1258
+ retrieveMetadataForQueueItem ( newEl ) ; // ToDo improve handling promise
1255
1259
}
1256
1260
1257
1261
if ( queueLength ( ) === 1 && ! isPlaying ( ) )
@@ -1266,22 +1270,18 @@ async function addSongToPlayQueue( fileObject, content ) {
1266
1270
/**
1267
1271
* Add a song or playlist to the play queue
1268
1272
*/
1269
- function addToPlayQueue ( fileObject , autoplay = false ) {
1270
-
1271
- let ret ;
1273
+ async function addToPlayQueue ( fileObject , autoplay = false ) {
1272
1274
1275
+ let n ;
1273
1276
if ( FILE_EXT_PLIST . includes ( parsePath ( fileObject . file ) . extension ) )
1274
- ret = loadPlaylist ( fileObject ) ;
1277
+ n = await loadPlaylist ( fileObject ) ;
1275
1278
else
1276
- ret = addSongToPlayQueue ( fileObject ) ;
1279
+ n = await addSongToPlayQueue ( fileObject ) ;
1277
1280
1278
1281
// when promise resolved, if autoplay requested start playing the first added song
1279
- ret . then ( n => {
1280
- if ( autoplay && ! isPlaying ( ) && n > 0 )
1281
- playSong ( queueLength ( ) - n ) ;
1282
- } ) ;
1283
1282
1284
- return ret ;
1283
+ if ( autoplay && ! isPlaying ( ) && n > 0 )
1284
+ playSong ( queueLength ( ) - n ) ;
1285
1285
}
1286
1286
1287
1287
/**
@@ -1290,7 +1290,7 @@ function addToPlayQueue( fileObject, autoplay = false ) {
1290
1290
function changeFsHeight ( incr ) {
1291
1291
const val = + elFsHeight . value ;
1292
1292
1293
- if ( incr == 1 && val < + elFsHeight . max || incr == - 1 && val > + elFsHeight . min ) {
1293
+ if ( incr === 1 && val < + elFsHeight . max || incr = == - 1 && val > + elFsHeight . min ) {
1294
1294
elFsHeight . value = val + elFsHeight . step * incr ;
1295
1295
setProperty ( elFsHeight ) ;
1296
1296
}
@@ -3274,28 +3274,22 @@ async function retrieveBackgrounds() {
3274
3274
catch ( e ) { } // needs permission to access local device
3275
3275
}
3276
3276
3277
- if ( bgLocation != BGFOLDER_NONE ) {
3277
+ if ( bgLocation !== BGFOLDER_NONE ) {
3278
3278
const imageCount = bgImages . length ,
3279
3279
videoCount = bgVideos . length ;
3280
3280
3281
- consoleLog ( 'Found ' + ( imageCount + videoCount == 0 ? 'no media' : imageCount + ' image files and ' + videoCount + ' video' ) + ' files in the backgrounds folder' ) ;
3281
+ consoleLog ( 'Found ' + ( imageCount + videoCount === 0 ? 'no media' : imageCount + ' image files and ' + videoCount + ' video' ) + ' files in the backgrounds folder' ) ;
3282
3282
}
3283
3283
3284
3284
populateBackgrounds ( ) ;
3285
3285
}
3286
3286
3287
3287
/**
3288
- * Retrieve metadata for the first MAX_METADATA_REQUESTS files in the play queue,
3289
- * which have no metadata assigned yet
3288
+ * Retrieve metadata for element queueItem
3290
3289
*/
3291
- async function retrieveMetadata ( ) {
3292
-
3293
- // Process in sequential order
3294
- for ( const queueItem of playlist . children ) {
3295
-
3296
- if ( ! queueItem . dataset . retrieve ) continue ;
3297
- delete queueItem . dataset . retrieve ;
3290
+ function retrieveMetadataForQueueItem ( queueItem ) {
3298
3291
3292
+ return metadataRetrievalQueue . add ( async ( ) => {
3299
3293
let metadata ;
3300
3294
let file ;
3301
3295
@@ -3348,7 +3342,7 @@ async function retrieveMetadata() {
3348
3342
}
3349
3343
3350
3344
syncMetadataToAudioElements ( queueItem ) ;
3351
- }
3345
+ } ) ;
3352
3346
}
3353
3347
3354
3348
/**
0 commit comments