SkipLast.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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<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 Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.SkipLast(observer, count)));
  22. }
  23. public static IAsyncObservable<TSource> SkipLast<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 source;
  32. }
  33. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.SkipLast(observer, duration)));
  34. }
  35. public static IAsyncObservable<TSource> SkipLast<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 source;
  46. }
  47. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.SkipLast(observer, duration, clock)));
  48. }
  49. }
  50. partial class AsyncObserver
  51. {
  52. public static IAsyncObserver<TSource> SkipLast<TSource>(IAsyncObserver<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. async x =>
  61. {
  62. queue.Enqueue(x);
  63. if (queue.Count > count)
  64. {
  65. await observer.OnNextAsync(queue.Dequeue()).ConfigureAwait(false);
  66. }
  67. },
  68. observer.OnErrorAsync,
  69. observer.OnCompletedAsync
  70. );
  71. }
  72. public static IAsyncObserver<TSource> SkipLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration) => SkipLast(observer, duration, Clock.Default);
  73. public static IAsyncObserver<TSource> SkipLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IClock clock)
  74. {
  75. if (observer == null)
  76. throw new ArgumentNullException(nameof(observer));
  77. if (duration < TimeSpan.Zero)
  78. throw new ArgumentOutOfRangeException(nameof(duration));
  79. if (clock == null)
  80. throw new ArgumentNullException(nameof(clock));
  81. var queue = new Queue<Timestamped<TSource>>();
  82. async Task FlushAsync(DateTimeOffset now)
  83. {
  84. while (queue.Count > 0 && now - queue.Peek().Timestamp >= duration)
  85. {
  86. await observer.OnNextAsync(queue.Dequeue().Value).ConfigureAwait(false);
  87. }
  88. }
  89. return Create<TSource>(
  90. async x =>
  91. {
  92. var now = clock.Now;
  93. queue.Enqueue(new Timestamped<TSource>(x, now));
  94. await FlushAsync(now).ConfigureAwait(false);
  95. },
  96. observer.OnErrorAsync,
  97. async () =>
  98. {
  99. await FlushAsync(clock.Now).ConfigureAwait(false);
  100. await observer.OnCompletedAsync().ConfigureAwait(false);
  101. }
  102. );
  103. }
  104. }
  105. }