soren.enemaerke
soren.enemaerke

Reputation: 4830

Reactive method to combine property in list to single observable

We are struggling to find an appropriate method to combine a property value from a list of elements into a single observable element. Here is some sample code (simplified):

public class Result {
  public ISubject<bool> Completed { get; }
}

public void SignalWhenAllIsDone(){
  List<Result> list = GetListOfResults(); //not important

  // somehow merge the list and the Completed property into a single observable
  IObservable<bool> allCompleted = ???;

  allCompleted.Subscribe(x => {
    Console.WriteLine("all results have completed");
  });  
}

We think that there is some magic in the reactive toolbox that can subscribe to all the Completed subjects and merge/evaluate/condense it into a single observable that we can use. We have been doing some manual bookkeeping but our instincts tell us that this is something reactive can assist with.

Any help?

Upvotes: 0

Views: 498

Answers (1)

Awesomni.Codes
Awesomni.Codes

Reputation: 2430

Almost all of the extension methods for combining Observables provide an overload where they take an IEnumerable<IObservable<T>>. This should fit your requirements:

IObservable<bool> allCompleted = Observable.CombineLatest(
              list.Select(res => res.Completed),
              completedValuesList => completedValuesList.All(isCompleted => isCompleted);

As an extension to this answer I want to inform you about DynamicData. With this you could also write a query against a special List that gives you a valid allCompleted-Observable even when the List changes.

Upvotes: 1

Related Questions