using System;
using System.Collections;
namespace Foundation.Tasks
{
///
/// A task encapsulates future work that may be waited on.
/// - Support running actions in background threads
/// - Supports running coroutines with return results
/// - Use the WaitForRoutine method to wait for the task in a coroutine
///
///
///
/// var task = Task.Run(() =>
/// {
/// //Debug.Log does not work in
/// Debug.Log("Sleeping...");
/// Task.Delay(2000);
/// Debug.Log("Slept");
/// });
/// // wait for it
/// yield return task;
///
/// // check exceptions
/// if(task.IsFaulted)
/// Debug.LogException(task.Exception)
///
///
public partial class AsyncTask
{
#region Task
///
/// Creates a new running task
///
public static AsyncTask Run(Action action)
{
var task = new AsyncTask(action);
task.Start();
return task;
}
///
/// Creates a new running task
///
public static AsyncTask RunOnMain(Action action)
{
var task = new AsyncTask(action, TaskStrategy.MainThread);
task.Start();
return task;
}
///
/// Creates a new running task
///
public static AsyncTask RunOnCurrent(Action action)
{
var task = new AsyncTask(action, TaskStrategy.CurrentThread);
task.Start();
return task;
}
#endregion
#region Coroutine
///
/// Creates a new running task
///
public static AsyncTask RunCoroutine(IEnumerator function)
{
var task = new AsyncTask(function);
task.Start();
return task;
}
///
/// Creates a new running task
///
public static AsyncTask RunCoroutine(Func function)
{
var task = new AsyncTask(function());
task.Start();
return task;
}
///
/// Creates a new running task
///
public static AsyncTask RunCoroutine(Func function)
{
var task = new AsyncTask();
task.Strategy = TaskStrategy.Coroutine;
task._routine = function(task);
task.Start();
return task;
}
#endregion
#if UNITY
#region Task With Result
///
/// Creates a new running task
///
public static AsyncTask Run(Func function)
{
var task = new AsyncTask(function);
task.Start();
return task;
}
///
/// Creates a new running task
///
public static AsyncTask RunOnMain(Func function)
{
var task = new AsyncTask(function, TaskStrategy.MainThread);
task.Start();
return task;
}
///
/// Creates a new running task
///
public static AsyncTask RunOnCurrent(Func function)
{
var task = new AsyncTask(function, TaskStrategy.CurrentThread);
task.Start();
return task;
}
///
/// Creates a new running task
///
public static AsyncTask RunCoroutine(IEnumerator function)
{
var task = new AsyncTask(function);
task.Start();
return task;
}
///
/// Creates a task which passes the task as a parameter
///
public static AsyncTask RunCoroutine(Func, IEnumerator> function)
{
var task = new AsyncTask();
task.Strategy = TaskStrategy.Coroutine;
task._routine = function(task);
task.Start();
return task;
}
#endregion
#region success / fails
///
/// A default task in the success state
///
static AsyncTask _successTask = new AsyncTask(TaskStrategy.Custom) { Status = TaskStatus.Success };
///
/// A default task in the success state
///
public static AsyncTask SuccessTask(T result)
{
return new AsyncTask(TaskStrategy.Custom) { Status = TaskStatus.Success, Result = result };
}
///
/// A default task in the faulted state
///
public static AsyncTask SuccessTask()
{
return _successTask;
}
///
/// A default task in the faulted state
///
public static AsyncTask FailedTask(string exception)
{
return FailedTask(new Exception(exception));
}
///
/// A default task in the faulted state
///
public static AsyncTask FailedTask(Exception ex)
{
return new AsyncTask(TaskStrategy.Custom) { Status = TaskStatus.Faulted, Exception = ex };
}
///
/// A default task in the faulted state
///
public static AsyncTask FailedTask(string exception)
{
return FailedTask(new Exception(exception));
}
///
/// A default task in the faulted state
///
public static AsyncTask FailedTask(Exception ex)
{
return new AsyncTask(TaskStrategy.Custom) { Status = TaskStatus.Faulted, Exception = ex };
}
#endregion
#endif
}
}