2017-09-13 19 views
2

私は.progressコールバックを提供するライブラリを使用しています。これはフェッチを行い、進行中にこのコールバックをトリガーします。私は次のようにしています:put in 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から削除すると、それが起動してconsole.logが表示されますが、putは効果がありません。私はRNFetchBlobを使用しています

、ネイティブのlibに反応は、ここではそのprogress callbacker上のドキュメントである - https://github.com/wkh237/react-native-fetch-blob/#user-content-uploaddownload-progress

答えて

2

function* progressDownload() {...}はジェネレータ関数ではなく、プレーンな機能です。

は、プレーン関数であるべき.progress(fn)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function%2A

そしてfnを参照してください。したがって、ジェネレータ関数は呼び出されません。 progressx値をreduxに設定する場合は、redux-sagaにchannel 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

+0

興味深いのおかげで非常に多くのリーを参照してください。私は理解し、実装しようとします。 – Noitidart

+0

Lee、ここでは 'channel'は最新の10個のメッセージしか取っていませんが、どうすれば制限なしで' channel'を行うことができますか? – Noitidart

+0

あなたのサーガはチャンネルの進行メッセージを受け取りません。 デフォルトでは、チャネルは10個のメッセージしかバッファしません。 (https://redux-saga.js.org/docs/api/#channelbuffer) また、別のバッファを設定することもできます。 (https://redux-saga.js.org/docs/api/#buffers) –

関連する問題