| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if !NO_PERF |
| 4 |
using System; |
| 5 |
|
| 6 |
namespace System.Reactive.Linq.ObservableImpl |
| 7 |
{ |
| 8 |
class Aggregate<TSource, TAccumulate, TResult> : Producer<TResult> |
| 9 |
{ |
| 10 |
private readonly IObservable<TSource> _source; |
| 11 |
private readonly TAccumulate _seed; |
| 12 |
private readonly Func<TAccumulate, TSource, TAccumulate> _accumulator; |
| 13 |
private readonly Func<TAccumulate, TResult> _resultSelector; |
| 14 |
|
| 15 |
public Aggregate(IObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector) |
| 16 |
{ |
| 17 |
_source = source; |
| 18 |
_seed = seed; |
| 19 |
_accumulator = accumulator; |
| 20 |
_resultSelector = resultSelector; |
| 21 |
} |
| 22 |
|
| 23 |
protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 24 |
{ |
| 25 |
var sink = new _(this, observer, cancel); |
| 26 |
setSink(sink); |
| 27 |
return _source.SubscribeSafe(sink); |
| 28 |
} |
| 29 |
|
| 30 |
class _ : Sink<TResult>, IObserver<TSource> |
| 31 |
{ |
| 32 |
private readonly Aggregate<TSource, TAccumulate, TResult> _parent; |
| 33 |
private TAccumulate _accumulation; |
| 34 |
|
| 35 |
public _(Aggregate<TSource, TAccumulate, TResult> parent, IObserver<TResult> observer, IDisposable cancel) |
| 36 |
: base(observer, cancel) |
| 37 |
{ |
| 38 |
_parent = parent; |
| 39 |
_accumulation = _parent._seed; |
| 40 |
} |
| 41 |
|
| 42 |
public void OnNext(TSource value) |
| 43 |
{ |
| 44 |
try |
| 45 |
{ |
| 46 |
_accumulation = _parent._accumulator(_accumulation, value); |
| 47 |
} |
| 48 |
catch (Exception exception) |
| 49 |
{ |
| 50 |
base._observer.OnError(exception); |
| 51 |
base.Dispose(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public void OnError(Exception error) |
| 56 |
{ |
| 57 |
base._observer.OnError(error); |
| 58 |
base.Dispose(); |
| 59 |
} |
| 60 |
|
| 61 |
public void OnCompleted() |
| 62 |
{ |
| 63 |
var result = default(TResult); |
| 64 |
try |
| 65 |
{ |
| 66 |
result = _parent._resultSelector(_accumulation); |
| 67 |
} |
| 68 |
catch (Exception exception) |
| 69 |
{ |
| 70 |
base._observer.OnError(exception); |
| 71 |
base.Dispose(); |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
base._observer.OnNext(result); |
| 76 |
base._observer.OnCompleted(); |
| 77 |
base.Dispose(); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
class Aggregate<TSource> : Producer<TSource> |
| 83 |
{ |
| 84 |
private readonly IObservable<TSource> _source; |
| 85 |
private readonly Func<TSource, TSource, TSource> _accumulator; |
| 86 |
|
| 87 |
public Aggregate(IObservable<TSource> source, Func<TSource, TSource, TSource> accumulator) |
| 88 |
{ |
| 89 |
_source = source; |
| 90 |
_accumulator = accumulator; |
| 91 |
} |
| 92 |
|
| 93 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 94 |
{ |
| 95 |
var sink = new _(this, observer, cancel); |
| 96 |
setSink(sink); |
| 97 |
return _source.SubscribeSafe(sink); |
| 98 |
} |
| 99 |
|
| 100 |
class _ : Sink<TSource>, IObserver<TSource> |
| 101 |
{ |
| 102 |
private readonly Aggregate<TSource> _parent; |
| 103 |
private TSource _accumulation; |
| 104 |
private bool _hasAccumulation; |
| 105 |
|
| 106 |
public _(Aggregate<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 107 |
: base(observer, cancel) |
| 108 |
{ |
| 109 |
_parent = parent; |
| 110 |
_accumulation = default(TSource); |
| 111 |
_hasAccumulation = false; |
| 112 |
} |
| 113 |
|
| 114 |
public void OnNext(TSource value) |
| 115 |
{ |
| 116 |
if (!_hasAccumulation) |
| 117 |
{ |
| 118 |
_accumulation = value; |
| 119 |
_hasAccumulation = true; |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
try |
| 124 |
{ |
| 125 |
_accumulation = _parent._accumulator(_accumulation, value); |
| 126 |
} |
| 127 |
catch (Exception exception) |
| 128 |
{ |
| 129 |
base._observer.OnError(exception); |
| 130 |
base.Dispose(); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public void OnError(Exception error) |
| 136 |
{ |
| 137 |
base._observer.OnError(error); |
| 138 |
base.Dispose(); |
| 139 |
} |
| 140 |
|
| 141 |
public void OnCompleted() |
| 142 |
{ |
| 143 |
if (!_hasAccumulation) |
| 144 |
{ |
| 145 |
base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS)); |
| 146 |
base.Dispose(); |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
base._observer.OnNext(_accumulation); |
| 151 |
base._observer.OnCompleted(); |
| 152 |
base.Dispose(); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
#endif |