<PackageReference Include="System.Reactive" Version="4.0.0-preview.2.build.379" />

Aggregate<TSource>

sealed class Aggregate<TSource> : Producer<TSource, _<TSource>>
namespace System.Reactive.Linq.ObservableImpl { internal sealed class Aggregate<TSource> : Producer<TSource, Aggregate<TSource>._> { internal sealed class _ : Sink<TSource>, IObserver<TSource> { private readonly Func<TSource, TSource, TSource> _accumulator; private TSource _accumulation; private bool _hasAccumulation; public _(Func<TSource, TSource, TSource> accumulator, IObserver<TSource> observer, IDisposable cancel) : base(observer, cancel) { _accumulator = accumulator; _accumulation = default(TSource); _hasAccumulation = false; } public void OnNext(TSource value) { if (_hasAccumulation) try { _accumulation = _accumulator(_accumulation, value); } catch (Exception error) { _observer.OnError(error); base.Dispose(); } else { _accumulation = value; _hasAccumulation = true; } } public void OnError(Exception error) { _observer.OnError(error); base.Dispose(); } public void OnCompleted() { if (!_hasAccumulation) { _observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS)); base.Dispose(); } else { _observer.OnNext(_accumulation); _observer.OnCompleted(); base.Dispose(); } } } private readonly IObservable<TSource> _source; private readonly Func<TSource, TSource, TSource> _accumulator; public Aggregate(IObservable<TSource> source, Func<TSource, TSource, TSource> accumulator) { _source = source; _accumulator = accumulator; } protected override _ CreateSink(IObserver<TSource> observer, IDisposable cancel) { return new _(_accumulator, observer, cancel); } protected override IDisposable Run(_ sink) { return ObservableExtensions.SubscribeSafe<TSource>(_source, (IObserver<TSource>)sink); } } }