2017-09-13 51 views
2

我正在使用一個提供.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,一個陣營本地庫,這裏是它的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應該是純功能。所以發電機功能不被調用。如果您想將進度值設置爲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

+0

有趣,非常感謝李。我會盡力去理解和實施。 – Noitidart

+0

李,這裏沒有'頻道'只帶最新的10條短信,我怎麼做無限制的'頻道'? – Noitidart

+0

我認爲你的傳奇並不把頻道中的進度信息。 默認情況下,通道僅緩衝10條消息。 (https://redux-saga.js.org/docs/api/#channelbuffer) 而且你可以設置另一個緩衝區。 (https://redux-saga.js.org/docs/api/#buffers) –