Skip to content

Instantly share code, notes, and snippets.

View in-async's full-sized avatar

inasync in-async

View GitHub Profile
@in-async
in-async / CollectionExtensions.cs
Last active September 8, 2020 12:08
オブジェクト初期化子により、コレクション型プロパティにコレクションを一括追加する為の拡張メソッド。
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)); }
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> {
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() {
@in-async
in-async / TaskEnumerableExtensions.cs
Last active September 14, 2020 14:38
TaskEnumerableExtensions.OrderByCompletedAsync() and TaskHelper.Run()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Inasync {
public static class TaskEnumerableExtensions {
/// <summary>
@in-async
in-async / TaskHelpers.cs
Created September 1, 2020 09:14
Task.WhenAny with predicate.
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)); }
@in-async
in-async / OneOf.cs
Last active June 18, 2020 02:40
JSON 対応 (Serialize: Newtonsoft.Json | System.Text.Json, Deserialize: Newtonsoft.Json)
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;
namespace Commons {
internal readonly struct BackingField<T> {
public readonly T Value;
public readonly bool HasValue;
public BackingField(T value) {
Value = value;
HasValue = true;
}
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;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace Commons {
/// <summary>
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)) {