ForEach.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. await foreach (var item in AsyncEnumerableExtensions.WithCancellation(_source, _cancellationToken).ConfigureAwait(false))
  26. {
  27. _action(item);
  28. }
  29. }
  30. }
  31. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken = default)
  32. {
  33. if (source == null)
  34. throw Error.ArgumentNull(nameof(source));
  35. if (action == null)
  36. throw Error.ArgumentNull(nameof(action));
  37. return Core(source, action, cancellationToken);
  38. static async Task Core(IAsyncEnumerable<TSource> _source, Action<TSource, int> _action, CancellationToken _cancellationToken)
  39. {
  40. var index = 0;
  41. await foreach (var item in AsyncEnumerableExtensions.WithCancellation(_source, _cancellationToken).ConfigureAwait(false))
  42. {
  43. _action(item, checked(index++));
  44. }
  45. }
  46. }
  47. internal static Task ForEachAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> action, CancellationToken cancellationToken = default)
  48. {
  49. if (source == null)
  50. throw Error.ArgumentNull(nameof(source));
  51. if (action == null)
  52. throw Error.ArgumentNull(nameof(action));
  53. return Core(source, action, cancellationToken);
  54. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, Task> _action, CancellationToken _cancellationToken)
  55. {
  56. await foreach (var item in AsyncEnumerableExtensions.WithCancellation(_source, _cancellationToken).ConfigureAwait(false))
  57. {
  58. await _action(item).ConfigureAwait(false);
  59. }
  60. }
  61. }
  62. internal static Task ForEachAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  63. {
  64. if (source == null)
  65. throw Error.ArgumentNull(nameof(source));
  66. if (action == null)
  67. throw Error.ArgumentNull(nameof(action));
  68. return Core(source, action, cancellationToken);
  69. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, Task> _action, CancellationToken _cancellationToken)
  70. {
  71. await foreach (var item in AsyncEnumerableExtensions.WithCancellation(_source, _cancellationToken).ConfigureAwait(false))
  72. {
  73. await _action(item, _cancellationToken).ConfigureAwait(false);
  74. }
  75. }
  76. }
  77. internal static Task ForEachAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, 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, int, Task> _action, CancellationToken _cancellationToken)
  85. {
  86. var index = 0;
  87. await foreach (var item in AsyncEnumerableExtensions.WithCancellation(_source, _cancellationToken).ConfigureAwait(false))
  88. {
  89. await _action(item, checked(index++)).ConfigureAwait(false);
  90. }
  91. }
  92. }
  93. internal static Task ForEachAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  94. {
  95. if (source == null)
  96. throw Error.ArgumentNull(nameof(source));
  97. if (action == null)
  98. throw Error.ArgumentNull(nameof(action));
  99. return Core(source, action, cancellationToken);
  100. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, int, CancellationToken, Task> _action, CancellationToken _cancellationToken)
  101. {
  102. var index = 0;
  103. await foreach (var item in AsyncEnumerableExtensions.WithCancellation(_source, _cancellationToken).ConfigureAwait(false))
  104. {
  105. await _action(item, checked(index++), _cancellationToken).ConfigureAwait(false);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. #endif