Throttle.cs 8.3 KB

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