| 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.Reactive.Disposables; |
| 7 |
using System.Threading; |
| 8 |
|
| 9 |
namespace System.Reactive.Concurrency |
| 10 |
{ |
| 11 |
class ObserveOn<TSource> : Producer<TSource> |
| 12 |
{ |
| 13 |
private readonly IObservable<TSource> _source; |
| 14 |
private readonly IScheduler _scheduler; |
| 15 |
|
| 16 |
public ObserveOn(IObservable<TSource> source, IScheduler scheduler) |
| 17 |
{ |
| 18 |
_source = source; |
| 19 |
_scheduler = scheduler; |
| 20 |
} |
| 21 |
|
| 22 |
#if !NO_SYNCCTX |
| 23 |
private readonly SynchronizationContext _context; |
| 24 |
|
| 25 |
public ObserveOn(IObservable<TSource> source, SynchronizationContext context) |
| 26 |
{ |
| 27 |
_source = source; |
| 28 |
_context = context; |
| 29 |
} |
| 30 |
#endif |
| 31 |
|
| 32 |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "2", Justification = "Visibility restricted to friend assemblies. Those should be correct by inspection.")] |
| 33 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 34 |
{ |
| 35 |
#if !NO_SYNCCTX |
| 36 |
if (_context != null) |
| 37 |
{ |
| 38 |
var sink = new ObserveOnSink(this, observer, cancel); |
| 39 |
setSink(sink); |
| 40 |
return sink.Run(); |
| 41 |
} |
| 42 |
else |
| 43 |
#endif |
| 44 |
{ |
| 45 |
var sink = new ObserveOnObserver<TSource>(_scheduler, observer, cancel); |
| 46 |
setSink(sink); |
| 47 |
return _source.SubscribeSafe(sink); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#if !NO_SYNCCTX |
| 52 |
class ObserveOnSink : Sink<TSource>, IObserver<TSource> |
| 53 |
{ |
| 54 |
private readonly ObserveOn<TSource> _parent; |
| 55 |
|
| 56 |
public ObserveOnSink(ObserveOn<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 57 |
: base(observer, cancel) |
| 58 |
{ |
| 59 |
_parent = parent; |
| 60 |
} |
| 61 |
|
| 62 |
public IDisposable Run() |
| 63 |
{ |
| 64 |
// |
| 65 |
// The interactions with OperationStarted/OperationCompleted below allow |
| 66 |
// for test frameworks to wait until a whole sequence is observed, running |
| 67 |
// asserts on a per-message level. Also, for ASP.NET pages, the use of the |
| 68 |
// built-in synchronization context would allow processing to finished in |
| 69 |
// its entirety before moving on with the page lifecycle. |
| 70 |
// |
| 71 |
_parent._context.OperationStarted(); |
| 72 |
|
| 73 |
var d = _parent._source.SubscribeSafe(this); |
| 74 |
var c = Disposable.Create(() => |
| 75 |
{ |
| 76 |
_parent._context.OperationCompleted(); |
| 77 |
}); |
| 78 |
|
| 79 |
return new CompositeDisposable(d, c); |
| 80 |
} |
| 81 |
|
| 82 |
public void OnNext(TSource value) |
| 83 |
{ |
| 84 |
_parent._context.Post(OnNextPosted, value); |
| 85 |
} |
| 86 |
|
| 87 |
public void OnError(Exception error) |
| 88 |
{ |
| 89 |
_parent._context.Post(OnErrorPosted, error); |
| 90 |
} |
| 91 |
|
| 92 |
public void OnCompleted() |
| 93 |
{ |
| 94 |
_parent._context.Post(OnCompletedPosted, null); |
| 95 |
} |
| 96 |
|
| 97 |
private void OnNextPosted(object value) |
| 98 |
{ |
| 99 |
base._observer.OnNext((TSource)value); |
| 100 |
} |
| 101 |
|
| 102 |
private void OnErrorPosted(object error) |
| 103 |
{ |
| 104 |
base._observer.OnError((Exception)error); |
| 105 |
base.Dispose(); |
| 106 |
} |
| 107 |
|
| 108 |
private void OnCompletedPosted(object ignored) |
| 109 |
{ |
| 110 |
base._observer.OnCompleted(); |
| 111 |
base.Dispose(); |
| 112 |
} |
| 113 |
} |
| 114 |
#endif |
| 115 |
} |
| 116 |
} |
| 117 |
#endif |