ForEach.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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)
  17. {
  18. if (source == null)
  19. throw Error.ArgumentNull(nameof(source));
  20. if (action == null)
  21. throw Error.ArgumentNull(nameof(action));
  22. return ForEachAsyncCore(source, action, CancellationToken.None);
  23. }
  24. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
  25. {
  26. if (source == null)
  27. throw Error.ArgumentNull(nameof(source));
  28. if (action == null)
  29. throw Error.ArgumentNull(nameof(action));
  30. return ForEachAsyncCore(source, action, cancellationToken);
  31. }
  32. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action)
  33. {
  34. if (source == null)
  35. throw Error.ArgumentNull(nameof(source));
  36. if (action == null)
  37. throw Error.ArgumentNull(nameof(action));
  38. return ForEachAsyncCore(source, action, CancellationToken.None);
  39. }
  40. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
  41. {
  42. if (source == null)
  43. throw Error.ArgumentNull(nameof(source));
  44. if (action == null)
  45. throw Error.ArgumentNull(nameof(action));
  46. return ForEachAsyncCore(source, action, cancellationToken);
  47. }
  48. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> action)
  49. {
  50. if (source == null)
  51. throw Error.ArgumentNull(nameof(source));
  52. if (action == null)
  53. throw Error.ArgumentNull(nameof(action));
  54. return ForEachAsyncCore(source, (x, ct) => action(x), CancellationToken.None);
  55. }
  56. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> action, CancellationToken cancellationToken)
  57. {
  58. if (source == null)
  59. throw Error.ArgumentNull(nameof(source));
  60. if (action == null)
  61. throw Error.ArgumentNull(nameof(action));
  62. return ForEachAsyncCore(source, (x, ct) => action(x), cancellationToken);
  63. }
  64. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  65. {
  66. if (source == null)
  67. throw Error.ArgumentNull(nameof(source));
  68. if (action == null)
  69. throw Error.ArgumentNull(nameof(action));
  70. return ForEachAsyncCore(source, action, cancellationToken);
  71. }
  72. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action)
  73. {
  74. if (source == null)
  75. throw Error.ArgumentNull(nameof(source));
  76. if (action == null)
  77. throw Error.ArgumentNull(nameof(action));
  78. return ForEachAsyncCore(source, (x, i, ct) => action(x, i), CancellationToken.None);
  79. }
  80. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action, CancellationToken cancellationToken)
  81. {
  82. if (source == null)
  83. throw Error.ArgumentNull(nameof(source));
  84. if (action == null)
  85. throw Error.ArgumentNull(nameof(action));
  86. return ForEachAsyncCore(source, (x, i, ct) => action(x, i), cancellationToken);
  87. }
  88. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  89. {
  90. if (source == null)
  91. throw Error.ArgumentNull(nameof(source));
  92. if (action == null)
  93. throw Error.ArgumentNull(nameof(action));
  94. return ForEachAsyncCore(source, action, cancellationToken);
  95. }
  96. private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
  97. {
  98. var e = source.GetAsyncEnumerator(cancellationToken);
  99. try
  100. {
  101. while (await e.MoveNextAsync().ConfigureAwait(false))
  102. {
  103. action(e.Current);
  104. }
  105. }
  106. finally
  107. {
  108. await e.DisposeAsync().ConfigureAwait(false);
  109. }
  110. }
  111. private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  112. {
  113. var e = source.GetAsyncEnumerator(cancellationToken);
  114. try
  115. {
  116. while (await e.MoveNextAsync().ConfigureAwait(false))
  117. {
  118. await action(e.Current, cancellationToken).ConfigureAwait(false);
  119. }
  120. }
  121. finally
  122. {
  123. await e.DisposeAsync().ConfigureAwait(false);
  124. }
  125. }
  126. private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
  127. {
  128. var index = 0;
  129. var e = source.GetAsyncEnumerator(cancellationToken);
  130. try
  131. {
  132. while (await e.MoveNextAsync().ConfigureAwait(false))
  133. {
  134. action(e.Current, checked(index++));
  135. }
  136. }
  137. finally
  138. {
  139. await e.DisposeAsync().ConfigureAwait(false);
  140. }
  141. }
  142. private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  143. {
  144. var index = 0;
  145. var e = source.GetAsyncEnumerator(cancellationToken);
  146. try
  147. {
  148. while (await e.MoveNextAsync().ConfigureAwait(false))
  149. {
  150. await action(e.Current, checked(index++), cancellationToken).ConfigureAwait(false);
  151. }
  152. }
  153. finally
  154. {
  155. await e.DisposeAsync().ConfigureAwait(false);
  156. }
  157. }
  158. }
  159. }
  160. #endif