Sample.cs 7.2 KB

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