Continuation
Represents various continuations.
using DotNext.Generic;
using DotNext.Runtime;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace DotNext.Threading.Tasks
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class Continuation
{
private static Task<T> ContinueWithConstant<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TConstant>(Task<T> task, bool completedSynchronously, Func<Task<T>, T> continuation, CancellationToken token = default(CancellationToken), [System.Runtime.CompilerServices.Nullable(2)] TaskScheduler scheduler = null) where TConstant : Constant<T>, new
{
if (!completedSynchronously)
return task.ContinueWith(continuation, token, TaskContinuationOptions.ExecuteSynchronously, scheduler ?? TaskScheduler.Current);
return CompletedTask<T, TConstant>.Task;
}
public static Task<Task> OnCompleted(this Task task)
{
return task.ContinueWith(Func.Identity<Task>(), Intrinsics.DefaultOf<CancellationToken>(), TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current);
}
public static Task<Task<TResult>> OnCompleted<[System.Runtime.CompilerServices.Nullable(2)] TResult>(this Task<TResult> task)
{
return task.ContinueWith(Func.Identity<Task<TResult>>(), Intrinsics.DefaultOf<CancellationToken>(), TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current);
}
public static Task<T> OnFaulted<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TConstant>(this Task<T> task, [System.Runtime.CompilerServices.Nullable(2)] TaskScheduler scheduler = null) where TConstant : Constant<T>, new
{
return ContinueWithConstant<T, TConstant>(task, task.IsFaulted, Continuation<T, TConstant>.WhenFaulted, Intrinsics.DefaultOf<CancellationToken>(), scheduler);
}
public static Task<T> OnFaultedOrCanceled<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TConstant>(this Task<T> task, [System.Runtime.CompilerServices.Nullable(2)] TaskScheduler scheduler = null) where TConstant : Constant<T>, new
{
return ContinueWithConstant<T, TConstant>(task, task.IsFaulted | task.IsCanceled, Continuation<T, TConstant>.WhenFaultedOrCanceled, Intrinsics.DefaultOf<CancellationToken>(), scheduler);
}
public static Task<T> OnCanceled<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TConstant>(this Task<T> task, [System.Runtime.CompilerServices.Nullable(2)] TaskScheduler scheduler = null) where TConstant : Constant<T>, new
{
return ContinueWithConstant<T, TConstant>(task, task.IsCanceled, Continuation<T, TConstant>.WhenCanceled, Intrinsics.DefaultOf<CancellationToken>(), scheduler);
}
internal static void OnCompleted(this Task task, AsyncCallback callback)
{
task.ConfigureAwait(false).GetAwaiter().OnCompleted(delegate {
callback(task);
});
}
}
}