This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Commons { | |
public static class CollectionExtensions { | |
public static void Add<T>(this ICollection<T> source, IEnumerable<T> collection) { | |
if (source is null) { throw new ArgumentNullException(nameof(source)); } | |
if (collection is null) { throw new ArgumentNullException(nameof(collection)); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Diagnostics.CodeAnalysis; | |
using System.Threading; | |
using System.Threading.Channels; | |
using System.Threading.Tasks; | |
namespace Inasync { | |
public abstract class PullChannelReader<T> : ChannelReader<T> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Commons { | |
/// <remarks>https://devblogs.microsoft.com/pfxteam/building-async-coordination-primitives-part-1-asyncmanualresetevent/</remarks> | |
public class AsyncManualResetEvent { | |
private volatile TaskCompletionSource<Void> _tcs = new TaskCompletionSource<Void>(TaskCreationOptions.RunContinuationsAsynchronously); | |
public Task WaitAsync() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Inasync { | |
public static class TaskEnumerableExtensions { | |
/// <summary> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
namespace Inasync { | |
public static class TaskHelpers { | |
public static Task<Task<T>> WhenAny<T>(IEnumerable<Task<T>> tasks, Func<T, bool> predicate) { | |
if (tasks is null) { throw new ArgumentNullException(nameof(tasks)); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics.CodeAnalysis; | |
namespace Commons { | |
public readonly struct OneOf<T1, T2> { | |
public OneOf(byte @case, [AllowNull]T1 value1, [AllowNull]T2 value2) { | |
Case = @case; | |
Value1 = value1; | |
Value2 = value2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Commons { | |
internal readonly struct BackingField<T> { | |
public readonly T Value; | |
public readonly bool HasValue; | |
public BackingField(T value) { | |
Value = value; | |
HasValue = true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class PromiseCompletionSource<T> { | |
readonly promise: Promise<T>; | |
readonly resolve: (value: T) => void; | |
readonly reject: (reason?: Error) => void; | |
constructor() { | |
let _resolve: (value: T) => void; | |
let _reject: (reason?: Error) => void; | |
this.promise = new Promise<T>((resolve, reject) => { | |
_resolve = resolve; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Threading; | |
using System.Threading.Channels; | |
using System.Threading.Tasks; | |
namespace Commons { | |
/// <summary> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Runtime.CompilerServices; | |
using System.Threading; | |
namespace Commons { | |
public static class ChannelExtensions { | |
public static async IAsyncEnumerable<T> ReadAllAsync<T>(this IChannelReader<T> reader, [EnumeratorCancellation] CancellationToken cancellationToken = default) { | |
while (await reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) { |