TakeLastBuffer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Threading.Tasks;
  7. namespace System.Reactive.Linq
  8. {
  9. public partial class AsyncObservable
  10. {
  11. public static IAsyncObservable<IList<TSource>> TakeLastBuffer<TSource>(this IAsyncObservable<TSource> source, int count)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (count < 0)
  16. throw new ArgumentOutOfRangeException(nameof(count));
  17. if (count == 0)
  18. {
  19. return Empty<IList<TSource>>();
  20. }
  21. return CreateAsyncObservable<IList<TSource>>.From(
  22. source,
  23. count,
  24. static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, count)));
  25. }
  26. public static IAsyncObservable<IList<TSource>> TakeLastBuffer<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)
  27. {
  28. if (source == null)
  29. throw new ArgumentNullException(nameof(source));
  30. if (duration < TimeSpan.Zero)
  31. throw new ArgumentOutOfRangeException(nameof(duration));
  32. if (duration == TimeSpan.Zero)
  33. {
  34. return Empty<IList<TSource>>();
  35. }
  36. return CreateAsyncObservable<IList<TSource>>.From(
  37. source,
  38. duration,
  39. static (source, duration, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, duration)));
  40. }
  41. public static IAsyncObservable<IList<TSource>> TakeLastBuffer<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IClock clock)
  42. {
  43. if (source == null)
  44. throw new ArgumentNullException(nameof(source));
  45. if (duration < TimeSpan.Zero)
  46. throw new ArgumentOutOfRangeException(nameof(duration));
  47. if (clock == null)
  48. throw new ArgumentNullException(nameof(clock));
  49. if (duration == TimeSpan.Zero)
  50. {
  51. return Empty<IList<TSource>>();
  52. }
  53. return CreateAsyncObservable<IList<TSource>>.From(
  54. source,
  55. (duration, clock),
  56. static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, state.duration, state.clock)));
  57. }
  58. }
  59. public partial class AsyncObserver
  60. {
  61. public static IAsyncObserver<TSource> TakeLastBuffer<TSource>(IAsyncObserver<IList<TSource>> observer, int count)
  62. {
  63. if (observer == null)
  64. throw new ArgumentNullException(nameof(observer));
  65. if (count <= 0)
  66. throw new ArgumentOutOfRangeException(nameof(count));
  67. var queue = new Queue<TSource>();
  68. return Create<TSource>(
  69. x =>
  70. {
  71. queue.Enqueue(x);
  72. if (queue.Count > count)
  73. {
  74. queue.Dequeue();
  75. }
  76. return default;
  77. },
  78. observer.OnErrorAsync,
  79. async () =>
  80. {
  81. var n = queue.Count;
  82. var res = new List<TSource>(n);
  83. while (queue.Count > 0)
  84. {
  85. res.Add(queue.Dequeue());
  86. }
  87. await observer.OnNextAsync(res).ConfigureAwait(false);
  88. await observer.OnCompletedAsync().ConfigureAwait(false);
  89. }
  90. );
  91. }
  92. public static IAsyncObserver<TSource> TakeLastBuffer<TSource>(IAsyncObserver<IList<TSource>> observer, TimeSpan duration) => TakeLastBuffer(observer, duration, Clock.Default);
  93. public static IAsyncObserver<TSource> TakeLastBuffer<TSource>(IAsyncObserver<IList<TSource>> observer, TimeSpan duration, IClock clock)
  94. {
  95. if (observer == null)
  96. throw new ArgumentNullException(nameof(observer));
  97. if (duration < TimeSpan.Zero)
  98. throw new ArgumentOutOfRangeException(nameof(duration));
  99. if (clock == null)
  100. throw new ArgumentNullException(nameof(clock));
  101. var queue = new Queue<Timestamped<TSource>>();
  102. void Trim(DateTimeOffset now)
  103. {
  104. while (queue.Count > 0 && now - queue.Peek().Timestamp >= duration)
  105. {
  106. queue.Dequeue();
  107. }
  108. }
  109. return Create<TSource>(
  110. x =>
  111. {
  112. var now = clock.Now;
  113. queue.Enqueue(new Timestamped<TSource>(x, now));
  114. Trim(now);
  115. return default;
  116. },
  117. observer.OnErrorAsync,
  118. async () =>
  119. {
  120. Trim(clock.Now);
  121. var n = queue.Count;
  122. var res = new List<TSource>(n);
  123. while (queue.Count > 0)
  124. {
  125. res.Add(queue.Dequeue().Value);
  126. }
  127. await observer.OnNextAsync(res).ConfigureAwait(false);
  128. await observer.OnCompletedAsync().ConfigureAwait(false);
  129. }
  130. );
  131. }
  132. }
  133. }