Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debouncing rx based on value

I'm trying to show a popup if my client is disconnected (false) for more than 10 seconds. However, I'm also dismissing the popup when the connection is regained (true). I need the popup to be dismissed instantly if true.

I think what I need to do is debounce based on value (false) but i'm not sure.

mConnectionObservable
.distinctUntilChanged()
.debounce(10, TimeUnit.SECONDS)
.subscribe(online -> {
    if (online) {
        //Dismiss popup
    } else {
        //Show popup about internet connection
    }
});
like image 761
Alan Avatar asked Dec 23 '22 16:12

Alan


1 Answers

You can try

mConnectionObservable.debounce(item -> (item? Observable.empty() : Observable.timer(10,TimeUnit.SECONDS)))
                     .distinctUntilChanged()

This dynamically changes the debounce period so that a true value is always emitted but a false value with have the 10 second debounce.

like image 133
JohnWowUs Avatar answered Jan 30 '23 23:01

JohnWowUs