简体   繁体   English

Redux-saga 有条件地获取最新信息

[英]Redux-saga takeLatest conditionally

I have a question related to redux-saga that is there any way to achieve takeLatest but with condition.我有一个与 redux-saga 相关的问题,有什么方法可以实现 takeLatest 但有条件。

For example, I have a dynamical list of song types (Rap, Pop, Hip_hop...), I want to fetch the songs by song type.例如,我有一个动态的歌曲类型列表(Rap、Pop、Hip_hop...),我想按歌曲类型获取歌曲。 I define an redux-action with type "FETCH_SONGS_BY_TYPE" then pass songType to it.我定义了一个类型为“FETCH_SONGS_BY_TYPE”的 redux-action,然后将 songType 传递给它。 The action will look like动作看起来像

// actions
const fetchSongsByType = songType => ({
  type: FETCH_SONGS_BY_TYPE,
  songType
});
--------------------------------------------------
//saga
function* fetchSongsByTypeSaga(action) {
   // request songs list by action.songType
}
function* Saga() {
  yield takeLatest(FETCH_SONGS_BY_TYPE, fetchSongsByTypeSaga);
}

So I want that the previous saga task is only canceled if the next saga run has the same songType.所以我希望只有在下一个 saga 运行具有相同的 SongType 时才取消上一个 saga 任务。

In above code, I got this:在上面的代码中,我得到了这个:

  1. fetchSongs - songType: Hip_hop (canceled because of [2]) fetchSongs - songType: Hip_hop(因 [2] 取消)
  2. fetchSongs - songType: Rap (canceled because of [3]) fetchSongs - songType: Rap(因 [3] 取消)
  3. fetchSongs - songType: Pop (canceled because of [4]) fetchSongs - songType: Pop(因 [4] 取消)
  4. fetchSongs - songType: Rap (canceled because of [5]) fetchSongs - songType: Rap(因 [5] 而取消)
  5. fetchSongs - songType: Pop fetchSongs - 歌曲类型:流行

But I expected it will be like this:但我预计它会是这样的:

  1. fetchSongs - songType: Hip_hop fetchSongs - 歌曲类型:Hip_hop
  2. fetchSongs - songType: Rap (canceled because of [4]) fetchSongs - songType: Rap(因 [4] 取消)
  3. fetchSongs - songType: Pop (canceled because of [5]) fetchSongs - songType: Pop(因 [5] 而取消)
  4. fetchSongs - songType: Rap fetchSongs - 歌曲类型:说唱
  5. fetchSongs - songType: Pop fetchSongs - 歌曲类型:流行

I appreciate any helps, thanks in advance.我感谢任何帮助,提前致谢。

If you take a look at the documentation oftakeLatest , you will see how this effect is built using the low-level effects.如果您查看takeLatest的文档,您将看到如何使用低级效果构建此效果。 With this example, you can easily create your custom effect which only cancels actions from the same music genre.通过此示例,您可以轻松创建自定义效果,该效果仅取消相同音乐流派的操作。

takeLatestByType: takeLatestByType:

const takeLatestByType = (patternOrChannel, saga, ...args) => fork(function*() {
  // hold a reference to each forked saga identified by the type property
  let lastTasks = {};

  while (true) {
    const action = yield take(patternOrChannel);

    // if there is a forked saga running with the same type, cancel it.
    if (lastTasks[action.type]) {
      yield cancel(lastTasks[action.type]);
    }

    lastTasks[action.type] = yield fork(saga, ...args.concat(action));
  }
});

Usage:用法:

function* Saga() {
  yield takeLatestByType(FETCH_SONGS_BY_TYPE, fetchSongsByTypeSaga);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM