Skip.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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> Skip<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 source;
  21. }
  22. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Skip(observer, count)));
  23. }
  24. public static IAsyncObservable<TSource> Skip<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 source;
  33. }
  34. // REVIEW: May be easier to just use SkipUntil with a Timer parameter. Do we want Skip on the observer?
  35. return Create<TSource>(async observer =>
  36. {
  37. var (sourceObserver, timer) = await AsyncObserver.Skip(observer, duration);
  38. var subscription = await source.SubscribeSafeAsync(sourceObserver).ConfigureAwait(false);
  39. return StableCompositeAsyncDisposable.Create(subscription, timer);
  40. });
  41. }
  42. public static IAsyncObservable<TSource> Skip<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 source;
  53. }
  54. // REVIEW: May be easier to just use SkipUntil with a Timer parameter. Do we want Skip on the observer?
  55. return Create<TSource>(async observer =>
  56. {
  57. var (sourceObserver, timer) = await AsyncObserver.Skip(observer, duration);
  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> Skip<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. if (count == 0)
  75. {
  76. await observer.OnNextAsync(x).ConfigureAwait(false);
  77. }
  78. else
  79. {
  80. --count;
  81. }
  82. },
  83. observer.OnErrorAsync,
  84. observer.OnCompletedAsync
  85. );
  86. }
  87. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Skip<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration) => Skip(observer, duration, TaskPoolAsyncScheduler.Default);
  88. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Skip<TSource>(IAsyncObserver<TSource> observer, TimeSpan duration, IAsyncScheduler scheduler)
  89. {
  90. if (observer == null)
  91. throw new ArgumentNullException(nameof(observer));
  92. if (duration < TimeSpan.Zero)
  93. throw new ArgumentOutOfRangeException(nameof(duration));
  94. if (scheduler == null)
  95. throw new ArgumentNullException(nameof(scheduler));
  96. return CoreAsync();
  97. async Task<(IAsyncObserver<TSource>, IAsyncDisposable)> CoreAsync()
  98. {
  99. // REVIEW: May be easier to just use SkipUntil with a Timer parameter. Do we want Skip on the observer?
  100. // DESIGN: It seems that if an observer would be an IAsyncDisposable, this could get a bit easier ("inject" the inner disposable).
  101. var gate = new AsyncLock();
  102. var open = false;
  103. return
  104. (
  105. Create<TSource>(
  106. async x =>
  107. {
  108. using (await gate.LockAsync().ConfigureAwait(false))
  109. {
  110. if (open)
  111. {
  112. await observer.OnNextAsync(x).ConfigureAwait(false);
  113. }
  114. }
  115. },
  116. async ex =>
  117. {
  118. using (await gate.LockAsync().ConfigureAwait(false))
  119. {
  120. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  121. }
  122. },
  123. async () =>
  124. {
  125. using (await gate.LockAsync().ConfigureAwait(false))
  126. {
  127. await observer.OnCompletedAsync().ConfigureAwait(false);
  128. }
  129. }
  130. ),
  131. await scheduler.ScheduleAsync(async ct =>
  132. {
  133. ct.ThrowIfCancellationRequested();
  134. using (await gate.LockAsync().RendezVous(scheduler, ct))
  135. {
  136. open = true;
  137. }
  138. }, duration)
  139. );
  140. }
  141. }
  142. }
  143. }