SkipLast.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.SubscribeAsync(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.SubscribeAsync(AsyncObserver.SkipLast(observer, duration)));
  34. }
  35. public static IAsyncObservable<TSource> SkipLast<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IAsyncScheduler scheduler)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (duration < TimeSpan.Zero)
  40. throw new ArgumentOutOfRangeException(nameof(duration));
  41. if (scheduler == null)
  42. throw new ArgumentNullException(nameof(scheduler));
  43. if (duration == TimeSpan.Zero)
  44. {
  45. return source;
  46. }
  47. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.SkipLast(observer, duration, scheduler)));
  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)
  73. {
  74. if (observer == null)
  75. throw new ArgumentNullException(nameof(observer));
  76. if (duration < TimeSpan.Zero)
  77. throw new ArgumentOutOfRangeException(nameof(duration));
  78. throw new NotImplementedException();
  79. }
  80. public static IAsyncObserver<TSource> SkipLast<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IAsyncScheduler scheduler)
  81. {
  82. if (observer == null)
  83. throw new ArgumentNullException(nameof(observer));
  84. if (duration < TimeSpan.Zero)
  85. throw new ArgumentOutOfRangeException(nameof(duration));
  86. if (scheduler == null)
  87. throw new ArgumentNullException(nameof(scheduler));
  88. throw new NotImplementedException();
  89. }
  90. }
  91. }