2
votes

I'm working in a GUI and I have multiple types of events that may cause a button to be disabled. I've tried several ways to combine these two Observables, but every solution that I've found has required both Observables to publish an event before a result is produced. For example, in this code:

public class Test {

    public static void main(String[] args) {
        PublishSubject<Boolean> conditionAStream = PublishSubject.create();
        PublishSubject<Boolean> conditionBStream = PublishSubject.create();

        conditionAStream
                .zipWith(conditionBStream, (conditionA, conditionB) -> conditionA && conditionB)
                .subscribe(result -> System.out.println(result));

        conditionAStream.onNext(false);
        conditionBStream.onNext(false);
        //Expected output: false

        conditionAStream.onNext(true);
        //Expected output: false

        conditionBStream.onNext(true);
        //Expected output: true

        conditionAStream.onNext(false);
        //Expected output: false
    }
}

But the output I get is:

false
true

Is there any way to combine to Observables so that they "cache" the last result and react to every change?

1

1 Answers

3
votes

You're looking for combineLatest() which will emit every time any of the combined observables emits. It still needs each of them to emit at least once, but you can add .startWith(), to all your combined observables to make sure they have an initial emission.