Skip to content

Commit

Permalink
refactor: improve CountAsync<T> and ToListAsync<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkallesen committed Aug 15, 2024
1 parent 6cf33c8 commit 06f05d0
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Atc/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the asynchronous operation to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the number of elements in the sequence.</returns>
/// <exception cref="ArgumentNullException">Thrown when the source sequence is null.</exception>
public static Task<int> CountAsync<T>(
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1312:Variable names should begin with lower-case letter", Justification = "False/Positive")]
public static async Task<int> CountAsync<T>(
this IEnumerable<T> source,
CancellationToken cancellationToken = default)
{
Expand All @@ -47,7 +48,15 @@ public static Task<int> CountAsync<T>(
throw new ArgumentNullException(nameof(source));
}

return Task.Run(source.Count, cancellationToken);
var count = 0;
foreach (var _ in source)
{
cancellationToken.ThrowIfCancellationRequested();
count++;
await Task.Yield();
}

return count;
}

/// <summary>
Expand All @@ -58,7 +67,7 @@ public static Task<int> CountAsync<T>(
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the asynchronous operation to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a list with the elements from the input sequence.</returns>
/// <exception cref="ArgumentNullException">Thrown when the source sequence is null.</exception>
public static Task<List<T>> ToListAsync<T>(
public static async Task<List<T>> ToListAsync<T>(
this IEnumerable<T> source,
CancellationToken cancellationToken = default)
{
Expand All @@ -67,7 +76,15 @@ public static Task<List<T>> ToListAsync<T>(
throw new ArgumentNullException(nameof(source));
}

return Task.Run(source.ToList, cancellationToken);
var list = new List<T>();
foreach (var item in source)
{
cancellationToken.ThrowIfCancellationRequested();
list.Add(item);
await Task.Yield();
}

return list;
}

/// <summary>
Expand Down

0 comments on commit 06f05d0

Please sign in to comment.