TakeLast.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.Collections.Generic;
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.Disposables;
  7. using System.Threading.Tasks;
  8. namespace System.Reactive.Linq
  9. {
  10. public partial class AsyncObservable
  11. {
  12. public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, int count)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (count < 0)
  17. throw new ArgumentOutOfRangeException(nameof(count));
  18. if (count == 0)
  19. {
  20. return Empty<TSource>();
  21. }
  22. return CreateAsyncObservable<TSource>.From(
  23. source,
  24. count,
  25. static async (source, count, observer) =>
  26. {
  27. var (sink, drain) = AsyncObserver.TakeLast(observer, count);
  28. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  29. return StableCompositeAsyncDisposable.Create(subscription, drain);
  30. });
  31. }
  32. public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, int count, IAsyncScheduler scheduler)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (count < 0)
  37. throw new ArgumentOutOfRangeException(nameof(count));
  38. if (scheduler == null)
  39. throw new ArgumentNullException(nameof(scheduler));
  40. if (count == 0)
  41. {
  42. return Empty<TSource>();
  43. }
  44. return CreateAsyncObservable<TSource>.From(
  45. source,
  46. (count, scheduler),
  47. static async (source, state, observer) =>
  48. {
  49. var (sink, drain) = AsyncObserver.TakeLast(observer, state.count, state.scheduler);
  50. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  51. return StableCompositeAsyncDisposable.Create(subscription, drain);
  52. });
  53. }
  54. public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)
  55. {
  56. if (source == null)
  57. throw new ArgumentNullException(nameof(source));
  58. if (duration < TimeSpan.Zero)
  59. throw new ArgumentOutOfRangeException(nameof(duration));
  60. if (duration == TimeSpan.Zero)
  61. {
  62. return Empty<TSource>();
  63. }
  64. return CreateAsyncObservable<TSource>.From(
  65. source,
  66. duration,
  67. static async (source, duration, observer) =>
  68. {
  69. var (sink, drain) = AsyncObserver.TakeLast(observer, duration);
  70. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  71. return StableCompositeAsyncDisposable.Create(subscription, drain);
  72. });
  73. }
  74. public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IClock clock)
  75. {
  76. if (source == null)
  77. throw new ArgumentNullException(nameof(source));
  78. if (duration < TimeSpan.Zero)
  79. throw new ArgumentOutOfRangeException(nameof(duration));
  80. if (clock == null)
  81. throw new ArgumentNullException(nameof(clock));
  82. if (duration == TimeSpan.Zero)
  83. {
  84. return Empty<TSource>();
  85. }
  86. return CreateAsyncObservable<TSource>.From(
  87. source,
  88. (duration, clock),
  89. static async (source, state, observer) =>
  90. {
  91. var (sink, drain) = AsyncObserver.TakeLast(observer, state.duration, state.clock);
  92. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  93. return StableCompositeAsyncDisposable.Create(subscription, drain);
  94. });
  95. }
  96. public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IClock clock, IAsyncScheduler scheduler)
  97. {
  98. if (source == null)
  99. throw new ArgumentNullException(nameof(source));
  100. if (duration < TimeSpan.Zero)
  101. throw new ArgumentOutOfRangeException(nameof(duration));
  102. if (clock == null)
  103. throw new ArgumentNullException(nameof(clock));
  104. if (scheduler == null)
  105. throw new ArgumentNullException(nameof(scheduler));
  106. if (duration == TimeSpan.Zero)
  107. {
  108. return Empty<TSource>();
  109. }
  110. return CreateAsyncObservable<TSource>.From(
  111. source,
  112. (duration, clock, scheduler),
  113. static async (source, state, observer) =>
  114. {
  115. var (sink, drain) = AsyncObserver.TakeLast(observer, state.duration, state.clock, state.scheduler);
  116. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  117. return StableCompositeAsyncDisposable.Create(subscription, drain);
  118. });
  119. }
  120. public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IAsyncScheduler scheduler) => TakeLast(source, duration, scheduler, scheduler);
  121. }
  122. public partial class AsyncObserver
  123. {
  124. public static (IAsyncObserver<TSource>, IAsyncDisposable) TakeLast<TSource>(IAsyncObserver<TSource> observer, int count) => TakeLast(observer, count, TaskPoolAsyncScheduler.Default);
  125. public static (IAsyncObserver<TSource>, IAsyncDisposable) TakeLast<TSource>(IAsyncObserver<TSource> observer, int count, IAsyncScheduler scheduler)
  126. {
  127. if (observer == null)
  128. throw new ArgumentNullException(nameof(observer));
  129. if (count <= 0)
  130. throw new ArgumentOutOfRangeException(nameof(count));
  131. if (scheduler == null)
  132. throw new ArgumentNullException(nameof(scheduler));
  133. var sad = new SingleAssignmentAsyncDisposable();
  134. var queue = new Queue<TSource>();
  135. return
  136. (
  137. Create<TSource>(
  138. x =>
  139. {
  140. queue.Enqueue(x);
  141. if (queue.Count > count)
  142. {
  143. queue.Dequeue();
  144. }
  145. return default;
  146. },
  147. observer.OnErrorAsync,
  148. async () =>
  149. {
  150. var drain = await scheduler.ScheduleAsync(async ct =>
  151. {
  152. while (!ct.IsCancellationRequested && queue.Count > 0)
  153. {
  154. await observer.OnNextAsync(queue.Dequeue()).RendezVous(scheduler, ct);
  155. }
  156. ct.ThrowIfCancellationRequested();
  157. await observer.OnCompletedAsync().RendezVous(scheduler, ct);
  158. }).ConfigureAwait(false);
  159. await sad.AssignAsync(drain).ConfigureAwait(false);
  160. }
  161. ),
  162. sad
  163. );
  164. }
  165. public static (IAsyncObserver<TSource>, IAsyncDisposable) TakeLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration) => TakeLast(observer, duration, Clock.Default, TaskPoolAsyncScheduler.Default);
  166. public static (IAsyncObserver<TSource>, IAsyncDisposable) TakeLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IAsyncScheduler scheduler) => TakeLast(observer, duration, scheduler, scheduler);
  167. public static (IAsyncObserver<TSource>, IAsyncDisposable) TakeLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IClock clock) => TakeLast(observer, duration, clock, TaskPoolAsyncScheduler.Default);
  168. public static (IAsyncObserver<TSource>, IAsyncDisposable) TakeLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IClock clock, IAsyncScheduler scheduler)
  169. {
  170. if (observer == null)
  171. throw new ArgumentNullException(nameof(observer));
  172. if (duration < TimeSpan.Zero)
  173. throw new ArgumentOutOfRangeException(nameof(duration));
  174. if (scheduler == null)
  175. throw new ArgumentNullException(nameof(scheduler));
  176. var sad = new SingleAssignmentAsyncDisposable();
  177. var queue = new Queue<Timestamped<TSource>>();
  178. void Trim(DateTimeOffset now)
  179. {
  180. while (queue.Count > 0 && now - queue.Peek().Timestamp >= duration)
  181. {
  182. queue.Dequeue();
  183. }
  184. }
  185. return
  186. (
  187. Create<TSource>(
  188. x =>
  189. {
  190. var now = clock.Now;
  191. queue.Enqueue(new Timestamped<TSource>(x, now));
  192. Trim(now);
  193. return default;
  194. },
  195. observer.OnErrorAsync,
  196. async () =>
  197. {
  198. Trim(clock.Now);
  199. var drain = await scheduler.ScheduleAsync(async ct =>
  200. {
  201. while (!ct.IsCancellationRequested && queue.Count > 0)
  202. {
  203. await observer.OnNextAsync(queue.Dequeue().Value).RendezVous(scheduler, ct);
  204. }
  205. ct.ThrowIfCancellationRequested();
  206. await observer.OnCompletedAsync().RendezVous(scheduler, ct);
  207. }).ConfigureAwait(false);
  208. await sad.AssignAsync(drain).ConfigureAwait(false);
  209. }
  210. ),
  211. sad
  212. );
  213. }
  214. }
  215. }