2017-05-08 98 views
1

在我的应用我有以下代码重试功能

componentWillUpdate(nextProps) { 
    if(nextProps.posts.request.status === 'failed') { 
    let timer = null; 

    timer = setTimeout(() => { 
     if(this.props.posts.request.timeOut == 1) { 
     clearTimeout(timer); 
     this.props.fetchData({ 
      page: this.props.posts.request.page 
     }); 
     } else { 
     this.props.decreaseTimeOut(); 
     } 
    }, 1000); 
    } 
} 

它的作用是,当API请求时遇到一个错误,也许是因为没有互联网连接(如Facebook的如何聊天作品),或者后端出现错误,它会在五秒后重试,但需要每隔一秒设置一次setTimeout以更新商店的一部分,即行this.props.decreaseTimeOut();,但是如果计数器已运行因此5秒钟过去了,if block将运行并重新发送fetchData action

它运行良好,我没有问题,至少在功能方面,但在代码设计方面,我知道这是一个side-effect,它不应该在我的反应组件中处理,因为我我使用了redux-saga(但是我对redux-saga是新手,我今天刚刚学到了),我想将这个功能转化为一个传奇,我还没有完全理解如何做到这一点,顺便提一句,这里是我的fetchData saga

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

import axios from 'axios'; 

export default function* fetchData() { 
    while(true) { 
    try { 
     let action = yield take('FETCH_DATA_START'); 
     let response = yield call(axios.get, '/posts/' + action.payload.page); 
     yield put({ type: 'FETCH_DATA_SUCCESS', items: [...response.data.items] }); 
    } catch(err) { 
     yield put({ type: 'FETCH_DATA_FAILED', timeOut: 5 }); 
    } 
    } 
} 

回答

2

为您的代码使用的是终极版,传奇的delay承诺的侵扰程度较低的事:

catch(err) { 
    yield put({ type: 'FETCH_DATA_FAILED'}); 

    for (let i = 0; i < 5; i++) { 
     yield call(delay, 1000); 
     yield put(/*Action for the timeout/*); 
    } 
} 

但是我重构这样的代码:

function* fetchData(action) { 
    try { 
     let response = yield call(axios.get, '/posts/' + action.payload.page); 
     yield put({ type: 'FETCH_DATA_SUCCESS', items:[...response.data.items] }); 
    } catch(err) { 
     yield put({ type: 'FETCH_DATA_FAILED'}); 
     yield put({ type: 'SET_TIMEOUT_SAGA', time: 5 }); 
    } 
    } 
} 

function *setTimeoutsaga(action) { 
    yield put({type: 'SET_STATE_TIMEOUT', time: action.time}); // Action that update your state 
    yield call(delay, 1000); 

    // Here you use a selector which take the value if is disconnected: 
    // https://redux-saga.js.org/docs/api/#selectselector-args 
    const isStillDisconnected = select() 
    if (isStillDisconnected) { 
     yield put({type: 'SET_TIMEOUT_SAGA', time: action.time - 1}); 
} 

function *fetchDataWatchers() { 
    yield takeEvery('FETCH_DATA_START', fetchData); 
    yield takeEvery('SET_TIMEOUT_SAGA', setTimeoutSaga); 

    // You can insert here as many watcher you want 
} 

export default [fetchDataWatchers]; // You will use run saga for registering this collection of watchers 
+0

看起来真棒但我不想使用takeEvery为我的FETCH_DATA_START,因为这将允许用户一次又一次地点击'提取数据按钮'发送多个请求,我不想这样做,我只想发送请求一次一个,所以我用了,是不是很糟糕? –

+0

@ four-eyes-04-04那么你可以使用'takeLatest'来完成你所需要的功能:) 但无论如何,你的代码是好的,我只是不喜欢'while true'结构,但是如果你更喜欢那就像我在第一个片段 – rpadovani

+0

中建议的那样使用'delay'感谢您的建议,我能够成功实现该功能。 :-) –