Sasha Tobin
Sasha Tobin

Reputation: 37

What is RxJS (redux-observable) alternative to takeLatest from redux-saga?

If new action is received by epic, stop processing old action(s). A little bit of context: I have an epic, that emits few delayed actions. I need to cancel them, if new action is received by epic.

Redux-saga effect that does exactly what I want: takeLatest

Upvotes: 0

Views: 1341

Answers (1)

Oles Savluk
Oles Savluk

Reputation: 4345

That's exactly what .switchMap operator does:

Projects each source value to an Observable which is merged in the output Observable, emitting values only from the most recently projected Observable.

In this example you can see that previous interval is no longer processing after new click arrives:

const clicks = Rx.Observable.fromEvent(document, 'click');
const result = clicks.switchMap((ev) => Rx.Observable.interval(1000));

result.subscribe(x => console.log(x));
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
<button>send new click</button>

Upvotes: 3

Related Questions