I have BehaviorSubject<Boolean> and I wonder how to get the following behaviour:
- When comes
Falseit should wait for 10 sec before emit the value - When comes
Trueit emits the value immediately, and also cancel previous 10 sec timer if exist
On
private val subject = BehaviorSubject.create<Boolean>()
private fun testSubject() {
subject
.doOnNext { println("onNext: $it") }
.debounce {
if (it) Observable.empty<Boolean>()
else Observable.timer(10, TimeUnit.SECONDS)
}
.subscribe {
println("Received $it")
}
}
On
Utilize RxJava operators and adjust how you handle BehaviorSubject changes. Here's how you can implement it:
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.subjects.BehaviorSubject;
import java.util.concurrent.TimeUnit;
BehaviorSubject < Boolean > subject = BehaviorSubject.createDefault(false);
subject
.flatMap(value - > {
if (value) {
return Observable.just(value);
} else {
return Observable.just(value)
.delay(10, TimeUnit.SECONDS)
.takeUntil(subject.filter(cancel - > cancel));
}
})
.subscribe(System.out::println);
// Simulate updates BehaviorSubject
subject.onNext(true); // Should emit immediately
Thread.sleep(3000);
subject.onNext(false); // Should wait 10 seconds before emitting
Thread.sleep(5000);
subject.onNext(true); // Should cancel previous delay and emit immediately
Thread.sleep(15000); // Wait for remaining 5 seconds in first delay
// Ensure that the program continues running
subject.onComplete();
subject a BehaviorSubject manage our boolean values.flatMap operator.Observable.just(value), we emit the value instantly when it is {true}.false. Additionally, in the event that a later true value is obtained, we employ takeUntil end the wait.
We mimic change {BehaviorSubject} by using various values when we use onNext(true/false).It can be achieved with this setup: emitting {true} values right away while canceling any prior delay, and waiting for 10 seconds before emitting `false} values.
You'll need a
flatMapto introduce a delay and atakeUntilwired into it to cancel it when a true appears.