using Cysharp.Threading.Tasks.Internal; using System; using System.Threading; namespace Cysharp.Threading.Tasks.Linq { public static partial class UniTaskAsyncEnumerable { public static UniTask ForEachAsync(this IUniTaskAsyncEnumerable source, Action 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(this IUniTaskAsyncEnumerable source, Action action, CancellationToken cancellationToken = default) { Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(action, nameof(action)); return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAsync(source, action, cancellationToken); } /// Obsolete(Error), Use Use ForEachAwaitAsync instead. [Obsolete("Use ForEachAwaitAsync instead.", true)] [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public static UniTask ForEachAsync(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken = default) { throw new NotSupportedException("Use ForEachAwaitAsync instead."); } /// Obsolete(Error), Use Use ForEachAwaitAsync instead. [Obsolete("Use ForEachAwaitAsync instead.", true)] [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public static UniTask ForEachAsync(this IUniTaskAsyncEnumerable source, Func action, CancellationToken cancellationToken = default) { throw new NotSupportedException("Use ForEachAwaitAsync instead."); } public static UniTask ForEachAwaitAsync(this IUniTaskAsyncEnumerable source, Func 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(this IUniTaskAsyncEnumerable source, Func 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(this IUniTaskAsyncEnumerable source, Func 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(this IUniTaskAsyncEnumerable source, Func 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(IUniTaskAsyncEnumerable source, Action 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(IUniTaskAsyncEnumerable source, Action 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(IUniTaskAsyncEnumerable source, Func 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(IUniTaskAsyncEnumerable source, Func 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(IUniTaskAsyncEnumerable source, Func 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(IUniTaskAsyncEnumerable source, Func 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(); } } } } }