Throttle.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 Throttle<TSource> : Producer<TSource, Throttle<TSource>._>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. private readonly TimeSpan _dueTime;
  12. private readonly IScheduler _scheduler;
  13. public Throttle(IObservable<TSource> source, TimeSpan dueTime, IScheduler scheduler)
  14. {
  15. _source = source;
  16. _dueTime = dueTime;
  17. _scheduler = scheduler;
  18. }
  19. protected override _ CreateSink(IObserver<TSource> observer) => new _(this, observer);
  20. protected override void Run(_ sink) => sink.Run(_source);
  21. internal sealed class _ : IdentitySink<TSource>
  22. {
  23. private readonly object _gate = new object();
  24. private readonly TimeSpan _dueTime;
  25. private readonly IScheduler _scheduler;
  26. public _(Throttle<TSource> parent, IObserver<TSource> observer)
  27. : base(observer)
  28. {
  29. _dueTime = parent._dueTime;
  30. _scheduler = parent._scheduler;
  31. }
  32. private TSource? _value;
  33. private bool _hasValue;
  34. private SerialDisposableValue _serialCancelable;
  35. private ulong _id;
  36. protected override void Dispose(bool disposing)
  37. {
  38. if (disposing)
  39. {
  40. _serialCancelable.Dispose();
  41. }
  42. base.Dispose(disposing);
  43. }
  44. public override void OnNext(TSource value)
  45. {
  46. ulong currentid;
  47. lock (_gate)
  48. {
  49. _hasValue = true;
  50. _value = value;
  51. _id = unchecked(_id + 1);
  52. currentid = _id;
  53. }
  54. _serialCancelable.Disposable = null;
  55. _serialCancelable.Disposable = _scheduler.ScheduleAction((@this: this, currentid), _dueTime, static tuple => [email protected](tuple.currentid));
  56. }
  57. private void Propagate(ulong currentid)
  58. {
  59. lock (_gate)
  60. {
  61. if (_hasValue && _id == currentid)
  62. {
  63. ForwardOnNext(_value!);
  64. }
  65. _hasValue = false;
  66. }
  67. }
  68. public override void OnError(Exception error)
  69. {
  70. _serialCancelable.Dispose();
  71. lock (_gate)
  72. {
  73. ForwardOnError(error);
  74. _hasValue = false;
  75. _id = unchecked(_id + 1);
  76. }
  77. }
  78. public override void OnCompleted()
  79. {
  80. _serialCancelable.Dispose();
  81. lock (_gate)
  82. {
  83. if (_hasValue)
  84. {
  85. ForwardOnNext(_value!);
  86. }
  87. ForwardOnCompleted();
  88. _hasValue = false;
  89. _id = unchecked(_id + 1);
  90. }
  91. }
  92. }
  93. }
  94. internal sealed class Throttle<TSource, TThrottle> : Producer<TSource, Throttle<TSource, TThrottle>._>
  95. {
  96. private readonly IObservable<TSource> _source;
  97. private readonly Func<TSource, IObservable<TThrottle>> _throttleSelector;
  98. public Throttle(IObservable<TSource> source, Func<TSource, IObservable<TThrottle>> throttleSelector)
  99. {
  100. _source = source;
  101. _throttleSelector = throttleSelector;
  102. }
  103. protected override _ CreateSink(IObserver<TSource> observer) => new _(this, observer);
  104. protected override void Run(_ sink) => sink.Run(_source);
  105. internal sealed class _ : IdentitySink<TSource>
  106. {
  107. private readonly object _gate = new object();
  108. private readonly Func<TSource, IObservable<TThrottle>> _throttleSelector;
  109. public _(Throttle<TSource, TThrottle> parent, IObserver<TSource> observer)
  110. : base(observer)
  111. {
  112. _throttleSelector = parent._throttleSelector;
  113. }
  114. private TSource? _value;
  115. private bool _hasValue;
  116. private SerialDisposableValue _serialCancelable;
  117. private ulong _id;
  118. protected override void Dispose(bool disposing)
  119. {
  120. if (disposing)
  121. {
  122. _serialCancelable.Dispose();
  123. }
  124. base.Dispose(disposing);
  125. }
  126. public override void OnNext(TSource value)
  127. {
  128. IObservable<TThrottle> throttle;
  129. try
  130. {
  131. throttle = _throttleSelector(value);
  132. }
  133. catch (Exception error)
  134. {
  135. lock (_gate)
  136. {
  137. ForwardOnError(error);
  138. }
  139. return;
  140. }
  141. ulong currentid;
  142. lock (_gate)
  143. {
  144. _hasValue = true;
  145. _value = value;
  146. _id = unchecked(_id + 1);
  147. currentid = _id;
  148. }
  149. _serialCancelable.Disposable = null;
  150. var newInnerObserver = new ThrottleObserver(this, value, currentid);
  151. newInnerObserver.SetResource(throttle.SubscribeSafe(newInnerObserver));
  152. _serialCancelable.Disposable = newInnerObserver;
  153. }
  154. public override void OnError(Exception error)
  155. {
  156. _serialCancelable.Dispose();
  157. lock (_gate)
  158. {
  159. ForwardOnError(error);
  160. _hasValue = false;
  161. _id = unchecked(_id + 1);
  162. }
  163. }
  164. public override void OnCompleted()
  165. {
  166. _serialCancelable.Dispose();
  167. lock (_gate)
  168. {
  169. if (_hasValue)
  170. {
  171. ForwardOnNext(_value!);
  172. }
  173. ForwardOnCompleted();
  174. _hasValue = false;
  175. _id = unchecked(_id + 1);
  176. }
  177. }
  178. private sealed class ThrottleObserver : SafeObserver<TThrottle>
  179. {
  180. private readonly _ _parent;
  181. private readonly TSource _value;
  182. private readonly ulong _currentid;
  183. public ThrottleObserver(_ parent, TSource value, ulong currentid)
  184. {
  185. _parent = parent;
  186. _value = value;
  187. _currentid = currentid;
  188. }
  189. public override void OnNext(TThrottle value)
  190. {
  191. lock (_parent._gate)
  192. {
  193. if (_parent._hasValue && _parent._id == _currentid)
  194. {
  195. _parent.ForwardOnNext(_value);
  196. }
  197. _parent._hasValue = false;
  198. Dispose();
  199. }
  200. }
  201. public override void OnError(Exception error)
  202. {
  203. lock (_parent._gate)
  204. {
  205. _parent.ForwardOnError(error);
  206. }
  207. }
  208. public override void OnCompleted()
  209. {
  210. lock (_parent._gate)
  211. {
  212. if (_parent._hasValue && _parent._id == _currentid)
  213. {
  214. _parent.ForwardOnNext(_value);
  215. }
  216. _parent._hasValue = false;
  217. Dispose();
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }