TakeLastBuffer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.SubscribeAsync(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.SubscribeAsync(AsyncObserver.TakeLastBuffer(observer, duration)));
  34. }
  35. public static IAsyncObservable<IList<TSource>> TakeLastBuffer<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 Empty<IList<TSource>>();
  46. }
  47. return Create<IList<TSource>>(observer => source.SubscribeAsync(AsyncObserver.TakeLastBuffer(observer, duration, scheduler)));
  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, TaskPoolAsyncScheduler.Default);
  84. public static IAsyncObserver<TSource> TakeLastBuffer<TSource>(IAsyncObserver<IList<TSource>> observer, TimeSpan duration, IAsyncScheduler scheduler)
  85. {
  86. if (observer == null)
  87. throw new ArgumentNullException(nameof(observer));
  88. if (duration < TimeSpan.Zero)
  89. throw new ArgumentOutOfRangeException(nameof(duration));
  90. if (scheduler == null)
  91. throw new ArgumentNullException(nameof(scheduler));
  92. throw new NotImplementedException();
  93. }
  94. }
  95. }