Reputation: 656
Closest to what I want the combineLatest
function does the following:
I looked at almost all the functions of RxJava, but did not see anything suitable.
Upvotes: 0
Views: 137
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
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