DoWhile.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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> DoWhile<TSource>(IAsyncObservable<TSource> source, Func<bool> condition)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (condition == null)
  16. throw new ArgumentNullException(nameof(condition));
  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 Subscribe()
  27. {
  28. var sad = new SingleAssignmentAsyncDisposable();
  29. await subscription.AssignAsync(sad).ConfigureAwait(false);
  30. var d = await source.SubscribeSafeAsync(o).ConfigureAwait(false);
  31. await sad.AssignAsync(d).ConfigureAwait(false);
  32. }
  33. async Task MoveNext()
  34. {
  35. var b = default(bool);
  36. try
  37. {
  38. b = condition();
  39. }
  40. catch (Exception ex)
  41. {
  42. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  43. }
  44. if (b)
  45. {
  46. await Subscribe().ConfigureAwait(false);
  47. }
  48. else
  49. {
  50. await observer.OnCompletedAsync().ConfigureAwait(false);
  51. }
  52. }
  53. await Subscribe().ConfigureAwait(false);
  54. return subscription;
  55. });
  56. }
  57. public static IAsyncObservable<TSource> DoWhile<TSource>(IAsyncObservable<TSource> source, Func<Task<bool>> condition)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException(nameof(source));
  61. if (condition == null)
  62. throw new ArgumentNullException(nameof(condition));
  63. return Create<TSource>(async observer =>
  64. {
  65. var subscription = new SerialAsyncDisposable();
  66. var o = default(IAsyncObserver<TSource>);
  67. o = AsyncObserver.CreateUnsafe<TSource>(
  68. observer.OnNextAsync,
  69. observer.OnErrorAsync,
  70. MoveNext
  71. );
  72. async Task Subscribe()
  73. {
  74. var sad = new SingleAssignmentAsyncDisposable();
  75. await subscription.AssignAsync(sad).ConfigureAwait(false);
  76. var d = await source.SubscribeSafeAsync(o).ConfigureAwait(false);
  77. await sad.AssignAsync(d).ConfigureAwait(false);
  78. }
  79. async Task MoveNext()
  80. {
  81. var b = default(bool);
  82. try
  83. {
  84. b = await condition().ConfigureAwait(false);
  85. }
  86. catch (Exception ex)
  87. {
  88. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  89. }
  90. if (b)
  91. {
  92. await Subscribe().ConfigureAwait(false);
  93. }
  94. else
  95. {
  96. await observer.OnCompletedAsync().ConfigureAwait(false);
  97. }
  98. }
  99. await Subscribe().ConfigureAwait(false);
  100. return subscription;
  101. });
  102. }
  103. }
  104. }