| 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 SingleAsync<TSource> : Producer<TSource> |
| 9 |
{ |
| 10 |
private readonly IObservable<TSource> _source; |
| 11 |
private readonly Func<TSource, bool> _predicate; |
| 12 |
private readonly bool _throwOnEmpty; |
| 13 |
|
| 14 |
public SingleAsync(IObservable<TSource> source, Func<TSource, bool> predicate, bool throwOnEmpty) |
| 15 |
{ |
| 16 |
_source = source; |
| 17 |
_predicate = predicate; |
| 18 |
_throwOnEmpty = throwOnEmpty; |
| 19 |
} |
| 20 |
|
| 21 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 22 |
{ |
| 23 |
if (_predicate != null) |
| 24 |
{ |
| 25 |
var sink = new SingleAsyncImpl(this, observer, cancel); |
| 26 |
setSink(sink); |
| 27 |
return _source.SubscribeSafe(sink); |
| 28 |
} |
| 29 |
else |
| 30 |
{ |
| 31 |
var sink = new _(this, observer, cancel); |
| 32 |
setSink(sink); |
| 33 |
return _source.SubscribeSafe(sink); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
class _ : Sink<TSource>, IObserver<TSource> |
| 38 |
{ |
| 39 |
private readonly SingleAsync<TSource> _parent; |
| 40 |
private TSource _value; |
| 41 |
private bool _seenValue; |
| 42 |
|
| 43 |
public _(SingleAsync<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 44 |
: base(observer, cancel) |
| 45 |
{ |
| 46 |
_parent = parent; |
| 47 |
|
| 48 |
_value = default(TSource); |
| 49 |
_seenValue = false; |
| 50 |
} |
| 51 |
|
| 52 |
public void OnNext(TSource value) |
| 53 |
{ |
| 54 |
if (_seenValue) |
| 55 |
{ |
| 56 |
base._observer.OnError(new InvalidOperationException(Strings_Linq.MORE_THAN_ONE_ELEMENT)); |
| 57 |
base.Dispose(); |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
_value = value; |
| 62 |
_seenValue = true; |
| 63 |
} |
| 64 |
|
| 65 |
public void OnError(Exception error) |
| 66 |
{ |
| 67 |
base._observer.OnError(error); |
| 68 |
base.Dispose(); |
| 69 |
} |
| 70 |
|
| 71 |
public void OnCompleted() |
| 72 |
{ |
| 73 |
if (!_seenValue && _parent._throwOnEmpty) |
| 74 |
{ |
| 75 |
base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS)); |
| 76 |
} |
| 77 |
else |
| 78 |
{ |
| 79 |
base._observer.OnNext(_value); |
| 80 |
base._observer.OnCompleted(); |
| 81 |
} |
| 82 |
|
| 83 |
base.Dispose(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
class SingleAsyncImpl : Sink<TSource>, IObserver<TSource> |
| 88 |
{ |
| 89 |
private readonly SingleAsync<TSource> _parent; |
| 90 |
private TSource _value; |
| 91 |
private bool _seenValue; |
| 92 |
|
| 93 |
public SingleAsyncImpl(SingleAsync<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 94 |
: base(observer, cancel) |
| 95 |
{ |
| 96 |
_parent = parent; |
| 97 |
|
| 98 |
_value = default(TSource); |
| 99 |
_seenValue = false; |
| 100 |
} |
| 101 |
|
| 102 |
public void OnNext(TSource value) |
| 103 |
{ |
| 104 |
var b = false; |
| 105 |
|
| 106 |
try |
| 107 |
{ |
| 108 |
b = _parent._predicate(value); |
| 109 |
} |
| 110 |
catch (Exception ex) |
| 111 |
{ |
| 112 |
base._observer.OnError(ex); |
| 113 |
base.Dispose(); |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
if (b) |
| 118 |
{ |
| 119 |
if (_seenValue) |
| 120 |
{ |
| 121 |
base._observer.OnError(new InvalidOperationException(Strings_Linq.MORE_THAN_ONE_MATCHING_ELEMENT)); |
| 122 |
base.Dispose(); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
_value = value; |
| 127 |
_seenValue = true; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public void OnError(Exception error) |
| 132 |
{ |
| 133 |
base._observer.OnError(error); |
| 134 |
base.Dispose(); |
| 135 |
} |
| 136 |
|
| 137 |
public void OnCompleted() |
| 138 |
{ |
| 139 |
if (!_seenValue && _parent._throwOnEmpty) |
| 140 |
{ |
| 141 |
base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_MATCHING_ELEMENTS)); |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
base._observer.OnNext(_value); |
| 146 |
base._observer.OnCompleted(); |
| 147 |
} |
| 148 |
|
| 149 |
base.Dispose(); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
#endif |