Is there a way to merge two IObservables such that if B finishes before A, it halts the result (but not vice-versa)?

StackOverflow https://stackoverflow.com/questions/21865774

Question

I have two IObservable sequences that will return at most one item before completing. I want to merge them, but while the first sequence will usually complete first, in rare cases may complete second. If it completes second, then I want to ignore its output.

Normal case:
A       1
B       -  2
Result  1  2

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

Is there a simple way of doing this? I'm not aware of a way to prematurely end a merged sequence based on which IObservable triggered the last OnNext. The best solution I have is to Select and return a Tuple indicating which sequence the value came from, and simply ignoring everything after a "B" result.

Was it helpful?

Solution

For this case, the following should work:

var combined = A.TakeUntil(B).Merge(B);

OTHER TIPS

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

IObservable<int> result = a.TakeUntil(b).Merge(b);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top