using System; using UniRx.Operators; namespace UniRx.Operators { internal class AggregateObservable : OperatorObservableBase { readonly IObservable source; readonly Func accumulator; public AggregateObservable(IObservable source, Func accumulator) : base(source.IsRequiredSubscribeOnCurrentThread()) { this.source = source; this.accumulator = accumulator; } protected override IDisposable SubscribeCore(IObserver observer, IDisposable cancel) { return source.Subscribe(new Aggregate(this, observer, cancel)); } class Aggregate : OperatorObserverBase { readonly AggregateObservable parent; TSource accumulation; bool seenValue; public Aggregate(AggregateObservable parent, IObserver observer, IDisposable cancel) : base(observer, cancel) { this.parent = parent; this.seenValue = false; } public override void OnNext(TSource value) { if (!seenValue) { seenValue = true; accumulation = value; } else { try { accumulation = parent.accumulator(accumulation, value); } catch (Exception ex) { try { observer.OnError(ex); } finally { Dispose(); } return; } } } public override void OnError(Exception error) { try { observer.OnError(error); } finally { Dispose(); } } public override void OnCompleted() { if (!seenValue) { throw new InvalidOperationException("Sequence contains no elements."); } observer.OnNext(accumulation); try { observer.OnCompleted(); } finally { Dispose(); } } } } internal class AggregateObservable : OperatorObservableBase { readonly IObservable source; readonly TAccumulate seed; readonly Func accumulator; public AggregateObservable(IObservable source, TAccumulate seed, Func accumulator) : base(source.IsRequiredSubscribeOnCurrentThread()) { this.source = source; this.seed = seed; this.accumulator = accumulator; } protected override IDisposable SubscribeCore(IObserver observer, IDisposable cancel) { return source.Subscribe(new Aggregate(this, observer, cancel)); } class Aggregate : OperatorObserverBase { readonly AggregateObservable parent; TAccumulate accumulation; public Aggregate(AggregateObservable parent, IObserver observer, IDisposable cancel) : base(observer, cancel) { this.parent = parent; this.accumulation = parent.seed; } public override void OnNext(TSource value) { try { accumulation = parent.accumulator(accumulation, value); } catch (Exception ex) { try { observer.OnError(ex); } finally { Dispose(); } return; } } public override void OnError(Exception error) { try { observer.OnError(error); } finally { Dispose(); } } public override void OnCompleted() { observer.OnNext(accumulation); try { observer.OnCompleted(); } finally { Dispose(); } } } } internal class AggregateObservable : OperatorObservableBase { readonly IObservable source; readonly TAccumulate seed; readonly Func accumulator; readonly Func resultSelector; public AggregateObservable(IObservable source, TAccumulate seed, Func accumulator, Func resultSelector) : base(source.IsRequiredSubscribeOnCurrentThread()) { this.source = source; this.seed = seed; this.accumulator = accumulator; this.resultSelector = resultSelector; } protected override IDisposable SubscribeCore(IObserver observer, IDisposable cancel) { return source.Subscribe(new Aggregate(this, observer, cancel)); } class Aggregate : OperatorObserverBase { readonly AggregateObservable parent; TAccumulate accumulation; public Aggregate(AggregateObservable parent, IObserver observer, IDisposable cancel) : base(observer, cancel) { this.parent = parent; this.accumulation = parent.seed; } public override void OnNext(TSource value) { try { accumulation = parent.accumulator(accumulation, value); } catch (Exception ex) { try { observer.OnError(ex); } finally { Dispose(); } return; } } public override void OnError(Exception error) { try { observer.OnError(error); } finally { Dispose(); } } public override void OnCompleted() { TResult result; try { result = parent.resultSelector(accumulation); } catch (Exception ex) { OnError(ex); return; } observer.OnNext(result); try { observer.OnCompleted(); } finally { Dispose(); } } } } }