Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

javascript - WinJS Promise based file upload queue

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem is that pendingFiles is always empty. In createTaskForFile, you would set it to an one-element array then, but immediately call uploadNextAsync() which shifts it out. I guess your script might work if you shifted the file after the file has been uploaded.

However, you actually don't need this array. You can just queue the action to filesOperation, which would be a promise representing the upload of all current files.

var filesOperation = WinJS.Promise.as();
function createTaskForFile(file) {
  return filesOperation = filesOperation.then(function() {
    return uploadNextAsync(file);
  });
}

function uploadAsync(next) {
  fileslogger.debug("Uploading " + next.name);
  return fileQuery.folder.getFileAsync(next.name).then(function(file) {
    return Server.sendFileAsync(file);
  }).then(function() {
    return filesOk += 1;
  }, function(error) {
    filesNok += 1;
    return logger.error(error.message, error);
  });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...