Skip to content

Commit 55934b0

Browse files
committed
Controlling retrieving metadata flow with p-queue
1 parent ded7a7d commit 55934b0

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"mini-css-extract-plugin": "^2.9.0",
2121
"music-metadata": "^11.7.1",
2222
"notie": "^4.3.1",
23+
"p-queue": "^8.1.0",
2324
"sortablejs": "^1.15.2",
2425
"style-loader": "^4.0.0",
2526
"webpack": "^5.91.0",

src/index.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {parseBlob, parseWebStream} from 'music-metadata';
3636
import './scrollIntoViewIfNeeded-polyfill.js';
3737
import { get, set, del } from 'idb-keyval';
3838
import * as yaml from 'js-yaml';
39+
import PQueue from 'p-queue';
3940

4041
import Sortable, { MultiDrag } from 'sortablejs';
4142
Sortable.mount( new MultiDrag() );
@@ -958,6 +959,9 @@ const getCurrentSettings = _ => ({
958959
weighting : getControlValue( elWeighting )
959960
});
960961

962+
// Limit the number of parallel metadata requests
963+
const metadataRetrievalQueue = new PQueue({ concurrency: MAX_METADATA_REQUESTS });
964+
961965
// get the array index for a preset key, or validate a given index; if invalid or not found returns -1
962966
const getPresetIndex = key => {
963967
const index = ( +key == key ) ? key : presets.findIndex( item => item.key == key );
@@ -1251,7 +1255,7 @@ async function addSongToPlayQueue( fileObject, content ) {
12511255
if ( FILE_EXT_AUDIO.includes( extension ) || ! extension ) {
12521256
// disable retrieving metadata of video files for now - https://github.com/Borewit/music-metadata-browser/issues/950
12531257
trackData.retrieve = 1; // flag this item as needing metadata
1254-
await retrieveMetadata();
1258+
retrieveMetadataForQueueItem( newEl ); // ToDo improve handling promise
12551259
}
12561260

12571261
if ( queueLength() === 1 && ! isPlaying() )
@@ -1266,22 +1270,18 @@ async function addSongToPlayQueue( fileObject, content ) {
12661270
/**
12671271
* Add a song or playlist to the play queue
12681272
*/
1269-
function addToPlayQueue( fileObject, autoplay = false ) {
1270-
1271-
let ret;
1273+
async function addToPlayQueue( fileObject, autoplay = false ) {
12721274

1275+
let n;
12731276
if ( FILE_EXT_PLIST.includes( parsePath( fileObject.file ).extension ) )
1274-
ret = loadPlaylist( fileObject );
1277+
n = await loadPlaylist( fileObject );
12751278
else
1276-
ret = addSongToPlayQueue( fileObject );
1279+
n =await addSongToPlayQueue( fileObject );
12771280

12781281
// 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-
});
12831282

1284-
return ret;
1283+
if ( autoplay && ! isPlaying() && n > 0 )
1284+
playSong( queueLength() - n );
12851285
}
12861286

12871287
/**
@@ -1290,7 +1290,7 @@ function addToPlayQueue( fileObject, autoplay = false ) {
12901290
function changeFsHeight( incr ) {
12911291
const val = +elFsHeight.value;
12921292

1293-
if ( incr == 1 && val < +elFsHeight.max || incr == -1 && val > +elFsHeight.min ) {
1293+
if ( incr === 1 && val < +elFsHeight.max || incr === -1 && val > +elFsHeight.min ) {
12941294
elFsHeight.value = val + elFsHeight.step * incr;
12951295
setProperty( elFsHeight );
12961296
}
@@ -3274,28 +3274,22 @@ async function retrieveBackgrounds() {
32743274
catch( e ) {} // needs permission to access local device
32753275
}
32763276

3277-
if ( bgLocation != BGFOLDER_NONE ) {
3277+
if ( bgLocation !== BGFOLDER_NONE ) {
32783278
const imageCount = bgImages.length,
32793279
videoCount = bgVideos.length;
32803280

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' );
32823282
}
32833283

32843284
populateBackgrounds();
32853285
}
32863286

32873287
/**
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
32903289
*/
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) {
32983291

3292+
return metadataRetrievalQueue.add(async () => {
32993293
let metadata;
33003294
let file;
33013295

@@ -3348,7 +3342,7 @@ async function retrieveMetadata() {
33483342
}
33493343

33503344
syncMetadataToAudioElements( queueItem );
3351-
}
3345+
});
33523346
}
33533347

33543348
/**

0 commit comments

Comments
 (0)