Synchronization.ObserveOn.cs 3.8 KB

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