SkipUntil.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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> SkipUntil<TSource, TUntil>(this IAsyncObservable<TSource> source, IAsyncObservable<TUntil> until)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (until == null)
  17. throw new ArgumentNullException(nameof(until));
  18. return Create<TSource>(async observer =>
  19. {
  20. var (sourceObserver, untilObserver) = AsyncObserver.SkipUntil<TSource, TUntil>(observer);
  21. var sourceTask = source.SubscribeAsync(sourceObserver);
  22. var untilTask = until.SubscribeAsync(untilObserver);
  23. var d1 = await sourceTask.ConfigureAwait(false);
  24. var d2 = await untilTask.ConfigureAwait(false);
  25. return StableCompositeAsyncDisposable.Create(d1, d2);
  26. });
  27. }
  28. public static IAsyncObservable<TSource> SkipUntil<TSource, TUntil>(this IAsyncObservable<TSource> source, DateTimeOffset endTime)
  29. {
  30. if (source == null)
  31. throw new ArgumentNullException(nameof(source));
  32. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.SkipUntil(observer, endTime)));
  33. }
  34. public static IAsyncObservable<TSource> SkipUntil<TSource, TUntil>(this IAsyncObservable<TSource> source, DateTimeOffset endTime, IAsyncScheduler scheduler)
  35. {
  36. if (source == null)
  37. throw new ArgumentNullException(nameof(source));
  38. if (scheduler == null)
  39. throw new ArgumentNullException(nameof(scheduler));
  40. return Create<TSource>(observer => source.SubscribeAsync(AsyncObserver.SkipUntil(observer, endTime, scheduler)));
  41. }
  42. }
  43. partial class AsyncObserver
  44. {
  45. public static (IAsyncObserver<TSource>, IAsyncObserver<TUntil>) SkipUntil<TSource, TUntil>(IAsyncObserver<TSource> observer)
  46. {
  47. if (observer == null)
  48. throw new ArgumentNullException(nameof(observer));
  49. var gate = new AsyncLock();
  50. var open = false;
  51. return
  52. (
  53. Create<TSource>(
  54. async x =>
  55. {
  56. using (await gate.LockAsync().ConfigureAwait(false))
  57. {
  58. if (open)
  59. {
  60. await observer.OnNextAsync(x).ConfigureAwait(false);
  61. }
  62. }
  63. },
  64. async ex =>
  65. {
  66. using (await gate.LockAsync().ConfigureAwait(false))
  67. {
  68. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  69. }
  70. },
  71. async () =>
  72. {
  73. using (await gate.LockAsync().ConfigureAwait(false))
  74. {
  75. await observer.OnCompletedAsync().ConfigureAwait(false);
  76. }
  77. }
  78. ),
  79. Create<TUntil>(
  80. async y =>
  81. {
  82. using (await gate.LockAsync().ConfigureAwait(false))
  83. {
  84. open = true;
  85. }
  86. },
  87. async ex =>
  88. {
  89. using (await gate.LockAsync().ConfigureAwait(false))
  90. {
  91. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  92. }
  93. },
  94. () => Task.CompletedTask
  95. )
  96. );
  97. }
  98. public static IAsyncObserver<TSource> SkipUntil<TSource>(IAsyncObserver<TSource> observer, DateTimeOffset endTime)
  99. {
  100. if (observer == null)
  101. throw new ArgumentNullException(nameof(observer));
  102. throw new NotImplementedException();
  103. }
  104. public static IAsyncObserver<TSource> SkipUntil<TSource>(IAsyncObserver<TSource> observer, DateTimeOffset endTime, IAsyncScheduler scheduler)
  105. {
  106. if (observer == null)
  107. throw new ArgumentNullException(nameof(observer));
  108. if (scheduler == null)
  109. throw new ArgumentNullException(nameof(scheduler));
  110. throw new NotImplementedException();
  111. }
  112. }
  113. }