Take.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.Reactive.Concurrency;
  5. namespace System.Reactive.Linq
  6. {
  7. partial class AsyncObservable
  8. {
  9. public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, int count)
  10. {
  11. if (source == null)
  12. throw new ArgumentNullException(nameof(source));
  13. if (count < 0)
  14. throw new ArgumentOutOfRangeException(nameof(count));
  15. if (count == 0)
  16. {
  17. return Empty<TSource>();
  18. }
  19. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.Take(observer, count)));
  20. }
  21. public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)
  22. {
  23. if (source == null)
  24. throw new ArgumentNullException(nameof(source));
  25. if (duration < TimeSpan.Zero)
  26. throw new ArgumentOutOfRangeException(nameof(duration));
  27. if (duration == TimeSpan.Zero)
  28. {
  29. return Empty<TSource>();
  30. }
  31. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.Take(observer, duration)));
  32. }
  33. public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IAsyncScheduler scheduler)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. if (duration < TimeSpan.Zero)
  38. throw new ArgumentOutOfRangeException(nameof(duration));
  39. if (scheduler == null)
  40. throw new ArgumentNullException(nameof(scheduler));
  41. if (duration == TimeSpan.Zero)
  42. {
  43. return Empty<TSource>();
  44. }
  45. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.Take(observer, duration, scheduler)));
  46. }
  47. }
  48. partial class AsyncObserver
  49. {
  50. public static IAsyncObserver<TSource> Take<TSource>(IAsyncObserver<TSource> observer, int count)
  51. {
  52. if (observer == null)
  53. throw new ArgumentNullException(nameof(observer));
  54. if (count <= 0)
  55. throw new ArgumentOutOfRangeException(nameof(count));
  56. return Create<TSource>(
  57. async x =>
  58. {
  59. var remaining = --count;
  60. await observer.OnNextAsync(x).ConfigureAwait(false);
  61. if (remaining == 0)
  62. {
  63. await observer.OnCompletedAsync().ConfigureAwait(false);
  64. }
  65. },
  66. observer.OnErrorAsync,
  67. observer.OnCompletedAsync
  68. );
  69. }
  70. public static IAsyncObserver<TSource> Take<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration) => Take(observer, duration, TaskPoolAsyncScheduler.Default);
  71. public static IAsyncObserver<TSource> Take<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IAsyncScheduler scheduler)
  72. {
  73. if (observer == null)
  74. throw new ArgumentNullException(nameof(observer));
  75. if (duration < TimeSpan.Zero)
  76. throw new ArgumentOutOfRangeException(nameof(duration));
  77. if (scheduler == null)
  78. throw new ArgumentNullException(nameof(scheduler));
  79. throw new NotImplementedException();
  80. }
  81. }
  82. }