Take.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Concurrency;
  5. using System.Reactive.Disposables;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Reactive.Linq
  9. {
  10. public partial class AsyncObservable
  11. {
  12. public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, int count)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (count < 0)
  17. throw new ArgumentOutOfRangeException(nameof(count));
  18. if (count == 0)
  19. {
  20. return Empty<TSource>();
  21. }
  22. return CreateAsyncObservable<TSource>.From(
  23. source,
  24. count,
  25. static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.Take(observer, count)));
  26. }
  27. public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (duration < TimeSpan.Zero)
  32. throw new ArgumentOutOfRangeException(nameof(duration));
  33. if (duration == TimeSpan.Zero)
  34. {
  35. return Empty<TSource>();
  36. }
  37. // REVIEW: May be easier to just use TakeUntil with a Timer parameter. Do we want Take on the observer?
  38. return CreateAsyncObservable<TSource>.From(
  39. source,
  40. duration,
  41. static async (source, duration, observer) =>
  42. {
  43. var (sourceObserver, timer) = await AsyncObserver.Take(observer, duration).ConfigureAwait(false);
  44. var subscription = await source.SubscribeSafeAsync(sourceObserver).ConfigureAwait(false);
  45. return StableCompositeAsyncDisposable.Create(subscription, timer);
  46. });
  47. }
  48. public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IAsyncScheduler scheduler)
  49. {
  50. if (source == null)
  51. throw new ArgumentNullException(nameof(source));
  52. if (duration < TimeSpan.Zero)
  53. throw new ArgumentOutOfRangeException(nameof(duration));
  54. if (scheduler == null) // REVIEW: scheduler not used.
  55. throw new ArgumentNullException(nameof(scheduler));
  56. if (duration == TimeSpan.Zero)
  57. {
  58. return Empty<TSource>();
  59. }
  60. // REVIEW: May be easier to just use TakeUntil with a Timer parameter. Do we want Take on the observer?
  61. return CreateAsyncObservable<TSource>.From(
  62. source,
  63. (duration, scheduler),
  64. static async (source, state, observer) =>
  65. {
  66. var (sourceObserver, timer) = await AsyncObserver.Take(observer, state.duration).ConfigureAwait(false);
  67. var subscription = await source.SubscribeSafeAsync(sourceObserver).ConfigureAwait(false);
  68. return StableCompositeAsyncDisposable.Create(subscription, timer);
  69. });
  70. }
  71. }
  72. public partial class AsyncObserver
  73. {
  74. public static IAsyncObserver<TSource> Take<TSource>(IAsyncObserver<TSource> observer, int count)
  75. {
  76. if (observer == null)
  77. throw new ArgumentNullException(nameof(observer));
  78. if (count <= 0)
  79. throw new ArgumentOutOfRangeException(nameof(count));
  80. return Create<TSource>(
  81. async x =>
  82. {
  83. var remaining = --count;
  84. await observer.OnNextAsync(x).ConfigureAwait(false);
  85. if (remaining == 0)
  86. {
  87. await observer.OnCompletedAsync().ConfigureAwait(false);
  88. }
  89. },
  90. observer.OnErrorAsync,
  91. observer.OnCompletedAsync
  92. );
  93. }
  94. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Take<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration) => Take(observer, duration, TaskPoolAsyncScheduler.Default);
  95. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Take<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IAsyncScheduler scheduler)
  96. {
  97. if (observer == null)
  98. throw new ArgumentNullException(nameof(observer));
  99. if (duration < TimeSpan.Zero)
  100. throw new ArgumentOutOfRangeException(nameof(duration));
  101. if (scheduler == null)
  102. throw new ArgumentNullException(nameof(scheduler));
  103. return CoreAsync();
  104. async Task<(IAsyncObserver<TSource>, IAsyncDisposable)> CoreAsync()
  105. {
  106. // REVIEW: May be easier to just use TakeUntil with a Timer parameter. Do we want TakeUntil on the observer?
  107. // DESIGN: It seems that if an observer would be an IAsyncDisposable, this could get a bit easier ("inject" the inner disposable).
  108. var gate = new AsyncGate();
  109. return
  110. (
  111. Synchronize(observer, gate),
  112. await scheduler.ScheduleAsync(async ct =>
  113. {
  114. if (!ct.IsCancellationRequested)
  115. {
  116. using (await gate.LockAsync().RendezVous(scheduler, ct))
  117. {
  118. await observer.OnCompletedAsync().RendezVous(scheduler, ct);
  119. }
  120. }
  121. }, duration).ConfigureAwait(false)
  122. );
  123. }
  124. }
  125. }
  126. }