Skip to content

Commit 2c1f38c

Browse files
Add ReadAllAsync polyfill for .NET Standard 2.0
Introduced ReadAllAsync extension for ChannelReader<T> as a polyfill for netstandard2.0, enabling async enumeration of channel data. Updated package version to 9.3.0 to reflect this addition.
1 parent 01a1d17 commit 2c1f38c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Open.ChannelExtensions/Extensions.Read.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,4 +1074,25 @@ public static async ValueTask<List<T>> ToListAsync<T>(this ChannelReader<T> read
10741074
await ReadAll(reader, list.Add).ConfigureAwait(false);
10751075
return list;
10761076
}
1077+
1078+
#if NETSTANDARD2_0
1079+
/// <summary>
1080+
/// Creates an <see cref="IAsyncEnumerable{T}"/> that enables reading all of the data from the channel.
1081+
/// </summary>
1082+
/// <remarks>Polyfill for netstandard2.0 where the built-in instance method is not available.</remarks>
1083+
public static async IAsyncEnumerable<T> ReadAllAsync<T>(
1084+
this ChannelReader<T> reader,
1085+
[EnumeratorCancellation] CancellationToken cancellationToken = default)
1086+
{
1087+
if (reader is null) throw new ArgumentNullException(nameof(reader));
1088+
1089+
while (await reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false))
1090+
{
1091+
while (reader.TryRead(out T? item))
1092+
{
1093+
yield return item;
1094+
}
1095+
}
1096+
}
1097+
#endif
10771098
}

Open.ChannelExtensions/Open.ChannelExtensions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<RepositoryType>git</RepositoryType>
2323
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2424
<GenerateDocumentationFile>true</GenerateDocumentationFile>
25-
<Version>9.2.0</Version>
25+
<Version>9.3.0</Version>
2626
<PackageReleaseNotes>Lowered .NET Standard 2.0/2.1 dependency versions to 6.0.0 to reduce minimum requirements for consumers.</PackageReleaseNotes>
2727
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2828
<PublishRepositoryUrl>true</PublishRepositoryUrl>

0 commit comments

Comments
 (0)