CombineLatest<TSource, TResult>
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Disposables;
namespace System.Reactive.Linq.ObservableImpl
{
internal sealed class CombineLatest<TSource, TResult> : Producer<TResult, CombineLatest<TSource, TResult>._>
{
internal sealed class _ : Sink<TResult>
{
private sealed class SourceObserver : IObserver<TSource>
{
private readonly _ _parent;
private readonly int _index;
public SourceObserver(_ parent, int index)
{
_parent = parent;
_index = index;
}
public void OnNext(TSource value)
{
_parent.OnNext(_index, value);
}
public void OnError(Exception error)
{
_parent.OnError(error);
}
public void OnCompleted()
{
_parent.OnCompleted(_index);
}
}
private readonly Func<IList<TSource>, TResult> _resultSelector;
private object _gate;
private bool[] _hasValue;
private bool _hasValueAll;
private List<TSource> _values;
private bool[] _isDone;
private IDisposable[] _subscriptions;
public _(Func<IList<TSource>, TResult> resultSelector, IObserver<TResult> observer, IDisposable cancel)
: base(observer, cancel)
{
_resultSelector = resultSelector;
}
public IDisposable Run(IEnumerable<IObservable<TSource>> sources)
{
IObservable<TSource>[] array = Enumerable.ToArray<IObservable<TSource>>(sources);
int num = array.Length;
_hasValue = new bool[num];
_hasValueAll = false;
_values = new List<TSource>(num);
for (int i = 0; i < num; i++) {
_values.Add(default(TSource));
}
_isDone = new bool[num];
_subscriptions = new IDisposable[num];
_gate = new object();
for (int j = 0; j < num; j++) {
int num2 = j;
SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
_subscriptions[num2] = singleAssignmentDisposable;
SourceObserver observer = new SourceObserver(this, num2);
singleAssignmentDisposable.Disposable = ObservableExtensions.SubscribeSafe<TSource>(array[num2], (IObserver<TSource>)observer);
}
return StableCompositeDisposable.Create(_subscriptions);
}
private void OnNext(int index, TSource value)
{
lock (_gate) {
_values[index] = value;
_hasValue[index] = true;
if (_hasValueAll || (_hasValueAll = _hasValue.All())) {
TResult val = default(TResult);
try {
val = _resultSelector(new ReadOnlyCollection<TSource>(_values));
} catch (Exception error) {
_observer.OnError(error);
base.Dispose();
return;
}
_observer.OnNext(val);
} else if (_isDone.AllExcept(index)) {
_observer.OnCompleted();
base.Dispose();
}
}
}
private void OnError(Exception error)
{
lock (_gate) {
_observer.OnError(error);
base.Dispose();
}
}
private void OnCompleted(int index)
{
lock (_gate) {
_isDone[index] = true;
if (_isDone.All()) {
_observer.OnCompleted();
base.Dispose();
} else
_subscriptions[index].Dispose();
}
}
private static bool All(bool[] values)
{
for (int i = 0; i < values.Length; i++) {
if (!values[i])
return false;
}
return true;
}
private static bool AllExcept(bool[] values, int index)
{
for (int i = 0; i < values.Length; i++) {
if (i != index && !values[i])
return false;
}
return true;
}
}
private readonly IEnumerable<IObservable<TSource>> _sources;
private readonly Func<IList<TSource>, TResult> _resultSelector;
public CombineLatest(IEnumerable<IObservable<TSource>> sources, Func<IList<TSource>, TResult> resultSelector)
{
_sources = sources;
_resultSelector = resultSelector;
}
protected override _ CreateSink(IObserver<TResult> observer, IDisposable cancel)
{
return new _(_resultSelector, observer, cancel);
}
protected override IDisposable Run(_ sink)
{
return sink.Run(_sources);
}
}
}