using System;
using System.Collections;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace Nullframes.Threading
{
///
/// Class containing multiple extension methods for dispatching the execution of a
/// , or
/// from a background thread to the main thread.
///
public static class DispatchExtensions
{
#region --- Action ---
///
/// Dispatch the execution of an to the main thread.
/// Actions are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or Tick cycle.
/// Use
/// for more control over the cycle in which the dispatched is executed.
///
/// dispatched action.
///
public static void Dispatch(this Action action)
{
Dispatcher.Invoke(action);
}
///
/// Dispatch the execution of an to the main thread.
/// Actions are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or Tick cycle.
/// Use
/// for more control over the cycle in which the dispatched is executed.
///
/// dispatched action.
/// first argument
///
public static void Dispatch(this Action action, T arg)
{
Dispatcher.Invoke(() => action(arg));
}
///
/// Dispatch the execution of an to the main thread.
/// Actions are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or Tick cycle.
/// Use
/// for more control over the cycle in which the dispatched is executed.
///
/// dispatched action.
/// first argument
/// second argument
///
public static void Dispatch(this Action action, T arg1, TS arg2)
{
Dispatcher.Invoke(() => action(arg1, arg2));
}
///
/// Dispatch the execution of an to the main thread.
/// Actions are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or Tick cycle.
/// Use
/// for more control over the cycle in which the dispatched is executed.
///
/// dispatched action.
/// first argument
/// second argument
/// third argument
///
public static void Dispatch(this Action action, T arg1, TS arg2, TU arg3)
{
Dispatcher.Invoke(() => action(arg1, arg2, arg3));
}
///
/// Dispatch the execution of an to the main thread.
/// Actions are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or Tick cycle.
/// Use
/// for more control over the cycle in which the dispatched is executed.
///
/// dispatched action.
/// first argument
/// second argument
/// third argument
/// fourth argument
///
public static void Dispatch(this Action action, T arg1, TS arg2, TU arg3, TV arg4)
{
Dispatcher.Invoke(() => action(arg1, arg2, arg3, arg4));
}
///
/// Dispatch an that will be executed on the main thread and determine the exact cycle,
/// during which the passed action will be executed.
///
/// dispatched action.
/// The execution cycle during which the passed is executed.
///
public static void Dispatch(this Action action, ExecutionCycle cycle)
{
Dispatcher.Invoke(action, cycle);
}
///
/// Dispatch an that will be executed on the main thread and determine the exact cycle,
/// during which the passed action will be executed.
///
/// dispatched action.
/// The execution cycle during which the passed is executed.
/// first argument
///
public static void Dispatch(this Action action, T arg, ExecutionCycle cycle)
{
Dispatcher.Invoke(() => action(arg), cycle);
}
///
/// Dispatch an that will be executed on the main thread and determine the exact cycle,
/// during which the passed action will be executed.
///
/// dispatched action.
/// The execution cycle during which the passed is executed.
/// first argument
/// second argument
///
public static void Dispatch(this Action action, T arg1, TS arg2, ExecutionCycle cycle)
{
Dispatcher.Invoke(() => action(arg1, arg2), cycle);
}
///
/// Dispatch an that will be executed on the main thread and determine the exact cycle,
/// during which the passed action will be executed.
///
/// dispatched action.
/// The execution cycle during which the passed is executed.
/// first argument
/// second argument
/// third argument
///
public static void Dispatch(this Action action, T arg1, TS arg2, TU arg3, ExecutionCycle cycle)
{
Dispatcher.Invoke(() => action(arg1, arg2, arg3), cycle);
}
///
/// Dispatch an that will be executed on the main thread and determine the exact cycle,
/// during which the passed action will be executed.
///
/// dispatched action.
/// The execution cycle during which the passed is executed.
/// first argument
/// second argument
/// third argument
/// fourth argument
///
public static void Dispatch(this Action action, T arg1, TS arg2, TU arg3, TV arg4, ExecutionCycle cycle)
{
Dispatcher.Invoke(() => action(arg1, arg2, arg3, arg4), cycle);
}
#endregion
#region --- Action Async ---
///
/// Dispatch an that will be executed on the main thread and return a ,
/// that when awaited on the calling thread, returns after the
/// was executed on the main thread.
///
/// that will be invoked.
/// When enabled a null check is performed before dispatching the action and return
/// a completed task if the action is null.
/// Task that will complete on the calling thread after the passed action has been executed.
///
public static Task DispatchAsync(this Action action, bool nullCheck = true)
{
return nullCheck && action == null ? Task.CompletedTask : Dispatcher.InvokeAsync(action);
}
///
/// Dispatch an that will be executed on the main thread and return a ,
/// that when awaited on the calling thread, returns after the
/// was executed on the main thread.
///
/// that will be invoked.
/// The execution cycle during which the will be invoked.
/// Task that will complete on the calling thread after the passed action has been executed.
/// When enabled a null check is performed before dispatching the action and return
/// a completed task if the action is null.
///
public static Task DispatchAsync(this Action action, ExecutionCycle cycle, bool nullCheck = true)
{
return nullCheck && action == null ? Task.CompletedTask : Dispatcher.InvokeAsync(action, cycle);
}
///
/// Dispatch an that will be executed on the main thread and return a ,
/// that when awaited on the calling thread, returns after the
/// was executed on the main thread.
///
/// that will be invoked.
/// optional cancellation token that can be passed to abort the task prematurely.
///
/// When enabled a null check is performed before dispatching the action and return
/// a completed task if the action is null.
/// exception is thrown if the task is cancelled prematurely.
/// Task that will complete on the calling thread after the passed action has been executed.
///
public static Task DispatchAsync(this Action action, CancellationToken ct, bool throwOnCancellation = true, bool nullCheck = true)
{
return nullCheck && action == null ? Task.CompletedTask : Dispatcher.InvokeAsync(action, ct, throwOnCancellation);
}
///
/// Dispatch an that will be executed on the main thread and return a ,
/// that when awaited on the calling thread, returns after the
/// was executed on the main thread.
///
/// that will be invoked.
/// the execution cycle during which the passed is executed.
/// optional cancellation token that can be passed to abort the task prematurely.
/// optional parameter that determines if an
/// is thrown if the Task is cancelled prematurely.
/// When enabled a null check is performed before dispatching the action and return
/// a completed task if the action is null.
/// exception is thrown if the task is cancelled prematurely.
/// Task that will complete on the calling thread after the passed action has been executed.
///
public static Task DispatchAsync(this Action action, ExecutionCycle cycle, CancellationToken ct, bool throwOnCancellation = true, bool nullCheck = true)
{
return nullCheck && action == null ? Task.CompletedTask : Dispatcher.InvokeAsync(action, cycle, ct, throwOnCancellation);
}
#endregion
//--------------------------------------------------------------------------------------------------------------
#region --- Func Async ---
///
/// Dispatch a that wil executed on the main thread; and return a ,
/// that when awaited on the calling thread, returns the result of the passed
/// after it was executed on the main thread.
///
/// delegate that will be executed on the main thread.
/// exception is thrown if the task is cancelled prematurely.
/// Task that will complete on the calling thread after the delegate was executed.
///
public static Task DispatchAsync(this Func func)
{
return Dispatcher.InvokeAsync(func);
}
///
/// Dispatch a that wil executed on the main thread; and return a ,
/// that when awaited on the calling thread, returns the result of the passed
/// after it was executed on the main thread.
///
/// delegate that will be executed on the main thread.
/// The execution cycle during which the passed is executed.
/// Task that will complete on the calling thread after the delegate was executed.
///
public static Task DispatchAsync(this Func func, ExecutionCycle cycle)
{
return Dispatcher.InvokeAsync(func, cycle);
}
///
/// Dispatch a that wil executed on the main thread; and return a ,
/// that when awaited on the calling thread, returns the result of the passed
/// after it was executed on the main thread.
///
/// delegate that will be executed on the main thread.
/// optional cancellation token that can be passed to abort the task prematurely.
/// exception is thrown if the task is cancelled prematurely.
/// Task that will complete on the calling thread after the delegate was executed.
///
public static Task DispatchAsync(this Func func, CancellationToken ct)
{
return Dispatcher.InvokeAsync(func, ct);
}
///
/// Dispatch a that wil executed on the main thread; and return a ,
/// that when awaited on the calling thread, returns the result of the passed
/// after it was executed on the main thread.
///
/// delegate that will be executed on the main thread.
/// The execution cycle during which the passed is executed.
/// optional cancellation token that can be passed to abort the task prematurely.
/// exception is thrown if the task is cancelled prematurely.
/// Task that will complete on the calling thread after the delegate was executed.
///
public static Task DispatchAsync(this Func func, ExecutionCycle cycle, CancellationToken ct)
{
return Dispatcher.InvokeAsync(func, cycle, ct);
}
#endregion
//--------------------------------------------------------------------------------------------------------------
#region --- Coroutine ---
///
/// Dispatch an that will be started and executed as a on the main thread.
///
/// that is started as a .
///
public static void Dispatch(this IEnumerator enumerator)
{
Dispatcher.Invoke(enumerator);
}
///
/// Dispatch an that will be started and executed as a on the main thread.
///
/// that is started as a .
/// the target on which the coroutine will run.
///
public static void Dispatch(this IEnumerator enumerator, MonoBehaviour target)
{
Dispatcher.Invoke(enumerator, target);
}
///
/// Dispatch an that will be started and executed as a on the main thread.
///
/// that is started as a .
/// the execution cycle during which the passed is started.
///
public static void Dispatch(this IEnumerator enumerator, ExecutionCycle cycle)
{
Dispatcher.Invoke(enumerator, cycle);
}
///
/// Dispatch an that will be started and executed as a on the main thread.
///
/// that is started as a .
/// the execution cycle during which the passed is started.
/// the target on which the coroutine will run.
///
public static void Dispatch(this IEnumerator enumerator, ExecutionCycle cycle, MonoBehaviour target)
{
Dispatcher.Invoke(enumerator, cycle, target);
}
#endregion
#region --- Coroutine Async Await Start ---
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after it was started on the main thread.
///
/// that is started as a .
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitStart(this IEnumerator enumerator)
{
return Dispatcher.InvokeAsyncAwaitStart(enumerator);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after it was started on the main thread.
///
/// that is started as a .
/// the target on which the coroutine will run.
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitStart(this IEnumerator enumerator, MonoBehaviour target)
{
return Dispatcher.InvokeAsyncAwaitStart(enumerator, target);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after it was started on the main thread.
///
/// that is started as a .
/// optional cancellation token that can be passed to abort the task prematurely.
/// exception is thrown if an is dispatched during edit mode.
/// exception is thrown if the task is cancelled prematurely.
///
public static Task DispatchAsyncAwaitStart(this IEnumerator enumerator, CancellationToken ct)
{
return Dispatcher.InvokeAsyncAwaitStart(enumerator, ct);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after it was started on the main thread.
///
/// that is started as a .
/// the target on which the coroutine will run.
/// optional cancellation token that can be passed to abort the task prematurely.
/// exception is thrown if an is dispatched during edit mode.
/// exception is thrown if the task is cancelled prematurely.
///
public static Task DispatchAsyncAwaitStart(this IEnumerator enumerator, MonoBehaviour target, CancellationToken ct)
{
return Dispatcher.InvokeAsyncAwaitStart(enumerator, target, ct);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after it was started on the main thread.
///
/// that is started as a .
/// the execution cycle during which the passed is started.
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitStart(this IEnumerator enumerator, ExecutionCycle cycle)
{
return Dispatcher.InvokeAsyncAwaitStart(enumerator, cycle);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after it was started on the main thread.
///
/// that is started as a .
/// the execution cycle during which the passed is started.
/// the target on which the coroutine will run.
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitStart(this IEnumerator enumerator, ExecutionCycle cycle, MonoBehaviour target)
{
return Dispatcher.InvokeAsyncAwaitStart(enumerator, cycle, target);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after it was started on the main thread.
///
/// that is started as a .
/// the execution cycle during which the passed is started.
/// optional cancellation token that can be passed to abort the task prematurely.
/// exception is thrown if an is dispatched during edit mode.
/// exception is thrown if the task is cancelled prematurely.
///
public static Task DispatchAsyncAwaitStart(this IEnumerator enumerator, ExecutionCycle cycle, CancellationToken ct)
{
return Dispatcher.InvokeAsyncAwaitStart(enumerator, cycle, ct);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after it was started on the main thread.
///
/// that is started as a .
/// the execution cycle during which the passed is started.
/// the target on which the coroutine will run.
/// optional cancellation token that can be passed to abort the task prematurely.
/// exception is thrown if an is dispatched during edit mode.
/// exception is thrown if the task is cancelled prematurely.
///
public static Task DispatchAsyncAwaitStart(this IEnumerator enumerator, ExecutionCycle cycle, MonoBehaviour target, CancellationToken ct)
{
return Dispatcher.InvokeAsyncAwaitStart(enumerator, cycle, target, ct);
}
#endregion
#region --- Coroutine Async Await Completion ---
///
/// Dispatch an that is executed as a
/// on the main thread and return a , that can be awaited and returns
/// after the has completed on the main thread.
///
/// that is started as a .
///
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitCompletion(this IEnumerator enumerator, bool throwExceptions = true)
{
return Dispatcher.InvokeAsyncAwaitCompletion(enumerator, throwExceptions);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after the has completed on the main thread.
///
/// that is started as a .
///
///
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitCompletion(this IEnumerator enumerator, CancellationToken ct, bool throwExceptions = true)
{
return Dispatcher.InvokeAsyncAwaitCompletion(enumerator, ct, throwExceptions);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after the has completed on the main thread.
///
/// that is started as a .
/// the target on which the coroutine will run.
///
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitCompletion(this IEnumerator enumerator, MonoBehaviour target, bool throwExceptions = true)
{
return Dispatcher.InvokeAsyncAwaitCompletion(enumerator, target, throwExceptions);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after the has completed on the main thread.
///
/// that is started as a .
/// the target on which the coroutine will run.
///
///
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitCompletion(this IEnumerator enumerator, MonoBehaviour target, CancellationToken ct, bool throwExceptions = true)
{
return Dispatcher.InvokeAsyncAwaitCompletion(enumerator, target, ct, throwExceptions);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after the has completed on the main thread.
///
/// that is started as a .
///
///
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitCompletion(this IEnumerator enumerator, ExecutionCycle cycle, bool throwExceptions = true)
{
return Dispatcher.InvokeAsyncAwaitCompletion(enumerator, cycle, throwExceptions);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after the has completed on the main thread.
///
/// that is started as a .
///
///
///
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitCompletion(this IEnumerator enumerator, ExecutionCycle cycle, CancellationToken ct, bool throwExceptions = true)
{
return Dispatcher.InvokeAsyncAwaitCompletion(enumerator, cycle, ct, throwExceptions);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after the has completed on the main thread.
///
/// that is started as a .
///
/// the target on which the coroutine will run.
///
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitCompletion(this IEnumerator enumerator, ExecutionCycle cycle, MonoBehaviour target, bool throwExceptions = true)
{
return Dispatcher.InvokeAsyncAwaitCompletion(enumerator, cycle, target, throwExceptions);
}
///
/// Dispatch an that will be started and executed as a
/// on the main thread; and return a , that when awaited on the calling thread, returns
/// the after the has completed on the main thread.
///
/// that is started as a .
///
/// the target on which the coroutine will run.
///
///
/// exception is thrown if an is dispatched during edit mode.
///
public static Task DispatchAsyncAwaitCompletion(this IEnumerator enumerator, ExecutionCycle cycle, MonoBehaviour target, CancellationToken ct, bool throwExceptions = true)
{
return Dispatcher.InvokeAsyncAwaitCompletion(enumerator, cycle, target, ct, throwExceptions);
}
#endregion
//--------------------------------------------------------------------------------------------------------------
#region --- 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 Dispatch(this Func function)
{
Dispatcher.Invoke(function);
}
///
/// Dispatch the execution of a to the main thread.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// function that returns the dispatched task.
/// The execution cycle during which the passed is executed.
///
public static void Dispatch(this Func function, ExecutionCycle cycle)
{
Dispatcher.Invoke(function, 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.
///
/// function that returns the dispatched task.
/// optional cancellation token that can be passed to abort the task prematurely.
///
///
public static void Dispatch(this Func function, CancellationToken ct, bool throwOnCancellation = true)
{
Dispatcher.Invoke(function, ct, throwOnCancellation);
}
///
/// Dispatch the execution of a to the main thread.
/// Tasks are by default executed during the next available
/// Update, FixedUpdate, LateUpdate or TickUpdate cycle.
///
/// function that returns the 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 Dispatch(this Func function, ExecutionCycle cycle, CancellationToken ct, bool throwOnCancellation = true)
{
Dispatcher.Invoke(function, cycle, ct, throwOnCancellation);
}
#endregion
#region --- 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 DispatchAsync(this Func function)
{
return Dispatcher.InvokeAsync(function);
}
///
/// 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.
///
/// function that returns the dispatched task.
/// The execution cycle during which the passed is executed.
///
public static Task DispatchAsync(this Func function, ExecutionCycle cycle)
{
return Dispatcher.InvokeAsync(function, cycle);
}
///
/// 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.
///
/// function that returns the dispatched task.
/// optional cancellation token that can be passed to abort the task prematurely.
///
///
public static Task DispatchAsync(this Func function, CancellationToken ct, bool throwOnCancellation = true)
{
return Dispatcher.InvokeAsync(function, ct, throwOnCancellation);
}
///
/// 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.
///
/// function that returns the 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 DispatchAsync(this Func function, ExecutionCycle cycle, CancellationToken ct, bool throwOnCancellation = true)
{
return Dispatcher.InvokeAsync(function, cycle, ct, throwOnCancellation);
}
#endregion
#region --- 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 DispatchAsync(this Func> function)
{
return Dispatcher.InvokeAsync(function);
}
///
/// 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 DispatchAsync(this Func> function, ExecutionCycle cycle)
{
return Dispatcher.InvokeAsync(function, cycle);
}
///
/// 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 DispatchAsync(this Func> function, CancellationToken ct)
{
return Dispatcher.InvokeAsync(function, ct);
}
///
/// 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 DispatchAsync(this Func> function, ExecutionCycle cycle, CancellationToken ct)
{
return Dispatcher.InvokeAsync(function, cycle, ct);
}
#endregion
}
}