| 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 Dematerialize<TSource> : Producer<TSource> |
| 9 |
{ |
| 10 |
private readonly IObservable<Notification<TSource>> _source; |
| 11 |
|
| 12 |
public Dematerialize(IObservable<Notification<TSource>> source) |
| 13 |
{ |
| 14 |
_source = source; |
| 15 |
} |
| 16 |
|
| 17 |
protected override IDisposable Run(IObserver<TSource> 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<TSource>, IObserver<Notification<TSource>> |
| 25 |
{ |
| 26 |
public _(IObserver<TSource> observer, IDisposable cancel) |
| 27 |
: base(observer, cancel) |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
public void OnNext(Notification<TSource> value) |
| 32 |
{ |
| 33 |
switch (value.Kind) |
| 34 |
{ |
| 35 |
case NotificationKind.OnNext: |
| 36 |
base._observer.OnNext(value.Value); |
| 37 |
break; |
| 38 |
case NotificationKind.OnError: |
| 39 |
base._observer.OnError(value.Exception); |
| 40 |
base.Dispose(); |
| 41 |
break; |
| 42 |
case NotificationKind.OnCompleted: |
| 43 |
base._observer.OnCompleted(); |
| 44 |
base.Dispose(); |
| 45 |
break; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public void OnError(Exception error) |
| 50 |
{ |
| 51 |
base._observer.OnError(error); |
| 52 |
base.Dispose(); |
| 53 |
} |
| 54 |
|
| 55 |
public void OnCompleted() |
| 56 |
{ |
| 57 |
base._observer.OnCompleted(); |
| 58 |
base.Dispose(); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
#endif |