SkipLast.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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<TSource> SkipLast<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 source;
  20. }
  21. return CreateAsyncObservable<TSource>.From(
  22. source,
  23. count,
  24. static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.SkipLast(observer, count)));
  25. }
  26. public static IAsyncObservable<TSource> SkipLast<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 source;
  35. }
  36. return CreateAsyncObservable<TSource>.From(
  37. source,
  38. duration,
  39. static (source, duration, observer) => source.SubscribeSafeAsync(AsyncObserver.SkipLast(observer, duration)));
  40. }
  41. public static IAsyncObservable<TSource> SkipLast<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 source;
  52. }
  53. return CreateAsyncObservable<TSource>.From(
  54. source,
  55. (duration, clock),
  56. static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.SkipLast(observer, state.duration, state.clock)));
  57. }
  58. }
  59. public partial class AsyncObserver
  60. {
  61. public static IAsyncObserver<TSource> SkipLast<TSource>(IAsyncObserver<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. async x =>
  70. {
  71. queue.Enqueue(x);
  72. if (queue.Count > count)
  73. {
  74. await observer.OnNextAsync(queue.Dequeue()).ConfigureAwait(false);
  75. }
  76. },
  77. observer.OnErrorAsync,
  78. observer.OnCompletedAsync
  79. );
  80. }
  81. public static IAsyncObserver<TSource> SkipLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration) => SkipLast(observer, duration, Clock.Default);
  82. public static IAsyncObserver<TSource> SkipLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IClock clock)
  83. {
  84. if (observer == null)
  85. throw new ArgumentNullException(nameof(observer));
  86. if (duration < TimeSpan.Zero)
  87. throw new ArgumentOutOfRangeException(nameof(duration));
  88. if (clock == null)
  89. throw new ArgumentNullException(nameof(clock));
  90. var queue = new Queue<Timestamped<TSource>>();
  91. async Task FlushAsync(DateTimeOffset now)
  92. {
  93. while (queue.Count > 0 && now - queue.Peek().Timestamp >= duration)
  94. {
  95. await observer.OnNextAsync(queue.Dequeue().Value).ConfigureAwait(false);
  96. }
  97. }
  98. return Create<TSource>(
  99. async x =>
  100. {
  101. var now = clock.Now;
  102. queue.Enqueue(new Timestamped<TSource>(x, now));
  103. await FlushAsync(now).ConfigureAwait(false);
  104. },
  105. observer.OnErrorAsync,
  106. async () =>
  107. {
  108. await FlushAsync(clock.Now).ConfigureAwait(false);
  109. await observer.OnCompletedAsync().ConfigureAwait(false);
  110. }
  111. );
  112. }
  113. }
  114. }