1
0

ForEach.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 ForEachAsyncCore(source, action, cancellationToken);
  23. }
  24. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken = default)
  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, Func<TSource, Task> action, CancellationToken cancellationToken = default)
  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, (x, ct) => action(x), cancellationToken);
  39. }
  40. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> 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, int, Task> action, CancellationToken cancellationToken = default)
  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, i, ct) => action(x, i), cancellationToken);
  55. }
  56. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, 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, action, cancellationToken);
  63. }
  64. private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
  65. {
  66. var e = source.GetAsyncEnumerator(cancellationToken);
  67. try
  68. {
  69. while (await e.MoveNextAsync().ConfigureAwait(false))
  70. {
  71. action(e.Current);
  72. }
  73. }
  74. finally
  75. {
  76. await e.DisposeAsync().ConfigureAwait(false);
  77. }
  78. }
  79. private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  80. {
  81. var e = source.GetAsyncEnumerator(cancellationToken);
  82. try
  83. {
  84. while (await e.MoveNextAsync().ConfigureAwait(false))
  85. {
  86. await action(e.Current, cancellationToken).ConfigureAwait(false);
  87. }
  88. }
  89. finally
  90. {
  91. await e.DisposeAsync().ConfigureAwait(false);
  92. }
  93. }
  94. private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
  95. {
  96. var index = 0;
  97. var e = source.GetAsyncEnumerator(cancellationToken);
  98. try
  99. {
  100. while (await e.MoveNextAsync().ConfigureAwait(false))
  101. {
  102. action(e.Current, checked(index++));
  103. }
  104. }
  105. finally
  106. {
  107. await e.DisposeAsync().ConfigureAwait(false);
  108. }
  109. }
  110. private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  111. {
  112. var index = 0;
  113. var e = source.GetAsyncEnumerator(cancellationToken);
  114. try
  115. {
  116. while (await e.MoveNextAsync().ConfigureAwait(false))
  117. {
  118. await action(e.Current, checked(index++), cancellationToken).ConfigureAwait(false);
  119. }
  120. }
  121. finally
  122. {
  123. await e.DisposeAsync().ConfigureAwait(false);
  124. }
  125. }
  126. }
  127. }
  128. #endif