Scenario
I need a background upload queue that sends files to a server. The queue should send the files in sequential order as they are pushed into the queue (FIFO).
My solution
var pendingFiles = [];
var filesOperation = null;
uploadNextAsync = function(file) {
var next;
if (!pendingFiles.length) {
return WinJS.Promise.as();
}
next = pendingFiles.shift();
fileslogger.debug("Uploading " + next);
return fileQuery.folder.getFileAsync(next).then(function(file) {
return Server.sendFileAsync(file).then(function() {
return filesOk += 1;
}, function(error) {
filesNok += 1;
return logger.error(error.message, error);
}).then(function() {
if (pendingFiles.length) {
return uploadNextAsync(inspection);
}
});
});
};
createTaskForFile = function(file) {
if (pendingFiles.length == 0) {
pendingFiles = [file.name]
filesOperation = uploadNextAsync(file);
} else {
pendingFiles.push(file.name);
return filesOperation.then(function() {
return uploadNextAsync(file);
});
}
};
Problem
It seems that sometimes if createTaskForFile is called very quickly in succession then 2 files end up being uploaded at the same time. So somewhere is a little glitch either in the createTastForFile
function on how it uses the fileOperation.then
construct or inside the uploadNextAsync does something wrong?