Delay.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace UniRx.Operators
  6. {
  7. internal class DelayObservable<T> : OperatorObservableBase<T>
  8. {
  9. readonly IObservable<T> source;
  10. readonly TimeSpan dueTime;
  11. readonly IScheduler scheduler;
  12. public DelayObservable(IObservable<T> source, TimeSpan dueTime, IScheduler scheduler)
  13. : base(scheduler == Scheduler.CurrentThread || source.IsRequiredSubscribeOnCurrentThread())
  14. {
  15. this.source = source;
  16. this.dueTime = dueTime;
  17. this.scheduler = scheduler;
  18. }
  19. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  20. {
  21. return new Delay(this, observer, cancel).Run();
  22. }
  23. class Delay : OperatorObserverBase<T, T>
  24. {
  25. readonly DelayObservable<T> parent;
  26. readonly object gate = new object();
  27. bool hasFailed;
  28. bool running;
  29. bool active;
  30. Exception exception;
  31. Queue<Timestamped<T>> queue;
  32. bool onCompleted;
  33. DateTimeOffset completeAt;
  34. IDisposable sourceSubscription;
  35. TimeSpan delay;
  36. bool ready;
  37. SerialDisposable cancelable;
  38. public Delay(DelayObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  39. {
  40. this.parent = parent;
  41. }
  42. public IDisposable Run()
  43. {
  44. cancelable = new SerialDisposable();
  45. active = false;
  46. running = false;
  47. queue = new Queue<Timestamped<T>>();
  48. onCompleted = false;
  49. completeAt = default(DateTimeOffset);
  50. hasFailed = false;
  51. exception = default(Exception);
  52. ready = true;
  53. delay = Scheduler.Normalize(parent.dueTime);
  54. var _sourceSubscription = new SingleAssignmentDisposable();
  55. sourceSubscription = _sourceSubscription; // assign to field
  56. _sourceSubscription.Disposable = parent.source.Subscribe(this);
  57. return StableCompositeDisposable.Create(sourceSubscription, cancelable);
  58. }
  59. public override void OnNext(T value)
  60. {
  61. var next = parent.scheduler.Now.Add(delay);
  62. var shouldRun = false;
  63. lock (gate)
  64. {
  65. queue.Enqueue(new Timestamped<T>(value, next));
  66. shouldRun = ready && !active;
  67. active = true;
  68. }
  69. if (shouldRun)
  70. {
  71. cancelable.Disposable = parent.scheduler.Schedule(delay, DrainQueue);
  72. }
  73. }
  74. public override void OnError(Exception error)
  75. {
  76. sourceSubscription.Dispose();
  77. var shouldRun = false;
  78. lock (gate)
  79. {
  80. queue.Clear();
  81. exception = error;
  82. hasFailed = true;
  83. shouldRun = !running;
  84. }
  85. if (shouldRun)
  86. {
  87. try { base.observer.OnError(error); } finally { Dispose(); }
  88. }
  89. }
  90. public override void OnCompleted()
  91. {
  92. sourceSubscription.Dispose();
  93. var next = parent.scheduler.Now.Add(delay);
  94. var shouldRun = false;
  95. lock (gate)
  96. {
  97. completeAt = next;
  98. onCompleted = true;
  99. shouldRun = ready && !active;
  100. active = true;
  101. }
  102. if (shouldRun)
  103. {
  104. cancelable.Disposable = parent.scheduler.Schedule(delay, DrainQueue);
  105. }
  106. }
  107. void DrainQueue(Action<TimeSpan> recurse)
  108. {
  109. lock (gate)
  110. {
  111. if (hasFailed) return;
  112. running = true;
  113. }
  114. var shouldYield = false;
  115. while (true)
  116. {
  117. var hasFailed = false;
  118. var error = default(Exception);
  119. var hasValue = false;
  120. var value = default(T);
  121. var hasCompleted = false;
  122. var shouldRecurse = false;
  123. var recurseDueTime = default(TimeSpan);
  124. lock (gate)
  125. {
  126. if (hasFailed)
  127. {
  128. error = exception;
  129. hasFailed = true;
  130. running = false;
  131. }
  132. else
  133. {
  134. if (queue.Count > 0)
  135. {
  136. var nextDue = queue.Peek().Timestamp;
  137. if (nextDue.CompareTo(parent.scheduler.Now) <= 0 && !shouldYield)
  138. {
  139. value = queue.Dequeue().Value;
  140. hasValue = true;
  141. }
  142. else
  143. {
  144. shouldRecurse = true;
  145. recurseDueTime = Scheduler.Normalize(nextDue.Subtract(parent.scheduler.Now));
  146. running = false;
  147. }
  148. }
  149. else if (onCompleted)
  150. {
  151. if (completeAt.CompareTo(parent.scheduler.Now) <= 0 && !shouldYield)
  152. {
  153. hasCompleted = true;
  154. }
  155. else
  156. {
  157. shouldRecurse = true;
  158. recurseDueTime = Scheduler.Normalize(completeAt.Subtract(parent.scheduler.Now));
  159. running = false;
  160. }
  161. }
  162. else
  163. {
  164. running = false;
  165. active = false;
  166. }
  167. }
  168. }
  169. if (hasValue)
  170. {
  171. base.observer.OnNext(value);
  172. shouldYield = true;
  173. }
  174. else
  175. {
  176. if (hasCompleted)
  177. {
  178. try { base.observer.OnCompleted(); } finally { Dispose(); }
  179. }
  180. else if (hasFailed)
  181. {
  182. try { base.observer.OnError(error); } finally { Dispose(); }
  183. }
  184. else if (shouldRecurse)
  185. {
  186. recurse(recurseDueTime);
  187. }
  188. return;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }