具有相同后续流程的 2 out 3 race redux-saga 效果的智能方式

Smart way for 2 out 3 raced redux-saga effects having the same follow up flow

我有 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
  }
}

我想知道是否有更聪明的方法来促进比赛的 return 结果?

你的方法似乎不错,但你可以做的一件事是编写比赛。像(未经测试):

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
  }
}

顺便说一句,其他一些注意事项:

  • 你的两次延迟比赛似乎有点傻
  • 根据您的具体需要,您可以使用去抖动?或者写类似的东西