These are chat archives for ReactiveX/RxJava

4th
Jul 2018
SlawpLabs
@SlawpLabs
Jul 04 2018 08:54
Hello people :) I have a small question about the throttleLast() operator (or what I think it does).
What I'm trying to achieve: I'm getting several data refreshes in my app, and I would like to avoid refreshes that I consider too close to each other. Basically, I would like to "throttle" the events on my Observable so that no more than 1 event would be emitted during, say, 500ms. In the event (bad choice of word) of several events during that window (500ms), I would only like to emit the last one.
I chose to use the throttleLast() operator, that seem to do what I'm trying to achieve, but if my events are too close from each other (less than 500ms), nothing gets emitted :o Did I miss something? I also tried to change the target thread, without any luck. What am I doing wroooong? :)
Thx people
Ignacio Baca Moreno-Torres
@ibaca
Jul 04 2018 16:21
@SlawpLabs throtleLast should do what you expect, BUT if the observable completes before the 500ms nothing get emmited, is this the casse?
Ignacio Baca Moreno-Torres
@ibaca
Jul 04 2018 16:31
if your observable completes, you can use something like this to “flush” the last item…
static <T> ObservableTransformer<T, T> debounceLast(long intervalDuration, TimeUnit unit) {
    return o -> o.publish(s -> {
        return Observable.merge(
                just(s.throttleLast(intervalDuration, unit)), // throttle emmit until…
                s.lastElement().map(Observable::just).toObservable() // …the last element
        ).switchMap(identity());
    });
}