如何运行并行Sagas同步

时间:2019-08-19 18:23:06

标签: react-redux redux-saga saga

我已将佐贺文件上传到服务器

function * uploadImageSaga (action: Object): Saga {
  const {name, id} = action

  const payload = {
      image: action.base64
    }

    //....upload....

  yield put(uploadFileSucc(id, name, result.link))
}

function * uploadImagesSagas (): Saga {
  yield all([
    takeEvery(ACTIONS.UPLOAD_FILE, uploadImageSaga),
  ])
}

export default uploadImagesSagas

它工作正常。

问题是,在上传10张图片(浏览器冻结:-))时,佐贺被称为10次的故事...

我想在第一局之后跑第二次,在第二局之后跑第三次..同步。

这是可能的,怎么办? saga核心中有任何功能吗?

1 个答案:

答案 0 :(得分:1)

是的,这正是actionChannel效果的用途。您可以在此处的saga文档中阅读有关此内容的信息:https://redux-saga.js.org/docs/advanced/Channels.html

在您的情况下:

function * uploadImageSaga (action: Object): Saga {
  // no change to this saga from what you have
}

function * uploadImagesSagas (): Saga {
  const chan = yield actionChannel(ACTIONS.UPLOAD_FILE);
  while (true) {
    const action = yield take(chan);
    yield call(uploadImageSaga, action);
  }
}

export default uploadImagesSagas