我正在使用提供.progress
回调的库。它进行了提取,并且在进度上会触发此回调。我这样做:
const res = yield call(function fetchDownload() {
return RNFetchBlob.config({ fileCache:true }).fetch('GET', url)
.progress(function* progressDownload(received, total) {
console.log(`progressed received: "${received}" total: "${total}"`);
yield put(update(url, { progress:received/total }));
});
});
但是,progressDownload
回调永远不会触发。如果我从 function* progressDownload
中删除超级巨星,然后触发和触发控制台。但是, put
没有效果。
我正在使用rnfetchblob,一个反应本机lib,这是其progress
呼叫者上的文档-https://github.com/wkh237/react-native-fetch-blob/#user-content-content-content-uploaddownload-progress-progress
function* progressDownload() {...}
是生成器函数,而不是普通函数。
和 .progress(fn)
中的 fn
应该是一个普通的函数。因此未调用发电机函数。如果要将进度值放在redux中,则可以在redux-saga中使用频道API。
喜欢以下
import {channel} from 'redux-saga';
import {/*... effects */} from 'redux-saga/effects;
//....
const progressChan = yield call(channel);
const putProgressToChannel = (received, total) => progressChan.put({received, total});
yield fork(updateProgressSaga, progressChan)
...blahblah.progress(putProgressToCahnnel);
//....
function* updateProgressSaga(progressChan) {
while(true) {
const {received, total} = take(progressChan);
put(....);
}
}
请参阅更多https://redux-saga.js.org/docs/advanced/channels.html
这是我的解决方案,感谢@lee:
const url = 'blah.com';
const progressChan = channel();
const progressTask = yield fork(
function*() {
while (true) {
const { percent } = take(progressChan);
yield put(update(url, { progress:percent }));
}
}
);
const res = yield call(
RNFetchBlob.config({ fileCache:true }).fetch('GET', url)
.progress((received, total) => progressChan.put({ type:'PROGRESS', percent:received/total })
);
yield cancel(progressTask);
yield put(setProgress(100));