QueryLanguage.Imperative.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.Disposables;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Reactive.Linq
  10. {
  11. using ObservableImpl;
  12. internal partial class QueryLanguage
  13. {
  14. #region ForEachAsync
  15. public virtual Task ForEachAsync<TSource>(IObservable<TSource> source, Action<TSource> onNext)
  16. {
  17. return ForEachAsync_(source, onNext, CancellationToken.None);
  18. }
  19. public virtual Task ForEachAsync<TSource>(IObservable<TSource> source, Action<TSource> onNext, CancellationToken cancellationToken)
  20. {
  21. return ForEachAsync_(source, onNext, cancellationToken);
  22. }
  23. public virtual Task ForEachAsync<TSource>(IObservable<TSource> source, Action<TSource, int> onNext)
  24. {
  25. var i = 0;
  26. return ForEachAsync_(source, x => onNext(x, checked(i++)), CancellationToken.None);
  27. }
  28. public virtual Task ForEachAsync<TSource>(IObservable<TSource> source, Action<TSource, int> onNext, CancellationToken cancellationToken)
  29. {
  30. var i = 0;
  31. return ForEachAsync_(source, x => onNext(x, checked(i++)), cancellationToken);
  32. }
  33. private static Task ForEachAsync_<TSource>(IObservable<TSource> source, Action<TSource> onNext, CancellationToken cancellationToken)
  34. {
  35. var tcs = new TaskCompletionSource<object>();
  36. var subscription = new SingleAssignmentDisposable();
  37. var ctr = default(CancellationTokenRegistration);
  38. if (cancellationToken.CanBeCanceled)
  39. {
  40. ctr = cancellationToken.Register(() =>
  41. {
  42. tcs.TrySetCanceled(cancellationToken);
  43. subscription.Dispose();
  44. });
  45. }
  46. if (!cancellationToken.IsCancellationRequested)
  47. {
  48. // Making sure we always complete, even if disposing throws.
  49. var dispose = new Action<Action>(action =>
  50. {
  51. try
  52. {
  53. ctr.Dispose(); // no null-check needed (struct)
  54. subscription.Dispose();
  55. }
  56. catch (Exception ex)
  57. {
  58. tcs.TrySetException(ex);
  59. return;
  60. }
  61. action();
  62. });
  63. var taskCompletionObserver = new AnonymousObserver<TSource>(
  64. x =>
  65. {
  66. if (!subscription.IsDisposed)
  67. {
  68. try
  69. {
  70. onNext(x);
  71. }
  72. catch (Exception exception)
  73. {
  74. dispose(() => tcs.TrySetException(exception));
  75. }
  76. }
  77. },
  78. exception =>
  79. {
  80. dispose(() => tcs.TrySetException(exception));
  81. },
  82. () =>
  83. {
  84. dispose(() => tcs.TrySetResult(null));
  85. }
  86. );
  87. //
  88. // Subtle race condition: if the source completes before we reach the line below, the SingleAssigmentDisposable
  89. // will already have been disposed. Upon assignment, the disposable resource being set will be disposed on the
  90. // spot, which may throw an exception. (See TFS 487142)
  91. //
  92. try
  93. {
  94. //
  95. // [OK] Use of unsafe Subscribe: we're catching the exception here to set the TaskCompletionSource.
  96. //
  97. // Notice we could use a safe subscription to route errors through OnError, but we still need the
  98. // exception handling logic here for the reason explained above. We cannot afford to throw here
  99. // and as a result never set the TaskCompletionSource, so we tunnel everything through here.
  100. //
  101. subscription.Disposable = source.Subscribe/*Unsafe*/(taskCompletionObserver);
  102. }
  103. catch (Exception ex)
  104. {
  105. tcs.TrySetException(ex);
  106. }
  107. }
  108. return tcs.Task;
  109. }
  110. #endregion
  111. #region + Case +
  112. public virtual IObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IObservable<TResult>> sources)
  113. {
  114. return Case(selector, sources, Empty<TResult>());
  115. }
  116. public virtual IObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IObservable<TResult>> sources, IScheduler scheduler)
  117. {
  118. return Case(selector, sources, Empty<TResult>(scheduler));
  119. }
  120. public virtual IObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IObservable<TResult>> sources, IObservable<TResult> defaultSource)
  121. {
  122. return new Case<TValue, TResult>(selector, sources, defaultSource);
  123. }
  124. #endregion
  125. #region + DoWhile +
  126. public virtual IObservable<TSource> DoWhile<TSource>(IObservable<TSource> source, Func<bool> condition)
  127. {
  128. return new DoWhile<TSource>(source, condition);
  129. }
  130. #endregion
  131. #region + For +
  132. public virtual IObservable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IObservable<TResult>> resultSelector)
  133. {
  134. return new For<TSource, TResult>(source, resultSelector);
  135. }
  136. #endregion
  137. #region + If +
  138. public virtual IObservable<TResult> If<TResult>(Func<bool> condition, IObservable<TResult> thenSource)
  139. {
  140. return If(condition, thenSource, Empty<TResult>());
  141. }
  142. public virtual IObservable<TResult> If<TResult>(Func<bool> condition, IObservable<TResult> thenSource, IScheduler scheduler)
  143. {
  144. return If(condition, thenSource, Empty<TResult>(scheduler));
  145. }
  146. public virtual IObservable<TResult> If<TResult>(Func<bool> condition, IObservable<TResult> thenSource, IObservable<TResult> elseSource)
  147. {
  148. return new If<TResult>(condition, thenSource, elseSource);
  149. }
  150. #endregion
  151. #region + While +
  152. public virtual IObservable<TSource> While<TSource>(Func<bool> condition, IObservable<TSource> source)
  153. {
  154. return new While<TSource>(condition, source);
  155. }
  156. #endregion
  157. }
  158. }