ForEach.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #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. /// <summary>
  17. /// Invokes an action for each element in the async-enumerable sequence, and returns a Task object that will get signaled when the sequence terminates.
  18. /// </summary>
  19. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  20. /// <param name="source">Source sequence.</param>
  21. /// <param name="action">Action to invoke for each element in the async-enumerable sequence.</param>
  22. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  23. /// <returns>Task that signals the termination of the sequence.</returns>
  24. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="action"/> is null.</exception>
  25. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  26. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken = default)
  27. {
  28. if (source == null)
  29. throw Error.ArgumentNull(nameof(source));
  30. if (action == null)
  31. throw Error.ArgumentNull(nameof(action));
  32. return Core(source, action, cancellationToken);
  33. static async Task Core(IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
  34. {
  35. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  36. {
  37. action(item);
  38. }
  39. }
  40. }
  41. /// <summary>
  42. /// Invokes an action for each element in the async-enumerable sequence, incorporating the element's index, and returns a Task object that will get signaled when the sequence terminates.
  43. /// </summary>
  44. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  45. /// <param name="source">Source sequence.</param>
  46. /// <param name="action">Action to invoke for each element in the async-enumerable sequence.</param>
  47. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  48. /// <returns>Task that signals the termination of the sequence.</returns>
  49. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="action"/> is null.</exception>
  50. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  51. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken = default)
  52. {
  53. if (source == null)
  54. throw Error.ArgumentNull(nameof(source));
  55. if (action == null)
  56. throw Error.ArgumentNull(nameof(action));
  57. return Core(source, action, cancellationToken);
  58. static async Task Core(IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
  59. {
  60. var index = 0;
  61. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  62. {
  63. action(item, checked(index++));
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// Invokes and awaits an asynchronous action on each element in the source sequence, and returns a task that is signaled when the sequence terminates.
  69. /// </summary>
  70. /// <typeparam name="TSource">Type of elements in the sequence.</typeparam>
  71. /// <param name="source">Source sequence.</param>
  72. /// <param name="action">Asynchronous action to invoke and await for each element in the source sequence.</param>
  73. /// <param name="cancellationToken">Optional cancellation token for cancelling the sequence at any time.</param>
  74. /// <returns>Task that signals the termination of the sequence.</returns>
  75. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="action"/> is <see langword="null"/>.</exception>
  76. [GenerateAsyncOverload]
  77. private static Task ForEachAwaitAsyncCore<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. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  87. {
  88. await action(item).ConfigureAwait(false);
  89. }
  90. }
  91. }
  92. #if !NO_DEEP_CANCELLATION
  93. [GenerateAsyncOverload]
  94. internal static Task ForEachAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  95. {
  96. if (source == null)
  97. throw Error.ArgumentNull(nameof(source));
  98. if (action == null)
  99. throw Error.ArgumentNull(nameof(action));
  100. return Core(source, action, cancellationToken);
  101. static async Task Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  102. {
  103. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  104. {
  105. await action(item, cancellationToken).ConfigureAwait(false);
  106. }
  107. }
  108. }
  109. #endif
  110. /// <summary>
  111. /// Invokes and awaits an asynchronous action on each element in the source sequence, incorporating the element's index, and returns a task that is signaled when the sequence terminates.
  112. /// </summary>
  113. /// <typeparam name="TSource">Type of elements in the sequence.</typeparam>
  114. /// <param name="source">Source sequence.</param>
  115. /// <param name="action">Asynchronous action to invoke and await for each element in the source sequence; the second parameter represents the index of the element.</param>
  116. /// <param name="cancellationToken">Optional cancellation token for cancelling the sequence at any time.</param>
  117. /// <returns>Task that signals the termination of the sequence.</returns>
  118. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="action"/> is <see langword="null"/>.</exception>
  119. [GenerateAsyncOverload]
  120. private static Task ForEachAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action, CancellationToken cancellationToken = default)
  121. {
  122. if (source == null)
  123. throw Error.ArgumentNull(nameof(source));
  124. if (action == null)
  125. throw Error.ArgumentNull(nameof(action));
  126. return Core(source, action, cancellationToken);
  127. static async Task Core(IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action, CancellationToken cancellationToken)
  128. {
  129. var index = 0;
  130. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  131. {
  132. await action(item, checked(index++)).ConfigureAwait(false);
  133. }
  134. }
  135. }
  136. #if !NO_DEEP_CANCELLATION
  137. [GenerateAsyncOverload]
  138. internal static Task ForEachAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  139. {
  140. if (source == null)
  141. throw Error.ArgumentNull(nameof(source));
  142. if (action == null)
  143. throw Error.ArgumentNull(nameof(action));
  144. return Core(source, action, cancellationToken);
  145. static async Task Core(IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  146. {
  147. var index = 0;
  148. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  149. {
  150. await action(item, checked(index++), cancellationToken).ConfigureAwait(false);
  151. }
  152. }
  153. }
  154. #endif
  155. }
  156. }
  157. #endif