123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- #if !HAS_AWAIT_FOREACH
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerable
- {
- // REVIEW: Once we have C# 8.0 language support, we may want to do away with these methods. An open question is how to
- // provide support for cancellation, which could be offered through WithCancellation on the source. If we still
- // want to keep these methods, they may be a candidate for System.Interactive.Async if we consider them to be
- // non-standard (i.e. IEnumerable<T> doesn't have a ForEach extension method either).
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, action, CancellationToken.None);
- }
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, action, cancellationToken);
- }
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, action, CancellationToken.None);
- }
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, action, cancellationToken);
- }
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> action)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, (x, ct) => action(x), CancellationToken.None);
- }
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> action, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, (x, ct) => action(x), cancellationToken);
- }
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, action, cancellationToken);
- }
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, (x, i, ct) => action(x, i), CancellationToken.None);
- }
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, (x, i, ct) => action(x, i), cancellationToken);
- }
- public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (action == null)
- throw Error.ArgumentNull(nameof(action));
- return ForEachAsyncCore(source, action, cancellationToken);
- }
- private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync().ConfigureAwait(false))
- {
- action(e.Current);
- }
- }
- finally
- {
- await e.DisposeAsync().ConfigureAwait(false);
- }
- }
- private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync().ConfigureAwait(false))
- {
- await action(e.Current, cancellationToken).ConfigureAwait(false);
- }
- }
- finally
- {
- await e.DisposeAsync().ConfigureAwait(false);
- }
- }
- private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
- {
- var index = 0;
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync().ConfigureAwait(false))
- {
- action(e.Current, checked(index++));
- }
- }
- finally
- {
- await e.DisposeAsync().ConfigureAwait(false);
- }
- }
- private static async Task ForEachAsyncCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
- {
- var index = 0;
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync().ConfigureAwait(false))
- {
- await action(e.Current, checked(index++), cancellationToken).ConfigureAwait(false);
- }
- }
- finally
- {
- await e.DisposeAsync().ConfigureAwait(false);
- }
- }
- }
- }
- #endif
|