gpt4 book ai didi

reactjs - redux-saga 中的重试功能

转载 作者:行者123 更新时间:2023-12-04 12:47:07 25 4
gpt4 key购买 nike

在我的应用程序中,我有以下代码

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 的聊天如何工作),或者后端出现错误时,它会在 5 秒后重试,但 setTimeout 需要每隔一秒设置一次以更新存储的一部分,即行 this.props.decreaseTimeOut(); ,但如果计数器已用完,那么五秒钟过去了, if block 将运行并重新调度 fetchData action

它运行良好,我对它没有任何问题,至少在功能方面,但在代码设计方面,我知道它是 side-effect,不应该在我的 react-component 中处理,因为我使用的是 redux -saga(但我是 redux-saga 的新手,我今天才学会),我想将该功能转换为 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 });
}
}
}

最佳答案

对您的代码来说,侵入性较小的是使用 delay来自 redux-saga 的 promise :

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

关于reactjs - redux-saga 中的重试功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43853198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com