Is there a way to combine the two IObservables so that if B ends before A, it stops the result (but not vice versa)?

I have two sequences IObservablethat will return no more than one element before completion. I want to combine them, but while the first sequence, as a rule, ends with the first, in rare cases it can end with a second. If it ends second, I want to ignore its conclusion.

Normal case:
A       1
B       -  2
Result  1  2

Rare, "slow A" case:
A       -  1
B       2
Result  2

Is there an easy way to do this? I do not know how to prematurely complete the combined sequence, on the basis of which the IObservablelatter was called OnNext. The best solution I have is to Selectreturn a Tuple, indicating from which sequence the value was obtained, and simply ignore everything after the result of "B".

+3
source share
2 answers

In this case, the following should work:

var combined = A.TakeUntil(B).Merge(B);
+4
source

If we have two variables for our threads IObservable<int> a,b;, then:

IObservable<int> result = a.TakeUntil(b).Merge(b);
+3
source

All Articles