如何在React Redux中从async.queue调度动作?

时间:2019-02-18 05:50:19

标签: reactjs redux

当某个音频上传任务开始时,我需要从async.queue调度动作。异步队列用于同时上传文件。 无法从以下代码中的req.on函数调度。

let q = async.queue(function (task, callback) {
  uploadFileOnS3(audioName, task).then(function (audioS3) {
    console.log("s3 value is", audioS3);
    let params = { Key: audioName, Body: task.file, ACL: 'public-read' };
    let req = audioS3.upload(params);

    req.on('httpUploadProgress', function (evt) {
      let percentage = (evt.loaded / evt.total) * 100;
      console.log("percentage value is", percentage);
      dispatch({type: t.PERCENTAGE, payload : percentage})
    });
  });
}, 1);


export const audioUpload = audioObj => async dispatch => {
  q.push(audioObj);
};

2 个答案:

答案 0 :(得分:0)

也许您可以使用redux-thunk?然后,您可以异步调度动作并执行类似的操作

private string Decompress(string aValue)
{
    byte[] gzBuffer = Convert.FromBase64String(aValue);
    using (MemoryStream ms = new MemoryStream())
    {
        int msgLength = BitConverter.ToInt32(gzBuffer, 0);
        ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

        byte[] buffer = new byte[msgLength];

        ms.Position = 0;
        using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
        {
            zip.Read(buffer, 0, buffer.Length);
        }

        return System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length);
    }
}

答案 1 :(得分:0)

只需在队列中发送带有args的“ dispatch”对象即可。

  q.push({ audioName, audioObj, dispatch },()=>{
})

在队列内,您可以执行以下操作

let q = async.queue(async function (task, callback) {
  uploadFileOnS3(audioName, task).then(function (audioS3) {
    console.log("s3 value is", audioS3);
    let params = { Key: audioName, Body: task.file, ACL: 'public-read' };
    let req = audioS3.upload(params);

    req.on('httpUploadProgress', function (evt) {
      let percentage = (evt.loaded / evt.total) * 100;
      console.log("percentage value is", percentage);
      task.dispatch({type: t.PERCENTAGE, payload : percentage}) //here
    });
  });
}, 1);