ForEach.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 (TSource item in _source.WithCancellation(_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 (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  42. {
  43. _action(item, checked(index++));
  44. }
  45. }
  46. }
  47. public static Task ForEachAsync<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 (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  57. {
  58. await _action(item).ConfigureAwait(false);
  59. }
  60. }
  61. }
  62. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  63. {
  64. return Core(source, action, cancellationToken);
  65. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, Task> _action, CancellationToken _cancellationToken)
  66. {
  67. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  68. {
  69. await _action(item, _cancellationToken).ConfigureAwait(false);
  70. }
  71. }
  72. }
  73. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action, CancellationToken cancellationToken = default)
  74. {
  75. if (source == null)
  76. throw Error.ArgumentNull(nameof(source));
  77. if (action == null)
  78. throw Error.ArgumentNull(nameof(action));
  79. return Core(source, action, cancellationToken);
  80. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, int, Task> _action, CancellationToken _cancellationToken)
  81. {
  82. var index = 0;
  83. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  84. {
  85. await _action(item, checked(index++)).ConfigureAwait(false);
  86. }
  87. }
  88. }
  89. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  90. {
  91. if (source == null)
  92. throw Error.ArgumentNull(nameof(source));
  93. if (action == null)
  94. throw Error.ArgumentNull(nameof(action));
  95. return Core(source, action, cancellationToken);
  96. static async Task Core(IAsyncEnumerable<TSource> _source, Func<TSource, int, CancellationToken, Task> _action, CancellationToken _cancellationToken)
  97. {
  98. var index = 0;
  99. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  100. {
  101. await _action(item, checked(index++), _cancellationToken).ConfigureAwait(false);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. #endif