TakeLast.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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> TakeLast<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<TSource>();
  20. }
  21. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.TakeLast(observer, count)));
  22. }
  23. public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, int count, IAsyncScheduler scheduler)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. if (count < 0)
  28. throw new ArgumentOutOfRangeException(nameof(count));
  29. if (scheduler == null)
  30. throw new ArgumentNullException(nameof(scheduler));
  31. if (count == 0)
  32. {
  33. return Empty<TSource>();
  34. }
  35. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.TakeLast(observer, count, scheduler)));
  36. }
  37. public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)
  38. {
  39. if (source == null)
  40. throw new ArgumentNullException(nameof(source));
  41. if (duration < TimeSpan.Zero)
  42. throw new ArgumentOutOfRangeException(nameof(duration));
  43. if (duration == TimeSpan.Zero)
  44. {
  45. return Empty<TSource>();
  46. }
  47. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.TakeLast(observer, duration)));
  48. }
  49. public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IAsyncScheduler scheduler)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException(nameof(source));
  53. if (duration < TimeSpan.Zero)
  54. throw new ArgumentOutOfRangeException(nameof(duration));
  55. if (scheduler == null)
  56. throw new ArgumentNullException(nameof(scheduler));
  57. if (duration == TimeSpan.Zero)
  58. {
  59. return Empty<TSource>();
  60. }
  61. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.TakeLast(observer, duration, scheduler)));
  62. }
  63. }
  64. partial class AsyncObserver
  65. {
  66. public static IAsyncObserver<TSource> TakeLast<TSource>(IAsyncObserver<TSource> observer, int count)
  67. {
  68. if (observer == null)
  69. throw new ArgumentNullException(nameof(observer));
  70. if (count <= 0)
  71. throw new ArgumentOutOfRangeException(nameof(count));
  72. var queue = new Queue<TSource>();
  73. return Create<TSource>(
  74. x =>
  75. {
  76. queue.Enqueue(x);
  77. if (queue.Count > count)
  78. {
  79. queue.Dequeue();
  80. }
  81. return Task.CompletedTask;
  82. },
  83. observer.OnErrorAsync,
  84. async () =>
  85. {
  86. var n = queue.Count;
  87. while (queue.Count > 0)
  88. {
  89. await observer.OnNextAsync(queue.Dequeue()).ConfigureAwait(false);
  90. }
  91. await observer.OnCompletedAsync().ConfigureAwait(false);
  92. }
  93. );
  94. }
  95. public static IAsyncObserver<TSource> TakeLast<TSource>(IAsyncObserver<TSource> observer, int count, IAsyncScheduler scheduler)
  96. {
  97. if (observer == null)
  98. throw new ArgumentNullException(nameof(observer));
  99. if (count <= 0)
  100. throw new ArgumentOutOfRangeException(nameof(count));
  101. if (scheduler == null)
  102. throw new ArgumentNullException(nameof(scheduler));
  103. throw new NotImplementedException();
  104. }
  105. public static IAsyncObserver<TSource> TakeLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration) => TakeLast(observer, duration, TaskPoolAsyncScheduler.Default);
  106. public static IAsyncObserver<TSource> TakeLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IAsyncScheduler scheduler)
  107. {
  108. if (observer == null)
  109. throw new ArgumentNullException(nameof(observer));
  110. if (duration < TimeSpan.Zero)
  111. throw new ArgumentOutOfRangeException(nameof(duration));
  112. if (scheduler == null)
  113. throw new ArgumentNullException(nameof(scheduler));
  114. throw new NotImplementedException();
  115. }
  116. }
  117. }