本文介绍了如何“收益看跌"在回调中的 redux-saga 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为回调中不允许使用yield"语句,我如何在回调中使用 redux-saga 的put"功能?

Because "yield"-statement isn't allowed within a callback, how can i use the "put" feature of redux-saga within a callback?

我想要以下回调:

function onDownloadFileProgress(progress) {
  yield put({type: ACTIONS.S_PROGRESS, progress})
}

这不起作用并以意外令牌"结束,因为 yield 在普通函数中是不允许的.否则我无法将回调作为函数 *"传递,这将允许屈服.ES6 在这里似乎坏了.

This isn't working and ends up in "unexpected token", because yield is not allowed in a plain function. Otherwise i can't pass a callback as a "function *", that would allow yield. ES6 seems broken here.

我读过 redux-saga 提供了一些称为频道"的功能,但老实说,我不明白.我已多次阅读有关这些渠道和示例代码的内容,但在所有示例中,它们都解决了非常困难且不同的问题,而不是我的简单案例,最终我已经解决了.

I've read that redux-saga provides some features called "channels", but to be honest, i didn't get it. I've read several times about these channels and example code, but in all examples they've solved very difficult and different problems, not my simple case and at the end of the day i've got up there.

有人能告诉我如何解决这个问题吗?

Can someone tell me a solution how to deal with this issue?

整个上下文:

function onDownloadFileProgress(progress) {
  yield put({type: ACTIONS.S_PROGRESS, progress})
}

export function * loadFile(id) {
  let url = `media/files/${id}`;

  const tempFilename = RNFS.CachesDirectoryPath + '/' + id;

  const download = RNFS.downloadFile( {
    fromUrl: url,
    toFile: tempFilename,
    background: false,
    progressDivider: 10,
    progress: onDownloadFileProgress,
  })

  yield download.promise;

}

推荐答案

正如您已经提到的,一种可能的解决方案是使用 channels.这是一个适用于您的情况的示例:

One possible solution, as you already mentioned, is to use channels. Here is an example that should work in your case:

import { channel } from 'redux-saga'
import { put, take } from 'redux-saga/effects'

const downloadFileChannel = channel()

export function* loadFile(id) {
  ...
  const download = RNFS.downloadFile({
     ...
     // push `S_PROGRESS` action into channel on each progress event
     progress: (progress) => downloadFileChannel.put({
       type: ACTIONS.S_PROGRESS,
       progress,
     }),
  })
  ...
}

export function* watchDownloadFileChannel() {
  while (true) {
    const action = yield take(downloadFileChannel)
    yield put(action)
  }
}

这里的想法是,对于从 RNFS.downloadFile 发出的每个进度事件,我们将在通道上推送一个 S_PROGRESS 操作.

The idea here is that we will push a S_PROGRESS action on the channel for each progress event that is emitted from RNFS.downloadFile.

我们还必须启动另一个 saga 函数,该函数在 while 循环 (watchDownloadFileChannel) 中监听每个推送的动作.每次从通道中执行操作时,我们都会使用正常的 yield put 告诉 redux-saga 应该调度此操作.

We also have to start another saga function that is listening to each pushed action in a while loop (watchDownloadFileChannel). Everytime an action has been taken from the channel, we use the normal yield put to tell redux-saga that this action should be dispatched.

希望这个回答能帮到你.

I hope this answer will help you.

这篇关于如何“收益看跌"在回调中的 redux-saga 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 16:22