| 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 Cast<TSource, TResult> : Producer<TResult> /* Could optimize further by deriving from Select<TResult> and providing Omega<TResult2>. We're not doing this (yet) for debuggability. */ |
| 9 |
{ |
| 10 |
private readonly IObservable<TSource> _source; |
| 11 |
|
| 12 |
public Cast(IObservable<TSource> source) |
| 13 |
{ |
| 14 |
_source = source; |
| 15 |
} |
| 16 |
|
| 17 |
protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 18 |
{ |
| 19 |
var sink = new _(observer, cancel); |
| 20 |
setSink(sink); |
| 21 |
return _source.SubscribeSafe(sink); |
| 22 |
} |
| 23 |
|
| 24 |
class _ : Sink<TResult>, IObserver<TSource> |
| 25 |
{ |
| 26 |
public _(IObserver<TResult> observer, IDisposable cancel) |
| 27 |
: base(observer, cancel) |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
public void OnNext(TSource value) |
| 32 |
{ |
| 33 |
var result = default(TResult); |
| 34 |
try |
| 35 |
{ |
| 36 |
result = (TResult)(object)value; |
| 37 |
} |
| 38 |
catch (Exception exception) |
| 39 |
{ |
| 40 |
base._observer.OnError(exception); |
| 41 |
base.Dispose(); |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
base._observer.OnNext(result); |
| 46 |
} |
| 47 |
|
| 48 |
public void OnError(Exception error) |
| 49 |
{ |
| 50 |
base._observer.OnError(error); |
| 51 |
base.Dispose(); |
| 52 |
} |
| 53 |
|
| 54 |
public void OnCompleted() |
| 55 |
{ |
| 56 |
base._observer.OnCompleted(); |
| 57 |
base.Dispose(); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
#endif |