| 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 Any<TSource> : Producer<bool> |
| 9 |
{ |
| 10 |
private readonly IObservable<TSource> _source; |
| 11 |
private readonly Func<TSource, bool> _predicate; |
| 12 |
|
| 13 |
public Any(IObservable<TSource> source) |
| 14 |
{ |
| 15 |
_source = source; |
| 16 |
} |
| 17 |
|
| 18 |
public Any(IObservable<TSource> source, Func<TSource, bool> predicate) |
| 19 |
{ |
| 20 |
_source = source; |
| 21 |
_predicate = predicate; |
| 22 |
} |
| 23 |
|
| 24 |
protected override IDisposable Run(IObserver<bool> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 25 |
{ |
| 26 |
if (_predicate != null) |
| 27 |
{ |
| 28 |
var sink = new AnyImpl(this, observer, cancel); |
| 29 |
setSink(sink); |
| 30 |
return _source.SubscribeSafe(sink); |
| 31 |
} |
| 32 |
else |
| 33 |
{ |
| 34 |
var sink = new _(observer, cancel); |
| 35 |
setSink(sink); |
| 36 |
return _source.SubscribeSafe(sink); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
class _ : Sink<bool>, IObserver<TSource> |
| 41 |
{ |
| 42 |
public _(IObserver<bool> observer, IDisposable cancel) |
| 43 |
: base(observer, cancel) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
public void OnNext(TSource value) |
| 48 |
{ |
| 49 |
base._observer.OnNext(true); |
| 50 |
base._observer.OnCompleted(); |
| 51 |
base.Dispose(); |
| 52 |
} |
| 53 |
|
| 54 |
public void OnError(Exception error) |
| 55 |
{ |
| 56 |
base._observer.OnError(error); |
| 57 |
base.Dispose(); |
| 58 |
} |
| 59 |
|
| 60 |
public void OnCompleted() |
| 61 |
{ |
| 62 |
base._observer.OnNext(false); |
| 63 |
base._observer.OnCompleted(); |
| 64 |
base.Dispose(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
class AnyImpl : Sink<bool>, IObserver<TSource> |
| 69 |
{ |
| 70 |
private readonly Any<TSource> _parent; |
| 71 |
|
| 72 |
public AnyImpl(Any<TSource> parent, IObserver<bool> observer, IDisposable cancel) |
| 73 |
: base(observer, cancel) |
| 74 |
{ |
| 75 |
_parent = parent; |
| 76 |
} |
| 77 |
|
| 78 |
public void OnNext(TSource value) |
| 79 |
{ |
| 80 |
var res = false; |
| 81 |
try |
| 82 |
{ |
| 83 |
res = _parent._predicate(value); |
| 84 |
} |
| 85 |
catch (Exception ex) |
| 86 |
{ |
| 87 |
base._observer.OnError(ex); |
| 88 |
base.Dispose(); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
if (res) |
| 93 |
{ |
| 94 |
base._observer.OnNext(true); |
| 95 |
base._observer.OnCompleted(); |
| 96 |
base.Dispose(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public void OnError(Exception error) |
| 101 |
{ |
| 102 |
base._observer.OnError(error); |
| 103 |
base.Dispose(); |
| 104 |
} |
| 105 |
|
| 106 |
public void OnCompleted() |
| 107 |
{ |
| 108 |
base._observer.OnNext(false); |
| 109 |
base._observer.OnCompleted(); |
| 110 |
base.Dispose(); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
#endif |