<PackageReference Include="System.Reactive" Version="5.0.0-preview.16" />

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 _ : IdentitySink<TSource> { private readonly Func<TSource, TSource, TSource> _accumulator; private TSource _accumulation; private bool _hasAccumulation; public _(Func<TSource, TSource, TSource> accumulator, IObserver<TSource> observer) : base(observer) { _accumulator = accumulator; } public override void OnNext(TSource value) { if (_hasAccumulation) try { _accumulation = _accumulator(_accumulation, value); } catch (Exception error) { _accumulation = default(TSource); ForwardOnError(error); } else { _accumulation = value; _hasAccumulation = true; } } public override void OnError(Exception error) { _accumulation = default(TSource); ForwardOnError(error); } public override void OnCompleted() { if (!_hasAccumulation) ForwardOnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS)); else { TSource accumulation = _accumulation; _accumulation = default(TSource); ForwardOnNext(accumulation); ForwardOnCompleted(); } } } 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) { return new _(_accumulator, observer); } protected override void Run(_ sink) { sink.Run(_source); } } }