Alektas
Alektas

Reputation: 656

Is there any function in RxJava2, like the CombineLatest that does not expect events from both observers?

Closest to what I want the combineLatest function does the following: enter image description here

But I need this: enter image description here

I looked at almost all the functions of RxJava, but did not see anything suitable.

Upvotes: 0

Views: 137

Answers (2)

Alektas
Alektas

Reputation: 656

Thanks to @zella and @Gustavo for their solutions, this is almost what I need, but in their cases, the combined source emits only on the event of one of the source sources. So the snippet below does exactly what I want:

Observable.combineLatest(
    source1.startWith(""),
    source2.startWith(""),
    BiFunction<String, String, String> { s1, s2->
        s1 + s2
    })

Upvotes: 0

zella
zella

Reputation: 4685

You could add "empty-like" element in the beginning, so "1" + "" = "1":

    Subject<String> two = PublishSubject.create();
    Subject<Integer> one = PublishSubject.create();

    Observable
            .combineLatest(one, Observable.just("").concatWith(two), (i, s) -> "" + i + s)
            .subscribe(s -> System.out.print(s + " "));

    one.onNext(1);
    one.onNext(2);
    two.onNext("A");
    two.onNext("B");
    two.onNext("C");
    two.onNext("D");
    one.onNext(3);
    one.onNext(4);
    one.onNext(5);

prints:

1 2 2A 2B 2C 2D 3D 4D 5D 

Upvotes: 1

Related Questions