Utilizing the web worker concept for file uploads has resulted in creating a web worker for each selected file. The idea now is to optimize this process by creating 5 web worker threads to handle the first batch of five files, terminating them afterwards before moving on to the next batch.
The current logic used is as follows:
for (var i = 0; i<files.length; i++){
//create web worker
//upload the file
//terminate the web worker
}
EDIT:
An attempt was made to implement a solution where files are picked in batches of 5, but it seems to be stuck in an infinite loop. Assistance is needed to identify and correct the issue.
var num = 5;
var alreadyPicked = new Set();
var pickList = [];
var isPicked = false;
while(true){
if( pickList.length == num ){
$timeout( function() {}, 3000); //wait for 3 secs
}
for(var j=0; j<files.length; j++){
isPicked = alreadyPicked.has( files[j] );
if( !isPicked ){
pickList.push( files[j] ); //add the file to the array if it is not processed
}
if( pickList.length === num ){
break;
}
}
if( pickList.length > 0 ){
for( var j=0; j<pickList.length; j++ ){
$scope.process( pickList[j] );
}
}
else{
break;
}
}
$scope.process = function(item){
//rest api call to upload the file to server
if( success || error ){
pickList.splice( pickList.indexOf(item,1));
alreadyPicked.add(item);
}