1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-08-16 21:31:45 +02:00
Files
.config
.run
.workdir
Agent
Common
Controller
Docker
Utils
Phantom.Utils
Collections
EnumerableExtensions.cs
ReferenceEqualityComparer.cs
RingBuffer.cs
RwLockedDictionary.cs
TableData.cs
Cryptography
IO
Processes
Result
Runtime
Tasks
Threading
Phantom.Utils.csproj
Phantom.Utils.Actor
Phantom.Utils.Events
Phantom.Utils.Logging
Phantom.Utils.Rpc
Phantom.Utils.Tests
Web
.dockerignore
.gitattributes
.gitignore
AddMigration.bat
AddMigration.sh
Directory.Build.props
Directory.Build.targets
Dockerfile
LICENSE
Packages.props
PhantomPanel.sln
README.md
global.json

57 lines
2.0 KiB
C#

using System.Collections.Immutable;
namespace Phantom.Utils.Collections;
public static class EnumerableExtensions {
public static async Task<ImmutableArray<TSource>> ToImmutableArrayAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default) {
var builder = ImmutableArray.CreateBuilder<TSource>();
await foreach (var element in source.WithCancellation(cancellationToken)) {
builder.Add(element);
}
return builder.ToImmutable();
}
public static async Task<ImmutableArray<TSource>> ToImmutableArrayCatchingExceptionsAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<Exception> onException, CancellationToken cancellationToken = default) {
var builder = ImmutableArray.CreateBuilder<TSource>();
await using (var enumerator = source.GetAsyncEnumerator(cancellationToken)) {
while (true) {
try {
if (!await enumerator.MoveNextAsync()) {
break;
}
} catch (Exception e) {
onException(e);
continue;
}
builder.Add(enumerator.Current);
}
}
return builder.ToImmutable();
}
public static async Task<ImmutableHashSet<TSource>> ToImmutableSetAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default) {
var builder = ImmutableHashSet.CreateBuilder<TSource>();
await foreach (var element in source.WithCancellation(cancellationToken)) {
builder.Add(element);
}
return builder.ToImmutable();
}
public static async Task<ImmutableDictionary<TKey, TValue>> ToImmutableDictionaryAsync<TSource, TKey, TValue>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector, CancellationToken cancellationToken = default) where TKey : notnull {
var builder = ImmutableDictionary.CreateBuilder<TKey, TValue>();
await foreach (var element in source.WithCancellation(cancellationToken)) {
builder.Add(keySelector(element), valueSelector(element));
}
return builder.ToImmutable();
}
}