While.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.Disposables;
  5. using System.Threading.Tasks;
  6. namespace System.Reactive.Linq
  7. {
  8. partial class AsyncObservable
  9. {
  10. // REVIEW: Use a tail-recursive sink.
  11. public static IAsyncObservable<TSource> While<TSource>(Func<bool> condition, IAsyncObservable<TSource> source)
  12. {
  13. if (condition == null)
  14. throw new ArgumentNullException(nameof(condition));
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. return Create<TSource>(async observer =>
  18. {
  19. var subscription = new SerialAsyncDisposable();
  20. var o = default(IAsyncObserver<TSource>);
  21. o = AsyncObserver.CreateUnsafe<TSource>(
  22. observer.OnNextAsync,
  23. observer.OnErrorAsync,
  24. MoveNext
  25. );
  26. async Task MoveNext()
  27. {
  28. var b = default(bool);
  29. try
  30. {
  31. b = condition();
  32. }
  33. catch (Exception ex)
  34. {
  35. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  36. return;
  37. }
  38. if (b)
  39. {
  40. var sad = new SingleAssignmentAsyncDisposable();
  41. await subscription.AssignAsync(sad).ConfigureAwait(false);
  42. var d = await source.SubscribeSafeAsync(o).ConfigureAwait(false);
  43. await sad.AssignAsync(d).ConfigureAwait(false);
  44. }
  45. else
  46. {
  47. await observer.OnCompletedAsync().ConfigureAwait(false);
  48. }
  49. }
  50. await MoveNext().ConfigureAwait(false);
  51. return subscription;
  52. });
  53. }
  54. public static IAsyncObservable<TSource> While<TSource>(Func<Task<bool>> condition, IAsyncObservable<TSource> source)
  55. {
  56. if (condition == null)
  57. throw new ArgumentNullException(nameof(condition));
  58. if (source == null)
  59. throw new ArgumentNullException(nameof(source));
  60. return Create<TSource>(async observer =>
  61. {
  62. var subscription = new SerialAsyncDisposable();
  63. var o = default(IAsyncObserver<TSource>);
  64. o = AsyncObserver.CreateUnsafe<TSource>(
  65. observer.OnNextAsync,
  66. observer.OnErrorAsync,
  67. MoveNext
  68. );
  69. async Task MoveNext()
  70. {
  71. var b = default(bool);
  72. try
  73. {
  74. b = await condition().ConfigureAwait(false);
  75. }
  76. catch (Exception ex)
  77. {
  78. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  79. return;
  80. }
  81. if (b)
  82. {
  83. var sad = new SingleAssignmentAsyncDisposable();
  84. await subscription.AssignAsync(sad).ConfigureAwait(false);
  85. var d = await source.SubscribeSafeAsync(o).ConfigureAwait(false);
  86. await sad.AssignAsync(d).ConfigureAwait(false);
  87. }
  88. else
  89. {
  90. await observer.OnCompletedAsync().ConfigureAwait(false);
  91. }
  92. }
  93. await MoveNext().ConfigureAwait(false);
  94. return subscription;
  95. });
  96. }
  97. }
  98. }