TakeLastBuffer.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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. 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 Create<IList<TSource>>(observer => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, count)));
  22. }
  23. public static IAsyncObservable<IList<TSource>> TakeLastBuffer<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. if (duration < TimeSpan.Zero)
  28. throw new ArgumentOutOfRangeException(nameof(duration));
  29. if (duration == TimeSpan.Zero)
  30. {
  31. return Empty<IList<TSource>>();
  32. }
  33. return Create<IList<TSource>>(observer => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, duration)));
  34. }
  35. public static IAsyncObservable<IList<TSource>> TakeLastBuffer<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IClock clock)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (duration < TimeSpan.Zero)
  40. throw new ArgumentOutOfRangeException(nameof(duration));
  41. if (clock == null)
  42. throw new ArgumentNullException(nameof(clock));
  43. if (duration == TimeSpan.Zero)
  44. {
  45. return Empty<IList<TSource>>();
  46. }
  47. return Create<IList<TSource>>(observer => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, duration, clock)));
  48. }
  49. }
  50. partial class AsyncObserver
  51. {
  52. public static IAsyncObserver<TSource> TakeLastBuffer<TSource>(IAsyncObserver<IList<TSource>> observer, int count)
  53. {
  54. if (observer == null)
  55. throw new ArgumentNullException(nameof(observer));
  56. if (count <= 0)
  57. throw new ArgumentOutOfRangeException(nameof(count));
  58. var queue = new Queue<TSource>();
  59. return Create<TSource>(
  60. x =>
  61. {
  62. queue.Enqueue(x);
  63. if (queue.Count > count)
  64. {
  65. queue.Dequeue();
  66. }
  67. return Task.CompletedTask;
  68. },
  69. observer.OnErrorAsync,
  70. async () =>
  71. {
  72. var n = queue.Count;
  73. var res = new List<TSource>(n);
  74. while (queue.Count > 0)
  75. {
  76. res.Add(queue.Dequeue());
  77. }
  78. await observer.OnNextAsync(res).ConfigureAwait(false);
  79. await observer.OnCompletedAsync().ConfigureAwait(false);
  80. }
  81. );
  82. }
  83. public static IAsyncObserver<TSource> TakeLastBuffer<TSource>(IAsyncObserver<IList<TSource>> observer, TimeSpan duration) => TakeLastBuffer(observer, duration, Clock.Default);
  84. public static IAsyncObserver<TSource> TakeLastBuffer<TSource>(IAsyncObserver<IList<TSource>> observer, TimeSpan duration, IClock clock)
  85. {
  86. if (observer == null)
  87. throw new ArgumentNullException(nameof(observer));
  88. if (duration < TimeSpan.Zero)
  89. throw new ArgumentOutOfRangeException(nameof(duration));
  90. if (clock == null)
  91. throw new ArgumentNullException(nameof(clock));
  92. var queue = new Queue<Timestamped<TSource>>();
  93. void Trim(DateTimeOffset now)
  94. {
  95. while (queue.Count > 0 && now - queue.Peek().Timestamp >= duration)
  96. {
  97. queue.Dequeue();
  98. }
  99. }
  100. return Create<TSource>(
  101. x =>
  102. {
  103. var now = clock.Now;
  104. queue.Enqueue(new Timestamped<TSource>(x, now));
  105. Trim(now);
  106. return Task.CompletedTask;
  107. },
  108. observer.OnErrorAsync,
  109. async () =>
  110. {
  111. Trim(clock.Now);
  112. var n = queue.Count;
  113. var res = new List<TSource>(n);
  114. while (queue.Count > 0)
  115. {
  116. res.Add(queue.Dequeue().Value);
  117. }
  118. await observer.OnNextAsync(res).ConfigureAwait(false);
  119. await observer.OnCompletedAsync().ConfigureAwait(false);
  120. }
  121. );
  122. }
  123. }
  124. }