调度特定操作时如何取消传奇任务

编程入门 行业动态 更新时间:2024-10-23 03:13:28
本文介绍了调度特定操作时如何取消传奇任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在想办法解决我的问题,但我没有在网上找到足够好的解决方案.

I'm trying to figure out how to solve my problem, but I didn't find good enough solution on the web.

当操作 LoginActionType.REQUEST_SEND 被调度时,我需要取消 checkAuth 和注销任务.

I need to cancel checkAuth and logout tasks when action LoginActionType.REQUEST_SEND is dispatched.

function* handleLoginFetch(userCredentialsAction: PayloadAction<LoginActionType, UserCredentials>) { try { const response: AxiosResponse<AuthResponse> = yield call($http.put, '/users/login', userCredentialsAction.payload); if (response.status === HttpStatusCode.OK) { yield put(login.success(response.data.user)); } } catch (error) { yield put(login.failure()); } } function* handleCheckAuthFetch() { try { const response: AxiosResponse<AuthResponse> = yield call($http.get, '/users/logged-user', { params: { 'include': 'user_user_permissions' } }); if (response.status === HttpStatusCode.OK) { if (yield select(getUserLoggedIn)) { yield put(login.success(response.data.user)); } else { yield put(checkLocalAuth.success(response.data.user)); } } } catch (error) { yield put(checkLocalAuth.failure()); } } function* handleLogoutFetch() { try { const response: AxiosResponse = yield call($http.put, '/users/logout'); if (response.status === HttpStatusCode.OK) { yield put(logout.success()); } } catch (error) { yield put(logout.failure()) } } export default function* userSaga() { yield takeLatest(LoginActionType.REQUEST_SEND, handleLoginFetch); yield takeLatest(CheckLocalAuthActionType.REQUEST_SEND, handleCheckAuthFetch); yield takeEvery(LogoutActionType.REQUEST_SEND, handleLogoutFetch); }

推荐答案

NoriSte 的回答 真的帮助我理解了如何解决我的问题.我对 Saga 文档进行了更多研究,最终得到了这个解决方案.

NoriSte's answer really helped me to understand how to solve my issue. I studied Saga documentation a little bit more and I ended up with this solution.

export default function* userSaga() { const checkAuthTask = yield takeLatest(CheckLocalAuthActionType.REQUEST_SEND, handleCheckAuthFetch); const logoutTask = yield takeEvery(LogoutActionType.REQUEST_SEND, handleLogoutFetch); yield takeLatest(LoginActionType.REQUEST_SEND, function*(userCredentialsAction: PayloadAction<LoginActionType, UserCredentials>) { if(checkAuthTask) yield cancel(checkAuthTask); if(logoutTask) yield cancel(logoutTask); yield fork(handleLoginFetch, userCredentialsAction); }); }

更多推荐

调度特定操作时如何取消传奇任务

本文发布于:2023-11-08 07:04:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   传奇

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!