While.cs 3.9 KB

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