Throttle.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_PERF
  3. using System;
  4. using System.Reactive.Concurrency;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. class Throttle<TSource> : Producer<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 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 Throttle<TSource> _parent;
  28. public _(Throttle<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  29. : base(observer, cancel)
  30. {
  31. _parent = parent;
  32. }
  33. private object _gate;
  34. private TSource _value;
  35. private bool _hasValue;
  36. private SerialDisposable _cancelable;
  37. private ulong _id;
  38. public IDisposable Run()
  39. {
  40. _gate = new object();
  41. _value = default(TSource);
  42. _hasValue = false;
  43. _cancelable = new SerialDisposable();
  44. _id = 0UL;
  45. var subscription = _parent._source.SubscribeSafe(this);
  46. return StableCompositeDisposable.Create(subscription, _cancelable);
  47. }
  48. public void OnNext(TSource value)
  49. {
  50. var currentid = default(ulong);
  51. lock (_gate)
  52. {
  53. _hasValue = true;
  54. _value = value;
  55. _id = unchecked(_id + 1);
  56. currentid = _id;
  57. }
  58. var d = new SingleAssignmentDisposable();
  59. _cancelable.Disposable = d;
  60. d.Disposable = _parent._scheduler.Schedule(currentid, _parent._dueTime, Propagate);
  61. }
  62. private IDisposable Propagate(IScheduler self, ulong currentid)
  63. {
  64. lock (_gate)
  65. {
  66. if (_hasValue && _id == currentid)
  67. base._observer.OnNext(_value);
  68. _hasValue = false;
  69. }
  70. return Disposable.Empty;
  71. }
  72. public void OnError(Exception error)
  73. {
  74. _cancelable.Dispose();
  75. lock (_gate)
  76. {
  77. base._observer.OnError(error);
  78. base.Dispose();
  79. _hasValue = false;
  80. _id = unchecked(_id + 1);
  81. }
  82. }
  83. public void OnCompleted()
  84. {
  85. _cancelable.Dispose();
  86. lock (_gate)
  87. {
  88. if (_hasValue)
  89. base._observer.OnNext(_value);
  90. base._observer.OnCompleted();
  91. base.Dispose();
  92. _hasValue = false;
  93. _id = unchecked(_id + 1);
  94. }
  95. }
  96. }
  97. }
  98. class Throttle<TSource, TThrottle> : Producer<TSource>
  99. {
  100. private readonly IObservable<TSource> _source;
  101. private readonly Func<TSource, IObservable<TThrottle>> _throttleSelector;
  102. public Throttle(IObservable<TSource> source, Func<TSource, IObservable<TThrottle>> throttleSelector)
  103. {
  104. _source = source;
  105. _throttleSelector = throttleSelector;
  106. }
  107. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  108. {
  109. var sink = new _(this, observer, cancel);
  110. setSink(sink);
  111. return sink.Run();
  112. }
  113. class _ : Sink<TSource>, IObserver<TSource>
  114. {
  115. private readonly Throttle<TSource, TThrottle> _parent;
  116. public _(Throttle<TSource, TThrottle> parent, IObserver<TSource> observer, IDisposable cancel)
  117. : base(observer, cancel)
  118. {
  119. _parent = parent;
  120. }
  121. private object _gate;
  122. private TSource _value;
  123. private bool _hasValue;
  124. private SerialDisposable _cancelable;
  125. private ulong _id;
  126. public IDisposable Run()
  127. {
  128. _gate = new object();
  129. _value = default(TSource);
  130. _hasValue = false;
  131. _cancelable = new SerialDisposable();
  132. _id = 0UL;
  133. var subscription = _parent._source.SubscribeSafe(this);
  134. return StableCompositeDisposable.Create(subscription, _cancelable);
  135. }
  136. public void OnNext(TSource value)
  137. {
  138. var throttle = default(IObservable<TThrottle>);
  139. try
  140. {
  141. throttle = _parent._throttleSelector(value);
  142. }
  143. catch (Exception error)
  144. {
  145. lock (_gate)
  146. {
  147. base._observer.OnError(error);
  148. base.Dispose();
  149. }
  150. return;
  151. }
  152. ulong currentid;
  153. lock (_gate)
  154. {
  155. _hasValue = true;
  156. _value = value;
  157. _id = unchecked(_id + 1);
  158. currentid = _id;
  159. }
  160. var d = new SingleAssignmentDisposable();
  161. _cancelable.Disposable = d;
  162. d.Disposable = throttle.SubscribeSafe(new Delta(this, value, currentid, d));
  163. }
  164. public void OnError(Exception error)
  165. {
  166. _cancelable.Dispose();
  167. lock (_gate)
  168. {
  169. base._observer.OnError(error);
  170. base.Dispose();
  171. _hasValue = false;
  172. _id = unchecked(_id + 1);
  173. }
  174. }
  175. public void OnCompleted()
  176. {
  177. _cancelable.Dispose();
  178. lock (_gate)
  179. {
  180. if (_hasValue)
  181. base._observer.OnNext(_value);
  182. base._observer.OnCompleted();
  183. base.Dispose();
  184. _hasValue = false;
  185. _id = unchecked(_id + 1);
  186. }
  187. }
  188. class Delta : IObserver<TThrottle>
  189. {
  190. private readonly _ _parent;
  191. private readonly TSource _value;
  192. private readonly ulong _currentid;
  193. private readonly IDisposable _self;
  194. public Delta(_ parent, TSource value, ulong currentid, IDisposable self)
  195. {
  196. _parent = parent;
  197. _value = value;
  198. _currentid = currentid;
  199. _self = self;
  200. }
  201. public void OnNext(TThrottle value)
  202. {
  203. lock (_parent._gate)
  204. {
  205. if (_parent._hasValue && _parent._id == _currentid)
  206. _parent._observer.OnNext(_value);
  207. _parent._hasValue = false;
  208. _self.Dispose();
  209. }
  210. }
  211. public void OnError(Exception error)
  212. {
  213. lock (_parent._gate)
  214. {
  215. _parent._observer.OnError(error);
  216. _parent.Dispose();
  217. }
  218. }
  219. public void OnCompleted()
  220. {
  221. lock (_parent._gate)
  222. {
  223. if (_parent._hasValue && _parent._id == _currentid)
  224. _parent._observer.OnNext(_value);
  225. _parent._hasValue = false;
  226. _self.Dispose();
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. #endif