Synchronization.ObserveOn.cs 3.7 KB

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