using Nullframes.Threading.Tasks;
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace Nullframes.Threading
{
public sealed partial class Dispatcher
{
#region --- Dispatch: Task ---
///
/// Dispatch the execution of a to the main thread.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched task.
///
public static void Invoke(Func function)
{
async void Action()
{
await function();
}
Invoke((Action) Action);
}
///
/// Dispatch the execution of a to the main thread.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched task.
/// The execution cycle during which the passed is executed.
///
public static void Invoke(Func function, ExecutionCycle cycle)
{
async void Action()
{
await function();
}
Invoke((Action) Action, cycle);
}
///
/// Dispatch the execution of a to the main thread.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched task.
/// optional cancellation token that can be passed to abort the task prematurely.
///
///
public static void Invoke(Func function, CancellationToken ct, bool throwOnCancellation = true)
{
async void Action()
{
try
{
ct.ThrowIfCancellationRequested();
await function(ct);
}
catch (OperationCanceledException oce)
{
if (throwOnCancellation)
{
Debug.LogException(oce);
}
}
catch (Exception exception)
{
Debug.LogException(exception);
}
}
Invoke((Action) Action);
}
///
/// Dispatch the execution of a to the main thread.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched task.
/// The execution cycle during which the passed is executed.
/// optional cancellation token that can be passed to abort the task prematurely.
///
///
public static void Invoke(Func function, ExecutionCycle cycle, CancellationToken ct,
bool throwOnCancellation = true)
{
async void Action()
{
try
{
ct.ThrowIfCancellationRequested();
await function(ct);
}
catch (OperationCanceledException oce)
{
if (throwOnCancellation)
{
Debug.LogException(oce);
}
}
catch (Exception exception)
{
Debug.LogException(exception);
}
}
Invoke((Action) Action, cycle);
}
#endregion
//--------------------------------------------------------------------------------------------------------------
#region --- Dispatch: Task Async ---
///
/// Dispatch the execution of a to the main thread; and return a ,
/// that can be awaited.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched task.
///
public static Task InvokeAsync(Func function)
{
var tcs = new TaskCompletionSource();
async void Action()
{
try
{
await function();
tcs.SetCompleted();
}
catch (Exception exception)
{
if (!tcs.TrySetException(exception))
{
Debug.LogException(exception);
}
}
}
Invoke((Action) Action);
return tcs.Task;
}
///
/// Dispatch the execution of a to the main thread; and return a ,
/// that can be awaited.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched task.
/// The execution cycle during which the passed is executed.
///
public static Task InvokeAsync(Func function, ExecutionCycle cycle)
{
var tcs = new TaskCompletionSource();
async void Action()
{
try
{
await function();
tcs.SetCompleted();
}
catch (Exception exception)
{
if (!tcs.TrySetException(exception))
{
Debug.LogException(exception);
}
}
}
Invoke((Action) Action, cycle);
return tcs.Task;
}
///
/// Dispatch the execution of a to the main thread; and return a ,
/// that can be awaited.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched task.
/// optional cancellation token that can be passed to abort the task prematurely.
///
///
public static Task InvokeAsync(Func function, CancellationToken ct, bool throwOnCancellation = true)
{
var tcs = new TaskCompletionSource();
async void Action()
{
try
{
ct.ThrowIfCancellationRequested();
await function(ct);
tcs.SetCompleted();
}
catch (OperationCanceledException oce)
{
if (throwOnCancellation)
{
tcs.SetException(oce);
}
else
{
tcs.SetCompleted();
}
}
catch (Exception exception)
{
if (!tcs.TrySetException(exception))
{
Debug.LogException(exception);
}
}
}
Invoke((Action) Action);
return tcs.Task;
}
///
/// Dispatch the execution of a to the main thread; and return a ,
/// that can be awaited.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched task.
/// The execution cycle during which the passed is executed.
/// optional cancellation token that can be passed to abort the task prematurely.
///
///
public static Task InvokeAsync(Func function, ExecutionCycle cycle, CancellationToken ct,
bool throwOnCancellation = true)
{
var tcs = new TaskCompletionSource();
async void Action()
{
try
{
ct.ThrowIfCancellationRequested();
await function(ct);
tcs.SetCompleted();
}
catch (OperationCanceledException oce)
{
if (throwOnCancellation)
{
tcs.SetException(oce);
}
else
{
tcs.SetCompleted();
}
}
catch (Exception exception)
{
if (!tcs.TrySetException(exception))
{
Debug.LogException(exception);
}
}
}
Invoke((Action) Action, cycle);
return tcs.Task;
}
#endregion
#region --- Dispatch: Task Async ---
///
/// Dispatch the execution of a to the main thread, which yields a
/// that will then be executed on the main thread. This call returns a that when awaited
/// will yield the result of the executed on the main thread.
/// The passed delegate is by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched function that yields a .
///
public static Task InvokeAsync(Func> function)
{
var tcs = new TaskCompletionSource();
async void Action()
{
try
{
tcs.SetResult(await function());
}
catch (Exception exception)
{
if (!tcs.TrySetException(exception))
{
Debug.LogException(exception);
}
}
}
Invoke((Action) Action);
return tcs.Task;
}
///
/// Dispatch the execution of a to the main thread, which yields a
/// that will then be executed on the main thread. This call returns a that when awaited
/// will yield the result of the executed on the main thread.
/// The passed delegate is by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched function that yields a .
/// The execution cycle during which the passed is executed.
///
public static Task InvokeAsync(Func> function, ExecutionCycle cycle)
{
var tcs = new TaskCompletionSource();
async void Action()
{
try
{
tcs.SetResult(await function());
}
catch (Exception exception)
{
if (!tcs.TrySetException(exception))
{
Debug.LogException(exception);
}
}
}
Invoke((Action) Action, cycle);
return tcs.Task;
}
///
/// Dispatch the execution of a to the main thread, which yields a
/// that will then be executed on the main thread. This call returns a that when awaited
/// will yield the result of the executed on the main thread.
/// The passed delegate is by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched function that yields a .
///
///
public static Task InvokeAsync(Func> function, CancellationToken ct)
{
var tcs = new TaskCompletionSource();
async void Action()
{
try
{
ct.ThrowIfCancellationRequested();
tcs.SetResult(await function(ct));
}
catch (Exception exception)
{
if (!tcs.TrySetException(exception))
{
Debug.LogException(exception);
}
}
}
Invoke((Action) Action);
return tcs.Task;
}
///
/// Dispatch the execution of a to the main thread, which yields a
/// that will then be executed on the main thread. This call returns a that when awaited
/// will yield the result of the executed on the main thread.
/// The passed delegate is by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// dispatched function that yields a .
/// The execution cycle during which the passed is executed.
///
///
public static Task InvokeAsync(Func> function, ExecutionCycle cycle,
CancellationToken ct)
{
var tcs = new TaskCompletionSource();
async void Action()
{
try
{
ct.ThrowIfCancellationRequested();
tcs.SetResult(await function(ct));
}
catch (Exception exception)
{
if (!tcs.TrySetException(exception))
{
Debug.LogException(exception);
}
}
}
Invoke((Action) Action, cycle);
return tcs.Task;
}
#endregion
}
}