QueryLanguage.Imperative.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.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. #if HAS_TPL46
  43. tcs.TrySetCanceled(cancellationToken);
  44. #else
  45. tcs.TrySetCanceled();
  46. #endif
  47. subscription.Dispose();
  48. });
  49. }
  50. if (!cancellationToken.IsCancellationRequested)
  51. {
  52. // Making sure we always complete, even if disposing throws.
  53. var dispose = new Action<Action>(action =>
  54. {
  55. try
  56. {
  57. ctr.Dispose(); // no null-check needed (struct)
  58. subscription.Dispose();
  59. }
  60. catch (Exception ex)
  61. {
  62. tcs.TrySetException(ex);
  63. return;
  64. }
  65. action();
  66. });
  67. var taskCompletionObserver = new AnonymousObserver<TSource>(
  68. x =>
  69. {
  70. if (!subscription.IsDisposed)
  71. {
  72. try
  73. {
  74. onNext(x);
  75. }
  76. catch (Exception exception)
  77. {
  78. dispose(() => tcs.TrySetException(exception));
  79. }
  80. }
  81. },
  82. exception =>
  83. {
  84. dispose(() => tcs.TrySetException(exception));
  85. },
  86. () =>
  87. {
  88. dispose(() => tcs.TrySetResult(null));
  89. }
  90. );
  91. //
  92. // Subtle race condition: if the source completes before we reach the line below, the SingleAssigmentDisposable
  93. // will already have been disposed. Upon assignment, the disposable resource being set will be disposed on the
  94. // spot, which may throw an exception. (See TFS 487142)
  95. //
  96. try
  97. {
  98. //
  99. // [OK] Use of unsafe Subscribe: we're catching the exception here to set the TaskCompletionSource.
  100. //
  101. // Notice we could use a safe subscription to route errors through OnError, but we still need the
  102. // exception handling logic here for the reason explained above. We cannot afford to throw here
  103. // and as a result never set the TaskCompletionSource, so we tunnel everything through here.
  104. //
  105. subscription.Disposable = source.Subscribe/*Unsafe*/(taskCompletionObserver);
  106. }
  107. catch (Exception ex)
  108. {
  109. tcs.TrySetException(ex);
  110. }
  111. }
  112. return tcs.Task;
  113. }
  114. #endregion
  115. #region + Case +
  116. public virtual IObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IObservable<TResult>> sources)
  117. {
  118. return Case(selector, sources, Empty<TResult>());
  119. }
  120. public virtual IObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IObservable<TResult>> sources, IScheduler scheduler)
  121. {
  122. return Case(selector, sources, Empty<TResult>(scheduler));
  123. }
  124. public virtual IObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IObservable<TResult>> sources, IObservable<TResult> defaultSource)
  125. {
  126. return new Case<TValue, TResult>(selector, sources, defaultSource);
  127. }
  128. #endregion
  129. #region + DoWhile +
  130. public virtual IObservable<TSource> DoWhile<TSource>(IObservable<TSource> source, Func<bool> condition)
  131. {
  132. return new DoWhile<TSource>(source, condition);
  133. }
  134. #endregion
  135. #region + For +
  136. public virtual IObservable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IObservable<TResult>> resultSelector)
  137. {
  138. return new For<TSource, TResult>(source, resultSelector);
  139. }
  140. #endregion
  141. #region + If +
  142. public virtual IObservable<TResult> If<TResult>(Func<bool> condition, IObservable<TResult> thenSource)
  143. {
  144. return If(condition, thenSource, Empty<TResult>());
  145. }
  146. public virtual IObservable<TResult> If<TResult>(Func<bool> condition, IObservable<TResult> thenSource, IScheduler scheduler)
  147. {
  148. return If(condition, thenSource, Empty<TResult>(scheduler));
  149. }
  150. public virtual IObservable<TResult> If<TResult>(Func<bool> condition, IObservable<TResult> thenSource, IObservable<TResult> elseSource)
  151. {
  152. return new If<TResult>(condition, thenSource, elseSource);
  153. }
  154. #endregion
  155. #region + While +
  156. public virtual IObservable<TSource> While<TSource>(Func<bool> condition, IObservable<TSource> source)
  157. {
  158. return new While<TSource>(condition, source);
  159. }
  160. #endregion
  161. }
  162. }