Sample.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.Concurrency;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. internal sealed class Sample<TSource, TSample> : Producer<TSource, Sample<TSource, TSample>._>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. private readonly IObservable<TSample> _sampler;
  12. public Sample(IObservable<TSource> source, IObservable<TSample> sampler)
  13. {
  14. _source = source;
  15. _sampler = sampler;
  16. }
  17. protected override _ CreateSink(IObserver<TSource> observer, IDisposable cancel) => new _(observer, cancel);
  18. protected override IDisposable Run(_ sink) => sink.Run(this);
  19. internal sealed class _ : Sink<TSource>, IObserver<TSource>
  20. {
  21. private readonly object _gate = new object();
  22. public _(IObserver<TSource> observer, IDisposable cancel)
  23. : base(observer, cancel)
  24. {
  25. }
  26. private IDisposable _sourceSubscription;
  27. private bool _hasValue;
  28. private TSource _value;
  29. private bool _atEnd;
  30. public IDisposable Run(Sample<TSource, TSample> parent)
  31. {
  32. var sourceSubscription = new SingleAssignmentDisposable();
  33. _sourceSubscription = sourceSubscription;
  34. sourceSubscription.Disposable = parent._source.SubscribeSafe(this);
  35. var samplerSubscription = parent._sampler.SubscribeSafe(new SampleObserver(this));
  36. return StableCompositeDisposable.Create(_sourceSubscription, samplerSubscription);
  37. }
  38. public void OnNext(TSource value)
  39. {
  40. lock (_gate)
  41. {
  42. _hasValue = true;
  43. _value = value;
  44. }
  45. }
  46. public void OnError(Exception error)
  47. {
  48. lock (_gate)
  49. {
  50. base._observer.OnError(error);
  51. base.Dispose();
  52. }
  53. }
  54. public void OnCompleted()
  55. {
  56. lock (_gate)
  57. {
  58. _atEnd = true;
  59. _sourceSubscription.Dispose();
  60. }
  61. }
  62. private sealed class SampleObserver : IObserver<TSample>
  63. {
  64. private readonly _ _parent;
  65. public SampleObserver(_ parent)
  66. {
  67. _parent = parent;
  68. }
  69. public void OnNext(TSample value)
  70. {
  71. lock (_parent._gate)
  72. {
  73. if (_parent._hasValue)
  74. {
  75. _parent._hasValue = false;
  76. _parent._observer.OnNext(_parent._value);
  77. }
  78. if (_parent._atEnd)
  79. {
  80. _parent._observer.OnCompleted();
  81. _parent.Dispose();
  82. }
  83. }
  84. }
  85. public void OnError(Exception error)
  86. {
  87. // BREAKING CHANGE v2 > v1.x - This error used to be swallowed
  88. lock (_parent._gate)
  89. {
  90. _parent._observer.OnError(error);
  91. _parent.Dispose();
  92. }
  93. }
  94. public void OnCompleted()
  95. {
  96. lock (_parent._gate)
  97. {
  98. if (_parent._hasValue)
  99. {
  100. _parent._hasValue = false;
  101. _parent._observer.OnNext(_parent._value);
  102. }
  103. if (_parent._atEnd)
  104. {
  105. _parent._observer.OnCompleted();
  106. _parent.Dispose();
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. internal sealed class Sample<TSource> : Producer<TSource, Sample<TSource>._>
  114. {
  115. private readonly IObservable<TSource> _source;
  116. private readonly TimeSpan _interval;
  117. private readonly IScheduler _scheduler;
  118. public Sample(IObservable<TSource> source, TimeSpan interval, IScheduler scheduler)
  119. {
  120. _source = source;
  121. _interval = interval;
  122. _scheduler = scheduler;
  123. }
  124. protected override _ CreateSink(IObserver<TSource> observer, IDisposable cancel) => new _(observer, cancel);
  125. protected override IDisposable Run(_ sink) => sink.Run(this);
  126. internal sealed class _ : Sink<TSource>, IObserver<TSource>
  127. {
  128. private object _gate = new object();
  129. public _(IObserver<TSource> observer, IDisposable cancel)
  130. : base(observer, cancel)
  131. {
  132. }
  133. private IDisposable _sourceSubscription;
  134. private bool _hasValue;
  135. private TSource _value;
  136. private bool _atEnd;
  137. public IDisposable Run(Sample<TSource> parent)
  138. {
  139. var sourceSubscription = new SingleAssignmentDisposable();
  140. _sourceSubscription = sourceSubscription;
  141. sourceSubscription.Disposable = parent._source.SubscribeSafe(this);
  142. return StableCompositeDisposable.Create(
  143. sourceSubscription,
  144. parent._scheduler.SchedulePeriodic(parent._interval, Tick)
  145. );
  146. }
  147. private void Tick()
  148. {
  149. lock (_gate)
  150. {
  151. if (_hasValue)
  152. {
  153. _hasValue = false;
  154. base._observer.OnNext(_value);
  155. }
  156. if (_atEnd)
  157. {
  158. base._observer.OnCompleted();
  159. base.Dispose();
  160. }
  161. }
  162. }
  163. public void OnNext(TSource value)
  164. {
  165. lock (_gate)
  166. {
  167. _hasValue = true;
  168. _value = value;
  169. }
  170. }
  171. public void OnError(Exception error)
  172. {
  173. lock (_gate)
  174. {
  175. base._observer.OnError(error);
  176. base.Dispose();
  177. }
  178. }
  179. public void OnCompleted()
  180. {
  181. lock (_gate)
  182. {
  183. _atEnd = true;
  184. _sourceSubscription.Dispose();
  185. }
  186. }
  187. }
  188. }
  189. }