Search code examples
c#.netsystem.reactivereactiverx.net

How to create an observable that signals the completion of two other observables?


I have two IObservable<T>, and want to create a IObservable<Unit> (or some sort of ISingle<Unit>, if such a thing exists) that emits a completion signal when both complete, and nothing else. What's the idiomatic way to do this in Rx.NET?

I'm used to Reactor Core, in which case I'd use flux1.then().and(flux2.then()). But I don't believe Rx.NET offers the then and and operators.


Solution

  • Assuming that both sequences are of the same type, you could use the Merge operator. It might be beneficial to throw the IgnoreElements in the mix, in order to reduce the synchronization stress associated with merging sequences that may contain huge number of elements.

    IObservable<Unit> completion = sequence1.IgnoreElements()
        .Merge(sequence2.IgnoreElements())
        .Select(_ => Unit.Default);
    

    If the sequences are of different type, you could project each one of them to an IObservable<Unit> before merging, or use the CombineLatest instead of the Merge. Lots of options.