Take.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. using System.Reactive.Disposables;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Reactive.Linq
  9. {
  10. 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 Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Take(observer, count)));
  23. }
  24. public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. if (duration < TimeSpan.Zero)
  29. throw new ArgumentOutOfRangeException(nameof(duration));
  30. if (duration == TimeSpan.Zero)
  31. {
  32. return Empty<TSource>();
  33. }
  34. // REVIEW: May be easier to just use TakeUntil with a Timer parameter. Do we want Take on the observer?
  35. return Create<TSource>(async observer =>
  36. {
  37. var (sourceObserver, timer) = await AsyncObserver.Take(observer, duration).ConfigureAwait(false);
  38. var subscription = await source.SubscribeSafeAsync(sourceObserver).ConfigureAwait(false);
  39. return StableCompositeAsyncDisposable.Create(subscription, timer);
  40. });
  41. }
  42. public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IAsyncScheduler scheduler)
  43. {
  44. if (source == null)
  45. throw new ArgumentNullException(nameof(source));
  46. if (duration < TimeSpan.Zero)
  47. throw new ArgumentOutOfRangeException(nameof(duration));
  48. if (scheduler == null)
  49. throw new ArgumentNullException(nameof(scheduler));
  50. if (duration == TimeSpan.Zero)
  51. {
  52. return Empty<TSource>();
  53. }
  54. // REVIEW: May be easier to just use TakeUntil with a Timer parameter. Do we want Take on the observer?
  55. return Create<TSource>(async observer =>
  56. {
  57. var (sourceObserver, timer) = await AsyncObserver.Take(observer, duration).ConfigureAwait(false);
  58. var subscription = await source.SubscribeSafeAsync(sourceObserver).ConfigureAwait(false);
  59. return StableCompositeAsyncDisposable.Create(subscription, timer);
  60. });
  61. }
  62. }
  63. partial class AsyncObserver
  64. {
  65. public static IAsyncObserver<TSource> Take<TSource>(IAsyncObserver<TSource> observer, int count)
  66. {
  67. if (observer == null)
  68. throw new ArgumentNullException(nameof(observer));
  69. if (count <= 0)
  70. throw new ArgumentOutOfRangeException(nameof(count));
  71. return Create<TSource>(
  72. async x =>
  73. {
  74. var remaining = --count;
  75. await observer.OnNextAsync(x).ConfigureAwait(false);
  76. if (remaining == 0)
  77. {
  78. await observer.OnCompletedAsync().ConfigureAwait(false);
  79. }
  80. },
  81. observer.OnErrorAsync,
  82. observer.OnCompletedAsync
  83. );
  84. }
  85. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Take<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration) => Take(observer, duration, TaskPoolAsyncScheduler.Default);
  86. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Take<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IAsyncScheduler scheduler)
  87. {
  88. if (observer == null)
  89. throw new ArgumentNullException(nameof(observer));
  90. if (duration < TimeSpan.Zero)
  91. throw new ArgumentOutOfRangeException(nameof(duration));
  92. if (scheduler == null)
  93. throw new ArgumentNullException(nameof(scheduler));
  94. return CoreAsync();
  95. async Task<(IAsyncObserver<TSource>, IAsyncDisposable)> CoreAsync()
  96. {
  97. // REVIEW: May be easier to just use TakeUntil with a Timer parameter. Do we want TakeUntil on the observer?
  98. // DESIGN: It seems that if an observer would be an IAsyncDisposable, this could get a bit easier ("inject" the inner disposable).
  99. var gate = new AsyncLock();
  100. return
  101. (
  102. Synchronize(observer, gate),
  103. await scheduler.ScheduleAsync(async ct =>
  104. {
  105. if (!ct.IsCancellationRequested)
  106. {
  107. using (await gate.LockAsync().RendezVous(scheduler, ct))
  108. {
  109. await observer.OnCompletedAsync().RendezVous(scheduler, ct);
  110. }
  111. }
  112. }, duration).ConfigureAwait(false)
  113. );
  114. }
  115. }
  116. }
  117. }