我有3个效果中的2个相互竞争,如果他们赢得比赛,应该从服务器刷新。
我目前的表现如何:
function* refreshItems() {
while (true) {
bool refreshFromServer = true
const { nextRefresh, items } = yield call(fetchItems)
const racer = {
duration: call(delay, 60*1000),
manual: take(REFRESH_ITEMS),
}
if (nextRefresh > 0) {
racer.remote = call(delay, nextRefresh * 1000)
}
const { remote, manual, duration } = yield race(racer)
refreshFromServer = remote || manual
// alternative: refreshFromServer = !duration
}
}
我想知道是否有更聪明的方法来促进比赛的回归结果?
答案 0 :(得分:0)
你的方式似乎很好,但你可以做的一件事就是参加比赛。像(未经测试)的东西:
function* refreshItems() {
while (true) {
const { nextRefresh, items } = yield call(fetchItems)
const refreshRace = {
manual: take(REFRESH_ITEMS)
}
if (nextRefresh > 0) {
refreshRace.remote = call(delay, nextRefresh * 1000)
}
const racer = {
duration: call(delay, 60*1000),
refresh: race(refreshRace),
}
const { refresh, duration } = yield race(racer)
... just use refresh, now
}
}
顺便说一下,其他一些说明: