| 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 |
using System.Reactive.Concurrency; |
| 6 |
using System.Threading; |
| 7 |
|
| 8 |
namespace System.Reactive.Linq.ObservableImpl |
| 9 |
{ |
| 10 |
class ObserveOn<TSource> : Producer<TSource> |
| 11 |
{ |
| 12 |
private readonly IObservable<TSource> _source; |
| 13 |
private readonly IScheduler _scheduler; |
| 14 |
|
| 15 |
public ObserveOn(IObservable<TSource> source, IScheduler scheduler) |
| 16 |
{ |
| 17 |
_source = source; |
| 18 |
_scheduler = scheduler; |
| 19 |
} |
| 20 |
|
| 21 |
#if !NO_SYNCCTX |
| 22 |
private readonly SynchronizationContext _context; |
| 23 |
|
| 24 |
public ObserveOn(IObservable<TSource> source, SynchronizationContext context) |
| 25 |
{ |
| 26 |
_source = source; |
| 27 |
_context = context; |
| 28 |
} |
| 29 |
#endif |
| 30 |
|
| 31 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 32 |
{ |
| 33 |
#if !NO_SYNCCTX |
| 34 |
if (_context != null) |
| 35 |
{ |
| 36 |
var sink = new ObserveOnImpl(this, observer, cancel); |
| 37 |
setSink(sink); |
| 38 |
return _source.Subscribe(sink); |
| 39 |
} |
| 40 |
else |
| 41 |
#endif |
| 42 |
{ |
| 43 |
var sink = new ObserveOnObserver<TSource>(_scheduler, observer, cancel); |
| 44 |
setSink(sink); |
| 45 |
return _source.Subscribe(sink); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
#if !NO_SYNCCTX |
| 50 |
class ObserveOnImpl : Sink<TSource>, IObserver<TSource> |
| 51 |
{ |
| 52 |
private readonly ObserveOn<TSource> _parent; |
| 53 |
|
| 54 |
public ObserveOnImpl(ObserveOn<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 55 |
: base(observer, cancel) |
| 56 |
{ |
| 57 |
_parent = parent; |
| 58 |
} |
| 59 |
|
| 60 |
public void OnNext(TSource value) |
| 61 |
{ |
| 62 |
_parent._context.PostWithStartComplete(() => |
| 63 |
{ |
| 64 |
base._observer.OnNext(value); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
public void OnError(Exception error) |
| 69 |
{ |
| 70 |
_parent._context.PostWithStartComplete(() => |
| 71 |
{ |
| 72 |
base._observer.OnError(error); |
| 73 |
base.Dispose(); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
public void OnCompleted() |
| 78 |
{ |
| 79 |
_parent._context.PostWithStartComplete(() => |
| 80 |
{ |
| 81 |
base._observer.OnCompleted(); |
| 82 |
base.Dispose(); |
| 83 |
}); |
| 84 |
} |
| 85 |
} |
| 86 |
#endif |
| 87 |
} |
| 88 |
} |
| 89 |
#endif |