<PackageReference Include="System.Reactive" Version="6.0.1-preview.1" />
Aggregate<TSource>
using System.
Runtime.
CompilerServices;
namespace System.
Reactive.
Linq.
ObservableImpl
{
[
System.
Runtime.
CompilerServices.
NullableContext(
1)]
[
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
0,
1,
1,
1
})]
internal sealed class Aggregate<[
System.
Runtime.
CompilerServices.
Nullable(
2)]
TSource> :
Producer<
TSource,
Aggregate<
TSource>.
_>
{
[
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
0,
1
})]
internal sealed class _ :
IdentitySink<
TSource>
{
private readonly Func<
TSource,
TSource,
TSource>
_accumulator;
[
System.
Runtime.
CompilerServices.
Nullable(
2)]
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)
try {
throw new InvalidOperationException(
Strings_Linq.
NO_ELEMENTS);
}
catch (
Exception error) {
ForwardOnError(
error);
}
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;
}
[
return:
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
1,
0
})]
protected override _ CreateSink(
IObserver<
TSource>
observer)
{
return new _(
_accumulator,
observer);
}
protected override void Run([
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
1,
0
})]
_ sink)
{
sink.
Run(
_source);
}
}
}