Repeat.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 static class Repeat<TResult>
  10. {
  11. internal sealed class ForeverRecursive : Producer<TResult, ForeverRecursive._>
  12. {
  13. private readonly TResult _value;
  14. private readonly IScheduler _scheduler;
  15. public ForeverRecursive(TResult value, IScheduler scheduler)
  16. {
  17. _value = value;
  18. _scheduler = scheduler;
  19. }
  20. protected override _ CreateSink(IObserver<TResult> observer) => new _(_value, observer);
  21. protected override void Run(_ sink) => sink.Run(_scheduler);
  22. internal sealed class _ : IdentitySink<TResult>
  23. {
  24. private readonly TResult _value;
  25. private IDisposable _task;
  26. public _(TResult value, IObserver<TResult> observer)
  27. : base(observer)
  28. {
  29. _value = value;
  30. }
  31. public void Run(IScheduler scheduler)
  32. {
  33. var first = scheduler.Schedule(this, static (innerScheduler, @this) => @this.LoopRecInf(innerScheduler));
  34. Disposable.TrySetSingle(ref _task, first);
  35. }
  36. protected override void Dispose(bool disposing)
  37. {
  38. base.Dispose(disposing);
  39. if (disposing)
  40. {
  41. Disposable.Dispose(ref _task);
  42. }
  43. }
  44. private IDisposable LoopRecInf(IScheduler scheduler)
  45. {
  46. ForwardOnNext(_value);
  47. var next = scheduler.Schedule(this, static (innerScheduler, @this) => @this.LoopRecInf(innerScheduler));
  48. Disposable.TrySetMultiple(ref _task, next);
  49. return Disposable.Empty;
  50. }
  51. }
  52. }
  53. internal sealed class ForeverLongRunning : Producer<TResult, ForeverLongRunning._>
  54. {
  55. private readonly TResult _value;
  56. private readonly ISchedulerLongRunning _scheduler;
  57. public ForeverLongRunning(TResult value, ISchedulerLongRunning scheduler)
  58. {
  59. _value = value;
  60. _scheduler = scheduler;
  61. }
  62. protected override _ CreateSink(IObserver<TResult> observer) => new _(_value, observer);
  63. protected override void Run(_ sink) => sink.Run(_scheduler);
  64. internal sealed class _ : IdentitySink<TResult>
  65. {
  66. private readonly TResult _value;
  67. public _(TResult value, IObserver<TResult> observer)
  68. : base(observer)
  69. {
  70. _value = value;
  71. }
  72. public void Run(ISchedulerLongRunning longRunning)
  73. {
  74. SetUpstream(longRunning.ScheduleLongRunning(this, static (@this, c) => @this.LoopInf(c)));
  75. }
  76. private void LoopInf(ICancelable cancel)
  77. {
  78. var value = _value;
  79. while (!cancel.IsDisposed)
  80. {
  81. ForwardOnNext(value);
  82. }
  83. Dispose();
  84. }
  85. }
  86. }
  87. internal sealed class CountRecursive : Producer<TResult, CountRecursive._>
  88. {
  89. private readonly TResult _value;
  90. private readonly IScheduler _scheduler;
  91. private readonly int _repeatCount;
  92. public CountRecursive(TResult value, int repeatCount, IScheduler scheduler)
  93. {
  94. _value = value;
  95. _scheduler = scheduler;
  96. _repeatCount = repeatCount;
  97. }
  98. protected override _ CreateSink(IObserver<TResult> observer) => new _(_value, _repeatCount, observer);
  99. protected override void Run(_ sink) => sink.Run(_scheduler);
  100. internal sealed class _ : IdentitySink<TResult>
  101. {
  102. private readonly TResult _value;
  103. private int _remaining;
  104. private IDisposable _task;
  105. public _(TResult value, int repeatCount, IObserver<TResult> observer)
  106. : base(observer)
  107. {
  108. _value = value;
  109. _remaining = repeatCount;
  110. }
  111. public void Run(IScheduler scheduler)
  112. {
  113. var first = scheduler.Schedule(this, static (innerScheduler, @this) => @this.LoopRec(innerScheduler));
  114. Disposable.TrySetSingle(ref _task, first);
  115. }
  116. protected override void Dispose(bool disposing)
  117. {
  118. base.Dispose(disposing);
  119. if (disposing)
  120. {
  121. Disposable.Dispose(ref _task);
  122. }
  123. }
  124. private IDisposable LoopRec(IScheduler scheduler)
  125. {
  126. var remaining = _remaining;
  127. if (remaining > 0)
  128. {
  129. ForwardOnNext(_value);
  130. _remaining = --remaining;
  131. }
  132. if (remaining == 0)
  133. {
  134. ForwardOnCompleted();
  135. }
  136. else
  137. {
  138. var next = scheduler.Schedule(this, static (innerScheduler, @this) => @this.LoopRec(innerScheduler));
  139. Disposable.TrySetMultiple(ref _task, next);
  140. }
  141. return Disposable.Empty;
  142. }
  143. }
  144. }
  145. internal sealed class CountLongRunning : Producer<TResult, CountLongRunning._>
  146. {
  147. private readonly TResult _value;
  148. private readonly ISchedulerLongRunning _scheduler;
  149. private readonly int _repeatCount;
  150. public CountLongRunning(TResult value, int repeatCount, ISchedulerLongRunning scheduler)
  151. {
  152. _value = value;
  153. _scheduler = scheduler;
  154. _repeatCount = repeatCount;
  155. }
  156. protected override _ CreateSink(IObserver<TResult> observer) => new _(_value, _repeatCount, observer);
  157. protected override void Run(_ sink) => sink.Run(_scheduler);
  158. internal sealed class _ : IdentitySink<TResult>
  159. {
  160. private readonly TResult _value;
  161. private readonly int _remaining;
  162. public _(TResult value, int remaining, IObserver<TResult> observer)
  163. : base(observer)
  164. {
  165. _value = value;
  166. _remaining = remaining;
  167. }
  168. public void Run(ISchedulerLongRunning longRunning)
  169. {
  170. SetUpstream(longRunning.ScheduleLongRunning(this, static (@this, cancel) => @this.Loop(cancel)));
  171. }
  172. private void Loop(ICancelable cancel)
  173. {
  174. var value = _value;
  175. var n = _remaining;
  176. while (n > 0 && !cancel.IsDisposed)
  177. {
  178. ForwardOnNext(value);
  179. n--;
  180. }
  181. if (!cancel.IsDisposed)
  182. {
  183. ForwardOnCompleted();
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }