1
0

Synchronization.ObserveOn.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_PERF
  3. using System;
  4. using System.Reactive.Concurrency;
  5. using System.Reactive.Disposables;
  6. using System.Threading;
  7. namespace System.Reactive.Concurrency
  8. {
  9. class ObserveOn<TSource> : Producer<TSource>
  10. {
  11. private readonly IObservable<TSource> _source;
  12. private readonly IScheduler _scheduler;
  13. public ObserveOn(IObservable<TSource> source, IScheduler scheduler)
  14. {
  15. _source = source;
  16. _scheduler = scheduler;
  17. }
  18. #if !NO_SYNCCTX
  19. private readonly SynchronizationContext _context;
  20. public ObserveOn(IObservable<TSource> source, SynchronizationContext context)
  21. {
  22. _source = source;
  23. _context = context;
  24. }
  25. #endif
  26. [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.")]
  27. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  28. {
  29. #if !NO_SYNCCTX
  30. if (_context != null)
  31. {
  32. var sink = new ObserveOnSink(this, observer, cancel);
  33. setSink(sink);
  34. return sink.Run();
  35. }
  36. else
  37. #endif
  38. {
  39. var sink = new ObserveOnObserver<TSource>(_scheduler, observer, cancel);
  40. setSink(sink);
  41. return _source.SubscribeSafe(sink);
  42. }
  43. }
  44. #if !NO_SYNCCTX
  45. class ObserveOnSink : Sink<TSource>, IObserver<TSource>
  46. {
  47. private readonly ObserveOn<TSource> _parent;
  48. public ObserveOnSink(ObserveOn<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  49. : base(observer, cancel)
  50. {
  51. _parent = parent;
  52. }
  53. public IDisposable Run()
  54. {
  55. //
  56. // The interactions with OperationStarted/OperationCompleted below allow
  57. // for test frameworks to wait until a whole sequence is observed, running
  58. // asserts on a per-message level. Also, for ASP.NET pages, the use of the
  59. // built-in synchronization context would allow processing to finished in
  60. // its entirety before moving on with the page lifecycle.
  61. //
  62. _parent._context.OperationStarted();
  63. var d = _parent._source.SubscribeSafe(this);
  64. var c = Disposable.Create(() =>
  65. {
  66. _parent._context.OperationCompleted();
  67. });
  68. return StableCompositeDisposable.Create(d, c);
  69. }
  70. public void OnNext(TSource value)
  71. {
  72. _parent._context.Post(OnNextPosted, value);
  73. }
  74. public void OnError(Exception error)
  75. {
  76. _parent._context.Post(OnErrorPosted, error);
  77. }
  78. public void OnCompleted()
  79. {
  80. _parent._context.Post(OnCompletedPosted, null);
  81. }
  82. private void OnNextPosted(object value)
  83. {
  84. base._observer.OnNext((TSource)value);
  85. }
  86. private void OnErrorPosted(object error)
  87. {
  88. base._observer.OnError((Exception)error);
  89. base.Dispose();
  90. }
  91. private void OnCompletedPosted(object ignored)
  92. {
  93. base._observer.OnCompleted();
  94. base.Dispose();
  95. }
  96. }
  97. #endif
  98. }
  99. }
  100. #endif