TakeLast.cs 11 KB

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