I have a question related to redux-saga that is there any way to achieve takeLatest but with condition.
For example, I have a dynamical list of song types (Rap, Pop, Hip_hop...), I want to fetch the songs by song type. I define an redux-action with type "FETCH_SONGS_BY_TYPE" then pass songType to it. 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.
In above code, I got this:
- fetchSongs - songType: Hip_hop (canceled because of [2])
- fetchSongs - songType: Rap (canceled because of [3])
- fetchSongs - songType: Pop (canceled because of [4])
- fetchSongs - songType: Rap (canceled because of [5])
- fetchSongs - songType: Pop
But I expected it will be like this:
- fetchSongs - songType: Hip_hop
- fetchSongs - songType: Rap (canceled because of [4])
- fetchSongs - songType: Pop (canceled because of [5])
- fetchSongs - songType: Rap
- fetchSongs - songType: Pop
I appreciate any helps, thanks in advance.