[英]How to structure sagas depending on an API accessToken
我有一个传奇的loginSaga
,它执行api调用以获得accessToken
。 一旦成功,我想让其他的sagas来使用该accessToken
加速下载数据
这些sagas取决于accessToken
,因此它们应该只运行直到LOGOUT_SUCCESS
出现。 然后他们应该完成。 如果他们当前正在下载某些东西,则应取消此操作,结果将被忽略。
这就是我现在所拥有的:
function *authorize(credentials) {
try {
const { email, password, device_name, device_family, device_token } = credentials
const { access_token, user } = yield call(api.loginAsync, email, password, { device_name, device_family, device_token })
yield put(actions.loginSuccess(access_token, user))
} catch(error) {
console.error(error)
if (!isCancelError(error)) {
if (error.response && error.response.status == 401) {
error.message = "E-Mail or Password wrong"
}
yield put(actions.loginError(error))
}
}
}
function *loginFlow() {
while(true) {
console.info("Waiting for account/LOGIN")
const credentials = yield take(actions.LOGIN)
const authorizeTask = yield fork(authorize, credentials)
console.info("Waiting for account/LOGOUT")
const { type } = yield take([actions.LOGOUT, actions.LOGIN_ERROR])
cancel(authorizeTask)
if (type == actions.LOGOUT) {
yield call(api.logoutAsync)
yield put(actions.logoutSuccess())
}
}
}
然后在LOGIN_SUCESS
取决于sagas:
function *refreshUser() {
while (true) {
const result = yield take([actions.LOGIN_SUCCESS, actions.REFRESH_USER])
try {
const user = yield call(api.getUserAsync)
yield put(actions.userRefreshed(user))
} catch (error) {
}
}
}
function *getActivities(access_token) {
console.info("fetch activities")
try {
const activities = yield call(api.getActivitiesAsync, access_token)
console.info("activities fetched")
yield put(activitiesUpdated(activities))
} catch (error) {
}
}
function *updateActivities() {
while (true) {
const { access_token } = yield take(actions.LOGIN_SUCCESS)
console.info("Calling getActivities")
yield call(getActivities, access_token)
while (true) {
const {type } = yield take([actions.REFRESH_ACTIVITIES, actions.LOGOUT])
if (type == actions.LOGOUT) {
break
}
yield call(getActivities, access_token)
}
}
}
并且由于它们都需要accessToken
因此它们都必须等到LOGIN_FINISHED
为止,因为这LOGIN_FINISHED
其携带在有效负载中。
另外,我的sagas都是从一开始就开始等待的。
我想,也许,当authorize
流程共完成那么它可以旋转起来在loginDependingSagas
用accessToken
作为PARAM。
像这样:
函数forkLoginDependingSagas(accessToken){fork refreshUser(accessToken)fork updateActivities(accessToken)}
那是一个好模式吗?
我使用了一个单独的传奇来解决此问题,该传奇等待携带access_token
动作,然后根据此access_token
派生sagas。
function* authorizedFlow() {
let payload
while (({ payload } = yield take([actions.LOGIN_SUCCESS, actions.START_SIGNUP_SUCCESS]))) {
const { access_token } = payload
yield fork(logoutFlow, access_token)
yield race({
watchers: [
call(refreshUser, access_token),
call(refreshInvoices, access_token),
call(refreshPendingTransactions, access_token),
],
logout: take(actions.LOGOUT_SUCCESS),
})
}
}
function* logoutFlow(access_token) {
while (yield take(actions.LOGOUT)) {
try {
yield call(api.logoutAsync, access_token)
yield put(actions.logoutSuccess())
} catch (error) {
yield put(actions.logoutError(error))
}
}
}
如果成功注销,则logoutFlow将确保依赖access_token
sagas之间的race
将导致取消它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.