React redux saga - 使用 race 根本没有效果
React redux saga - using race has no effect at all
我有以下传奇效果:
function* loginSaga() {
const logoutTimeoutCreationDate: string | null = yield localStorage.getItem('logoutTimeoutCreationDate');
let logoutTimeout: number;
if (!logoutTimeoutCreationDate || +logoutTimeoutCreationDate + 5000 < new Date().getTime()) {
yield localStorage.setItem('logoutTimeoutCreationDate', new Date().getTime().toString());
logoutTimeout = yield 5000;
} else {
logoutTimeout = yield +logoutTimeoutCreationDate + 5000 - new Date().getTime();
}
yield race([
function*() {
yield delay(logoutTimeout);
yield put(authActions.logout());
},
function*() {
yield take(authActions.AUTH_LOGOUT);
},
]);
}
yield race(..)
之前的所有代码都按预期工作,但 yield race(..)
没有效果。意味着它甚至不存在。我在两者中插入 console.log()
s 行来检查它(在 function*() {...}
内)- 但没有得到任何安慰。
怎么了?
您 race
的使用方式有误。您可以这样修复:
const [delayAction, logoutAction] = yield race([
yield delay(logoutTimeout),
yield take(authActions.AUTH_LOGOUT)
]);
if(delayAction){
yield put(authActions.logout());
}
你应该使用其他没有 yield 的语法:
const [delayAction]: [CallEffect, TakeEffect] = yield race<CallEffect | TakeEffect>([
delay(logoutTimeout),
take(authActions.AUTH_LOGOUT)
]);
if(delayAction){
yield put(authActions.logout());
}
我有以下传奇效果:
function* loginSaga() {
const logoutTimeoutCreationDate: string | null = yield localStorage.getItem('logoutTimeoutCreationDate');
let logoutTimeout: number;
if (!logoutTimeoutCreationDate || +logoutTimeoutCreationDate + 5000 < new Date().getTime()) {
yield localStorage.setItem('logoutTimeoutCreationDate', new Date().getTime().toString());
logoutTimeout = yield 5000;
} else {
logoutTimeout = yield +logoutTimeoutCreationDate + 5000 - new Date().getTime();
}
yield race([
function*() {
yield delay(logoutTimeout);
yield put(authActions.logout());
},
function*() {
yield take(authActions.AUTH_LOGOUT);
},
]);
}
yield race(..)
之前的所有代码都按预期工作,但 yield race(..)
没有效果。意味着它甚至不存在。我在两者中插入 console.log()
s 行来检查它(在 function*() {...}
内)- 但没有得到任何安慰。
怎么了?
您 race
的使用方式有误。您可以这样修复:
const [delayAction, logoutAction] = yield race([
yield delay(logoutTimeout),
yield take(authActions.AUTH_LOGOUT)
]);
if(delayAction){
yield put(authActions.logout());
}
你应该使用其他没有 yield 的语法:
const [delayAction]: [CallEffect, TakeEffect] = yield race<CallEffect | TakeEffect>([
delay(logoutTimeout),
take(authActions.AUTH_LOGOUT)
]);
if(delayAction){
yield put(authActions.logout());
}