ForEach.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #if !HAS_AWAIT_FOREACH
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. // REVIEW: Once we have C# 8.0 language support, we may want to do away with these methods. An open question is how to
  13. // provide support for cancellation, which could be offered through WithCancellation on the source. If we still
  14. // want to keep these methods, they may be a candidate for System.Interactive.Async if we consider them to be
  15. // non-standard (i.e. IEnumerable<T> doesn't have a ForEach extension method either).
  16. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken = default)
  17. {
  18. if (source == null)
  19. throw Error.ArgumentNull(nameof(source));
  20. if (action == null)
  21. throw Error.ArgumentNull(nameof(action));
  22. return Core(source, action, cancellationToken);
  23. static async Task Core(IAsyncEnumerable<TSource> _source, Action<TSource> _action, CancellationToken _cancellationToken)
  24. {
  25. #if CSHARP8
  26. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  27. {
  28. _action(item);
  29. }
  30. #else
  31. var e = _source.GetAsyncEnumerator(_cancellationToken);
  32. try
  33. {
  34. while (await e.MoveNextAsync().ConfigureAwait(false))
  35. {
  36. _action(e.Current);
  37. }
  38. }
  39. finally
  40. {
  41. await e.DisposeAsync().ConfigureAwait(false);
  42. }
  43. #endif
  44. }
  45. }
  46. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken = default)
  47. {
  48. if (source == null)
  49. throw Error.ArgumentNull(nameof(source));
  50. if (action == null)
  51. throw Error.ArgumentNull(nameof(action));
  52. return Core(source, action, cancellationToken);
  53. static async Task Core(IAsyncEnumerable<TSource> _source, Action<TSource, int> _action, CancellationToken _cancellationToken)
  54. {
  55. var index = 0;
  56. #if CSHARP8
  57. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  58. {
  59. _action(item, checked(index++));
  60. }
  61. #else
  62. var e = _source.GetAsyncEnumerator(_cancellationToken);
  63. try
  64. {
  65. while (await e.MoveNextAsync().ConfigureAwait(false))
  66. {
  67. _action(e.Current, checked(index++));
  68. }
  69. }
  70. finally
  71. {
  72. await e.DisposeAsync().ConfigureAwait(false);
  73. }
  74. #endif
  75. }
  76. }
  77. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> action, CancellationToken cancellationToken = default)
  78. {
  79. if (source == null)
  80. throw Error.ArgumentNull(nameof(source));
  81. if (action == null)
  82. throw Error.ArgumentNull(nameof(action));
  83. return Core(source, action, cancellationToken);
  84. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, Task> _action, CancellationToken _cancellationToken)
  85. {
  86. #if CSHARP8
  87. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  88. {
  89. await _action(item).ConfigureAwait(false);
  90. }
  91. #else
  92. var e = _source.GetAsyncEnumerator(_cancellationToken);
  93. try
  94. {
  95. while (await e.MoveNextAsync().ConfigureAwait(false))
  96. {
  97. await _action(e.Current).ConfigureAwait(false);
  98. }
  99. }
  100. finally
  101. {
  102. await e.DisposeAsync().ConfigureAwait(false);
  103. }
  104. #endif
  105. }
  106. }
  107. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  108. {
  109. return Core(source, action, cancellationToken);
  110. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, Task> _action, CancellationToken _cancellationToken)
  111. {
  112. #if CSHARP8
  113. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  114. {
  115. await _action(item, _cancellationToken).ConfigureAwait(false);
  116. }
  117. #else
  118. var e = _source.GetAsyncEnumerator(_cancellationToken);
  119. try
  120. {
  121. while (await e.MoveNextAsync().ConfigureAwait(false))
  122. {
  123. await _action(e.Current, _cancellationToken).ConfigureAwait(false);
  124. }
  125. }
  126. finally
  127. {
  128. await e.DisposeAsync().ConfigureAwait(false);
  129. }
  130. #endif
  131. }
  132. }
  133. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action, CancellationToken cancellationToken = default)
  134. {
  135. if (source == null)
  136. throw Error.ArgumentNull(nameof(source));
  137. if (action == null)
  138. throw Error.ArgumentNull(nameof(action));
  139. return Core(source, action, cancellationToken);
  140. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, int, Task> _action, CancellationToken _cancellationToken)
  141. {
  142. var index = 0;
  143. #if CSHARP8
  144. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  145. {
  146. await _action(item, checked(index++)).ConfigureAwait(false);
  147. }
  148. #else
  149. var e = _source.GetAsyncEnumerator(_cancellationToken);
  150. try
  151. {
  152. while (await e.MoveNextAsync().ConfigureAwait(false))
  153. {
  154. await _action(e.Current, checked(index++)).ConfigureAwait(false);
  155. }
  156. }
  157. finally
  158. {
  159. await e.DisposeAsync().ConfigureAwait(false);
  160. }
  161. #endif
  162. }
  163. }
  164. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  165. {
  166. if (source == null)
  167. throw Error.ArgumentNull(nameof(source));
  168. if (action == null)
  169. throw Error.ArgumentNull(nameof(action));
  170. return Core(source, action, cancellationToken);
  171. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, int, CancellationToken, Task> _action, CancellationToken _cancellationToken)
  172. {
  173. var index = 0;
  174. #if CSHARP8
  175. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  176. {
  177. await _action(item, checked(index++), _cancellationToken).ConfigureAwait(false);
  178. }
  179. #else
  180. var e = _source.GetAsyncEnumerator(_cancellationToken);
  181. try
  182. {
  183. while (await e.MoveNextAsync().ConfigureAwait(false))
  184. {
  185. await _action(e.Current, checked(index++), _cancellationToken).ConfigureAwait(false);
  186. }
  187. }
  188. finally
  189. {
  190. await e.DisposeAsync().ConfigureAwait(false);
  191. }
  192. #endif
  193. }
  194. }
  195. }
  196. }
  197. #endif