123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using Cysharp.Threading.Tasks.Internal;
- using System;
- using System.Threading;
- namespace Cysharp.Threading.Tasks.Linq
- {
- public static partial class UniTaskAsyncEnumerable
- {
- public static UniTask ForEachAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAsync(source, action, cancellationToken);
- }
- public static UniTask ForEachAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource, Int32> action, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAsync(source, action, cancellationToken);
- }
- /// <summary>Obsolete(Error), Use Use ForEachAwaitAsync instead.</summary>
- [Obsolete("Use ForEachAwaitAsync instead.", true)]
- [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
- public static UniTask ForEachAsync<T>(this IUniTaskAsyncEnumerable<T> source, Func<T, UniTask> action, CancellationToken cancellationToken = default)
- {
- throw new NotSupportedException("Use ForEachAwaitAsync instead.");
- }
- /// <summary>Obsolete(Error), Use Use ForEachAwaitAsync instead.</summary>
- [Obsolete("Use ForEachAwaitAsync instead.", true)]
- [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
- public static UniTask ForEachAsync<T>(this IUniTaskAsyncEnumerable<T> source, Func<T, int, UniTask> action, CancellationToken cancellationToken = default)
- {
- throw new NotSupportedException("Use ForEachAwaitAsync instead.");
- }
- public static UniTask ForEachAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> action, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitAsync(source, action, cancellationToken);
- }
- public static UniTask ForEachAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask> action, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitAsync(source, action, cancellationToken);
- }
- public static UniTask ForEachAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> action, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitWithCancellationAsync(source, action, cancellationToken);
- }
- public static UniTask ForEachAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask> action, CancellationToken cancellationToken = default)
- {
- Error.ThrowArgumentNullException(source, nameof(source));
- Error.ThrowArgumentNullException(action, nameof(action));
- return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitWithCancellationAsync(source, action, cancellationToken);
- }
- }
- internal static class ForEach
- {
- public static async UniTask ForEachAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync())
- {
- action(e.Current);
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTask ForEachAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Action<TSource, Int32> action, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- int index = 0;
- while (await e.MoveNextAsync())
- {
- action(e.Current, checked(index++));
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTask ForEachAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> action, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync())
- {
- await action(e.Current);
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTask ForEachAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask> action, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- int index = 0;
- while (await e.MoveNextAsync())
- {
- await action(e.Current, checked(index++));
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTask ForEachAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> action, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync())
- {
- await action(e.Current, cancellationToken);
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- public static async UniTask ForEachAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask> action, CancellationToken cancellationToken)
- {
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- int index = 0;
- while (await e.MoveNextAsync())
- {
- await action(e.Current, checked(index++), cancellationToken);
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- }
- }
|